Commit 2c2560fb authored by Alex MOULIN's avatar Alex MOULIN
Browse files

first implementation createGame and join

parent b5d55e28
......@@ -11,6 +11,14 @@ exception GameNotFound {
2: string description
}
struct Player{
1: i32 id
2: string name
3: string ip
4: string port
}
struct Response {
1: bool value
2: string detal
......
......@@ -4,35 +4,25 @@ namespace java fr.univnantes.alma.thrift
namespace js common
struct JoinRequest {
1: string name
}
struct InfoClient{
1: string ip
2: string port
}
struct PlayerID{
1: string id
1: common.Player player;
2: i32 gameId;
}
service GameService {
i32 createGame(i32 numberOfPlayers)
i32 join(i32 gameId, JoinRequest request) throws (1:common.GameNotFound e)
i32 createGame(common.Player creator, i32 numberOfPlayers)
common.Response isConnected(PlayerID player, InfoClient params)
i32 join(JoinRequest request) throws (1:common.GameNotFound e)
common.Phase nextPhase(common.Phase phase)
common.Response resits(PlayerID hunted)
common.Response resits(common.Player hunted)
common.Response toGo(PlayerID hunted)
common.Response toGo(common.Player hunted)
common.Response playPlaceCard(PlayerID hunted, common.Card card)
common.Response playPlaceCard(common.Player hunted, common.Card card)
common.Response putToken(common.Token token, common.Place place)
common.Response playHuntCard(PlayerID creature, common.Card card)
}
common.Response playHuntCard(common.Player creature, common.Card card)
}
\ No newline at end of file
include "common.thrift"
namespace java fr.univnantes.alma.thrift
//namespace js
namespace js common
enum PlayerState {
UNBLOCKED = 0,
......@@ -13,7 +13,7 @@ struct PlayerStateResponse {
2: string message
}
struct Player {
struct GamePlayer {
1: i32 id,
2: string name,
3: PlayerState state,
......@@ -21,7 +21,7 @@ struct Player {
5: optional list<PlaceCard> placeCards,
}
struct Winner {
1: Player winner,
1: GamePlayer winner,
2: string message
}
struct HuntCard {
......@@ -39,7 +39,7 @@ struct BoardGameState {
1: i32 assimilationTokenPosition,
2: i32 rescuePawnPosition,
3: optional bool isBoardFacing,
4: list<Player> players
4: list<GamePlayer> players
}
service PlayerService {
......
package fr.univnantes.alma.common;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.thrift.GameNotFound;
import fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Player;
import fr.univnantes.alma.thrift.Response;
public interface GameInterface {
......@@ -10,14 +13,12 @@ public interface GameInterface {
*
* @return an int, the game identification
*/
int createGame(int expectedPlayers);
int createGame(Player creator, int expectedPlayers) throws InvalidOperationException;
/**
*
* @param gameId
* @param request
* @return
*/
int join(int gameId, GameJoinRequest request);
int join(GameJoinRequest request) throws InvalidOperationException, GameNotFound;
}
package fr.univnantes.alma.common;
import fr.univnantes.alma.thrift.Player;
public class GameJoinRequest {
private Player player;
private int gameId;
public GameJoinRequest(Player player, int gameId) {
this.player = player;
this.gameId = gameId;
}
public Player getPlayer() {
return player;
}
public int getGameId() {
return gameId;
}
}
......@@ -4,12 +4,15 @@ 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 fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Player;
import org.atlanmod.commons.log.Log;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.HashMap;
import java.util.Map;
/**
* Single Game.
*/
......@@ -19,6 +22,10 @@ public class Game {
*/
private final BlockingQueue<GameJoinRequest> requests;
/**
* The id of the game
*/
private int id;
/**
* Counter used to increment player identifications.
*/
......@@ -29,6 +36,11 @@ public class Game {
*/
private final AtomicInteger expectedPlayers;
/**
* List of players in the game
*/
private final Map<Integer, Player> players = new HashMap<>();
/**
* Thread play.
*/
......@@ -43,8 +55,9 @@ public class Game {
* Create new instance of game.
* @param expectedPlayersForGame Expected players
*/
public Game(int expectedPlayersForGame) {
public Game(int expectedPlayersForGame, int id) {
this.expectedPlayers = new AtomicInteger(expectedPlayersForGame);
this.id = id;
this.requests =
new ArrayBlockingQueue<GameJoinRequest>(expectedPlayersForGame);
boardGame = new BidirectionalOneToOne<>(this, BoardGame::getGame);
......@@ -57,9 +70,13 @@ public class Game {
* @param request game join request
* @return id of joined game.
*/
public int join(GameJoinRequest request) {
public int join(GameJoinRequest request) throws InvalidOperationException {
if (this.expectedPlayers.intValue() <= players.size()){
throw new InvalidOperationException(500,"The game is full");
}
int id = idCounter.getAndIncrement();
requests.offer(request);
players.put(id, request.getPlayer());
return id;
}
......@@ -105,4 +122,4 @@ public class Game {
public OneToOneAssociation<Game, BoardGame> getBoardGame() {
return boardGame;
}
}
}
\ No newline at end of file
......@@ -3,6 +3,9 @@ package fr.univnantes.alma.game;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.common.GameInterface;
import fr.univnantes.alma.thrift.GameNotFound;
import fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Player;
import org.springframework.stereotype.Component;
import java.util.HashMap;
......@@ -16,21 +19,30 @@ public class GameController implements GameInterface {
private final AtomicInteger idCounter = new AtomicInteger(0);
private final Map<Integer, Game> games = new HashMap<>();
private final Map<Integer, Player> connectedPlayers = new HashMap<>();
@Override
public int createGame(int expectedPlayers) {
public int createGame(Player creator, int expectedPlayers) throws InvalidOperationException {
checkArgument(expectedPlayers > 1 && expectedPlayers <= 7, "A game must have between 2 and 7 players");
if (connectedPlayers.get(creator.getId()) != null){
throw new InvalidOperationException(400,"Player is already registered in a game");
}
int newId = idCounter.incrementAndGet();
Game newGame = new Game(expectedPlayers);
Game newGame = new Game(expectedPlayers, newId);
games.put(newId, newGame);
connectedPlayers.put(newId,creator);
return newId;
}
@Override
public int join(int gameId, GameJoinRequest request) {
Game game = games.get(gameId);
public int join(GameJoinRequest request) throws InvalidOperationException, GameNotFound {
if (connectedPlayers.get(request.getPlayer()) != null) {
throw new InvalidOperationException(400, "Player is already registered in a game");
}
Game game = games.get(request.getGameId());
if (game == null) {
throw new GameNotFound(404,"Error 404: the was not found");
}
return game.join(request);
}
......
......@@ -3,7 +3,6 @@ package fr.univnantes.alma.handler;
import fr.univnantes.alma.common.GameInterface;
import fr.univnantes.alma.common.GameJoinRequest;
import fr.univnantes.alma.thrift.*;
import fr.univnantes.alma.thrift.GameService;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -15,19 +14,14 @@ public class GameServiceHandler implements GameService.Iface {
GameInterface service;
@Override
public int createGame(int numberOfPlayers) throws TException {
return service.createGame(numberOfPlayers);
public int createGame(Player creator, int numberOfPlayers) throws TException {
return service.createGame(creator,numberOfPlayers);
}
@Override
public int join(int gameId, JoinRequest request) throws TException {
public int join(JoinRequest request) throws TException {
//TODO: translate JoinRequest into GameJoinRequest
return service.join(gameId, new GameJoinRequest());
}
@Override
public Response isConnected(PlayerID player, InfoClient params) throws TException {
return null;
return service.join(new GameJoinRequest(request.getPlayer(), request.getGameId()));
}
@Override
......@@ -36,17 +30,17 @@ public class GameServiceHandler implements GameService.Iface {
}
@Override
public Response resits(PlayerID hunted) throws TException {
public Response resits(Player hunted) throws TException {
return null;
}
@Override
public Response toGo(PlayerID hunted) throws TException {
public Response toGo(Player hunted) throws TException {
return null;
}
@Override
public Response playPlaceCard(PlayerID hunted, Card card) throws TException {
public Response playPlaceCard(Player hunted, Card card) throws TException {
return null;
}
......@@ -56,8 +50,8 @@ public class GameServiceHandler implements GameService.Iface {
}
@Override
public Response playHuntCard(PlayerID creature, Card card) throws TException {
public Response playHuntCard(Player creature, Card card) throws TException {
return null;
}
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package fr.univnantes.alma;
import fr.univnantes.alma.thrift.GameService;
import fr.univnantes.alma.thrift.JoinRequest;
import fr.univnantes.alma.thrift.Player;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
......@@ -37,19 +38,25 @@ class NotAloneApplicationTest {
@Test
public void testCreateGame() throws TException {
int id = client.createGame(4);
Player creator = new Player();
int id = client.createGame(creator,4);
assertThat(id).isGreaterThan(0);
}
@Test
public void testJoinGame() throws TException, InterruptedException {
int id = client.createGame(4);
Player creator = new Player();
Player player1 = new Player();
Player player2 = new Player();
Player player3 = new Player();
Player player4 = new Player();
int id = client.createGame(creator,4);
client.join(id, new JoinRequest("one"));
client.join(id, new JoinRequest("two"));
client.join(id, new JoinRequest("three"));
client.join(id, new JoinRequest("four"));
client.join(new JoinRequest(player1,id));
client.join(new JoinRequest(player2,id));
client.join(new JoinRequest(player3,id));
client.join(new JoinRequest(player4,id));
Thread.sleep(1000);
......
This diff is collapsed.
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