Commit 6540d223 authored by Mamadou Saliou DIALLO's avatar Mamadou Saliou DIALLO
Browse files

feat: handle BoardGame association with Game

parent 10684db8
package fr.univnantes.alma.game;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.model.BoardGame;
import fr.univnantes.alma.references.associations.BidirectionalOneToOne;
import fr.univnantes.alma.references.interfaces.OneToOneAssociation;
import org.atlanmod.commons.log.Log;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Single Game.
*/
public class Game {
/**
* Stores arriving registrations.
......@@ -18,32 +24,60 @@ public class Game {
*/
private final AtomicInteger idCounter = new AtomicInteger(0);
/**
* Expected players.
*/
private final AtomicInteger expectedPlayers;
/**
* Thread play.
*/
private Thread play;
public Game(int expectedPlayers) {
this.expectedPlayers = new AtomicInteger(expectedPlayers);
this.requests = new ArrayBlockingQueue<GameJoinRequest>(expectedPlayers);
/**
* Game board.
*/
private OneToOneAssociation<Game, BoardGame> boardGame;
/**
* Create new instance of game.
* @param expectedPlayersForGame Expected players
*/
public Game(int expectedPlayersForGame) {
this.expectedPlayers = new AtomicInteger(expectedPlayersForGame);
this.requests =
new ArrayBlockingQueue<GameJoinRequest>(expectedPlayersForGame);
boardGame = new BidirectionalOneToOne<>(this, BoardGame::getGame);
play = new Thread(() -> this.start());
play.start();
}
public int join(GameJoinRequest request) {
/**
* Join a game.
* @param request game join request
* @return id of joined game.
*/
public int join(GameJoinRequest request) {
int id = idCounter.getAndIncrement();
requests.offer(request);
return id;
}
/**
* Lunch the game.
*/
private void start() {
this.waitForPlayers();
Log.info("We can start !");
}
/**
* Wait for players until expected players not reached.
*/
private void waitForPlayers() {
Log.info("Waiting for request. Expecting {0} players.", expectedPlayers);
Log.info("Waiting for request. Expecting {0} players.",
expectedPlayers);
GameJoinRequest request;
......@@ -57,6 +91,18 @@ public class Game {
}
}
private void handleRequest(GameJoinRequest request) {
/**
* Handle game join request.
* @param request request to handle
*/
private void handleRequest(GameJoinRequest request) {
}
/**
* Return associated board game.
* @return BoardGame
*/
public OneToOneAssociation<Game, BoardGame> getBoardGame() {
return boardGame;
}
}
package fr.univnantes.alma.model;
import fr.univnantes.alma.game.Game;
import fr.univnantes.alma.model.square.Square;
import fr.univnantes.alma.references.associations.BidirectionalOneToMany;
import fr.univnantes.alma.references.associations.BidirectionalOneToOne;
import fr.univnantes.alma.references.interfaces.OneToManyAssociation;
import fr.univnantes.alma.references.interfaces.OneToOneAssociation;
/**
* Board game.
......@@ -11,17 +14,34 @@ public class BoardGame {
/**
* Board game squares.
*/
OneToManyAssociation<BoardGame, Square> squares;
private OneToManyAssociation<BoardGame, Square> squares;
/**
* Game on which the game board is associated.
*/
private OneToOneAssociation<BoardGame, Game> game;
/**
* Create new instance of BoardGame.
*/
public BoardGame() {
squares = new BidirectionalOneToMany<>(this, Square::getBoardGame);
game = new BidirectionalOneToOne<>(this, Game::getBoardGame);
}
/**
* Return all squares on the board game.
* @return
* @return List of square
*/
public OneToManyAssociation<BoardGame, Square> getSquares() {
return squares;
}
/**
* Return the game on which the game board is associated.
* @return Game
*/
public OneToOneAssociation<BoardGame, Game> getGame() {
return game;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment