Commit d16ed59f authored by Corentin Guillevic's avatar Corentin Guillevic
Browse files

Add link between Room and Game and between GameServiceHandler and...

Add link between Room and Game and between GameServiceHandler and RoomController ; Defines descriptions methods
parent 951f79b5
......@@ -18,11 +18,21 @@ struct Response{
struct TCard {
1: string cardType,
2: string cardName
2: string cardName,
3: list<TPair> attributes
}
struct TJeton{
1: string jetonId
1: string symbol
}
struct TBoard {
1: string board
}
struct TColor {
1: string color
}
struct TPositionJeton{
......@@ -30,11 +40,11 @@ struct TPositionJeton{
}
struct TPlacedJeton{
1: TJeton jetonId,
1: TJeton jeton,
2: TPositionJeton position
}
struct TPhaseId {
struct TPhase {
1: string name
}
......@@ -52,4 +62,77 @@ struct TAskAction {
1: string type,
2: string message,
3: list<TPair> choices
}
struct TPlayerTeam{
1: string type
}
struct THand{
1: list<TCard> cards,
2: list<TCard> visibleCards,
3: list<TJeton> jetons,
4: i32 numberPionVolonte,
5: list<TCard> defausse,
}
struct TScore{
1: i32 traqueScore,
2: i32 creatureScore,
3: TBoard boardDistribution,
4: TColor boardColor
}
struct TPlacedCard{
1: TCard card,
2: string place
}
struct TBalise{
1: string type,
2: i32 state
}
struct TPlanet{
1: list<TPlacedCard> placedCard,
2: list<TPlacedJeton> jetons,
3: TBalise balise
}
struct TCardReserve{
1: TCard card,
2: i32 number
}
struct TTraque{
1: string name,
2: list<TCard> visibleCards,
3: i32 hiddenPlaceCardNumber,
4: list<TCard> defausse,
5: i32 hiddenSurvivalCardNumber,
6: i32 numberPionVolonte
}
struct TCreature{
1: string name,
2: list<TCard> visibleCards,
3: i32 hiddenTrackingCardNumber,
4: list<TJeton> jetons
}
struct TGameVariable{
1: string name,
2: list<TPair> values
}
struct TDescription{
1: TPlayerTeam team,
2: THand hand,
3: TScore score,
4: TPlanet planet
5: list<TCardReserve> reserve,
6: list<TGameVariable> variables,
7: list<TTraque> traquesInformation,
8: TCreature creatureInformation
}
\ No newline at end of file
......@@ -20,14 +20,6 @@ struct TRoomId {
1: string token
}
struct TBoard {
1: string board
}
struct TColor {
1: string color
}
//Service
......@@ -35,25 +27,19 @@ service GameService {
TRoomId createRoom(TPlayer player) throws (1:common.InvalidOperationException e)
common.Response sendStartGame(TPlayer player, i32 creatureId, TBoard board, TColor color, list<common.TCard> placeCards)
common.Response sendStartGame(TPlayer player, i32 creatureId, common.TBoard board, common.TColor color, list<common.TCard> placeCards)
common.Response joinRoom(TPlayer player, TRoomId roomId)
common.Response getGameDescription(TPlayer player)
common.Response waitStartPhase(TPlayer player, common.TPhaseId phaseId)
common.Response sendFinishPhase(TPlayer player, common.TPhaseId phaseId)
common.Response sendPlayCards(TPlayer player, list<common.TCard> playerCards)
common.TDescription getGameDescription(TPlayer player)
common.Response sendPlaceJetons(TPlayer player, list<common.TPlacedJeton> placedJetons)
void sendFinishPhase(TPlayer player, common.TPhase phase)
common.Response sendResist(TPlayer player, i32 number)
void sendPlayCards(TPlayer player, list<common.TCard> playerCards)
common.Response sendGiveUp(TPlayer player)
void sendPlaceJetons(TPlayer player, list<common.TPlacedJeton> placedJetons)
//Is usefull ?
common.Response sendChooseAction(TPlayer player, common.TAction action)
void sendResist(TPlayer player, i32 number)
void sendGiveUp(TPlayer player)
}
......@@ -3,71 +3,27 @@ include "common.thrift"
namespace java fr.univnantes.alma.thrift
namespace js common
//Type
struct TPlayerTeam{
1: string type
}
struct THand{
1: list<common.TCard> cards,
2: list<common.TCard> visibleCards,
3: list<common.TJeton> jetons,
4: i32 numberPionVolonte,
5: list<common.TCard> defausse,
}
struct TScore{
1: i32 traqueScore,
2: i32 creatureScore
}
struct TPlanet{
1: common.TCard card,
2: list<common.TPlacedJeton> jetons,
3: i32 baliseState
}
struct TCardReserve{
1: common.TCard card,
2: i32 number
}
struct TTraque{
1: string name,
2: list<common.TCard> visibleCards,
3: i32 hiddenPlaceCardNumber,
4: list<common.TCard> defausse,
5: i32 hiddenSurvivalCardNumber,
6: i32 numberPionVolonte
}
struct TCreature{
1: string name,
2: list<common.TCard> visibleCards,
3: i32 hiddenTrackingCardNumber,
4: list<common.TJeton> jetons
}
//Service
service PlayerService {
bool ping() throws (1:common.InvalidOperationException e)
void sendGameDescription(TPlayerTeam team, THand hand, TScore score, TPlanet planet, list<TCardReserve> reserve, list<TTraque> traquesInformation, TCreature creatureInformation)
void sendGameDescription(common.TDescription gameDescription)
void sendGameStart()
void sendGameIsFinished(common.TPlayerTeam winner)
void sendFirstRoundStart()
void sendStartPhase(common.TPhaseId phaseId, TPlayerTeam team, THand hand, TScore score, TPlanet planet, list<TCardReserve> reserve, list<TTraque> traquesInformation, TCreature creatureInformation)
void sendStartPhase(common.TPhase phase, common.TDescription gameDescription)
common.TAction askAction(common.TAskAction askedAction)
void sendActionWithoutExpectedResponse(common.TAskAction askedAction);
void sendAction(common.TAskAction askedAction)
void sendResponse(common.Response response)
}
package fr.univnantes.alma.common;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.board.Board;
import fr.univnantes.alma.server.game.item.card.Card;
import fr.univnantes.alma.server.game.item.jeton.PlacedJeton;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.user.User;
import fr.univnantes.alma.thrift.GameNotFound;
import fr.univnantes.alma.thrift.InvalidOperationException;
import fr.univnantes.alma.thrift.Response;
import fr.univnantes.alma.thrift.TDescription;
import org.checkerframework.checker.units.qual.C;
import java.util.List;
public interface RoomService {
......@@ -21,7 +29,7 @@ public interface RoomService {
* @param roomId The room id
* @return A response
*/
Response joinRoom(User user, String roomId);
Response joinRoom(User user, String roomId) throws GameNotFound;
/**
* Start the game where the player is the game creator
......@@ -33,4 +41,43 @@ public interface RoomService {
*/
Response startGame(User user, int creatureId, Board board, Planet planet);
/**
* @param user A user
* @return The description of the user's game
*/
TDescription getGameDescription(User user) throws InvalidOperationException;
/**
* Send to the room that the user has finished a phase
* @param user The user
* @param phase The phase
*/
void sendFinishPhase(User user, Phase phase) throws InvalidOperationException;
/**
* Send to the room that the user play cards
* @param user The user
* @param cards The cards
*/
void sendPlayCards(User user, List<Card> cards) throws InvalidOperationException;
/**
* Send to the room that the user place jetons
* @param user The user
* @param placedJetons The placed jetons
*/
void sendPlaceJetons(User user, List<PlacedJeton> placedJetons) throws InvalidOperationException;
/**
* Send to the room that the user resist of number pawn Willingness
* @param user The user
* @param number The number of pawn Willingness lost
*/
void sendResist(User user, int number) throws InvalidOperationException;
/**
* Send to the room that the user give up
* @param user The user
*/
void sendGiveUp(User user) throws InvalidOperationException;
}
......@@ -14,17 +14,17 @@ import fr.univnantes.alma.server.game.item.planet.Place;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.game.item.player.Creature;
import fr.univnantes.alma.server.game.item.player.Player;
import fr.univnantes.alma.server.game.item.player.PlayerTeam;
import fr.univnantes.alma.server.game.item.player.Traque;
import fr.univnantes.alma.server.game.item.power.Power;
import fr.univnantes.alma.server.game.item.power.PowerType;
import fr.univnantes.alma.server.game.item.power.modifier.PowerModifier;
import fr.univnantes.alma.server.game.item.power.modifier.PowerModifierType;
import fr.univnantes.alma.server.game.item.power.recurrent.PowerRecurrent;
import fr.univnantes.alma.server.game.utilitary.Conversion;
import fr.univnantes.alma.server.game.utilitary.Pair;
import fr.univnantes.alma.server.user.User;
import fr.univnantes.alma.thrift.Response;
import fr.univnantes.alma.thrift.TAskAction;
import fr.univnantes.alma.thrift.TPair;
import fr.univnantes.alma.thrift.*;
import org.apache.thrift.TException;
import java.util.*;
import java.util.stream.Collectors;
......@@ -408,12 +408,6 @@ public class Game implements GameInterface {
}
}
@Override
//TODO : is useless ?
public boolean phaseIsFinish(Phase phase) {
return false;
}
@Override
public Response playerPlayCard(int playerId, Card card) {
Response response;
......@@ -604,12 +598,6 @@ public class Game implements GameInterface {
return response;
}
//TODO : Is useless ?
@Override
public Response playerChooseAction(int playerId, Action action) {
return null;
}
@Override
public Action askAction(int inGameIdPlayer, TAskAction askedAction) {
try{
......@@ -621,31 +609,55 @@ public class Game implements GameInterface {
}
@Override
public void sendActionWithoutExpectedResponse(int inGameIdPlayer, TAskAction askedAction) {
//TODO
public void sendAction(int inGameIdPlayer, TAskAction askedAction) {
try {
room.sendAction(inGameIdPlayer, askedAction);
} catch (TException e) {
throw new IllegalArgumentException("Unknown error");
}
}
@Override
public void sendFirstRoundStart() {
//TODO
try {
room.sendFirstRoundStart();
} catch (TException e) {
throw new IllegalArgumentException("Unknown error");
}
}
@Override
public void sendStartPhase() {
//TODO
for(Integer id : playersMap.keySet()) {
try {
room.sendStartPhase(id, new TPhase(state.toString()),createDescription(id));
} catch (TException e) {
e.printStackTrace();
}
}
}
@Override
public void sendDescription() {
//TODO
if(state.equals(Phase.PHASE_3)) {
//places cards played became visible
for(Integer id : playersMap.keySet()) {
try {
room.sendDescription(id, createDescription(id));
} catch (TException e) {
e.printStackTrace();
}
}
}
@Override
public void sendGameIsEnd() {
//TODO
public void sendGameIsFinished() {
TPlayerTeam winner = new TPlayerTeam(board.winner().toString());
for(Integer id : playersMap.keySet()) {
try {
room.sendGameIsFinished(id, winner);
} catch (TException e) {
e.printStackTrace();
}
}
}
/*************************************
......@@ -1128,7 +1140,7 @@ public class Game implements GameInterface {
}
}
if(isFinish()) {
sendGameIsEnd();
sendGameIsFinished();
}
else{
gameRoundVariables.reset();
......@@ -1302,7 +1314,7 @@ public class Game implements GameInterface {
public void sendCardsAction(int idPlayer, List<Card> cardsToShow) {
TAskAction askAction = createTActionForSendCardsAction(cardsToShow);
sendActionWithoutExpectedResponse(idPlayer, askAction);
sendAction(idPlayer, askAction);
}
private TAskAction createTActionForSendCardsAction(List<Card> cardsToShow){
......@@ -1463,4 +1475,33 @@ public class Game implements GameInterface {
}
return isJetonCibleOnThisPlace;
}
public TDescription createDescription(int playerId) {
Player player = playersMap.getOrDefault(playerId, null);
if(player == null) {
throw new IllegalArgumentException(playerId + " is not a valid id");
}
TScore tScore = Conversion.toTScore(board);
TPlanet tPlanet = Conversion.toTPlanet(planet);
List<TCardReserve> reserves = Conversion.toTCardReserveList(reserve);
List<TGameVariable> variables = Conversion.toTGameVariable(gameRoundVariables);
List<TTraque> traquesInformations;
TCreature creatureInformation = Conversion.toTCreature(creature);
if(state.equals(Phase.PHASE_3)) {
//places cards played became visible
traquesInformations = traques.stream().map(Conversion::toTTraquePlaceCardPlayedVisible).collect(Collectors.toList());
}
else {
traquesInformations = traques.stream().map(Conversion::toTTraque).collect(Collectors.toList());
}
THand tHand = Conversion.toTHand(player);
TPlayerTeam tPlayerTeam;
if(player.teamEqualsTo(CREATURE)) {
tPlayerTeam = new TPlayerTeam(CREATURE.toString());
}
else {
tPlayerTeam = new TPlayerTeam(TRAQUE.toString());
}
return new TDescription(tPlayerTeam, tHand, tScore, tPlanet, reserves, variables, traquesInformations, creatureInformation);
}
}
......@@ -26,12 +26,6 @@ public interface GameInterface {
*/
public Response playerHasFinished(int playerId, Phase phase);
/**
* Test if the current phase is finished
* @return true if the current phase is finished, false otherwise
*/
public boolean phaseIsFinish(Phase phase);
/**
* Inform the game that the player play a card
* @param playerId The game id of the player
......@@ -79,14 +73,6 @@ public interface GameInterface {
*/
public Response playerGiveUp(int playerId);
/**
* Inform the game that the player choose an action
* @param playerId The game id of the player
* @param action The action chosen
* @throws IllegalArgumentException if this action is not valid
*/
public Response playerChooseAction(int playerId, Action action);
/**
* Ask an action to a client
* @param inGameIdPlayer The id of player
......@@ -100,7 +86,7 @@ public interface GameInterface {
* @param inGameIdPlayer The id of player
* @param askedAction The asked action
*/
public void sendActionWithoutExpectedResponse(int inGameIdPlayer, TAskAction askedAction);
public void sendAction(int inGameIdPlayer, TAskAction askedAction);
/**
* Send that the game start
......@@ -120,9 +106,5 @@ public interface GameInterface {
/**
* Send that the game is end
*/
public void sendGameIsEnd();
//TODO find a describing game function
public void sendGameIsFinished();
}
......@@ -149,6 +149,10 @@ public class GameRoundVariables {
}
}
public int getNumberPawnWillingnessOnBoard() {
return numberPawnWillingnessOnBoard;
}
//Functions methods
public BiConsumer<Integer, Game> getActionJetonCible() {
......
package fr.univnantes.alma.server.game;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.action.Action;
import fr.univnantes.alma.server.game.item.board.Board;
import fr.univnantes.alma.server.game.item.board.Score;
import fr.univnantes.alma.server.game.item.card.Card;
import fr.univnantes.alma.server.game.item.jeton.JetonSymbol;
import fr.univnantes.alma.server.game.item.jeton.PlacedJeton;
import fr.univnantes.alma.server.game.item.planet.Planet;
import fr.univnantes.alma.server.game.utilitary.Conversion;
import fr.univnantes.alma.server.game.utilitary.Pair;
import fr.univnantes.alma.server.game.utilitary.request.GameJoinRequest;
import fr.univnantes.alma.server.game.utilitary.request.GameRequest;
import fr.univnantes.alma.server.game.utilitary.request.*;
import fr.univnantes.alma.server.user.User;
import fr.univnantes.alma.thrift.Response;
import fr.univnantes.alma.thrift.TAskAction;
import fr.univnantes.alma.thrift.*;
import org.apache.thrift.TException;
import org.atlanmod.commons.log.Log;
......@@ -120,7 +124,7 @@ public class Room {
}
else if(currentNumber + 1 <= MAX_PLAYERS){
if(requests.offer(new GameJoinRequest(user, currentNumber+1))){
response = new Response(true, "");
response = new Response(true, "Join the game");
}
else{
response = new Response(false, "Unknown error. Please retry.");
......@@ -144,11 +148,20 @@ public class Room {
else{
isStart = true;
this.game = new Game(playersToIdNamePairList(), creatureId, planet, board, this);
response = new Response(true, "");
response = new Response(true, "Game start");
}
return response;
}
public TDescription getGameDescription(User user) {
return game.createDescription(userToInGameId(user));
}
public Response addRequest(GameRequest request) {
requests.offer(request);
return new Response(true, "Request sent");
}
private void requestManagement(){
while(!isClose){
try {
......@@ -162,25 +175,113 @@ public class Room {
}
private void handleRequest(GameRequest request) {
User user;
switch (request.getType()){
case JOIN:
GameJoinRequest gameJoinRequest = (GameJoinRequest) request;
int inGameId = gameJoinRequest.getInGameId();
User user = gameJoinRequest.getUser();
user = gameJoinRequest.getUser();
players.put(user.getId(), inGameId);
users.