Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB integration #59

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-swagger-ui</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mongodb-panache</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import fr.zelytra.game.pool.PoolParty;
import fr.zelytra.game.pool.PoolPlayer;
import fr.zelytra.game.pool.data.*;
import fr.zelytra.game.pool.game.PoolGameManager;
import fr.zelytra.game.pool.game.PoolVictoryState;
import fr.zelytra.game.pool.game.services.PoolGameService;
import fr.zelytra.notification.Notification;
import fr.zelytra.notification.NotificationMessageKey;
import fr.zelytra.notification.NotificationMessageType;
Expand All @@ -29,6 +31,9 @@ public class PoolSocketService {
@Inject
UserService userService;

@Inject
PoolGameService poolGameService;

private final ConcurrentMap<String, PoolParty> games = new ConcurrentHashMap<>();

/**
Expand Down Expand Up @@ -382,7 +387,10 @@ public void playAction(GameAction gameAction, String socketSessionId) {
user.setPp(reportPlayer.pp());
user.setGamePlayed(user.getGamePlayed() + 1);
}
// Persist game
poolGameService.persist((PoolGameManager) poolParty.getGame());
}

broadcastPoolDataToParty(poolParty);
Log.info("[playAction][" + poolParty.getUuid() + "] User: " + poolPlayer.getUsername() + " play game action");
}
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/fr/zelytra/game/pool/PoolParty.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import fr.zelytra.game.manager.message.SocketTimeOutManager;
import fr.zelytra.game.manager.socket.PoolSocketService;
import fr.zelytra.game.pool.data.*;
import fr.zelytra.game.pool.game.AmericanEightPoolGame;
import fr.zelytra.game.pool.game.customs.AmericanEightPoolGame;
import fr.zelytra.game.pool.game.PoolGameInterface;
import fr.zelytra.game.pool.game.PoolVictoryState;
import fr.zelytra.notification.NotificationMessageKey;
Expand Down Expand Up @@ -171,6 +171,7 @@ public GameReport getGameReport(PoolVictoryState victoryState) {
gameReport.looserPlayer().add(new GameReportPlayer(player.getPp(), newPlayerPP, player.getUsername()));
}
}
game.setGameReport(gameReport);
return gameReport;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.zelytra.game.pool.game;

import fr.zelytra.game.pool.data.GameAction;
import fr.zelytra.game.pool.data.GameReport;
import fr.zelytra.game.pool.data.PoolTeam;

public interface PoolGameInterface {
Expand All @@ -11,4 +12,5 @@ public interface PoolGameInterface {
GameAction getCurrentAction();
void setCurrentAction(GameAction action);
void setVictoryState(PoolVictoryState state);
void setGameReport(GameReport report);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import fr.zelytra.game.pool.data.GameAction;
import fr.zelytra.game.pool.data.GameReport;
import fr.zelytra.game.pool.data.PoolTeam;
import io.quarkus.mongodb.panache.common.MongoEntity;
import jakarta.persistence.GeneratedValue;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.codecs.pojo.annotations.BsonIgnore;

import java.util.ArrayList;
import java.util.List;

@MongoEntity(collection = "PoolGames")
public class PoolGameManager {

@BsonId
@GeneratedValue
private int id;

private final PoolTeam teams = new PoolTeam(new ArrayList<>(), new ArrayList<>());
private final long startingTime;
private long endTime;
private final List<GameAction> history = new ArrayList<>();
private GameAction currentAction;
private boolean paused = false;
private PoolVictoryState victoryState = PoolVictoryState.NONE;
private GameReport gameReport;

public PoolGameManager() {
startingTime = System.currentTimeMillis();
Expand Down Expand Up @@ -50,6 +61,7 @@ public List<GameAction> getTeamActions(List<String> teamNames) {
}

@JsonIgnore
@BsonIgnore
public String getNextPlayer() {
// Get the list of players from both teams
List<String> team1 = teams.team1();
Expand Down Expand Up @@ -130,4 +142,12 @@ public List<GameAction> getHistory() {
public boolean isPaused() {
return paused;
}

public GameReport getGameReport() {
return gameReport;
}

public void setGameReport(GameReport gameReport) {
this.gameReport = gameReport;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fr.zelytra.game.pool.game;
package fr.zelytra.game.pool.game.customs;

import fr.zelytra.game.pool.data.GameAction;
import fr.zelytra.game.pool.data.PoolBalls;
import fr.zelytra.game.pool.data.PoolFault;
import fr.zelytra.game.pool.game.PoolGameInterface;
import fr.zelytra.game.pool.game.PoolGameManager;
import fr.zelytra.game.pool.game.PoolVictoryState;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.zelytra.game.pool.game.services;

import fr.zelytra.game.pool.game.PoolGameManager;
import io.quarkus.mongodb.panache.PanacheMongoRepository;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class PoolGameService implements PanacheMongoRepository<PoolGameManager> {
}
8 changes: 8 additions & 0 deletions backend/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ quarkus.datasource.password=${DB_PASSWORD:password}
quarkus.datasource.jdbc.url=jdbc:postgresql://${DB_HOST:127.0.0.1:24900}/${DB_DATABASE:onepool}
quarkus.hibernate-orm.database.generation=update

#MongoDB
quarkus.mongodb.database=onepool
#quarkus.mongodb.credentials.username=${DB_USER:admin}
#quarkus.mongodb.credentials.password=${DB_PASSWORD:password}
#quarkus.mongodb.hosts=${DB_HOST:127.0.0.1:24905}
#quarkus.mongodb.credentials.auth-source=admin
quarkus.mongodb.connection-string=mongodb://${DB_HOST:127.0.0.1:24905}/?replicaSet=rs0

# Test
%test.quarkus.datasource.db-kind=h2
%test.quarkus.datasource.jdbc.url=jdbc:h2:mem:db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import fr.zelytra.game.pool.data.GameRules;
import fr.zelytra.game.pool.data.GameStatus;
import fr.zelytra.game.pool.data.PoolTeam;
import fr.zelytra.game.pool.game.AmericanEightPoolGame;
import fr.zelytra.game.pool.game.customs.AmericanEightPoolGame;
import fr.zelytra.user.UserEntity;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.transaction.Transactional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.zelytra.game.pool.data.GameAction;
import fr.zelytra.game.pool.data.PoolBalls;
import fr.zelytra.game.pool.data.PoolFault;
import fr.zelytra.game.pool.game.customs.AmericanEightPoolGame;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down
4 changes: 3 additions & 1 deletion deployment/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
version: '3.4'
name: onepool
include:
- ./mongodb/mongo-replica-compose.yml

services:

postgres-app:
Expand Down
42 changes: 42 additions & 0 deletions deployment/dev/mongodb/init-mongo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
set -e

echo "Starting MongoDB initialization script..."
openssl rand -base64 756 > ~/rs_keyfile

mongosh <<EOF
use ${MONGO_INITDB_DATABASE}

if (rs.status().ok == 0) {
print("Initializing replica set...")
rs.initiate(
{
_id: "rs0",
version: 1,
members: [
{ _id: 0, host: "onepool-mongo-app:27017" }
]
}
)
print("Replica set initialized.")
} else {
print("Replica set already initialized.")
}

if (db.getUser('${MONGO_INITDB_USER}') == null) {
print("Creating user '${MONGO_INITDB_USER}'...")
db.createUser({
user: '${MONGO_INITDB_USER}',
pwd: '${MONGO_INITDB_PWD}',
roles: [{
role: 'readWrite',
db: '${MONGO_INITDB_DATABASE}'
}]
})
print("User '${MONGO_INITDB_USER}' created.")
} else {
print("User '${MONGO_INITDB_USER}' already exists.")
}
EOF

echo "MongoDB initialization script completed."
3 changes: 3 additions & 0 deletions deployment/dev/mongodb/keyFile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
openssl rand -base64 756 > ${PWD}/rs_keyfile
chmod 600 ${PWD}/rs_keyfile
chown 9999:9999 ${PWD}/rs_keyfile
29 changes: 29 additions & 0 deletions deployment/dev/mongodb/mongo-replica-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
services:
mongo-rs0-1:
image: mongo:latest
container_name: mongodb
ports:
- "24905:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: ${POSTGRES_USER}
MONGO_INITDB_ROOT_PASSWORD: ${POSTGRES_PASSWORD}
MONGO_INITDB_DATABASE: onepool
MONGO_REPLICA_SET_NAME: rs0
volumes:
- /onepool/mongo-data/app:/data/db
command: >
bash -c "mongod --replSet rs0 --bind_ip_all
&& mongo --eval 'rs.initiate({_id: \"rs0\", members: [{ _id: 0, host: \"localhost:27017\" }]})'"

mongo-init-replica:
image: mongo:latest
depends_on:
- mongo-rs0-1
entrypoint: >
bash -c "
sleep 10;
mongo --host mongo:27017 <<EOF
rs.initiate();
exit;
EOF
"
6 changes: 6 additions & 0 deletions deployment/dev/mongodb/mongod.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
net:
bindIpAll: true
security:
keyFile: /etc/mongodb/pki/keyfile/rs_keyfile.key
replication:
replSetName: rs0
5 changes: 0 additions & 5 deletions webapp/src/objects/stores/NotificationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export const useNotification =
socket = new WebSocket(
import.meta.env.VITE_BACKEND_HOST + "/notifications/" + response.data);
}).catch(() => {
useAlertStore().send({
content: t('alert.websocketAuthFailed.content'),
title: t('alert.websocketAuthFailed.title'),
type: AlertType.ERROR
})
})

if (!socket) return;
Expand Down
Loading