Skip to content

Commit

Permalink
Commit inizio test totale partita
Browse files Browse the repository at this point in the history
  • Loading branch information
dromano32 committed Feb 21, 2024
1 parent 30f03e5 commit b472be6
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void exit(
game = GameRepository.getInstance().getCompletedGame(id);
Player player = game.findPlayerByUsername(body.getUsername());
GameExit action = GameExit.builder().player(player).build();
game.onActionPlayer(action);
GameRepository.getInstance().removePlayer(body.getUsername());
game.onActionPlayer(action);
} catch (GameException | DatabaseConnectionException | UserException e) {LOGGER.error("Errore durante l'uscita dalla partita", e);} }

@MessageMapping("/partite/{id}/confermaSetup")
Expand All @@ -86,6 +86,7 @@ public void confirmSetup(@DestinationVariable String id, @Payload SetUpBody body
Player player = game.findPlayerByUsername(body.getUsername());
if (player != null) {
TerritorySetup action = TerritorySetup.builder().player(player).setUpBody(body).build();

game.onActionPlayer(action);
}
LOGGER.info("Fine setup player : {}", game.findPlayerByUsername(body.getUsername()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ private void assignTerritories(IDeck deckTerritory) throws GameException, Databa
int playerIndex = 0;
TerritoryCard card = (TerritoryCard)deckTerritory.drawCard();
while (card != null) {
//LOGGER.info("Card ----AssignTerritories---- {}", card);
if (card.getSymbol().equals(CardSymbol.JOLLY)) {
deckTerritory.insertCard(card);
deckTerritory.shuffle();
Expand All @@ -110,7 +109,6 @@ private void assignColor(List<Player> players) throws GameException, DatabaseCon
colors.remove(PlayerColor.GREY);
Collections.shuffle(colors);
for (int i = 0; i < players.size(); i++) {
LOGGER.info("Player {} has color -------------- {}", players.get(i).getUserName(), colors.get(i));
players.get(i).setColor(colors.get(i));
GameRepository.getInstance().updatePlayerColor(players.get(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LobbyState extends GameState{

@Override
public void onActionPlayer(GameEntry gameEntry) throws FullGameException, GameException, DatabaseConnectionException, UserException{
game.addPlayer(gameEntry.getPlayer());
game.addPlayer(gameEntry.getPlayer());
if (game.getPlayers().size() == game.getConfiguration().getNumberOfPlayers()) {
game.setState(GameSetupState.builder().game(game).build());
game.getState().setUpGame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,189 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.mvcguru.risiko.maven.eclipse.controller.body_request.TerritoryBody;
import com.mvcguru.risiko.maven.eclipse.controller.body_request.TerritoryCardBody;
import com.mvcguru.risiko.maven.eclipse.model.card.TerritoryCard;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mvcguru.risiko.maven.eclipse.actions.ComboRequest;
import com.mvcguru.risiko.maven.eclipse.actions.GameEntry;
import com.mvcguru.risiko.maven.eclipse.actions.GameExit;
import com.mvcguru.risiko.maven.eclipse.actions.TerritorySetup;
import com.mvcguru.risiko.maven.eclipse.controller.body_request.ComboRequestBody;
import com.mvcguru.risiko.maven.eclipse.controller.body_request.SetUpBody;
import com.mvcguru.risiko.maven.eclipse.exception.DatabaseConnectionException;
import com.mvcguru.risiko.maven.eclipse.exception.FullGameException;
import com.mvcguru.risiko.maven.eclipse.exception.GameException;
import com.mvcguru.risiko.maven.eclipse.exception.UserException;
import com.mvcguru.risiko.maven.eclipse.model.GameConfiguration;
import com.mvcguru.risiko.maven.eclipse.model.Territory;
import com.mvcguru.risiko.maven.eclipse.model.card.TerritoryCard.CardSymbol;
import com.mvcguru.risiko.maven.eclipse.model.GameConfiguration.GameMode;
import com.mvcguru.risiko.maven.eclipse.model.IGame;
import com.mvcguru.risiko.maven.eclipse.model.player.Player;
import com.mvcguru.risiko.maven.eclipse.service.FactoryGame;
import com.mvcguru.risiko.maven.eclipse.service.GameRepository;

class LobbyStateTest {

Logger LOGGER = LoggerFactory.getLogger(LobbyStateTest.class);
GameConfiguration config = GameConfiguration.builder()
.mode(GameMode.EASY)
.mode(GameMode.HARD)
.numberOfPlayers(2)
.idMap("TestMap")
.build();

@Test
void testOnActionPlayerGameEntry() throws FullGameException, GameException, DatabaseConnectionException, UserException, IOException {
IGame game = FactoryGame.getInstance().createGame(config);
String gameId = game.getId();

Player player1 = Player.builder().userName("Bobby").gameId(game.getId()).territories(new ArrayList<Territory>()).color(Player.PlayerColor.GREY).build();
@Test
void totalGameLogic() throws IOException, GameException, DatabaseConnectionException, UserException, FullGameException {

// Create a game with the configuration
//create LobbyState
IGame game = FactoryGame.getInstance().createGame(config);

assertEquals(game.getConfiguration(), config);
assertNotNull(game.getConfiguration());
assertNotNull(game.getContinents());
assertNotNull(game.getDeckObjective());
assertNotNull(game.getDeckTerritory());

assertEquals(game.getState().getClass().toString(), LobbyState.class.toString());
GameRepository.getInstance().insertGame(game);

// Create a player and add it to the game
Player player1 = Player.builder().userName("Bobby").gameId(game.getId()).territories(new ArrayList<Territory>()).color(Player.PlayerColor.GREY).build();
GameRepository.getInstance().addPlayer(player1);
GameEntry gameEntry = GameEntry.builder().player(player1).build();
game.getState().onActionPlayer(gameEntry);

assertEquals(game.getPlayers().size(), 1);
assertEquals(game.getPlayers().get(0).getUserName(), "Bobby");

// Remove the player from the game
GameExit exitState = GameExit.builder().player(player1).build();
game.getState().onActionPlayer(exitState);

assertEquals(game.getPlayers().size(), 0);

// Add the player again
game.getState().onActionPlayer(gameEntry);

assertEquals(game.getPlayers().size(), 1);
assertEquals(game.getPlayers().get(0).getUserName(), "Bobby");

// Create a second player and add it to the game
Player player2 = Player.builder().userName("Tommy").gameId(game.getId()).territories(new ArrayList<Territory>()).color(Player.PlayerColor.GREY).build();

assertEquals(game.getState().getClass().toString(), LobbyState.class.toString());

GameEntry gameEntry = GameEntry.builder().player(player1).build();
game.getState().onActionPlayer(gameEntry);

GameRepository.getInstance().addPlayer(player1);
GameRepository.getInstance().addPlayer(player2);

GameEntry gameEntry2 = GameEntry.builder().player(player2).build();
game.getState().onActionPlayer(gameEntry2);

GameRepository.getInstance().addPlayer(player2);
assertEquals(game.getPlayers().size(), 2);
assertEquals(game.getPlayers().get(1).getUserName(), "Tommy");

// SetupState
assertEquals(game.getState().getClass().toString(), GameSetupState.class.toString());

assertEquals(game.getPlayers().size(), game.getConfiguration().getNumberOfPlayers());

GameRepository.getInstance().deleteGame(game);
GameRepository.getInstance().insertGame(game);
GameRepository.getInstance().deleteGame(game);
GameRepository.getInstance().insertGame(game);
GameRepository.getInstance().deleteGame(game);
GameRepository.getInstance().insertGame(game);
GameRepository.getInstance().deleteGame(game);
GameRepository.getInstance().insertGame(game);
GameRepository.getInstance().deleteGame(game);
LOGGER.info("GameSetupState {}" , player1.getObjective().getObjective());
LOGGER.info("GameSetupState {}" , player2.getObjective().getObjective());

GameRepository.getInstance().removePlayer(player1.getUserName());
GameRepository.getInstance().removePlayer(player2.getUserName());
List<Territory> territories = player1.getTerritories();
List<TerritoryBody> territoryBodies = new ArrayList<>();
TerritoryBody territoryBody = null;
for (Territory territory : territories) {
territoryBody = TerritoryBody.builder().name(territory.getName()).troops(2).build();
territoryBodies.add(territoryBody);
}

assertTrue(true, "Test passed");
SetUpBody setUpBody = SetUpBody.builder().territories(territoryBodies).build();
TerritorySetup territorySetup = TerritorySetup.builder().player(player1).setUpBody(setUpBody).build();
game.getState().onActionPlayer(territorySetup);

for (Territory territory : territories) {
assertEquals(territory.getArmies(), 2);
}

List<Territory> territories2 = player2.getTerritories();
for (Territory territory : territories2) {
assertEquals(territory.getArmies(), 0);
}
List<TerritoryBody> territoryBodies2 = new ArrayList<>();
TerritoryBody territoryBody2 = null;
for (Territory territory : territories2) {
territoryBody2 = TerritoryBody.builder().name(territory.getName()).troops(2).build();
territoryBodies2.add(territoryBody2);
}

LOGGER.info("Player2: {}", GameRepository.getInstance().getPlayer(player2.getUserName(), game.getId()));

SetUpBody setUpBody2 = SetUpBody.builder().territories(territoryBodies2).build();
TerritorySetup territorySetup2 = TerritorySetup.builder().player(player2).setUpBody(setUpBody2).build();
game.getState().onActionPlayer(territorySetup2);

for (Territory territory : territories2) {
assertEquals(territory.getArmies(), 2);
}

assertEquals(game.getState().getClass().toString(), StartTurnState.class.toString());

// StartTurnState

TerritoryCardBody territoryCardBody1 = TerritoryCardBody.builder().name(territories.get(0).getName()).symbol(CardSymbol.ARTILLERY.toString()).build();
TerritoryCardBody territoryCardBody2 = TerritoryCardBody.builder().name(territories.get(1).getName()).symbol(CardSymbol.ARTILLERY.toString()).build();
TerritoryCardBody territoryCardBody3 = TerritoryCardBody.builder().name(territories.get(2).getName()).symbol(CardSymbol.ARTILLERY.toString()).build();

Territory t1 = GameRepository.getInstance().getTerritory(territoryCardBody1.getName(), player1.getUserName(), game.getId());
Territory t2 = GameRepository.getInstance().getTerritory(territoryCardBody2.getName(), player1.getUserName(), game.getId());
Territory t3 = GameRepository.getInstance().getTerritory(territoryCardBody3.getName(), player1.getUserName(), game.getId());

TerritoryCard territoryCard = TerritoryCard.builder().territory(t1).symbol(CardSymbol.ARTILLERY).build();
TerritoryCard territoryCard2 = TerritoryCard.builder().territory(t2).symbol(CardSymbol.ARTILLERY).build();
TerritoryCard territoryCard3 = TerritoryCard.builder().territory(t3).symbol(CardSymbol.ARTILLERY).build();



GameRepository.getInstance().insertComboCard(territoryCard, player1, game.getId());
GameRepository.getInstance().insertComboCard(territoryCard2, player1, game.getId());
GameRepository.getInstance().insertComboCard(territoryCard3, player1, game.getId());

List<TerritoryCard> territoryCardList = new ArrayList<>();

territoryCardList.add(territoryCard);
territoryCardList.add(territoryCard2);
territoryCardList.add(territoryCard3);

List<TerritoryCardBody> territoryCardBodies = new ArrayList<>();
territoryCardBodies.add(territoryCardBody1);
territoryCardBodies.add(territoryCardBody2);
territoryCardBodies.add(territoryCardBody3);

player1.setComboCards(territoryCardList);

ComboRequestBody comboRequestBody = ComboRequestBody.builder().username(player1.getUserName()).comboCards(territoryCardBodies).build();
ComboRequest comboRequest = ComboRequest.builder().player(player1).comboRequestBody(comboRequestBody).build();
game.getState().onActionPlayer(comboRequest);

assertTrue(player1.getComboCards().size() == 0);


List<TerritoryBody> territoryBodies3 = new ArrayList<>();
TerritoryBody territoryBody3 = null;

for (int i = 0; i < 4; i++) {
territoryBody3 = TerritoryBody.builder().name(territories.get(i).getName()).troops(1).build();
territoryBodies3.add(territoryBody3);
}

}
SetUpBody setUpBody3 = SetUpBody.builder().territories(territoryBodies3).build();
TerritorySetup territorySetup3 = TerritorySetup.builder().player(player1).setUpBody(setUpBody3).build();
game.getState().onActionPlayer(territorySetup3);

assertEquals(game.getState().getClass().toString(), BattleState.class.toString());

}


}

0 comments on commit b472be6

Please sign in to comment.