Added:(Network) Totem Choice first implementation

This commit is contained in:
rubenpirreram
2026-05-13 16:55:12 +02:00
parent 0f92696cbd
commit 110526d836
13 changed files with 58 additions and 25 deletions
@@ -137,7 +137,7 @@ public class ClientLauncherTUI {
if(admissibleChar.contains(action)) if(admissibleChar.contains(action))
{ {
if (!action.equals("7") && !action.equals("8") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) { if (!action.equals("7") && !action.equals("8")&& !action.equals("9") && !action.equals("A") && !action.equals("B") && !action.equals("C")&&!action.equals("a") && !action.equals("b") && !action.equals("c")) {
try { try {
System.out.println("Insert the required position:"); System.out.println("Insert the required position:");
pos = scanner.nextInt(); pos = scanner.nextInt();
@@ -156,6 +156,7 @@ public class ClientLauncherTUI {
case "6" -> controller.pickOptionalBuildingCard(username, pos); case "6" -> controller.pickOptionalBuildingCard(username, pos);
case "7" -> controller.noOptionalCard(username); case "7" -> controller.noOptionalCard(username);
case "8" -> controller.skipTurn(username); case "8" -> controller.skipTurn(username);
case "9" -> controller.totemChoice(username,pos);
case "A", "a" -> view.fullRender(); case "A", "a" -> view.fullRender();
case "B", "b" -> view.renderBoard(); case "B", "b" -> view.renderBoard();
case "C", "c" -> view.renderPlayer(); case "C", "c" -> view.renderPlayer();
@@ -239,4 +239,17 @@ public class ClientController {
} }
} }
//TODO
public void totemChoice(String playerUsername,int pos) {
if(!Objects.equals(playerUsername, miniModel.currentState.getCurrentPlayer().getUserName()))
view.showError("It's not your turn!");
else {
try {
client.totemChoice(playerUsername, String.valueOf(miniModel.availableTotems.get(pos)));
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
}
} }
@@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.Controller;
import it.polimi.ingsw.gc14.Model.Game; import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Model.Player; import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Totems;
/** /**
* Controller class that manages interactions between the client-side logic * Controller class that manages interactions between the client-side logic
@@ -200,8 +201,16 @@ public class GameController {
*/ */
public boolean slotChoice(String playerUsername,int pos) { public boolean slotChoice(String playerUsername,int pos) {
Player player= model.getPlayerByUsername(playerUsername); Player player= model.getPlayerByUsername(playerUsername);
if(player==null)//playerIndex>=model.) if(player==null)
return false; return false;
return model.SlotChoiceByIndex(model.getPlayerByUsername(playerUsername),pos); return model.SlotChoiceByIndex(model.getPlayerByUsername(playerUsername),pos);
} }
//TODO
public boolean TotemChoice(String playerUsername,String totem) {
Player player= model.getPlayerByUsername(playerUsername);
if(player==null)
return false;
return model.TotemChoice(player, Totems.valueOf(totem));
}
} }
@@ -50,7 +50,7 @@ public class Game implements Serializable {
private Queue<Player> totemChoiceQueue = new LinkedList<>(); private Queue<Player> totemChoiceQueue = new LinkedList<>();
//TODO //TODO
private boolean TotemChoice(Player player,Totems totem) { public boolean TotemChoice(Player player,Totems totem) {
if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE)) if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE))
return false; return false;
if(!getCurrentState().getCurrentPlayer().equals(player)) if(!getCurrentState().getCurrentPlayer().equals(player))
@@ -16,22 +16,23 @@ public class MiniModel implements Serializable {
public OrderLogicCard orderLogicCard; public OrderLogicCard orderLogicCard;
public CurrentState currentState; public CurrentState currentState;
public Map<String,Player> players; public Map<String,Player> players;
public List<Object>availableTotems; public List<Totems>availableTotems;
public MiniModel(Board board) { public MiniModel(Board board) {
this.board = board; this.board = board;
} }
public MiniModel(Board board, Map<Slot,Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List<Player> players, List<Object>availableTotems) { public MiniModel(Board board, Map<Slot,Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List<Player> players, List<Totems>availableTotems) {
this.board = board; this.board = board;
this.slotPlayerMap = slotPlayerMap; this.slotPlayerMap = slotPlayerMap;
this.orderLogicCard = orderLogicCard; this.orderLogicCard = orderLogicCard;
this.currentState = currentState; this.currentState = currentState;
this.players=new HashMap<>(); this.players=new HashMap<>();
this.availableTotems=availableTotems;
setPlayers(players); setPlayers(players);
} }
public void setBoard(Board board) { public void setBoard(Board board) {
this.board = board; this.board = board;
} }
public void setAvailableTotems(List<Object>availableTotems) { public void setAvailableTotems(List<Totems>availableTotems) {
this.availableTotems = availableTotems; this.availableTotems = availableTotems;
} }
public void setSlotPlayerMap(Map<Slot,Player> slotPlayerMap) { public void setSlotPlayerMap(Map<Slot,Player> slotPlayerMap) {
@@ -29,4 +29,5 @@ public interface IClient {
public void noOptionalCard(String playerUsername) throws RemoteException; public void noOptionalCard(String playerUsername) throws RemoteException;
public void slotChoice(String playerUsername,int pos) throws RemoteException; public void slotChoice(String playerUsername,int pos) throws RemoteException;
public void totemChoice(String playerUsername,String totem) throws RemoteException;
} }
@@ -2,6 +2,7 @@ package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController; import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Model.Totems;
import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView; import it.polimi.ingsw.gc14.View.IView;
@@ -15,20 +16,15 @@ import java.util.List;
public class TotemChoice extends NetworkEvent implements Serializable { public class TotemChoice extends NetworkEvent implements Serializable {
/** Index of the card to draw */ /** Index of the card to draw */
private int pos; private String totem;
private List<Object>availableTotems; private List<Totems>availableTotems;
public void setAvailableTotems(List<Object> availableTotems) { public void setAvailableTotems(List<Totems> availableTotems) {
this.availableTotems = availableTotems; this.availableTotems = availableTotems;
} }
/** //TODO
* Class constructor. public TotemChoice(String username, String totem) {
* Initializes all the attributes.
* @param username the name of the player requesting the event
* @param pos the index of the card to draw
*/
public TotemChoice(String username, int pos) {
super(username, EventType.TOTEM_CHOICE, false); super(username, EventType.TOTEM_CHOICE, false);
this.pos = pos; this.totem = totem;
} }
/** /**
@@ -37,7 +33,7 @@ public class TotemChoice extends NetworkEvent implements Serializable {
*/ */
@Override @Override
public boolean apply(GameController gameController) { public boolean apply(GameController gameController) {
return gameController.slotChoice(username, pos); return gameController.TotemChoice(username, totem);
} }
//TODO //TODO
@@ -224,6 +224,10 @@ public class RMIClient implements IClient {
public void slotChoice(String playerUsername,int pos) throws RemoteException { public void slotChoice(String playerUsername,int pos) throws RemoteException {
stub.slotChoice(playerUsername,pos); stub.slotChoice(playerUsername,pos);
} }
//TODO
public void totemChoice(String playerUsername,String totem) throws RemoteException {
stub.totemChoice(playerUsername,totem);
}
} }
@@ -22,7 +22,7 @@ public interface IGameServer extends Remote {
void pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException; void pickOptionalBuildingCard(String playerUsername, int pos) throws RemoteException;
void noOptionalCard(String playerUsername) throws RemoteException; void noOptionalCard(String playerUsername) throws RemoteException;
void slotChoice(String playerUsername, int pos) throws RemoteException; void slotChoice(String playerUsername, int pos) throws RemoteException;
void totemChoice(String playerUsername, String totems) throws RemoteException;
/** /**
* Heartbeat: called periodically by the client to signal it is still alive. * Heartbeat: called periodically by the client to signal it is still alive.
* Mirrors the PING/PONG mechanism used in the TCP heartbeat channel. * Mirrors the PING/PONG mechanism used in the TCP heartbeat channel.
@@ -91,7 +91,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
System.out.println("Reconnected player: " + username); System.out.println("Reconnected player: " + username);
startWatchdog(username); startWatchdog(username);
Game game = controller.getModel(); Game game = controller.getModel();
callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems()));
System.out.println("Model sent: " + username); System.out.println("Model sent: " + username);
actionQueue.add(new ReconnectPlayer(username)); actionQueue.add(new ReconnectPlayer(username));
return true; return true;
@@ -117,7 +117,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
System.out.println("Reconnected player: " + username); System.out.println("Reconnected player: " + username);
startWatchdog(username); startWatchdog(username);
Game game = controller.getModel(); Game game = controller.getModel();
callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); callback.onGameInit(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems()));
System.out.println("Model sent: " + username); System.out.println("Model sent: " + username);
actionQueue.add(new ReconnectPlayer(username)); actionQueue.add(new ReconnectPlayer(username));
return true; return true;
@@ -285,7 +285,9 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
} }
public void totemChoice(String playerUsername, String totems) throws RemoteException {
actionQueue.offer(new TotemChoice(playerUsername,totems));
}
// Metodi per avviare server RMI // Metodi per avviare server RMI
@@ -292,4 +292,9 @@ public class TCPClient implements IClient {
e.printStackTrace(); e.printStackTrace();
} }
} }
//TODO
public void totemChoice(String playerUsername,String totems) {
doEvent(new TotemChoice(playerUsername,totems));
}
} }
@@ -126,7 +126,7 @@ public class TCPServer {
clientSocket.getOutputStream().write(1); clientSocket.getOutputStream().write(1);
pendingHeartbeat.put(username, handler); pendingHeartbeat.put(username, handler);
Game game = controller.getModel(); Game game = controller.getModel();
handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems()));
Thread thread = new Thread(handler); Thread thread = new Thread(handler);
thread.start(); thread.start();
clientHandlers.add(handler); clientHandlers.add(handler);
@@ -175,7 +175,7 @@ public class TCPServer {
clientSocket.getOutputStream().write(1); clientSocket.getOutputStream().write(1);
pendingHeartbeat.put(username, handler); pendingHeartbeat.put(username, handler);
Game game = controller.getModel(); Game game = controller.getModel();
handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers())); handler.notifyMiniModel(new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(),game.getAvailableTotems()));
Thread thread = new Thread(handler); Thread thread = new Thread(handler);
thread.start(); thread.start();
clientHandlers.add(handler); clientHandlers.add(handler);
@@ -6,6 +6,7 @@ import it.polimi.ingsw.gc14.Model.Game;
import it.polimi.ingsw.gc14.Model.GamePackage.GameStages; import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import it.polimi.ingsw.gc14.Model.MiniModel; import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Model.Player; import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Totems;
import it.polimi.ingsw.gc14.Network.EventType; import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent; import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound; import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound;
@@ -215,7 +216,7 @@ public class ServerLauncher {
MiniModel miniModel; MiniModel miniModel;
synchronized (gameController) { synchronized (gameController) {
Game game = gameController.getModel(); Game game = gameController.getModel();
miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers()); miniModel= new MiniModel(game.getBoard(),game.getSlotMap(), game.orderLogicCard,game.getCurrentState(),game.getPlayers(), new ArrayList<>(List.of(Totems.values())));
} }
serverRMI.notifyAll(miniModel); serverRMI.notifyAll(miniModel);
serverTCP.notifyAll(miniModel); serverTCP.notifyAll(miniModel);