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 (!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 {
System.out.println("Insert the required position:");
pos = scanner.nextInt();
@@ -156,6 +156,7 @@ public class ClientLauncherTUI {
case "6" -> controller.pickOptionalBuildingCard(username, pos);
case "7" -> controller.noOptionalCard(username);
case "8" -> controller.skipTurn(username);
case "9" -> controller.totemChoice(username,pos);
case "A", "a" -> view.fullRender();
case "B", "b" -> view.renderBoard();
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.Player;
import it.polimi.ingsw.gc14.Model.Totems;
/**
* Controller class that manages interactions between the client-side logic
@@ -200,8 +201,16 @@ public class GameController {
*/
public boolean slotChoice(String playerUsername,int pos) {
Player player= model.getPlayerByUsername(playerUsername);
if(player==null)//playerIndex>=model.)
if(player==null)
return false;
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<>();
//TODO
private boolean TotemChoice(Player player,Totems totem) {
public boolean TotemChoice(Player player,Totems totem) {
if(!currentState.getGameStage().equals(GameStages.TOTEM_CHOICE))
return false;
if(!getCurrentState().getCurrentPlayer().equals(player))
@@ -16,22 +16,23 @@ public class MiniModel implements Serializable {
public OrderLogicCard orderLogicCard;
public CurrentState currentState;
public Map<String,Player> players;
public List<Object>availableTotems;
public List<Totems>availableTotems;
public MiniModel(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.slotPlayerMap = slotPlayerMap;
this.orderLogicCard = orderLogicCard;
this.currentState = currentState;
this.players=new HashMap<>();
this.availableTotems=availableTotems;
setPlayers(players);
}
public void setBoard(Board board) {
this.board = board;
}
public void setAvailableTotems(List<Object>availableTotems) {
public void setAvailableTotems(List<Totems>availableTotems) {
this.availableTotems = availableTotems;
}
public void setSlotPlayerMap(Map<Slot,Player> slotPlayerMap) {
@@ -29,4 +29,5 @@ public interface IClient {
public void noOptionalCard(String playerUsername) 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.Model.MiniModel;
import it.polimi.ingsw.gc14.Model.Totems;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
@@ -15,20 +16,15 @@ import java.util.List;
public class TotemChoice extends NetworkEvent implements Serializable {
/** Index of the card to draw */
private int pos;
private List<Object>availableTotems;
public void setAvailableTotems(List<Object> availableTotems) {
private String totem;
private List<Totems>availableTotems;
public void setAvailableTotems(List<Totems> availableTotems) {
this.availableTotems = availableTotems;
}
/**
* Class constructor.
* 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) {
//TODO
public TotemChoice(String username, String totem) {
super(username, EventType.TOTEM_CHOICE, false);
this.pos = pos;
this.totem = totem;
}
/**
@@ -37,7 +33,7 @@ public class TotemChoice extends NetworkEvent implements Serializable {
*/
@Override
public boolean apply(GameController gameController) {
return gameController.slotChoice(username, pos);
return gameController.TotemChoice(username, totem);
}
//TODO
@@ -224,6 +224,10 @@ public class RMIClient implements IClient {
public void slotChoice(String playerUsername,int pos) throws RemoteException {
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 noOptionalCard(String playerUsername) 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.
* 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);
startWatchdog(username);
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);
actionQueue.add(new ReconnectPlayer(username));
return true;
@@ -117,7 +117,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
System.out.println("Reconnected player: " + username);
startWatchdog(username);
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);
actionQueue.add(new ReconnectPlayer(username));
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
@@ -292,4 +292,9 @@ public class TCPClient implements IClient {
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);
pendingHeartbeat.put(username, handler);
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.start();
clientHandlers.add(handler);
@@ -175,7 +175,7 @@ public class TCPServer {
clientSocket.getOutputStream().write(1);
pendingHeartbeat.put(username, handler);
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.start();
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.MiniModel;
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.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.ApplyNextRound;
@@ -215,7 +216,7 @@ public class ServerLauncher {
MiniModel miniModel;
synchronized (gameController) {
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);
serverTCP.notifyAll(miniModel);