minor changes to the server state automaton

This commit is contained in:
Daniel Grigencha
2024-11-26 23:28:49 +01:00
parent 84c289cfd1
commit 5a911326ba
47 changed files with 1007 additions and 470 deletions

View File

@@ -1,14 +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.server.ServerMessage;
import pp.mdga.server.ServerGameLogic;
import pp.mdga.server.ServerSender;
@@ -16,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;
@@ -53,7 +51,7 @@ public static void main(String[] args) {
*/
public MdgaServer() {
LOGGER.log(Level.INFO, "Creating MdgaServer"); //NON-NLS
logic = new ServerGameLogic(new Game(), this);
logic = new ServerGameLogic(this, new Game());
}
public void run() {
@@ -92,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);
@@ -147,7 +145,7 @@ private void registerListeners() {
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);
@@ -156,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 = 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
@@ -206,4 +214,16 @@ 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.getValue().getId(), message);
}
}
}