Skip to content

Commit

Permalink
Commit fine refactoring per ultima release
Browse files Browse the repository at this point in the history
  • Loading branch information
dromano32 committed Feb 21, 2024
1 parent a5599ed commit 8675d4f
Show file tree
Hide file tree
Showing 27 changed files with 155 additions and 603 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/mvcguru/risiko/maven/eclipse/model/Turn.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.mvcguru.risiko.maven.eclipse.model;

import java.io.Serializable;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mvcguru.risiko.maven.eclipse.controller.body_request.ResultNoticeBody;
import com.mvcguru.risiko.maven.eclipse.exception.DatabaseConnectionException;
import com.mvcguru.risiko.maven.eclipse.exception.GameException;
import com.mvcguru.risiko.maven.eclipse.exception.UserException;
Expand All @@ -26,7 +25,7 @@
@SuperBuilder
@NoArgsConstructor
public class Turn implements Serializable{
private static final String log = "Attacker: {} | Defender: {}";
private static final String LOG = "Attacker: {} | Defender: {}";
private static final Logger LOGGER = LoggerFactory.getLogger(Turn.class);

private Player player;
Expand Down Expand Up @@ -130,22 +129,23 @@ private int troopsForJollydCombo(List<TerritoryCard> comboCards) {
}

private int troopsForTris(List<TerritoryCard> comboCards) {
if(!comboCards.stream().anyMatch(card -> card.getSymbol() == CardSymbol.JOLLY))
if (!comboCards.stream().noneMatch(card -> CardSymbol.JOLLY.equals(card.getSymbol())))
return 10;
return 0;
}

public void attack() throws GameException, DatabaseConnectionException, UserException {

SecureRandom random = new SecureRandom();

Integer[] attRolls = new Integer[numAttDice];
Integer[] defRolls = new Integer[numDefDice];

for (int i = 0; i < numAttDice; i++) {
attRolls[i] = (int) (Math.random() * 6) + 1;
attRolls[i] = random.nextInt(6) + 1;
}

for (int i = 0; i < numDefDice; i++) {
defRolls[i] = (int) (Math.random() * 6) + 1;
defRolls[i] = random.nextInt(6) + 1;
}

Arrays.sort(attRolls, Collections.reverseOrder());
Expand All @@ -161,18 +161,17 @@ public void attack() throws GameException, DatabaseConnectionException, UserExce
else
attLosses++;
}
LOGGER.info(log, attRolls, defRolls);
LOGGER.info(log, attLosses, defLosses);
LOGGER.info(LOG, attRolls, defRolls);
LOGGER.info(LOG, attLosses, defLosses);

if(defenderTerritory.getArmies() > defLosses) {
LOGGER.info("Siamo nell'if");
LOGGER.info(log, attackerTerritory.getArmies(), defenderTerritory.getArmies());
LOGGER.info(LOG, attackerTerritory.getArmies(), defenderTerritory.getArmies());
attackerTerritory.setArmies(attackerTerritory.getArmies() - attLosses);
GameRepository.getInstance().updateTerritoryArmies(attackerTerritory.getName(), player.getGameId(), attackerTerritory.getArmies());
defenderTerritory.setArmies(defenderTerritory.getArmies() - defLosses);
GameRepository.getInstance().updateTerritoryArmies(defenderTerritory.getName(), player.getGameId(), defenderTerritory.getArmies());
ResultNoticeBody result = ResultNoticeBody.builder().isConquered(false).lostAttTroops(attLosses).lostDefTroops(defLosses).build();
LOGGER.info(log, attackerTerritory.getArmies(), defenderTerritory.getArmies());
LOGGER.info(LOG, attackerTerritory.getArmies(), defenderTerritory.getArmies());
resetBattleInfo();
}
else {
Expand All @@ -183,7 +182,7 @@ public void attack() throws GameException, DatabaseConnectionException, UserExce
GameRepository.getInstance().updateTerritoryOwner(defenderTerritory.getName(), player);
defenderTerritory.setArmies(0);
GameRepository.getInstance().updateTerritoryArmies(defenderTerritory.getName(), player.getGameId(), 0);
LOGGER.info(log, attackerTerritory.getArmies(), defenderTerritory.getArmies());
LOGGER.info(LOG, attackerTerritory.getArmies(), defenderTerritory.getArmies());

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,27 @@ public class ConquerContinentObjective extends ObjectiveCard{

@Override
public boolean isComplete(IGame game, String username) {
Player player = game.findPlayerByUsername(username);
if (continent3 != 7) {
if (player.getTerritories().containsAll(game.getContinents().get(continent1).getTerritories())
&& player.getTerritories().containsAll(game.getContinents().get(continent2).getTerritories()))
return true;
}
else {
if (player.getTerritories().containsAll(game.getContinents().get(continent1).getTerritories())
&& player.getTerritories().containsAll(game.getContinents().get(continent2).getTerritories()))
for (Continent continent : game.getContinents())
if (continent != game.getContinents().get(continent1) && continent != game.getContinents().get(continent2) &&
player.getTerritories().containsAll(continent.getTerritories()))
return true;
}
return false;
Player player = game.findPlayerByUsername(username);

boolean ownsFirstTwoContinents = player.getTerritories().containsAll(game.getContinents().get(continent1).getTerritories()) &&
player.getTerritories().containsAll(game.getContinents().get(continent2).getTerritories());

if (!ownsFirstTwoContinents) {
return false;
}

if (continent3 == 7) {
for (Continent continent : game.getContinents()) {
if (continent == game.getContinents().get(continent1) || continent == game.getContinents().get(continent2)) {
continue;
}
if (player.getTerritories().containsAll(continent.getTerritories())) {
return true;
}
}
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ public class DestroyArmyObjective extends ObjectiveCard{
protected PlayerColor colorArmy ;

@Override
public boolean isComplete(IGame game, String unsernameDefender) {

Player attacker = game.getCurrentTurn().getPlayer();
Player defender = game.findPlayerByUsername(unsernameDefender);
Player colorOwner = game.findPlayerByColor(colorArmy);


if (defender.getTerritories().size() == 0 && defender.getColor() == colorArmy) {
return true;
}
// if (attacker.getColor() == colorArmy || colorOwner.getTerritories().size() == 0) {
if (attacker.getColor() == colorArmy) {
if (attacker.getTerritories().size() >= nTerritory) {
return true;
}
}
return false;
public boolean isComplete(IGame game, String usernameDefender) {
Player attacker = game.getCurrentTurn().getPlayer();
Player defender = game.findPlayerByUsername(usernameDefender);

boolean defenderEliminated = defender.getTerritories().size() == 0 && defender.getColor() == colorArmy;
if (defenderEliminated) {
return true;
}

return attacker.getColor() == colorArmy && attacker.getTerritories().size() >= nTerritory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ public class TerritoriesObjective extends ObjectiveCard{

@Override
public boolean isComplete(IGame game, String username) {
int territoriesWithRequiredTroops = 0;
if(nTerritory == 24)
if (game.findPlayerByUsername(username).getTerritories().size() >= nTerritory)
return true;
else if (nTerritory == 18) {
for (Territory territory : game.getCurrentTurn().getPlayer().getTerritories()) {
if (territory.getArmies() >= 2) {
territoriesWithRequiredTroops++;
}
}
return territoriesWithRequiredTroops >= nTerritory;
}
return false;
int territoriesWithRequiredTroops = 0;
if (nTerritory == 24 || nTerritory == 17) {
if (game.findPlayerByUsername(username).getTerritories().size() >= nTerritory) {
return true;
}
} else if (nTerritory == 18 || nTerritory == 12) {
for (Territory territory : game.getCurrentTurn().getPlayer().getTerritories()) {
if (territory.getArmies() >= 2) {
territoriesWithRequiredTroops++;
}
}
return territoriesWithRequiredTroops >= nTerritory;
}
return false;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class Player implements Serializable{
Logger LOGGER = LoggerFactory.getLogger(Player.class);

private String userName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ public IDeck createObjectiveDeck(GameConfiguration configuration) throws IOExcep
if (fileName == null) {
LOGGER.error("Unsupported game mode: {}", mode);
}
/*
ObjectMapper mapper = new ObjectMapper();
byte[] data = FileCopyUtils.copyToByteArray(new ClassPathResource(fileName).getInputStream());
String json = new String(data, StandardCharsets.UTF_8);
ObjectiveCard[] objectives = mapper.readValue(json, ObjectiveCard[].class);
*/

ObjectMapper mapper = new ObjectMapper();
byte[] data = FileCopyUtils.copyToByteArray(new ClassPathResource(fileName).getInputStream());
Expand Down Expand Up @@ -125,8 +118,7 @@ public IDeck createObjectiveDeck(GameConfiguration configuration) throws IOExcep

if (objective != null) {
String description = (String) map.get("objective");
//LOGGER.info("Objective: {}", description);
objective.setObjective(description); // Assicurati che ci sia un metodo setDescription nella classe ObjectiveCard
objective.setObjective(description);
objectives.add(objective);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mvcguru.risiko.maven.eclipse.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.mvcguru.risiko.maven.eclipse.exception.DatabaseConnectionException;
import com.mvcguru.risiko.maven.eclipse.exception.FullGameException;
Expand All @@ -10,7 +9,6 @@
import com.mvcguru.risiko.maven.eclipse.model.IGame;
import com.mvcguru.risiko.maven.eclipse.model.Territory;
import com.mvcguru.risiko.maven.eclipse.model.Turn;
import com.mvcguru.risiko.maven.eclipse.model.card.ObjectiveCard;
import com.mvcguru.risiko.maven.eclipse.model.card.TerritoryCard;
import com.mvcguru.risiko.maven.eclipse.model.player.Player;
import com.mvcguru.risiko.maven.eclipse.service.database.DataDao;
Expand Down Expand Up @@ -66,8 +64,8 @@ public synchronized IGame getGame(String gameId) throws GameException, IOExcepti
return db.getGame(gameId);
}

public synchronized ArrayList<IGame> getAllGames() throws GameException, FullGameException, IOException {
ArrayList<IGame> games = db.getAllGames();
public synchronized List<IGame> getAllGames() throws GameException, FullGameException, IOException {
List<IGame> games = db.getAllGames();
for (IGame g : games ) {
List<Player> lista = getAllPlayers(g.getId());
for (Player p : lista) {
Expand Down Expand Up @@ -96,7 +94,7 @@ public synchronized Player getPlayer(String username, String gameId) throws Game
return db.getPlayer(username, gameId);
}

public synchronized ArrayList<Player> getAllPlayers(String gameId) throws GameException, IOException {
public synchronized List<Player> getAllPlayers(String gameId) throws GameException, IOException {
return db.getAllPlayers(gameId);
}

Expand Down Expand Up @@ -128,7 +126,7 @@ public synchronized Territory getTerritory(String territoryName, String player,
return db.getTerritory(territoryName, player, gameId);
}

public synchronized ArrayList<Territory> getAllTerritories(String player, String gameId) throws GameException {
public synchronized List<Territory> getAllTerritories(String player, String gameId) throws GameException {
return db.getAllTerritories(player, gameId);
}

Expand Down Expand Up @@ -199,7 +197,7 @@ public synchronized void updateOwner(TerritoryCard t, String player, String game
db.updateOwner(t, player, gameId);
}

public synchronized ArrayList<TerritoryCard> getAllComboCards(String player, String gameId) throws GameException {
public synchronized List<TerritoryCard> getAllComboCards(String player, String gameId) throws GameException {
return db.getAllComboCards(player, gameId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private DaoSQLiteImpl(String dbUrl) throws DatabaseConnectionException {
}
}

public synchronized static DaoSQLiteImpl getInstance() throws DatabaseConnectionException {
public static synchronized DaoSQLiteImpl getInstance() throws DatabaseConnectionException {
if (instance == null) {
instance = new DaoSQLiteImpl(DatabaseConnection.getSqliteDbUrl());
}
Expand Down Expand Up @@ -668,12 +668,10 @@ private ObjectiveCard findObjectiveCard(String description, String gameId) throw
IGame game = getGame(gameId);
ObjectivesDeck objectiveCards = (ObjectivesDeck) game.getDeckObjective();
ObjectiveCard cardReturn = null;
//LOGGER.info("ObjectiveCards: {}", objectiveCards);
LOGGER.info("Description: {}", description);
List<ObjectiveCard> cards = new LinkedList<>(objectiveCards.getCards());
LOGGER.info("Cards: {}", cards.size());
for (ObjectiveCard card : cards) {
//LOGGER.info("Card: {}", card);
if (card.getObjective().equals(description)) {
cardReturn = card;
LOGGER.error("Carta {} ------ trovata", description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.mvcguru.risiko.maven.eclipse.model.Territory;
import com.mvcguru.risiko.maven.eclipse.model.Turn;
import com.mvcguru.risiko.maven.eclipse.model.User;
import com.mvcguru.risiko.maven.eclipse.model.card.ObjectiveCard;
import com.mvcguru.risiko.maven.eclipse.model.card.TerritoryCard;
import com.mvcguru.risiko.maven.eclipse.model.player.Player;
import com.mvcguru.risiko.maven.eclipse.states.GameState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void onActionPlayer(AttackRequest attackRequest) throws GameException, Da

@Override
public void onActionPlayer(DefenceRequest defenceRequest) throws GameException, DatabaseConnectionException, UserException {
System.out.println("DEFENCE REQUEST -------------------------------------------");
LOGGER.info("DefenceRequest");
game.getCurrentTurn().setNumDefDice(defenceRequest.getDefenderRequestBody().getNumDefDice());
GameRepository.getInstance().updateNumDefenseDice(game.getCurrentTurn(), game.getCurrentTurn().getNumDefDice());
game.getCurrentTurn().attack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public void onActionPlayer(EndTurnMovement endTurnMovement) throws GameException

@Override
public void onActionPlayer(EndTurn endTurn) throws GameException, DatabaseConnectionException, UserException {
//if(endTurn.getPlayer().getObjective()){
//game.setState(EndGameState.builder().game(game).build());
//setWinner(endTurn.getPlayer());
//game.
if(game.getCurrentTurn().isConquered())
endTurn.getPlayer().getComboCards().add((TerritoryCard)game.getDeckTerritory().drawCard());
game.changeTurn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void onActionPlayer(ConquerAssignment conquerAssignment)throws GameExcept

public void onActionPlayer(GoToEndTurn goToEndTurn) throws GameException, DatabaseConnectionException, UserException {}

public void onActionPlayer(EndTurnMovement EndTurnMovement) throws GameException, DatabaseConnectionException, UserException {}
public void onActionPlayer(EndTurnMovement endTurnMovement) throws GameException, DatabaseConnectionException, UserException {}

public void onActionPlayer(EndTurn endTurn) throws GameException, DatabaseConnectionException, UserException {};
public void onActionPlayer(EndTurn endTurn) throws GameException, DatabaseConnectionException, UserException {}
}
Loading

0 comments on commit 8675d4f

Please sign in to comment.