basic multiplayer
Some checks failed
Linux arm64 / Build (push) Failing after 5m3s

This commit is contained in:
2025-01-26 18:53:45 +01:00
parent 6658b0e884
commit df07f11a9c
11 changed files with 149 additions and 20 deletions

View File

@@ -12,6 +12,8 @@ import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
public class ClientConnexion extends Connexion {
@@ -65,4 +67,10 @@ public class ClientConnexion extends Connexion {
this.client.getGame().removePlayer(packet.getPlayer());
}
@Override
public void visitPacket(StartGamePacket packet) {
this.client.getGame().startGame(SudokuSerializer.deserializeSudoku(packet.getSerializedSudoku()));
this.client.onGameStarted.emit();
}
}

View File

@@ -6,6 +6,7 @@ import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
public interface PacketVisitor {
@@ -19,5 +20,6 @@ public interface PacketVisitor {
void visitPacket(LoginPacket packet);
void visitPacket(PlayerJoinPacket packet);
void visitPacket(PlayerLeavePacket packet);
void visitPacket(StartGamePacket packet);
}

View File

@@ -2,6 +2,6 @@ package network.protocol;
public enum Packets {
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave
ConnectionInfo, KeepAlive, Disconnect, Login, PlayerJoin, PlayerLeave, StartGame
}

View File

@@ -0,0 +1,26 @@
package network.protocol.packets;
import network.protocol.Packet;
import network.protocol.PacketVisitor;
import network.protocol.Packets;
public class StartGamePacket extends Packet {
static private final long serialVersionUID = Packets.StartGame.ordinal();
private final String serializedSudoku;
public StartGamePacket(String serializedSudoku) {
this.serializedSudoku = serializedSudoku;
}
public String getSerializedSudoku() {
return serializedSudoku;
}
@Override
public void accept(PacketVisitor packetVisitor) {
packetVisitor.visitPacket(this);
}
}

View File

@@ -8,6 +8,9 @@ import java.util.List;
import game.Game;
import game.Player;
import network.protocol.Packet;
import network.protocol.packets.StartGamePacket;
import sudoku.MultiDoku;
import sudoku.io.SudokuSerializer;
public class Server {
@@ -65,4 +68,9 @@ public class Server {
return game;
}
public void startGame(MultiDoku doku) {
this.game.startGame(doku);
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku)));
}
}

View File

@@ -4,6 +4,7 @@ import java.io.IOException;
import java.net.Socket;
import game.Player;
import game.Game.GameState;
import network.Connexion;
import network.protocol.packets.ConnexionInfoPacket;
import network.protocol.packets.DisconnectPacket;
@@ -11,6 +12,8 @@ import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
public class ServerConnexion extends Connexion {
@@ -56,6 +59,9 @@ public class ServerConnexion extends Connexion {
}
this.server.broadcastPacket(new PlayerJoinPacket(player));
sendPacket(new ConnexionInfoPacket(player.getId()));
if (this.server.getGame().getGameState() == GameState.GameGoing) {
sendPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(this.server.getGame().getDoku())));
}
}
@Override
@@ -91,4 +97,9 @@ public class ServerConnexion extends Connexion {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketPlayerLeave'");
}
@Override
public void visitPacket(StartGamePacket packet) {
throw new UnsupportedOperationException("Unimplemented method 'visitPacketStartGame'");
}
}