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

Refactoring of several classes ; Add some converting methods ; Defines methods...

Refactoring of several classes ; Add some converting methods ; Defines methods to manage the phases (in game.java) ; Start implementation of PowerApplicator ; Start the implementation of 'ask action' methods (also in game.java)
parent 58b10d4d
......@@ -36,4 +36,14 @@ struct TPlacedJeton{
struct TPhaseId {
1: string name
}
struct TPair {
1: string key,
2: string value
}
struct TAction {
1: string type,
2: list<TPair> params
}
\ No newline at end of file
......@@ -63,6 +63,8 @@ service GameClientService {
void sendStartPhase(common.TPhaseId phaseId, TPlayerTeam team, THand hand, TScore score, TPlanet planet, list<TCardReserve> reserve, list<TTraque> traquesInformation, TCreature creatureInformation)
common.TAction askAction(common.TAction actionAsked)
}
......@@ -16,11 +16,6 @@ struct TRoomId {
1: string token
}
struct TAction {
1: string type,
2: list<string> params
}
struct TBoard {
1: string board
}
......@@ -50,10 +45,11 @@ service GameServerService {
common.Response sendPlaceJetons(TPlayerId playerId, list<common.TPlacedJeton> placedJetons)
common.Response sendResist(TPlayerId playerId)
common.Response sendResist(TPlayerId playerId, i32 number)
common.Response sendGiveUp(TPlayerId playerId)
common.Response sendChooseAction(TPlayerId playerId, TAction action)
//Is usefull ?
common.Response sendChooseAction(TPlayerId playerId, common.TAction action)
}
......@@ -2,6 +2,7 @@ package fr.univnantes.alma.data;
import fr.univnantes.alma.data.item.User;
import fr.univnantes.alma.game.item.Phase;
import fr.univnantes.alma.game.item.card.CardName;
import fr.univnantes.alma.game.item.card.PlaceCard;
import fr.univnantes.alma.game.item.card.SurvivalCard;
import fr.univnantes.alma.game.item.card.TrackingCard;
......@@ -28,27 +29,27 @@ public class DatabaseStub implements NotAloneDatabase {
private void initializePlaceCardMap(){
placeCardMap = new HashMap<>();
placeCardMap.put("ANTRE", new PlaceCard("ANTRE", "Antre", "Lorem ipsum", PowerType.ANTRE, 1, "/image.png", "#4466bb"));
placeCardMap.put("NEXUS", new PlaceCard("NEXUS", "Nexus", "Lorem ipsum", PowerType.NEXUS, 1, "/image.png", "#4466bb"));
placeCardMap.put("JUNGLE", new PlaceCard("JUNGLE", "Jungle", "Lorem ipsum", PowerType.JUNGLE, 2, "/image.png", "#4466bb"));
placeCardMap.put("RIVIERE", new PlaceCard("RIVIERE", "Rivière", "Lorem ipsum", PowerType.RIVIERE, 3, "/image.png", "#4466bb"));
placeCardMap.put("DOME", new PlaceCard("DOME","Dôme", "Lorem ipsum", PowerType.DOME, 4, "/image.png", "#4466bb"));
placeCardMap.put("PLAGE", new PlaceCard("PLAGE","Plage", "Lorem ipsum", PowerType.PLAGE, 5, "/image.png", "#4466bb"));
placeCardMap.put("MANGROVE", new PlaceCard("MANGROVE", "Mangrove", "Lorem ipsum", PowerType.MANGROVE, 6, "/image.png", "#4466bb"));
placeCardMap.put("ABRI", new PlaceCard("ABRI", "Abri", "Lorem ipsum", PowerType.ABRI, 7, "/image.png", "#4466bb"));
placeCardMap.put("EPAVE", new PlaceCard("EPAVE", "Epave", "Lorem ipsum", PowerType.EPAVE, 8, "/image.png", "#4466bb"));
placeCardMap.put("FUNGI", new PlaceCard("FUNGI", "Fungi", "Lorem ipsum", PowerType.FUNGI, 9, "/image.png", "#4466bb"));
placeCardMap.put("PORTAIL", new PlaceCard("PORTAIL", "Portail", "Lorem ipsum", PowerType.PORTAIL, 10, "/image.png", "#4466bb"));
placeCardMap.put("ANTRE", new PlaceCard(CardName.ANTRE, "Antre", "Lorem ipsum",1, "/image.png", "#4466bb"));
placeCardMap.put("NEXUS", new PlaceCard(CardName.NEXUS, "Nexus", "Lorem ipsum", 1, "/image.png", "#4466bb"));
placeCardMap.put("JUNGLE", new PlaceCard(CardName.JUNGLE, "Jungle", "Lorem ipsum", 2, "/image.png", "#4466bb"));
placeCardMap.put("RIVIERE", new PlaceCard(CardName.RIVIERE, "Rivière", "Lorem ipsum", 3, "/image.png", "#4466bb"));
placeCardMap.put("DOME", new PlaceCard(CardName.DOME,"Dôme", "Lorem ipsum", 4, "/image.png", "#4466bb"));
placeCardMap.put("PLAGE", new PlaceCard(CardName.PLAGE,"Plage", "Lorem ipsum", 5, "/image.png", "#4466bb"));
placeCardMap.put("MANGROVE", new PlaceCard(CardName.MANGROVE, "Mangrove", "Lorem ipsum", 6, "/image.png", "#4466bb"));
placeCardMap.put("ABRI", new PlaceCard(CardName.ABRI, "Abri", "Lorem ipsum", 7, "/image.png", "#4466bb"));
placeCardMap.put("EPAVE", new PlaceCard(CardName.EPAVE, "Epave", "Lorem ipsum", 8, "/image.png", "#4466bb"));
placeCardMap.put("FUNGI", new PlaceCard(CardName.FUNGI, "Fungi", "Lorem ipsum", 9, "/image.png", "#4466bb"));
placeCardMap.put("PORTAIL", new PlaceCard(CardName.PORTAIL, "Portail", "Lorem ipsum", 10, "/image.png", "#4466bb"));
}
private void initializeSurvivalCardMap(){
survivalCardMap = new HashMap<>();
survivalCardMap.put("ADRENALINE", new SurvivalCard("ADRENALINE","Adrénaline", "Lorem ipsum", PowerType.ADRENALINE, Phase.PHASE_1));
survivalCardMap.put("ADRENALINE", new SurvivalCard(CardName.ADRENALINE,"Adrénaline", "Lorem ipsum", Phase.PHASE_1));
}
private void initializeTrackingCardMap(){
trackingCardMap = new HashMap<>();
trackingCardMap.put("ACHARNEMENT", new TrackingCard("ACHARNEMENT", "Acharnement", "Lorem ipsum", PowerType.ACHARNEMENT, Phase.PHASE_1));
trackingCardMap.put("ACHARNEMENT", new TrackingCard(CardName.ACHARNEMENT, "Acharnement", "Lorem ipsum", Phase.PHASE_1));
}
......
......@@ -5,8 +5,12 @@ import fr.univnantes.alma.data.NotAloneDatabase;
import fr.univnantes.alma.game.item.Phase;
import fr.univnantes.alma.game.item.Reserve;
import fr.univnantes.alma.game.item.action.Action;
import fr.univnantes.alma.game.item.action.ActionChooseCard;
import fr.univnantes.alma.game.item.action.ActionChoosePower;
import fr.univnantes.alma.game.item.action.ActionType;
import fr.univnantes.alma.game.item.board.Board;
import fr.univnantes.alma.game.item.card.*;
import fr.univnantes.alma.game.item.jeton.JetonSymbol;
import fr.univnantes.alma.game.item.jeton.PlacedJeton;
import fr.univnantes.alma.game.item.pioche.Pioche;
import fr.univnantes.alma.game.item.planet.Planet;
......@@ -14,12 +18,21 @@ import fr.univnantes.alma.game.item.player.Creature;
import fr.univnantes.alma.game.item.player.Player;
import fr.univnantes.alma.game.item.player.Traque;
import fr.univnantes.alma.game.item.power.Power;
import org.atlanmod.commons.log.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import fr.univnantes.alma.thrift.Response;
import fr.univnantes.alma.thrift.TAction;
import fr.univnantes.alma.thrift.TPair;
import java.util.*;
import java.util.stream.Collectors;
import static fr.univnantes.alma.game.PowerApplicator.applyPlayerCard;
import static fr.univnantes.alma.game.PowerApplicator.revealPlace;
import static fr.univnantes.alma.game.item.action.ActionType.CHOOSE_CARD;
import static fr.univnantes.alma.game.item.action.ActionType.CHOOSE_POWER;
import static fr.univnantes.alma.game.item.jeton.JetonSymbol.ARTEMIA;
import static fr.univnantes.alma.game.item.jeton.JetonSymbol.CIBLE;
import static fr.univnantes.alma.game.item.player.PlayerTeam.CREATURE;
import static fr.univnantes.alma.game.item.player.PlayerTeam.TRAQUE;
public class Game implements GameInterface {
/**
......@@ -82,18 +95,30 @@ public class Game implements GameInterface {
*/
private Phase state;
public Game(int playerNumber, int creatureId, Planet planet, Board board) {
/**
* Reference to the room
*/
private Room room;
/**
* Contains the round variables that may be modified by power
*/
private GameRoundVariables gameRoundVariables;
public Game(int playerNumber, int creatureId, Planet planet, Board board, Room room) {
this.planet = planet;
this.board = board;
this.room = room;
this.database = DatabaseFactory.getDatabase();
this.currentActivatePowers = new ArrayList<>();
this.nextRoundPowers = new ArrayList<>();
this.gameRoundVariables = new GameRoundVariables();
initializePlayer(playerNumber, creatureId);
initializeReserve(playerNumber);
initializeSurvivalCardPioche();
initializeTrackingCardPioche();
this.state = Phase.PREPHASE_1;
sendFirstRoundStart();
}
/***************************
......@@ -104,6 +129,7 @@ public class Game implements GameInterface {
* Initialize Creature, Traques and the playerMap
*/
private void initializePlayer(int numberPlayer, int creatureId){
this.traques = new ArrayList<>();
if(creatureId > numberPlayer){
throw new IllegalArgumentException("The in game id of Creature must be valid");
}
......@@ -126,7 +152,7 @@ public class Game implements GameInterface {
* @param playerNumber The number of players
*/
private void initializeReserve(int playerNumber){
List<PlaceCard> cards = planet.getPlaceDistribution().getPlaceCardsInInterval(6, 10);
List<PlaceCard> cards = planet.getPlaceCardsInInterval(6, 10);
reserve = new Reserve(cards, playerNumber);
}
......@@ -144,62 +170,720 @@ public class Game implements GameInterface {
this.trackingCardPioche = new Pioche<>(database.findAllTrackingCard());
}
/***************************
* Getters
**************************/
public List<Power> getCurrentActivatePowers() {
return currentActivatePowers;
}
public List<Power> getNextRoundPowers() {
return nextRoundPowers;
}
public Planet getPlanet() {
return planet;
}
public Reserve getReserve() {
return reserve;
}
public Pioche<SurvivalCard> getSurvivalCardPioche() {
return survivalCardPioche;
}
public Pioche<TrackingCard> getTrackingCardPioche() {
return trackingCardPioche;
}
public Board getBoard() {
return board;
}
public Creature getCreature() {
return creature;
}
public List<Traque> getTraques() {
return traques;
}
public Map<Integer, Player> getPlayersMap() {
return playersMap;
}
public Phase getState() {
return state;
}
public Room getRoom() {
return room;
}
/*************************************
* Room Controller Interface Methods
* Game Interface Methods
***********************************/
@Override
public boolean isFinish() {
return false;
return board.isFinish();
}
@Override
public void apply(Power power) {
PowerApplicator.apply(power, this);
}
@Override
public void playerHasFinished(int playerId, Phase phase) {
public Response playerHasFinished(int playerId, Phase phase) {
Response response;
Player player = playersMap.get(playerId);
if(state.equals(phase)) {
if (player.teamEqualsTo(CREATURE)) {
response = creatureHasFinished((Creature) player, phase);
} else {
response = traqueHasFinished((Traque) player, phase);
}
}
else {
throw new IllegalArgumentException("Phase current : " + this.state);
}
if(response.isState()) {
playerFinishedPhase(player, phase);
}
return response;
}
public Response creatureHasFinished(Creature creature, Phase phase) {
if (phase.equals(Phase.PHASE_2) && creature.jetonsPlayedIsEmpty()) {
return new Response(false, "You must to place at least one jeton");
}
else{
return new Response(true, "");
}
}
public Response traqueHasFinished(Traque traque, Phase phase) {
if(phase.equals(Phase.PHASE_1) && traque.placeCardPlayedIsEmpty()) {
return new Response(false, "You must play a place card");
}
else {
return new Response(true, "");
}
}
public void playerFinishedPhase(Player player, Phase phase) {
player.setCurrentPhase(nextPhase(phase));
if(allPlayersHasFinishedCurrentPhase()) {
startNextPhase();
}
}
@Override
public boolean phaseIsFinish() {
//TODO : is useless ?
public boolean phaseIsFinish(Phase phase) {
return false;
}
@Override
public void playerPlayCard(int playerId, Card card) {
public Response playerPlayCard(int playerId, Card card) {
Response response;
Player player = playersMap.get(playerId);
switch (state) {
case PREPHASE_1:
case POSTPHASE_1:
case PREPHASE_2:
case POSTPHASE_2:
case PREPHASE_3:
case POSTPHASE_3:
case PREPHASE_4:
case POSTPHASE_4:
response = playerPlayPlayerCard(player, card);
break;
case PHASE_1:
if(player.teamEqualsTo(TRAQUE)) {
try {
PlaceCard placeCard = (PlaceCard) card;
Traque traque = (Traque) player;
response = traquePlayPlaceCard(traque, placeCard);
}
catch (Exception e){
response = new Response(false, "Need a place card");
}
}
else{
response = new Response(false, "No card authorize in this phase");
}
break;
case PHASE_2:
case PHASE_3:
case PHASE_4:
default:
response = new Response(false, "No card authorize in this phase");
}
return response;
}
@Override
public Response playerPlayCard(int playerId, List<Card> cards) {
Response response;
Player player = playersMap.get(playerId);
switch (state) {
case PHASE_1:
if(player.getTeam().equals(TRAQUE)) {
try {
List<PlaceCard> placeCards = cards.stream().map(c -> (PlaceCard) c).collect(Collectors.toList());
Traque traque= (Traque) player;
response = traquePlayPlaceCard(traque, placeCards);
}
catch (Exception e){
response = new Response(false, "Need a place card");
}
}
else{
response = new Response(false, "No card authorize in this phase");
}
break;
case PREPHASE_1:
case POSTPHASE_1:
case PREPHASE_2:
case POSTPHASE_2:
case PREPHASE_3:
case POSTPHASE_3:
case PREPHASE_4:
case POSTPHASE_4:
response = playerPlayPlayerCard(player, cards);
break;
case PHASE_2:
case PHASE_3:
case PHASE_4:
default:
response = new Response(false, "No card authorize in this phase");
}
return response;
}
@Override
public void playerPlayCard(int playerId, List<Card> cards) {
public Response playerPlaceJeton(int playerId, PlacedJeton placedJeton) {
Response response;
Player player = playersMap.get(playerId);
if(player.teamEqualsTo(TRAQUE) || ! state.equals(Phase.PHASE_2)){
response = new Response(false, "Only the Creature can played jeton and only in the PHASE_2");
}
else{
response = creaturePlaceAJeton(creature, placedJeton);
}
return response;
}
@Override
public Response playerPlaceJeton(int playerId, List<PlacedJeton> jetons) {
Player player = playersMap.get(playerId);
Response response;
if(player.teamEqualsTo(TRAQUE) || ! state.equals(Phase.PHASE_2)){
response = new Response(false, "Only the Creature can played jeton and only in the PHASE_2");
}
else {
boolean canPlaceAllJetons = true;
for (PlacedJeton placedJeton : jetons) {
canPlaceAllJetons = canPlaceAllJetons && playerPlaceJeton(playerId, placedJeton).isState();
}
if (!canPlaceAllJetons) {
creature.initializationJeton();
response = new Response(false, "Invalid action");
}
else{
response = new Response(true, "");
}
}
return response;
}
@Override
public void playerPlaceJeton(int playerId, PlacedJeton jeton) {
public Response playerResist(int playerId, int number) {
Player player = playersMap.get(playerId);
Response response;
if(player.teamEqualsTo(CREATURE)) {
response = new Response(false, "Only traque can resist");
}
else{
Traque traque = (Traque) player;
if(number < 1 || number > 2) {
response = new Response(false, "Number must be 1 or 2");
}
else if(! state.equals(Phase.PHASE_1)){
response = new Response(false, "Resist is only possible in PHASE_1");
}
else if(traque.getNumberWillingness() > number && traque.canResist()) {
traque.subWillingness(number);
int numberCard = Math.min(2 * number, traque.defausseSize());
choosePlaceCardsAction(playerId, numberCard, traque.getDefausse());
traque.getRights().setCanGiveUp(false);
traque.getRights().setCanResist(false);
response = new Response(true, "");
}
else{
response = new Response(false, "You can't resist");
}
}
return response;
}
@Override
public Response playerGiveUp(int playerId) {
Player player = playersMap.get(playerId);
Response response;
if(player.teamEqualsTo(CREATURE)) {
response = new Response(false, "Only traque can resist");
}
else{
Traque traque = (Traque) player;
if(! state.equals(Phase.PHASE_1)){
response = new Response(false, "Give up is only possible in PHASE_1");
}
else if(traque.canGiveUp()) {
traque.fillWillingness();
traque.takeBackAllPlaceCardFromDefausse();
traque.setCanGiveUp(false);
traque.setCanResist(false);
board.moveForwardCreature();
response = new Response(true, "");
}
else{
response = new Response(false, "You can't resist");
}
}
return response;
}
//TODO : Is useless ?
@Override
public void playerPlaceJeton(int playerId, List<PlacedJeton> jetons) {
public Response playerChooseAction(int playerId, Action action) {
return null;
}
@Override
public Action askAction(int inGameIdPlayer, TAction askedAction) {
return null;
}
@Override
public void playerResist(int playerId) {
public void sendFirstRoundStart() {
}
@Override
public void playerGiveUp(int playerId) {
public void sendStartPhase() {
}
@Override
public void playerChooseAction(int playerId, Action action) {
public void sendDescription() {
if(state.equals(Phase.PHASE_3)) {
//places cards played became visible
}
}
@Override
public void sendGameIsEnd() {
}
/*************************************
* Player play card
*************************************/
/**
* A player play a survival card or a tracking card
* @param player The player
* @param card The player card
* @return A Response
*/
public Response playerPlayPlayerCard(Player player, Card card) {
Response response;
if(player.teamEqualsTo(TRAQUE)) {
try {
SurvivalCard survivalCard = (SurvivalCard) card;
Traque traque = (Traque) player;
response = traquePlaySurvivalCard(traque, survivalCard);
}
catch (Exception e){
response = new Response(false, "Need a survival card");
}
}
else{
try {
TrackingCard trackingCard = (TrackingCard) card;
response = creaturePlayTrackingCard(creature, trackingCard);
}
catch (Exception e){
response = new Response(false, "Need a tracking card");
}
}
return response;
}
/**
* A player play survival cards or tracking cards
* @param player The player
* @param cards The player cards
* @return A Response
*/
public Response playerPlayPlayerCard(Player player, List<Card> cards) {
Response response;
if(player.teamEqualsTo(TRAQUE)) {
try {
List<SurvivalCard> survivalCards = cards.stream().map(c -> (SurvivalCard) c).collect(Collectors.toList());