added the server and network functionality for mdga and fixed the communication in the Lobby

This commit is contained in:
Fleischer Hanno
2024-12-01 21:50:28 +01:00
parent 33ddea4221
commit 133f900ec7
22 changed files with 383 additions and 59 deletions

View File

@@ -28,9 +28,10 @@ public class ClientGameLogic implements ServerInterpreter {
private final InterruptState interruptState = new InterruptState(null, this);
private final SettingsState settingsState = new SettingsState(null, this);
public ClientGameLogic(Game game, ClientSender clientSender) {
this.game = game;
public ClientGameLogic(ClientSender clientSender) {
this.game = new Game();
this.clientSender = clientSender;
dialogsState.enter();
state = dialogsState;
}
@@ -244,7 +245,11 @@ public void selectName(String name){
}
public void selectReady(boolean ready){
state.selectReady(ready);
if(ready){
state.selectReady();
} else {
state.selectUnready();
}
}
public void selectHost(String name){
@@ -304,7 +309,11 @@ public SettingsState getSettings(){
}
public Notification getNotification(){
return notifications.remove(0);
if(!notifications.isEmpty()){
return notifications.remove(0);
} else {
return null;
}
}
public void addNotification(Notification notification){

View File

@@ -200,7 +200,7 @@ public void setName(String name) {
LOGGER.log(Level.DEBUG, "Setting name not allowed.");
}
public void selectReady(boolean ready) {
public void selectReady() {
LOGGER.log(Level.DEBUG, "Selecting ready not allowed.");
}

View File

@@ -37,7 +37,7 @@ public void enter(){
public void setState(DialogStates newState){
currentState.exit();
currentState.enter();
newState.enter();
currentState = newState;
}
@@ -75,6 +75,11 @@ public void selectLeave(){
currentState.selectLeave();
}
@Override
public void setName(String name){
currentState.setName(name);
}
@Override
public void selectTSK(Color color){
currentState.selectTSK(color);
@@ -86,8 +91,8 @@ public void deselectTSK(Color color){
}
@Override
public void selectReady(boolean ready){
currentState.selectReady(ready);
public void selectReady(){
currentState.selectReady();
}
@Override

View File

@@ -0,0 +1,9 @@
package pp.mdga.client;
public interface ServerConnection extends ClientSender {
boolean isConnected();
void connect();
void disconnect();
}

View File

@@ -11,6 +11,7 @@
import pp.mdga.message.server.ServerStartGameMessage;
import pp.mdga.message.server.UpdateReadyMessage;
import pp.mdga.message.server.UpdateTSKMessage;
import pp.mdga.notification.LobbyReadyNotification;
import pp.mdga.notification.TskSelectNotification;
import pp.mdga.notification.TskUnselectNotification;
@@ -49,7 +50,7 @@ public void deselectTSK(Color color) {
}
@Override
public void selectReady(boolean ready) {
public void selectReady() {
logic.send(new LobbyReadyMessage());
}
@@ -76,6 +77,7 @@ public void received(ServerStartGameMessage msg){
@Override
public void received(LobbyPlayerJoinedMessage msg){
logic.addNotification(new TskSelectNotification(msg.getPlayer().getColor(), msg.getPlayer().getName(), parent.getOwnPlayerId()== msg.getId()));
logic.getGame().getPlayers().put(msg.getId(), msg.getPlayer());
}
@@ -94,6 +96,7 @@ public void received(LobbyPlayerLeaveMessage msg){
@Override
public void received(UpdateReadyMessage msg){
logic.addNotification(new LobbyReadyNotification(logic.getGame().getPlayers().get(msg.getPlayerId()).getColor(), msg.isReady()));
logic.getGame().getPlayers().get(msg.getPlayerId()).setReady(msg.isReady());
}
}

View File

@@ -3,6 +3,7 @@
import pp.mdga.client.ClientGameLogic;
import pp.mdga.client.ClientState;
import pp.mdga.client.DialogsState;
import pp.mdga.notification.LobbyDialogNotification;
public class NetworkDialogState extends DialogStates {
@@ -55,9 +56,7 @@ public void selectLeave() {
}
public void selectJoin(String IP) {
if(checkIP(IP)){
parent.setState(parent.getLobby());
}
parent.setState(parent.getLobby());
logic.addNotification(new LobbyDialogNotification());
}
}

View File

@@ -37,6 +37,12 @@ public void selectHost(String name) {
logic.setHost(true);
}
@Override
public void setName(String name) {
parent.setState(parent.getNetworkDialog());
parent.setOwnPlayerName(name);
}
@Override
public void selectLeave() {
parent.exit();

View File

@@ -1,10 +1,13 @@
package pp.mdga.game;
import com.jme3.network.serializing.Serializable;
import java.util.ArrayList;
/**
* This class will be used to handle general PlayerData
*/
@Serializable
public class Player {
/**
* The name of the player.

View File

@@ -1,8 +1,11 @@
package pp.mdga.game;
import com.jme3.network.serializing.Serializable;
/**
* This class will be used to store Statistics during the Game;
*/
@Serializable
public class Statistic {
/**
* The number of cards played.

View File

@@ -18,7 +18,7 @@ public StartGameMessage(boolean forceStartGame){
/**
* Constructs a new ForceStartGame message.
*/
public StartGameMessage() {
private StartGameMessage() {
super();
forceStartGame = false;
}

View File

@@ -0,0 +1,43 @@
package pp.mdga.notification;
import pp.mdga.game.Color;
public class LobbyReadyNotification extends Notification{
/**
* The color of the player.
*/
private Color color;
/**
* Indicates if the player is ready.
*/
private boolean ready;
/**
* Constructor
*/
public LobbyReadyNotification(Color color, boolean ready) {
this.color = color;
this.ready = ready;
}
/**
* This method is used to get the color of the player
*
* @return the color of the player
*/
public Color getColor() {
return color;
}
/**
* This method is used to get the ready state of the player
*
* @return the ready state of the player
*/
public boolean isReady() {
return ready;
}
}