Updated client states.

Updated the client states by removing all references to the 'PlayerData' class.
This commit is contained in:
Daniel Grigencha
2024-12-04 01:47:31 +01:00
parent 7053b163e5
commit 964ff87b11
7 changed files with 99 additions and 295 deletions

View File

@@ -5,13 +5,13 @@
import pp.mdga.game.Color;
import pp.mdga.game.Game;
import pp.mdga.game.Piece;
import pp.mdga.game.PlayerData;
import pp.mdga.message.client.ClientMessage;
import pp.mdga.message.server.*;
import pp.mdga.notification.*;
import pp.mdga.notification.InfoNotification;
import pp.mdga.notification.Notification;
import pp.mdga.notification.StartDialogNotification;
import java.util.ArrayList;
import java.util.Map;
import java.util.UUID;
/**
@@ -59,12 +59,13 @@ public void send(ClientMessage msg){
/**
* This method is used to get a piece by its id
*
* @param pieceId the UUID of the piece
* @return the piece
*/
private Piece getPiece(UUID pieceId) {
for(Map.Entry<Color, PlayerData> entry : game.getBoard().getPlayerData().entrySet()){
for(Piece piece : entry.getValue().getPieces()){
for (var player : this.game.getPlayers().values()) {
for (Piece piece : player.getPieces()) {
if (piece.getUuid().equals(pieceId)) {
return piece;
}

View File

@@ -5,17 +5,13 @@
import pp.mdga.client.DialogsState;
import pp.mdga.game.Color;
import pp.mdga.game.Piece;
import pp.mdga.game.Player;
import pp.mdga.game.PlayerData;
import pp.mdga.message.client.*;
import pp.mdga.message.server.LobbyPlayerJoinedMessage;
import pp.mdga.message.server.LobbyPlayerLeaveMessage;
import pp.mdga.message.server.ServerStartGameMessage;
import pp.mdga.message.server.UpdateReadyMessage;
import pp.mdga.message.server.UpdateTSKMessage;
import pp.mdga.message.server.*;
import pp.mdga.notification.*;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class LobbyState extends DialogStates {
@@ -77,12 +73,12 @@ public void selectStart(){
public void received(ServerStartGameMessage msg) {
logic.getGame().setBoard(msg.getBoard());
logic.addNotification(new GameNotification(logic.getGame().getPlayerById(logic.getOwnPlayerId()).getColor()));
for (Map.Entry<Integer, Player> entry : logic.getGame().getPlayers().entrySet()) {
for (var player : logic.getGame().getPlayers().values()) {
List<UUID> pieces = new ArrayList<>();
for (Piece piece : logic.getGame().getBoard().getPlayerData().get(entry.getValue().getColor()).getPieces()) {
for (Piece piece : player.getPieces()) {
pieces.add(piece.getUuid());
}
logic.addNotification(new PlayerInGameNotification(entry.getValue().getColor(), pieces, entry.getValue().getName()));
logic.addNotification(new PlayerInGameNotification(player.getColor(), pieces, player.getName()));
}
logic.setState(logic.getGameState());
}

View File

@@ -36,12 +36,12 @@ protected void handlePowerCard(PlayCardMessage msg){
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(enemyPiece)].setOccupant(ownPiece);
logic.getGame().getBoard().getInfield()[ownIndex].setOccupant(enemyPiece);
}
logic.getGame().getDiscardPile().add(logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).removeHandCard(msg.getCard()));
logic.getGame().getDiscardPile().add(msg.getCard());
}
protected void throwPiece(Piece piece) {
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(piece)].clearOccupant();
logic.getGame().getBoard().getPlayerData().get(piece.getColor()).addWaitingPiece(piece);
logic.getGame().getPlayerByColor(piece.getColor()).addWaitingPiece(piece);
logic.addNotification(new ThrowPieceNotification(piece.getUuid()));
logic.getGame().getPlayerByColor(piece.getColor()).getPlayerStatistic().increasePiecesBeingThrown();
logic.getGame().getGameStatistics().increasePiecesBeingThrown();

View File

@@ -5,7 +5,10 @@
import pp.mdga.client.GameState;
import pp.mdga.game.Piece;
import pp.mdga.message.server.*;
import pp.mdga.notification.*;
import pp.mdga.notification.ActivePlayerNotification;
import pp.mdga.notification.HomeMoveNotification;
import pp.mdga.notification.MovePieceNotification;
import pp.mdga.notification.PlayCardNotification;
public class SpectatorState extends GameStates {
@@ -64,14 +67,14 @@ public void received(MoveMessage msg){
if (msg.isHomeMove()) {
logic.addNotification(new HomeMoveNotification(pieceToMove.getUuid(), msg.getTargetIndex()));
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)].clearOccupant();
logic.getGame().getBoard().getPlayerData().get(pieceToMove.getColor()).setPieceInHome(msg.getTargetIndex(), pieceToMove);
logic.getGame().getPlayerByColor(pieceToMove.getColor()).setPieceInHome(msg.getTargetIndex(), pieceToMove);
} else {
if (logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].isOccupied()) {
throwPiece(logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].getOccupant());
logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getPlayerStatistic().increasePiecesThrown();
logic.getGame().getGameStatistics().increasePiecesThrown();
}
if(logic.getGame().getBoard().getPlayerData().get(pieceToMove.getColor()).getStartNodeIndex() == logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)){
if (logic.getGame().getPlayerByColor(pieceToMove.getColor()).getStartNodeIndex() == logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)) {
logic.addNotification(new MovePieceNotification(pieceToMove.getUuid(), msg.getTargetIndex(), true));
logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].setOccupant(pieceToMove);
} else {

View File

@@ -3,7 +3,8 @@
import pp.mdga.client.ClientGameLogic;
import pp.mdga.client.ClientState;
import pp.mdga.client.GameState;
import pp.mdga.game.*;
import pp.mdga.game.Piece;
import pp.mdga.game.PieceState;
import pp.mdga.message.server.*;
import pp.mdga.notification.*;
@@ -70,9 +71,9 @@ public void received(MoveMessage msg){
if (msg.isHomeMove()) {
logic.addNotification(new HomeMoveNotification(pieceToMove.getUuid(), msg.getTargetIndex()));
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)].clearOccupant();
logic.getGame().getBoard().getPlayerData().get(pieceToMove.getColor()).setPieceInHome(msg.getTargetIndex(), pieceToMove);
logic.getGame().getPlayerByColor(pieceToMove.getColor()).setPieceInHome(msg.getTargetIndex(), pieceToMove);
for (int i = msg.getTargetIndex() + 1; i < 4; i++) {
if(!logic.getGame().getBoard().getPlayerData().get(pieceToMove.getColor()).getHomeNodes()[i].isOccupied()){
if (!logic.getGame().getPlayerByColor(pieceToMove.getColor()).getHomeNodes()[i].isOccupied()) {
pieceToMove.setState(PieceState.HOME);
break;
}
@@ -84,7 +85,7 @@ public void received(MoveMessage msg){
logic.getGame().getPlayerByColor(logic.getGame().getActiveColor()).getPlayerStatistic().increasePiecesThrown();
logic.getGame().getGameStatistics().increasePiecesThrown();
}
if(logic.getGame().getBoard().getPlayerData().get(pieceToMove.getColor()).getStartNodeIndex() == logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)){
if (logic.getGame().getPlayerByColor(pieceToMove.getColor()).getStartNodeIndex() == logic.getGame().getBoard().getInfieldIndexOfPiece(pieceToMove)) {
logic.addNotification(new MovePieceNotification(pieceToMove.getUuid(), msg.getTargetIndex(), true));
logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].setOccupant(pieceToMove);
} else {

View File

@@ -48,7 +48,7 @@ public void received(MoveMessage msg){
if (msg.isHomeMove()) {
logic.addNotification(new HomeMoveNotification(piece.getUuid(), msg.getTargetIndex()));
logic.getGame().getBoard().getInfield()[logic.getGame().getBoard().getInfieldIndexOfPiece(piece)].clearOccupant();
logic.getGame().getBoard().getPlayerData().get(piece.getColor()).setPieceInHome(msg.getTargetIndex(), piece);
logic.getGame().getPlayerByColor(piece.getColor()).setPieceInHome(msg.getTargetIndex(), piece);
} else {
if (logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].isOccupied()) {
throwPiece(logic.getGame().getBoard().getInfield()[msg.getTargetIndex()].getOccupant());

View File

@@ -1,197 +0,0 @@
package pp.mdga.game;
import com.jme3.network.serializing.Serializable;
import pp.mdga.Resources;
/**
* This class is used to represent PlayerData related to the board
*/
@Serializable
public class PlayerData {
/**
* An array of HomeNode objects representing the home nodes of the player.
*/
private HomeNode[] homeNodes;
/**
* The index of the start node for the player.
*/
private int startNodeIndex;
/**
* An array of Piece objects representing the waiting area of the player.
*/
private Piece[] waitingArea;
/**
* An array of Piece objects representing all the pieces of the player.
*/
private Piece[] pieces;
/**
* This constructor is used to create a new PlayerData object
*
* @param color the color of the player
*/
public PlayerData(Color color) {
homeNodes = new HomeNode[Resources.MAX_PIECES];
pieces = new Piece[Resources.MAX_PIECES];
waitingArea = new Piece[Resources.MAX_PIECES];
for (int i = 0; i < Resources.MAX_PIECES; i++) {
homeNodes[i] = new HomeNode();
pieces[i] = new Piece(color, PieceState.WAITING);
waitingArea[i] = pieces[i];
}
}
/**
* Constructor.
*/
private PlayerData() {
homeNodes = new HomeNode[Resources.MAX_PIECES];
waitingArea = new Piece[Resources.MAX_PIECES];
pieces = new Piece[Resources.MAX_PIECES];
}
/**
* This method will be used to check if the player is finished.
* ToDo: Currently return always false. Implement logic!
*
* @return true or false.
*/
public boolean isFinished() {
return false;
}
/**
* This method returns an Array of HomeNodes
*
* @return the array of HomeNodes
*/
public Node[] getHomeNodes() {
return homeNodes;
}
/**
* This method returns the index of the StartNode
*
* @return the index of the StartNode
*/
public int getStartNodeIndex() {
return startNodeIndex;
}
/**
* This method sets the index of the startNode.
*
* @param startNodeIndex the integer index of the startNode
*/
public void setStartNodeIndex(int startNodeIndex) {
this.startNodeIndex = startNodeIndex;
}
/**
* This method returns an array pieces representing the waiting area.
*
* @return the waiting area
*/
public Piece[] getWaitingArea() {
return waitingArea;
}
/**
* This method returns an array of pieces with all the players pieces
*
* @return the array of pieces
*/
public Piece[] getPieces() {
return pieces;
}
/**
* This method adds a piece to the waiting area
*
* @param piece the piece to be added to the waiting area
*/
public void addWaitingPiece(Piece piece) {
for (int i = 0; i < Resources.MAX_PIECES; i++) {
if (waitingArea[i] == null) {
waitingArea[i] = piece;
return;
}
}
}
/**
* This method removes a piece from the waiting area
*
* @return the piece that was removed from the waiting area
*/
public Piece removePieceFromWaitingArea() {
for (int i = 0; i < Resources.MAX_PIECES; i++) {
if (waitingArea[i] != null) {
Piece piece = waitingArea[i];
waitingArea[i] = null;
return piece;
}
}
return null;
}
/**
* This method sets a piece at the given index in the home area
*
* @param index the index of the node
* @param piece the piece to be set at the given index
*/
public void setPieceInHome(int index, Piece piece) {
homeNodes[index].setOccupant(piece);
}
/**
* This method will be used to return if the given piece parameter is inside the homNodes attribute of PlayerData
* class.
* If yes it will return true, otherwise false.
*
* @return true or false.
*/
public boolean homeIncludes(Piece piece) {
for (Node node : this.homeNodes) {
if (node.getOccupant() == piece) {
return true;
}
}
return false;
}
/**
* This method will be used to return the index of the given piece parameter in the homeNodes attribute of
* PlayerData class.
*
* @return index as an Integer.
*/
public int getIndexInHome(Piece piece) {
for (int i = 0; i < Resources.MAX_PIECES; i++) {
if (homeNodes[i].getOccupant() == piece) {
return i;
}
}
return -1;
}
/**
* This method will be usd to check if the waitingArea attribute of PlayerData class is empty.
* If yes it will return false, otherwise true.
*
* @return true or false.
*/
public boolean hasPieceInWaitingArea() {
for (Piece piece : waitingArea) {
if (piece != null) {
return true;
}
}
return false;
}
}