Merge main

This commit is contained in:
Felix Koppe
2024-11-28 15:02:41 +01:00
6 changed files with 366 additions and 337 deletions

View File

@@ -1,18 +1,11 @@
package pp.mdga.client.server;
import com.jme3.network.ConnectionListener;
import com.jme3.network.HostedConnection;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.network.*;
import com.jme3.network.serializing.Serializer;
import pp.mdga.game.Game;
import pp.mdga.game.Player;
import pp.mdga.message.client.*;
import pp.mdga.message.client.ClientMessage;
import pp.mdga.message.server.*;
import pp.mdga.message.server.ServerMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.ServerSender;
@@ -20,6 +13,7 @@
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.LogManager;
@@ -57,7 +51,7 @@ public static void main(String[] args) {
*/
public MdgaServer() {
LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS
logic = null;//new ServerGameLogic(new Game(), this);
logic = new ServerGameLogic(this, new Game());
}
public void run() {
@@ -96,14 +90,14 @@ private void initializeSerializables() {
Serializer.registerClass(ClientStartGame.class);
Serializer.registerClass(DeselectTSK.class);
Serializer.registerClass(ForceContinueGame.class);
Serializer.registerClass(ForceStartGame.class);
Serializer.registerClass(StartGame.class);
Serializer.registerClass(JoinServer.class);
Serializer.registerClass(LeaveGame.class);
Serializer.registerClass(LobbyNotReady.class);
Serializer.registerClass(LobbyReady.class);
Serializer.registerClass(NoPowerCard.class);
Serializer.registerClass(RequestBriefing.class);
Serializer.registerClass(RequestDice.class);
Serializer.registerClass(RequestDie.class);
Serializer.registerClass(RequestMove.class);
Serializer.registerClass(RequestPlayCard.class);
Serializer.registerClass(SelectCard.class);
@@ -114,7 +108,7 @@ private void initializeSerializables() {
Serializer.registerClass(AnyPiece.class);
Serializer.registerClass(Briefing.class);
Serializer.registerClass(CeremonyMessage.class);
Serializer.registerClass(Dice.class);
Serializer.registerClass(Die.class);
Serializer.registerClass(DiceAgain.class);
Serializer.registerClass(DiceNow.class);
Serializer.registerClass(EndOfTurn.class);
@@ -144,14 +138,14 @@ private void registerListeners() {
myServer.addMessageListener(this, ClientStartGame.class);
myServer.addMessageListener(this, DeselectTSK.class);
myServer.addMessageListener(this, ForceContinueGame.class);
myServer.addMessageListener(this, ForceStartGame.class);
myServer.addMessageListener(this, StartGame.class);
myServer.addMessageListener(this, JoinServer.class);
myServer.addMessageListener(this, LeaveGame.class);
myServer.addMessageListener(this, LobbyNotReady.class);
myServer.addMessageListener(this, LobbyReady.class);
myServer.addMessageListener(this, NoPowerCard.class);
myServer.addMessageListener(this, RequestBriefing.class);
myServer.addMessageListener(this, RequestDice.class);
myServer.addMessageListener(this, RequestDie.class);
myServer.addMessageListener(this, RequestMove.class);
myServer.addMessageListener(this, RequestPlayCard.class);
myServer.addMessageListener(this, SelectCard.class);
@@ -160,32 +154,42 @@ private void registerListeners() {
myServer.addConnectionListener(this);
}
@Override
public void messageReceived(HostedConnection source, Message message) {
public void messageReceived(HostedConnection source, ClientMessage message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
if (message instanceof ClientMessage clientMessage)
pendingMessages.add(new ReceivedMessage(clientMessage, source.getId()));
pendingMessages.add(new ReceivedMessage(message, source.getId()));
}
@Override
public void connectionAdded(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
//logic.addPlayer(hostedConnection.getId());
// ToDo: Synchronize data between server and client.
logic.getGame().addPlayer(hostedConnection.getId(), new Player());
}
@Override
public void connectionRemoved(Server server, HostedConnection hostedConnection) {
LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS
final Player player = null;// = logic.getPlayerById(hostedConnection.getId());
final Player player = logic.getGame().getPlayerById(hostedConnection.getId());
if (player == null)
LOGGER.log(Level.INFO, "closed connection does not belong to an active player"); //NON-NLS
else { //NON-NLS
LOGGER.log(Level.INFO, "closed connection belongs to {0}", player); //NON-NLS
exit(0);
// exit(0);
this.handleDisconnect(hostedConnection.getId());
}
}
private void exit(int exitValue) { //NON-NLS
/**
* This method will be used to handle unintentional disconnections from players.
*
* @param id as the id of the disconnected player.
*/
public void handleDisconnect(int id) {
this.logic.received(new Disconnected(), id);
}
public void exit(int exitValue) { //NON-NLS
LOGGER.log(Level.INFO, "close request"); //NON-NLS
if (myServer != null)
for (HostedConnection client : myServer.getConnections()) //NON-NLS
@@ -210,4 +214,22 @@ public void send(int id, ServerMessage message) {
else
LOGGER.log(Level.ERROR, "there is no connection with id={0}", id); //NON-NLS
}
/**
* This method will be used to send the given message parameter to all connected players which are saved inside the
* players attribute of Game class.
*
* @param message as the message which will be sent to all players as a ServerMessage.
*/
public void broadcast(ServerMessage message) {
for (Map.Entry<Integer, Player> entry: this.logic.getGame().getPlayers().entrySet()) {
this.send(entry.getKey(), message);
}
}
//TODO:
@Override
public void messageReceived(HostedConnection source, Message m) {
}
}

View File

@@ -1,20 +1,33 @@
package pp.mdga.client;
import pp.mdga.game.BonusCard;
import pp.mdga.game.Color;
import pp.mdga.game.Game;
import pp.mdga.game.Piece;
import pp.mdga.message.client.ClientMessage;
import pp.mdga.message.server.*;
import java.util.Map;
import java.util.UUID;
public class ClientGameLogic implements ServerInterpreter {
static final System.Logger LOGGER = System.getLogger(ClientGameLogic.class.getName());
private Game game;
private final ClientSender clientSender;
private ClientState state;
private Map<UUID, Piece> pieces;
private Map<UUID, BonusCard> cards;
private Dialogs dialogs = new Dialogs(null, this);
private GameState gameState = new GameState(null, this);
private Ceremony ceremony = new Ceremony(null, this);
private Interrupt interrupt = new Interrupt(null, this);
public ClientGameLogic(Game game, ClientSender clientSender) {
this.game = game;
this.clientSender = clientSender;
state = new ClientAutomaton(this);
state = dialogs;
}
public void send(ClientMessage msg){
@@ -36,136 +49,202 @@ public ClientState getState(){
@Override
public void received(ActivePlayer msg) {
state.received(msg);
}
@Override
public void received(AnyPiece msg) {
state.received(msg);
}
@Override
public void received(Briefing msg) {
state.received(msg);
}
@Override
public void received(CeremonyMessage msg) {
state.received(msg);
}
@Override
public void received(Dice msg) {
public void received(Die msg) {
state.received(msg);
}
@Override
public void received(DiceAgain msg) {
state.received(msg);
}
@Override
public void received(DiceNow msg) {
state.received(msg);
}
@Override
public void received(EndOfTurn msg) {
state.received(msg);
}
@Override
public void received(LobbyAccept msg) {
state.received(msg);
}
@Override
public void received(LobbyDeny msg) {
state.received(msg);
}
@Override
public void received(LobbyPlayerJoin msg) {
state.received(msg);
}
@Override
public void received(LobbyPlayerLeave msg) {
state.received(msg);
}
@Override
public void received(MoveMessage msg) {
state.received(msg);
}
@Override
public void received(NoTurn msg) {
state.received(msg);
}
@Override
public void received(PauseGame msg) {
state.received(msg);
}
@Override
public void received(PlayCard msg) {
state.received(msg);
}
@Override
public void received(PossibleCard msg) {
state.received(msg);
}
@Override
public void received(PossiblePiece msg) {
state.received(msg);
}
@Override
public void received(RankingResponse msg) {
state.received(msg);
}
@Override
public void received(RankingRollAgain msg) {
state.received(msg);
}
@Override
public void received(ReconnectBriefing msg) {
state.received(msg);
}
@Override
public void received(ResumeGame msg) {
state.received(msg);
}
@Override
public void received(ServerStartGame msg) {
state.received(msg);
}
@Override
public void received(StartPiece msg) {
state.received(msg);
}
@Override
public void received(UpdateReady msg) {
state.received(msg);
}
@Override
public void received(UpdateTSK msg) {
state.received(msg);
}
@Override
public void received(WaitPiece msg) {
state.received(msg);
}
@Override
public void received(Spectator msg) {
state.received(msg);
}
public void selectPiece(UUID pieceId){
state.selectPiece(pieceId);
}
public void selectCard(UUID cardId){
state.selectCard(cardId);
}
public void selectTsk(Color color){
state.selectTSK(color);
}
public void selectDice(){
state.selectDice();
}
public void selectName(String name){
state.setName(name);
}
public void selectReady(boolean ready){
state.selectReady();
}
public void selectHost(){
state.selectHost();
}
public void selectLeave(){
state.selectLeave();
}
public void selectJoin(){
state.selectJoin();
}
public void selectStart(){
state.selectStart();
}
public void setState(ClientState state){
this.state.exit();
this.state = state;
}
public GameState getGameState(){
return gameState;
}
public Ceremony getCeremony(){
return ceremony;
}
public Interrupt getInterrupt(){
return interrupt;
}
public Dialogs getDialogs(){
return dialogs;
}
}

View File

@@ -163,6 +163,11 @@ public void received(UpdateTSK msg) {
LOGGER.log(Level.DEBUG, "Received {0} not allowed.", msg);
}
@Override
public void received(Spectator msg) {
LOGGER.log(Level.DEBUG, "Received {0} not allowed.", msg);
}
@Override
public void received(WaitPiece msg) {
LOGGER.log(Level.DEBUG, "Received {0} not allowed.", msg);

View File

@@ -8,29 +8,27 @@
* The game state is updated by the game logic.
*/
public class Game {
/**
* Constants.
*/
public static final int AMOUNT_OF_TURBO_CARDS = 16;
public static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
/**
* Attributes.
*/
private int diceModifier = 1;
private int diceEyes;
private Map<Color, Player> players = new EnumMap<>(Color.class);
private Map<Integer, Player> players = new HashMap<>();
private Statistic gameStatistics;
private List<BonusCard> drawPile;
private List<BonusCard> discardPile = new ArrayList<>();
private Board board;
private Color activeColor;
private List<Color> order;
private final ArrayList<Player> playerList = new ArrayList<>(4);
private final ArrayList<Observer> observers = new ArrayList<>();
private Player startPlayer;
private Boolean gameHasStarted = false;
private Boolean playerHasDisconnected = false;
private Boolean gameIsInterrupted = false;
private Boolean allRanked = false;
private Boolean movablePieces = false;
private Boolean allReady = false;
private static final int AMOUNT_OF_TURBO_CARDS = 16;
private static final int AMOUNT_OF_SHIELD_AND_SWAP_CARDS = 12;
private boolean allRanked = false;
private boolean movablePieces = false;
private boolean allReady = false;
/**
* This constructor creates a new Game object.
@@ -48,185 +46,23 @@ public Game() {
board = new Board();
}
/**
* This method returns the dice modifier.
*
* @return the dice modifier
*/
public int getDiceModifier() {
return diceModifier;
}
/**
* This method sets the dice modifier.
*
* @param diceModifier the new dice modifier
*/
public void setDiceModifier(int diceModifier) {
this.diceModifier = diceModifier;
}
/**
* This method returns the dice eyes.
*
* @return the dice eyes
*/
public int getDiceEyes() {
return diceEyes;
}
/**
* This method sets the dice eyes.
*
* @param diceEyes the new dice eyes
*/
public void setDiceEyes(int diceEyes) {
this.diceEyes = diceEyes;
}
/**
* This method returns the players.
*
* @return the players
*/
public Map<Color, Player> getPlayers() {
return players;
}
/**
* This method sets the players.
*
* @param players the new players
*/
public void setPlayers(Map<Color, Player> players) {
this.players = players;
}
/**
* This method returns the game statistics.
*
* @return the game statistics
*/
public Statistic getGameStatistics() {
return gameStatistics;
}
/**
* This method sets the game statistics.
*
* @param gameStatistics the new game statistics
*/
public void setGameStatistics(Statistic gameStatistics) {
this.gameStatistics = gameStatistics;
}
/**
* This method returns the draw pile.
*
* @return the draw pile
*/
public List<BonusCard> getDrawPile() {
return drawPile;
}
/**
* This method sets the draw pile.
*
* @param drawPile the new draw pile
*/
public void setDrawPile(List<BonusCard> drawPile) {
this.drawPile = drawPile;
}
/**
* This method returns the discard pile.
*
* @return the discard pile
*/
public List<BonusCard> getDiscardPile() {
return discardPile;
}
/**
* This method sets the discard pile.
*
* @param discardPile the new discard pile
*/
public void setDiscardPile(List<BonusCard> discardPile) {
this.discardPile = discardPile;
}
/**
* This method returns the board.
*
* @return the board
*/
public Board getBoard() {
return board;
}
/**
* This method sets the board.
*
* @param board the new board
*/
public void setBoard(Board board) {
this.board = board;
}
/**
* This method returns the active color.
*
* @return the active color
*/
public Color getActiveColor() {
return activeColor;
}
/**
* This method sets the active color.
*
* @param activeColor the new active color
*/
public void setActiveColor(Color activeColor) {
this.activeColor = activeColor;
}
/**
* This method returns the order of the players.
*
* @return the order of the players
*/
public List<Color> getOrder() {
return order;
}
/**
* This method sets the order of the players.
*
* @param order the new order of the players
*/
public void setOrder(List<Color> order) {
this.order = order;
}
/**
* This method adds a player to the game.
*
* @param color the color of the player
* @param id the id of the player
* @param player the player to be added
*/
public void addPlayer(Color color, Player player) {
players.put(color, player);
public void addPlayer(int id, Player player) {
players.put(id, player);
}
/**
* This method removes a player from the game.
*
* @param color the color of the player
* @param id the color of the player
*/
public void removePlayer(Color color) {
players.remove(color);
public void removePlayer(int id) {
players.remove(id);
}
/**
@@ -248,49 +84,135 @@ public void removeObserver(Observer observer) {
}
/**
* This method returns the game has started.
*
* @return the game has started
* @param id
* @param active
*/
public Boolean getGameHasStarted() {
return gameHasStarted;
public void updatePlayerActiveState(int id, boolean active) {
this.players.get(id).setActive(active);
}
/**
* This method sets the game has started.
* This method will be used to return the player which has the given id parameter.
*
* @param gameHasStarted the new game has started
* @param id as the unique id of a player as an Integer.
* @return the player with the given id as a Player object.
*/
public void setGameHasStarted(Boolean gameHasStarted) {
this.gameHasStarted = gameHasStarted;
public Player getPlayerById(int id) {
return this.players.get(id);
}
/**
* This method returns the player has disconnected.
* This method will be used to the get the player depending on the given color parameter.
*
* @return the player has disconnected
* @param color as the color of the player as a Color enumeration.
* @return the player with the given color as a Player object.
*/
public Boolean playerHasDisconnected() {
return playerHasDisconnected;
public Player getPlayerByColor(Color color) {
for (Map.Entry<Integer, Player> entry : this.players.entrySet()) {
if (entry.getValue().getColor() == color) {
return entry.getValue();
}
}
return null;
}
/**
* This method sets the game interruption state.
* This method will be used to return the number of active players of this game.
*
* @param gameIsInterrupted the new game interruption state
* @return activePlayers as an Integer.
*/
public void setGameIsInterrupted(Boolean gameIsInterrupted) {
this.gameIsInterrupted = gameIsInterrupted;
if (Boolean.FALSE.equals(gameIsInterrupted)) notifyObservers();
public int getNumberOfActivePlayers() {
int activePlayers = 0;
for (Map.Entry<Integer, Player> entry : this.players.entrySet()) {
if (entry.getValue().isActive()) {
activePlayers++;
}
}
return activePlayers;
}
/**
* This method returns whether the game is interrupted.
*
* @return true if the game is interrupted, false otherwise
* This method notifies the observers.
*/
public Boolean gameIsInterrupted() {
return gameIsInterrupted;
public void notifyObservers() {
for (Observer observer : new ArrayList<>(observers)) {
//TODO: observer.update();
}
}
/**
* This method returns the dice modifier.
*
* @return the dice modifier
*/
public int getDiceModifier() {
return diceModifier;
}
/**
* This method returns the dice eyes.
*
* @return the dice eyes
*/
public int getDiceEyes() {
return diceEyes;
}
/**
* This method returns the players.
*
* @return the players
*/
public Map<Integer, Player> getPlayers() {
return players;
}
/**
* This method returns the game statistics.
*
* @return the game statistics
*/
public Statistic getGameStatistics() {
return gameStatistics;
}
/**
* This method returns the draw pile.
*
* @return the draw pile
*/
public List<BonusCard> getDrawPile() {
return drawPile;
}
/**
* This method returns the discard pile.
*
* @return the discard pile
*/
public List<BonusCard> getDiscardPile() {
return discardPile;
}
/**
* This method returns the board.
*
* @return the board
*/
public Board getBoard() {
return board;
}
/**
* This method returns the active color.
*
* @return the active color
*/
public Color getActiveColor() {
return activeColor;
}
/**
@@ -302,6 +224,78 @@ public Boolean getMovablePieces() {
return movablePieces;
}
/**
* This method sets the dice modifier.
*
* @param diceModifier the new dice modifier
*/
public void setDiceModifier(int diceModifier) {
this.diceModifier = diceModifier;
}
/**
* This method sets the dice eyes.
*
* @param diceEyes the new dice eyes
*/
public void setDiceEyes(int diceEyes) {
this.diceEyes = diceEyes;
}
/**
* This method sets the players.
*
* @param players the new players
*/
public void setPlayers(Map<Integer, Player> players) {
this.players = players;
}
/**
* This method sets the game statistics.
*
* @param gameStatistics the new game statistics
*/
public void setGameStatistics(Statistic gameStatistics) {
this.gameStatistics = gameStatistics;
}
/**
* This method sets the draw pile.
*
* @param drawPile the new draw pile
*/
public void setDrawPile(List<BonusCard> drawPile) {
this.drawPile = drawPile;
}
/**
* This method sets the discard pile.
*
* @param discardPile the new discard pile
*/
public void setDiscardPile(List<BonusCard> discardPile) {
this.discardPile = discardPile;
}
/**
* This method sets the board.
*
* @param board the new board
*/
public void setBoard(Board board) {
this.board = board;
}
/**
* This method sets the active color.
*
* @param activeColor the new active color
*/
public void setActiveColor(Color activeColor) {
this.activeColor = activeColor;
}
/**
* This method sets the game interruption state.
*
@@ -312,16 +306,6 @@ public void setMovablePieces(Boolean movablePieces) {
if (Boolean.FALSE.equals(movablePieces)) notifyObservers();
}
/**
* This method sets the player has disconnected.
*
* @param playerHasDisconnected the new player has disconnected
*/
public void setPlayerHasDisconnected(Boolean playerHasDisconnected) {
this.playerHasDisconnected = playerHasDisconnected;
if (Boolean.TRUE.equals(playerHasDisconnected)) notifyObservers();
}
/**
* This method returns whether all players have ranked.
*
@@ -341,24 +325,6 @@ public void setAllRanked(Boolean allRanked) {
if (Boolean.TRUE.equals(allRanked)) notifyObservers();
}
/**
* This method returns the start player.
*
* @return the start player
*/
public Player getStartPlayer() {
return startPlayer;
}
/**
* This method sets the start player.
*
* @param startPlayer the new start player
*/
public void setStartPlayer(Player startPlayer) {
this.startPlayer = startPlayer;
}
/**
* This method returns the all ready state.
*
@@ -371,53 +337,10 @@ public Boolean allReady() {
/**
* This method sets the all ready state.
*
* @param allReady the new all ready state
* @param allReady the new all-ready state
*/
public void setAllReady(Boolean allReady) {
this.allReady = allReady;
if (Boolean.TRUE.equals(allReady)) notifyObservers();
}
/**
* This method notifies the observers.
*/
public void notifyObservers() {
for (Observer observer : new ArrayList<>(observers)) {
//observer.update();
}
}
/**
* This method returns the piece through its identifier.
*
* @param identifier the identifier of the piece
* @return the piece
*/
public Piece getPieceThroughIdentifier(String identifier) {
String[] parts = identifier.split("-");
Color color = Color.valueOf(parts[0]);
int index = Integer.parseInt(parts[1]);
return board.getPlayerData().get(color).getPieces()[index];
}
public ArrayList<Player> getPlayerList() {
return playerList;
}
public void addPlayerToList(Player player) {
playerList.add(player);
}
public void removePlayerFromList(Player player) {
playerList.remove(player);
}
public Player getPlayerFromList(String name) {
for (Player player : playerList) {
if (player.getName().equals(name)) {
return player;
}
}
return null;
}
}

View File

@@ -11,7 +11,7 @@ public class UpdateReady extends ServerMessage {
/**
* The color associated with the update.
*/
private final Color color;
private final int playerId;
/**
* Indicates whether the player is ready.
@@ -21,12 +21,12 @@ public class UpdateReady extends ServerMessage {
/**
* Constructs a new UpdateReady instance with the specified color and readiness status.
*
* @param color the color associated with the update
* @param playerId the playerId associated with the update
* @param ready the readiness status
*/
public UpdateReady(Color color, boolean ready) {
public UpdateReady(int playerId, boolean ready) {
super();
this.color = color;
this.playerId = playerId;
this.ready = ready;
}
@@ -35,17 +35,17 @@ public UpdateReady(Color color, boolean ready) {
*/
private UpdateReady() {
super();
this.color = null;
this.playerId = 0;
this.ready = false;
}
/**
* Gets the color associated with the update.
* Gets the playerId associated with the update.
*
* @return the color
* @return the playerId
*/
public Color getColor() {
return color;
public int getPlayerId() {
return playerId;
}
/**

View File

@@ -81,7 +81,7 @@ public void received(LobbyNotReady msg, int from) {
* @param from as the client id of the player as an Integer.
*/
@Override
public void received(ForceStartGame msg, int from) {
public void received(StartGame msg, int from) {
this.logic.getServerSender().broadcast(new ServerStartGame());
this.logic.setCurrentState(this.logic.getGameState());
}