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

Small refactoring in PowerApplicator (signature) ; Add tests for...

Small refactoring in PowerApplicator (signature) ; Add tests for Conversion.java ; Add implementation of equals methods for Actions (in package action)
parent 0dbb0f9f
......@@ -42,10 +42,10 @@ public class PowerApplicator {
*/
public static void applyPlayerCard(Game game, int idPlayer, PlayerCard playerCard){
if(playerCard.getType().equals(Card.CardType.TRACKING)) {
applyTrackingCardPower(game, playerCard.getCardName(), idPlayer);
applyTrackingCardPower(game, idPlayer, playerCard.getCardName());
}
else{
applySurvivalCardPower(game, playerCard.getCardName(), idPlayer);
applySurvivalCardPower(game, idPlayer, playerCard.getCardName());
}
}
......@@ -77,7 +77,7 @@ public class PowerApplicator {
Traque traque = (Traque) game.getPlayer(idPlayer);
traque.addSurvivalCard(game.getPlanet().takeSurvivalCardOnPlace(placeCard));
}
applyPlaceCardPower(game, placeCard.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, placeCard.getCardName());
}
}
......@@ -170,10 +170,10 @@ public class PowerApplicator {
/**
* A traque apply the power of one place
* @param game The game
* @param cardName The card name
* @param idPlayer The id of the traque
* @param cardName The card name
*/
public static void applyPlaceCardPower(Game game, CardName cardName, int idPlayer) {
public static void applyPlaceCardPower(Game game, int idPlayer, CardName cardName) {
switch (cardName) {
case ANTRE:
applyPlaceAntre(game, idPlayer);
......@@ -240,7 +240,7 @@ public class PowerApplicator {
}
}
public static void applyTrackingCardPower(Game game, CardName cardName, int idPlayer) {
public static void applyTrackingCardPower(Game game, int idPlayer, CardName cardName) {
switch (cardName) {
case ACHARNEMENT:
applyTrackingAcharnement(game);
......@@ -355,7 +355,7 @@ public class PowerApplicator {
}
}
public static void applySurvivalCardPower(Game game, CardName cardName, int idPlayer) {
public static void applySurvivalCardPower(Game game, int idPlayer, CardName cardName) {
switch (cardName) {
case ADRENALINE:
applySurvivalAdrenaline(game, idPlayer);
......@@ -1494,7 +1494,7 @@ public class PowerApplicator {
while (revealedPlace == null || ! hiddenPlaces.contains(revealedPlace));
planet.revealPlace(revealedPlace);
PlaceCard placeCard = planet.placeToPlaceCard(revealedPlace);
applyPlaceCardPower(game, placeCard.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, placeCard.getCardName());
}
/**
......@@ -1595,7 +1595,7 @@ public class PowerApplicator {
}
PlaceCard card = placeCards.get(0);
if(card.getCardName() != ARTEFACT) {
applyPlaceCardPower(game, card.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, card.getCardName());
}
}
......@@ -1609,7 +1609,7 @@ public class PowerApplicator {
Traque traque = (Traque) game.getPlayer(idPlayer);
List<PlaceCard> placeCards = traque.getDefausse();
PlaceCard placeCard = game.choosePlaceCardsAction(idPlayer, 1, placeCards).get(0);
applyPlaceCardPower(game, placeCard.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, placeCard.getCardName());
}
/**
......@@ -1622,7 +1622,7 @@ public class PowerApplicator {
public static void copyPowerOfOneCardInInterval(Game game, int idPlayer, int a, int b) {
List<PlaceCard> choosableCards = game.getPlanet().getPlaceCardsInInterval(a, b);
PlaceCard placeCard = game.choosePlaceCardsAction(idPlayer, 1, choosableCards).get(0);
applyPlaceCardPower(game, placeCard.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, placeCard.getCardName());
}
/**
......@@ -1640,7 +1640,7 @@ public class PowerApplicator {
}
}
PlaceCard copiedCard = game.choosePlaceCardsAction(idPlayer, 1, choosableCards).get(0);
applyPlaceCardPower(game, copiedCard.getCardName(), idPlayer);
applyPlaceCardPower(game, idPlayer, copiedCard.getCardName());
}
/**
......
......@@ -2,6 +2,7 @@ package fr.univnantes.alma.server.game.item.action;
import fr.univnantes.alma.server.game.item.card.CardName;
import fr.univnantes.alma.server.game.item.planet.Place;
import fr.univnantes.alma.server.game.utilitary.Pair;
import fr.univnantes.alma.thrift.TPair;
import java.util.*;
......@@ -55,4 +56,16 @@ public class ActionAssociateCardNamesToPlaces extends Action {
public ActionType getActionType() {
return ActionType.ASSOCIATE_CARDNAMES_TO_PLACES;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionAssociateCardNamesToPlaces)){
return false;
}
ActionAssociateCardNamesToPlaces action = (ActionAssociateCardNamesToPlaces) obj;
return cardNamePlaceMap.equals(action.getCardNamePlaceMap()) && action.getActionType().equals(ActionType.ASSOCIATE_CARDNAMES_TO_PLACES);
}
}
......@@ -40,4 +40,16 @@ public class ActionChooseCard extends Action {
public ActionType getActionType() {
return ActionType.CHOOSE_CARD;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionChooseCard)){
return false;
}
ActionChooseCard action = (ActionChooseCard) obj;
return cards.equals(action.getCards()) && action.getActionType().equals(ActionType.CHOOSE_CARD);
}
}
......@@ -41,4 +41,16 @@ public class ActionChoosePlace extends Action {
public ActionType getActionType() {
return ActionType.CHOOSE_PLACE;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionChoosePlace)){
return false;
}
ActionChoosePlace action = (ActionChoosePlace) obj;
return places.equals(action.getPlaces()) && action.getActionType().equals(ActionType.CHOOSE_PLACE);
}
}
......@@ -45,4 +45,16 @@ public class ActionChoosePower extends Action{
public ActionType getActionType() {
return ActionType.CHOOSE_POWER;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionChoosePower)){
return false;
}
ActionChoosePower action = (ActionChoosePower) obj;
return idPowerChosen == action.getIdPowerChosen() && action.getActionType().equals(ActionType.CHOOSE_POWER);
}
}
package fr.univnantes.alma.server.game.item.action;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ActionContainer {
private List<ActionType> actions;
public ActionContainer(){
this.actions = new ArrayList<>();
}
public ActionContainer(List<ActionType> actions){
this.actions = actions;
}
public boolean add(Action action){
return this.actions.add(action.getActionType());
}
public boolean add(List<Action> actions){
return this.actions.addAll(
actions.stream()
.map(Action::getActionType)
.collect(Collectors.toList())
);
}
public boolean doAction(Action action){
if(this.actions.contains(action.getActionType())){
this.actions.remove(action.getActionType());
return true;
}
else {
return false;
}
}
public boolean doAction(List<Action> actions){
List<ActionType> types = actions.stream()
.map(Action::getActionType)
.collect(Collectors.toList());
if(this.actions.containsAll(types)){
return this.actions.removeAll(types);
}
else{
return false;
}
}
public boolean isEmpty(){
return this.actions.isEmpty();
}
}
package fr.univnantes.alma.server.game.item.action;
import fr.univnantes.alma.server.game.item.jeton.JetonSymbol;
import fr.univnantes.alma.server.game.item.planet.Place;
import fr.univnantes.alma.thrift.TPair;
import java.util.List;
import static fr.univnantes.alma.server.game.utilitary.Conversion.*;
public class ActionMoveJeton extends Action {
private JetonSymbol jetonSymbol;
private Place place;
public ActionMoveJeton(JetonSymbol jetonSymbol, Place place) {
this.place = place;
this.jetonSymbol = jetonSymbol;
}
public ActionMoveJeton(List<TPair> params) {
boolean invalid = false;
if(params.size() != 2) {
invalid = true;
}
else{
try{
TPairType type1 = toTPairType(params.get(0).getKey());
TPairType type2 = toTPairType(params.get(1).getKey());
if(type1.equals(TPairType.JETON) && type2.equals(TPairType.PLACE)) {
this.jetonSymbol = toJetonSymbol(params.get(0).getValue());
this.place = toPlace(params.get(1).getValue());
}
else if(type1.equals(TPairType.PLACE) && type2.equals(TPairType.JETON)) {
this.place = toPlace(params.get(0).getValue());
this.jetonSymbol = toJetonSymbol(params.get(0).getValue());
}
else{
invalid = true;
}
}
catch (Exception e){
invalid = true;
}
}
if(invalid){
throw new IllegalArgumentException("ActionTargetPlayer need one player id (integer)");
}
}
public JetonSymbol getJetonSymbol() {
return jetonSymbol;
}
public Place getPlace() {
return place;
}
@Override
public ActionType getActionType() {
return ActionType.MOVE_JETON;
}
}
......@@ -59,4 +59,16 @@ public class ActionMovePlayer extends Action {
public ActionType getActionType() {
return ActionType.MOVE_PLAYER;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionMovePlayer)){
return false;
}
ActionMovePlayer action = (ActionMovePlayer) obj;
return idPlayer == action.getIdPlayer() && place.equals(action.getPlace()) && action.getActionType().equals(ActionType.MOVE_PLAYER);
}
}
......@@ -54,4 +54,19 @@ public class ActionSwapJeton extends Action {
public ActionType getActionType() {
return ActionType.SWAP_JETONS;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionSwapJeton)){
return false;
}
ActionSwapJeton action = (ActionSwapJeton) obj;
JetonSymbol s1 = action.getJetonSymbol1();
JetonSymbol s2 = action.getJetonSymbol2();
boolean equals = (jetonSymbol1.equals(s1) && jetonSymbol2.equals(s2)) || (jetonSymbol1.equals(s2) && jetonSymbol2.equals(s1));
return equals && action.getActionType().equals(ActionType.SWAP_JETONS);
}
}
......@@ -45,4 +45,16 @@ public class ActionTargetPlayer extends Action {
public ActionType getActionType() {
return ActionType.TARGET_PLAYER;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof ActionTargetPlayer)){
return false;
}
ActionTargetPlayer action = (ActionTargetPlayer) obj;
return idPlayer == action.getIdPlayer() && action.getActionType().equals(ActionType.TARGET_PLAYER);
}
}
package fr.univnantes.alma.server.game.item.action;
public enum ActionType{
CHOOSE_POWER, TARGET_PLAYER, MOVE_PLAYER, CHOOSE_CARD, CHOOSE_PLACE, SWAP_JETONS, MOVE_JETON, ASSOCIATE_CARDNAMES_TO_PLACES, SHOW_CARD
CHOOSE_POWER, TARGET_PLAYER, MOVE_PLAYER, CHOOSE_CARD, CHOOSE_PLACE, SWAP_JETONS, ASSOCIATE_CARDNAMES_TO_PLACES, SHOW_CARD
}
package fr.univnantes.alma.server.game.item.jeton;
import fr.univnantes.alma.server.game.item.action.ActionTargetPlayer;
import fr.univnantes.alma.server.game.item.action.ActionType;
import fr.univnantes.alma.server.game.item.planet.Place;
import java.util.ArrayList;
......@@ -28,4 +30,16 @@ public class PlacedJeton {
public List<Place> getPlaces() {
return places;
}
@Override
public boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof PlacedJeton)){
return false;
}
PlacedJeton placedJeton = (PlacedJeton) obj;
return jetonSymbol.equals(placedJeton.getJetonSymbol()) && places.equals(placedJeton.getPlaces());
}
}
......@@ -150,7 +150,7 @@ public class Conversion {
break;
}
if(card == null){
throw new IllegalArgumentException("Card not foud");
throw new IllegalArgumentException("Card not found");
}
return card;
}
......@@ -317,12 +317,9 @@ public class Conversion {
return new ActionChoosePlace(tAction.getParams());
case SWAP_JETONS:
return new ActionSwapJeton(tAction.getParams());
case MOVE_JETON:
return new ActionMoveJeton(tAction.getParams());
case ASSOCIATE_CARDNAMES_TO_PLACES:
return new ActionAssociateCardNamesToPlaces(tAction.getParams());
default:
throw new IllegalArgumentException(tAction.type + " doesn't represent a valid ActionType");
return new ActionAssociateCardNamesToPlaces(tAction.getParams());
}
}
}
package fr.univnantes.alma.server.game.utilitary;
import fr.univnantes.alma.server.game.item.Phase;
import fr.univnantes.alma.server.game.item.action.*;
import fr.univnantes.alma.server.game.item.board.BoardColor;
import fr.univnantes.alma.server.game.item.board.BoardDistribution;
import fr.univnantes.alma.server.game.item.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.Place;
import fr.univnantes.alma.thrift.*;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class ConversionTest {
@Test
void testToPhase_validPhase() {
assertEquals(Phase.PREPHASE_1, Conversion.toPhase("PREPHASE_1"));
assertEquals(Phase.PHASE_1, Conversion.toPhase("PHASE_1"));
assertEquals(Phase.POSTPHASE_1, Conversion.toPhase("POSTPHASE_1"));
assertEquals(Phase.PREPHASE_2, Conversion.toPhase("PREPHASE_2"));
assertEquals(Phase.PHASE_2, Conversion.toPhase("PHASE_2"));
assertEquals(Phase.POSTPHASE_2, Conversion.toPhase("POSTPHASE_2"));
assertEquals(Phase.PREPHASE_3, Conversion.toPhase("PREPHASE_3"));
assertEquals(Phase.PHASE_3, Conversion.toPhase("PHASE_3"));
assertEquals(Phase.POSTPHASE_3, Conversion.toPhase("POSTPHASE_3"));
assertEquals(Phase.PREPHASE_4, Conversion.toPhase("PREPHASE_4"));
assertEquals(Phase.PHASE_4, Conversion.toPhase("PHASE_4"));
assertEquals(Phase.POSTPHASE_4, Conversion.toPhase("POSTPHASE_4"));
assertEquals(Phase.PREPHASE_1, Conversion.toPhase(new TPhaseId("PREPHASE_1")));
assertEquals(Phase.PHASE_1, Conversion.toPhase(new TPhaseId("PHASE_1")));
assertEquals(Phase.POSTPHASE_1, Conversion.toPhase(new TPhaseId("POSTPHASE_1")));
assertEquals(Phase.PREPHASE_2, Conversion.toPhase(new TPhaseId("PREPHASE_2")));
assertEquals(Phase.PHASE_2, Conversion.toPhase(new TPhaseId("PHASE_2")));
assertEquals(Phase.POSTPHASE_2, Conversion.toPhase(new TPhaseId("POSTPHASE_2")));
assertEquals(Phase.PREPHASE_3, Conversion.toPhase(new TPhaseId("PREPHASE_3")));
assertEquals(Phase.PHASE_3, Conversion.toPhase(new TPhaseId("PHASE_3")));
assertEquals(Phase.POSTPHASE_3, Conversion.toPhase(new TPhaseId("POSTPHASE_3")));
assertEquals(Phase.PREPHASE_4, Conversion.toPhase(new TPhaseId("PREPHASE_4")));
assertEquals(Phase.PHASE_4, Conversion.toPhase(new TPhaseId("PHASE_4")));
assertEquals(Phase.POSTPHASE_4, Conversion.toPhase(new TPhaseId("POSTPHASE_4")));
}
@Test
void testToPhase_invalidPhase(){
assertThrows(IllegalArgumentException.class, () -> Conversion.toPhase("PHASE_5"));
}
@Test
void testToBoardColor_validColor() {
assertEquals(BoardColor.BLUE, Conversion.toBoardColor("BLUE"));
assertEquals(BoardColor.GREEN, Conversion.toBoardColor("GREEN"));
assertEquals(BoardColor.RED, Conversion.toBoardColor("RED"));
assertEquals(BoardColor.YELLOW, Conversion.toBoardColor("YELLOW"));
assertEquals(BoardColor.BLUE, Conversion.toBoardColor(new TColor("BLUE")));
assertEquals(BoardColor.GREEN, Conversion.toBoardColor(new TColor("GREEN")));
assertEquals(BoardColor.RED, Conversion.toBoardColor(new TColor("RED")));
assertEquals(BoardColor.YELLOW, Conversion.toBoardColor(new TColor("YELLOW")));
}
@Test
void testToBoardColor_invalidColor() {
assertThrows(IllegalArgumentException.class, () -> Conversion.toBoardColor("MAGENTA"));
}
@Test
void testToBoardDistribution_validDistribution() {
assertEquals(BoardDistribution.BACK, Conversion.toBoardDistribution("BACK"));
assertEquals(BoardDistribution.FRONT, Conversion.toBoardDistribution("FRONT"));
assertEquals(BoardDistribution.BACK, Conversion.toBoardDistribution(new TBoard("BACK")));
assertEquals(BoardDistribution.FRONT, Conversion.toBoardDistribution(new TBoard("FRONT")));
}
@Test
void testToBoardDistribution_invalidDistribution() {
assertThrows(IllegalArgumentException.class, () -> Conversion.toBoardDistribution("SIDE"));
}
@Test
void testToCardType_validType() {
assertEquals(Card.CardType.PLACE, Conversion.toCardType("PLACE"));
assertEquals(Card.CardType.TRACKING, Conversion.toCardType("TRACKING"));
assertEquals(Card.CardType.SURVIVAL, Conversion.toCardType("SURVIVAL"));
}
@Test
void testToCardType_invalidType() {
assertThrows(IllegalArgumentException.class, () -> Conversion.toCardType("BONUS"));
}
@Test
void testToCard_validCard() {
PlaceCard placeCard = new PlaceCard(CardName.ANTRE, "Antre", "", 1, "url", "color");
assertEquals(placeCard, Conversion.toCard(new TCard("PLACE", "ANTRE")));
}
@Test
void testToCard_invalidCard() {
TCard tCard = new TCard("PLACE", "NANTES");
assertThrows(IllegalArgumentException.class, () -> Conversion.toCard(tCard));
}
@Test
void testToCardList_validCards() {
PlaceCard placeCard = new PlaceCard(CardName.ANTRE, "Antre", "", 1, "url", "color");
SurvivalCard survivalCard = new SurvivalCard(CardName.RESISTANCE, "Résistance", "", Phase.PREPHASE_1);
TrackingCard trackingCard = new TrackingCard(CardName.ACHARNEMENT, "Acharnement", "", Phase.PREPHASE_1);
List<Card> cards = new ArrayList<>(Arrays.asList(placeCard, survivalCard, trackingCard));
List<TCard> tCards = new ArrayList<>(Arrays.asList(
new TCard("PLACE", "ANTRE"),
new TCard("SURVIVAL", "RESISTANCE"),
new TCard("TRACKING", "ACHARNEMENT")
));
assertEquals(cards, Conversion.toCardList(tCards));
}
@Test
void testToPlaceCardList_validCards() {
PlaceCard placeCard1 = new PlaceCard(CardName.ANTRE, "Antre", "", 1, "url", "color");
PlaceCard placeCard2 = new PlaceCard(CardName.JUNGLE, "Jungle", "", 1, "url", "color");
PlaceCard placeCard3 = new PlaceCard(CardName.ROVER, "Rover", "", 1, "url", "color");
List<Card> cards = new ArrayList<>(Arrays.asList(placeCard1, placeCard2, placeCard3));
List<TCard> tCards = new ArrayList<>(Arrays.asList(
new TCard("PLACE", "ANTRE"),
new TCard("PLACE", "JUNGLE"),
new TCard("PLACE", "ROVER")
));
assertEquals(cards, Conversion.toPlaceCardList(tCards));
}
@Test
void testToPlaceCardList_invalidCards() {
List<TCard> tCards = new ArrayList<>(Arrays.asList(
new TCard("PLACE", "ANTRE"),
new TCard("SURVIVAL", "RESISTANCE"),
new TCard("PLACE", "ROVER")
));