Add: Endgame event creation end sending

This commit is contained in:
rubenpirreram
2026-05-15 18:02:34 +02:00
parent 826ae992ba
commit 6cb25bae63
5 changed files with 85 additions and 17 deletions
@@ -18,6 +18,7 @@ public class MiniModel implements Serializable {
public CurrentState currentState; public CurrentState currentState;
public Map<String, Player> players; public Map<String, Player> players;
public List<Totems> availableTotems; public List<Totems> availableTotems;
public List<Player> standingPlayers;
// --- Costruttori --- // --- Costruttori ---
@@ -37,6 +38,7 @@ public class MiniModel implements Serializable {
this.currentState = currentState; this.currentState = currentState;
this.players = new HashMap<>(); this.players = new HashMap<>();
this.availableTotems = availableTotems; this.availableTotems = availableTotems;
this.standingPlayers = new ArrayList<>();
setPlayers(players); setPlayers(players);
} }
@@ -72,6 +74,9 @@ public class MiniModel implements Serializable {
public void setPlayer(Player player) { public void setPlayer(Player player) {
players.put(player.getUserName(), player); players.put(player.getUserName(), player);
} }
public void setStandingPlayers(List<Player> standingPlayers) {
this.standingPlayers = standingPlayers;
}
public int getPositionByUsername(String username) { public int getPositionByUsername(String username) {
@@ -18,5 +18,6 @@ public enum EventType {
DISCONNECTED_PLAYER, DISCONNECTED_PLAYER,
RECONNECT_PLAYER, RECONNECT_PLAYER,
NEXT_ROUND, NEXT_ROUND,
ENDED_GAME,
NO_OPTIONAL_CARD NO_OPTIONAL_CARD
} }
@@ -0,0 +1,50 @@
package it.polimi.ingsw.gc14.Network.NetworkEvents;
import it.polimi.ingsw.gc14.Controller.GameController;
import it.polimi.ingsw.gc14.Model.GamePackage.CurrentState;
import it.polimi.ingsw.gc14.Model.MiniModel;
import it.polimi.ingsw.gc14.Model.OrderLogicCard;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Slot;
import it.polimi.ingsw.gc14.Network.EventType;
import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.View.IView;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* NetworkEvent to draw a tribe card from the lower card list.
*/
public class EndedGame extends NetworkEvent implements Serializable{
List<Player> players;
//TODO
public EndedGame(Map<Slot, Player> slotPlayerMap, OrderLogicCard orderLogicCard, CurrentState currentState, List<Player> players){
super("SERVER",EventType.ENDED_GAME,false);
this.slotPlayerMap = slotPlayerMap;
this.orderLogicCard = orderLogicCard;
this.currentState = currentState;
this.players = players;
}
/**
* @param gameController the Game Controller on which to apply the event
* @return true if the player could draw the card, false otherwise
*/
@Override
public boolean apply(GameController gameController){
return false;
}
public boolean apply(MiniModel miniModel)
{
miniModel.setSlotPlayerMap(slotPlayerMap);
miniModel.setOrderLogicCard(orderLogicCard);
miniModel.setCurrentState(currentState);
miniModel.setPlayers(players);
miniModel.setStandingPlayers(players);
return true;
}
}
@@ -156,20 +156,24 @@ public class TCPClient implements IClient {
e.printStackTrace(); e.printStackTrace();
break; break;
} }
synchronized (controller) {
if (read instanceof NetworkEvent event) { if (read instanceof NetworkEvent event) {
if (event.getIsError()) { if (event.getIsError()) {
controller.view.showError(event.toString()); controller.view.showError(event.toString());
} else { } else {
synchronized (controller) {
event.apply(controller.miniModel); event.apply(controller.miniModel);
}
controller.view.render(); controller.view.render();
} }
} else if (read instanceof MiniModel model) { } else if (read instanceof MiniModel model) {
synchronized (controller) {
controller.setModel(model); controller.setModel(model);
}
controller.view.render(); controller.view.render();
} }
}
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
break; break;
@@ -11,6 +11,7 @@ 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;
import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer; import it.polimi.ingsw.gc14.Network.NetworkEvents.DisconnectedPlayer;
import it.polimi.ingsw.gc14.Network.NetworkEvents.EndedGame;
import it.polimi.ingsw.gc14.Network.NetworkEvents.TotemChoice; import it.polimi.ingsw.gc14.Network.NetworkEvents.TotemChoice;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
@@ -119,6 +120,11 @@ public class ServerLauncher {
Game game=gameController.getModel(); Game game=gameController.getModel();
if(!event.getIsError()) if(!event.getIsError())
{ {
if(game.getCurrentState().getGameStage().equals(GameStages.ENDED))
{
actionQueue.offer(new EndedGame(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayerStanding()));
}
else {
if(event.getEventType().equals(EventType.DISCONNECTED_PLAYER)&& game.getCurrentState().getGameStage().equals(GameStages.WAITING)) if(event.getEventType().equals(EventType.DISCONNECTED_PLAYER)&& game.getCurrentState().getGameStage().equals(GameStages.WAITING))
{ {
playerList.remove(event.getUsername()); playerList.remove(event.getUsername());
@@ -131,6 +137,7 @@ public class ServerLauncher {
} }
} }
} }
}
serverRMI.notifyAll(event); serverRMI.notifyAll(event);
serverTCP.notifyAll(event); serverTCP.notifyAll(event);
if(game.getCurrentState().getRound()!=roundPrev) if(game.getCurrentState().getRound()!=roundPrev)
@@ -161,6 +168,7 @@ public class ServerLauncher {
} }
disconnectionTimer = timerExecutor.schedule(() -> { disconnectionTimer = timerExecutor.schedule(() -> {
game.EndGameForFeit(game.getPlayerByUsername(playerList.keySet().stream().toList().getFirst())); game.EndGameForFeit(game.getPlayerByUsername(playerList.keySet().stream().toList().getFirst()));
actionQueue.offer(new EndedGame(game.getSlotMap(), game.orderLogicCard, game.getCurrentState(),game.getPlayerStanding()));
System.out.println("Timer expired: no player reconnected in 60s."); System.out.println("Timer expired: no player reconnected in 60s.");
}, 1, TimeUnit.MINUTES); }, 1, TimeUnit.MINUTES);
} }