Skip to content

Commit

Permalink
Commit fase attacco iniziale
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto-Negro committed Feb 21, 2024
1 parent 60fb703 commit 7201c5e
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public void enterInTheGame(@Payload PlayerBody body, @DestinationVariable String
game = GameRepository.getInstance().getCompletedGame(id);
Player player = Player.builder().userName(body.getUsername()).gameId(id).territories(new ArrayList<Territory>()).color(Player.PlayerColor.GREY).build();
GameEntry action = GameEntry.builder().player(player).build();
game.onActionPlayer(action);
GameRepository.getInstance().addPlayer(player);
game.onActionPlayer(action);

} catch (GameException | DatabaseConnectionException | UserException e) {throw e;
} catch (FullGameException e) {
MessageBrokerSingleton.getInstance().getTemplate().convertAndSend("/topic/partite/" + id, "Partita piena");
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/mvcguru/risiko/maven/eclipse/model/IGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
public abstract class IGame implements Serializable{
protected static final Logger LOGGER = LoggerFactory.getLogger(IGame.class);

protected String id;
protected String id; //

protected GameConfiguration configuration;
protected GameConfiguration configuration; //

protected IDeck deckObjective;
protected IDeck deckObjective; //

protected IDeck deckTerritory;
protected IDeck deckTerritory; //

private List<Continent> continents;
private List<Continent> continents; //

@Builder.Default
protected ArrayList<Player> players = new ArrayList<>();

protected GameState state;
protected GameState state; //

protected Turn currentTurn;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public int continentCheck(List<Territory> territories){
troops += c.getBonusArmies();
} else {
LOGGER.info("Player doesn't have all the territories of the continent");
LOGGER.info("Player objective: {}", player.getObjective().toString());
}
}
return troops;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mvcguru.risiko.maven.eclipse.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
public class User {

private String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public abstract class ObjectiveCard extends ICard{

@JsonProperty("objective")
private String description;
private String objective;

public abstract boolean isComplete(IGame game, String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import com.mvcguru.risiko.maven.eclipse.model.player.Player;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
public class ConquerContinentObjective extends ObjectiveCard{

@JsonProperty("objective")
private String description;

@JsonProperty("continent1")
protected int continent1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
import com.mvcguru.risiko.maven.eclipse.model.player.Player.PlayerColor;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
public class DestroyArmyObjective extends ObjectiveCard{

@JsonProperty("objective")
private String description;

@JsonProperty("nTerritory")
protected int nTerritory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import com.mvcguru.risiko.maven.eclipse.model.Territory;
import com.mvcguru.risiko.maven.eclipse.model.card.ObjectiveCard;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@NoArgsConstructor
public class TerritoriesObjective extends ObjectiveCard{

@JsonProperty("objective")
private String description;

@JsonProperty("nTerritory")
protected int nTerritory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mvcguru.risiko.maven.eclipse.model.Continent;
import com.mvcguru.risiko.maven.eclipse.model.Game;
Expand All @@ -20,6 +22,9 @@
import com.mvcguru.risiko.maven.eclipse.model.GameConfiguration.GameMode;
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.card.objectives.ConquerContinentObjective;
import com.mvcguru.risiko.maven.eclipse.model.card.objectives.DestroyArmyObjective;
import com.mvcguru.risiko.maven.eclipse.model.card.objectives.TerritoriesObjective;
import com.mvcguru.risiko.maven.eclipse.model.deck.IDeck;
import com.mvcguru.risiko.maven.eclipse.model.deck.ObjectivesDeck;
import com.mvcguru.risiko.maven.eclipse.model.deck.TerritoriesDeck;
Expand Down Expand Up @@ -82,12 +87,52 @@ 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());
String json = new String(data, StandardCharsets.UTF_8);

// Deserializza il JSON in una lista di mappe
List<Map<String,Object>> objectivesList = mapper.readValue(json, new TypeReference<List<Map<String,Object>>>(){});

List<ObjectiveCard> objectives = new ArrayList<>();

for (Map<String, Object> map : objectivesList) {
String type = (String) map.get("type");
ObjectiveCard objective = null;

switch (type) {
case "conquerContinent":
ConquerContinentObjective objective1 = mapper.convertValue(map, ConquerContinentObjective.class);
objective = objective1;
break;
case "destroyArmy":
DestroyArmyObjective objective2 = mapper.convertValue(map, DestroyArmyObjective.class);
objective = objective2;
break;
case "territories":
TerritoriesObjective objective3 = mapper.convertValue(map, TerritoriesObjective.class);
objective = objective3;
break;
default:
throw new IllegalArgumentException("Unknown type: " + type);
}

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
objectives.add(objective);
}
}


IDeck deck = new ObjectivesDeck();
for (ObjectiveCard o : objectives) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public synchronized ArrayList<Player> getAllPlayers(String gameId) throws GameEx
return db.getAllPlayers(gameId);
}

public synchronized void updateObjective(String username, ObjectiveCard objective) throws GameException {
db.updatePlayerObjective(username, objective);
public synchronized void updateObjective(String username, String description) throws GameException {
db.updatePlayerObjective(username, description);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ private void executeInsert(String sql, Object... values) throws GameException {
for (int i = 0; i < values.length; i++) {
pstmt.setObject(i + 1, values[i]);
}
LOGGER.info("----------------------------------");
LOGGER.info("Inserting into database: {}", pstmt);
LOGGER.info("----------------------------------");

pstmt.executeUpdate();
} catch (SQLException e) {throw new GameException("Errore durante l'inserimento", e);
Expand Down Expand Up @@ -260,7 +257,7 @@ public Player getPlayer(String username, String gameId) throws GameException, IO
.setUpCompleted(rs.getBoolean("setUpCompleted"))
.build();
}
LOGGER.info("Player: {}", player);
LOGGER.info("Player sdsdsaadasdasdasdasd: {}", player);
return player;
} catch (SQLException e) {
throw new GameException("Errore durante il recupero del giocatore.", e);
Expand Down Expand Up @@ -313,7 +310,6 @@ public Territory getTerritory(String territoryName, String player, String gameId
.svgPath(svgPath)
.build();
}
LOGGER.info("Territory: {}", territory);
return territory;
} catch (SQLException e) {
throw new GameException("Errore durante il recupero del territorio.", e);
Expand All @@ -337,7 +333,6 @@ public ArrayList<Territory> getAllTerritories(String player, String gameId) thro
Territory territory = getTerritory(rs.getString("name"), player, gameId);
result.add(territory);
}
LOGGER.info("Territories: {}", result);
return result;
}
} catch (SQLException e) {
Expand Down Expand Up @@ -511,9 +506,9 @@ public void updateSetUpCompleted(String username, boolean setUpCompleted) throws
}

@Override
public void updatePlayerObjective(String username, ObjectiveCard objective) throws GameException {
public void updatePlayerObjective(String username, String description) throws GameException {
String sql = "UPDATE players SET objective = ? WHERE username = ?";
executeUpdate(sql, (objective).getDescription(), username);
executeUpdate(sql, description, username);
}

@Override
Expand Down Expand Up @@ -664,22 +659,28 @@ private IGame extractGameFromResultSet(ResultSet rs) throws GameException, IOExc
LOGGER.error("Stato non riconosciuto");
break;
}
} catch (SQLException e) {throw new GameException("Errore durante il recupero di una partita", e);
}
} catch (SQLException e) {throw new GameException("Errore durante il recupero di una partita", e); }
return newGame;
}

private ObjectiveCard findObjectiveCard(String description, String gameId) throws GameException, IOException {
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) {
if (card.getDescription().equals(description)) {
//LOGGER.info("Card: {}", card);
if (card.getObjective().equals(description)) {
cardReturn = card;
LOGGER.error("Carta {} ------ trovata", description);
return cardReturn;
}
}
LOGGER.error("Carta {} non trovata", description);
return cardReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface DataDao {
void deletePlayer(String username) throws GameException;
ArrayList<Player> getAllPlayers(String gameId) throws GameException, IOException;
Player getPlayer(String username, String gameId) throws GameException, IOException;
void updatePlayerObjective(String username, ObjectiveCard objective) throws GameException;
void updatePlayerObjective(String username, String description) throws GameException;

//TerritoryDao
void insertTerritory(Territory territory, String gameId) throws GameException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,45 @@ private void assignObjective(IDeck deckObjective) throws GameException, Database
deckObjective.shuffle();
ObjectiveCard card = null;

card = (ObjectiveCard) deckObjective.drawCard();
Player player = game.getPlayers().get(0);
player.setObjective(card);
LOGGER.info("Player {} has objective -------------- {}", player.getUserName(), card.getObjective());
GameRepository.getInstance().updateObjective(player.getUserName(), card.getObjective());

card = (ObjectiveCard) deckObjective.drawCard();
player = game.getPlayers().get(1);
player.setObjective(card);
LOGGER.info("Player {} has objective -------------- {}", player.getUserName(), card.getObjective());
GameRepository.getInstance().updateObjective(player.getUserName(), card.getObjective());

card = (ObjectiveCard) deckObjective.drawCard();
player = game.getPlayers().get(2);
player.setObjective(card);
LOGGER.info("Player {} has objective -------------- {}", player.getUserName(), card.getObjective());
GameRepository.getInstance().updateObjective(player.getUserName(), card.getObjective());

/*
for (Player player : game.getPlayers()) {
try {
card = (ObjectiveCard) deckObjective.drawCard();
player.setObjective(card);
GameRepository.getInstance().updateObjective(player.getUserName(), card);
GameRepository.getInstance().updateObjective(player.getUserName(), card.getObjective());
} catch (Exception e) {
LOGGER.error("--------------Errore nell'assegnamento degli obiettivi-------------", e);
}*/

if(game.getConfiguration().getMode().name().equals("EASY"))
deckObjective.insertCard(card);

}

}

private void assignTerritories(IDeck deckTerritory) throws GameException, DatabaseConnectionException, UserException {
deckTerritory.shuffle();
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 @@ -108,6 +132,7 @@ 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 @@ -43,7 +43,7 @@ public void onActionPlayer(ComboRequest comboRequest) {
LOGGER.error("Errore nella rimozione della carta combo");
}
}
}
}
game.getCurrentTurn().comboCardsCheck(result);
try {
GameRepository.getInstance().updateTurnNumberOfTroops(game.getCurrentTurn(), game.getCurrentTurn().getNumberOfTroops());
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/objectives_hard.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
{
"type": "destroyArmy",
"objective": "Distruggere completamente l'armata blu, se le armate non sono presenti nel gioco, se le armate sono possedute dal giocatore che ha l'obiettivo di distruggerle o se l'ultima armata viene distrutta da un altro giocatore, l'obiettivo diventa conquistare 24 territori.",
"colorArmy": "BLU",
"colorArmy": "BLUE",
"nTerritory": "24"
},
{
Expand Down

0 comments on commit 7201c5e

Please sign in to comment.