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

@@ -3,12 +3,21 @@ package game;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import sudoku.MultiDoku;
public class Game { public class Game {
public static enum GameState {
GameNotStarted, GameGoing, GameEnd
}
private final Map<Integer, Player> players; private final Map<Integer, Player> players;
private GameState gameState;
private MultiDoku doku;
public Game() { public Game() {
this.players = new HashMap<>(); this.players = new HashMap<>();
this.gameState = GameState.GameNotStarted;
} }
public Player getPlayerById(int id) { public Player getPlayerById(int id) {
@@ -27,4 +36,17 @@ public class Game {
return players; return players;
} }
public void startGame(MultiDoku doku) {
this.doku = doku;
this.gameState = GameState.GameGoing;
}
public GameState getGameState() {
return gameState;
}
public MultiDoku getDoku() {
return doku;
}
} }

View File

@@ -12,36 +12,41 @@ import imgui.flag.ImGuiCol;
import imgui.flag.ImGuiStyleVar; import imgui.flag.ImGuiStyleVar;
import sudoku.Block; import sudoku.Block;
import sudoku.Cell; import sudoku.Cell;
import sudoku.MultiDoku;
import sudoku.Sudoku; import sudoku.Sudoku;
public class SudokuRenderer { public class SudokuRenderer {
private final Sudoku sudoku; private final MultiDoku doku;
private Sudoku currentSudoku;
private int currentIndex = -1; private int currentIndex = -1;
private final Map<Block, Color> colorPalette; private final Map<Block, Color> colorPalette;
public SudokuRenderer(Sudoku sudoku) { public SudokuRenderer(MultiDoku doku) {
this.sudoku = sudoku; this.doku = doku;
this.colorPalette = new HashMap<>(); this.currentSudoku = doku.getSubGrid(0);
initColors(); this.colorPalette = initColors();
} }
private void initColors() { private Map<Block, Color> initColors() {
List<Color> colors = ColorGenerator.greatPalette(sudoku.getSize()); List<Color> colors = ColorGenerator.greatPalette(currentSudoku.getSize());
Map<Block, Color> colorPalette = new HashMap<>();
int index = 0; int index = 0;
for (Block block : sudoku.getBlocks()) { for (Block block : currentSudoku.getBlocks()) {
colorPalette.put(block, colors.get(index)); colorPalette.put(block, colors.get(index));
index++; index++;
} }
return colorPalette;
} }
private void renderPopup() { private void renderPopup() {
if (ImGui.beginPopup("editPopup")) { if (ImGui.beginPopup("editPopup")) {
for (int i = 1; i < sudoku.getSize() + 1; i++) { for (int i = 1; i < currentSudoku.getSize() + 1; i++) {
if (i % (int) (Math.sqrt(sudoku.getSize())) != 1) if (i % (int) (Math.sqrt(currentSudoku.getSize())) != 1)
ImGui.sameLine(); ImGui.sameLine();
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) { if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
this.sudoku.setCellSymbol(currentIndex % sudoku.getSize(), currentIndex / sudoku.getSize(), i - 1); this.currentSudoku.setCellSymbol(currentIndex % currentSudoku.getSize(),
currentIndex / currentSudoku.getSize(), i - 1);
ImGui.closeCurrentPopup(); ImGui.closeCurrentPopup();
} }
} }
@@ -50,13 +55,12 @@ public class SudokuRenderer {
} }
public void render() { public void render() {
ImGui.begin("Sudoku Window"); for (int y = 0; y < currentSudoku.getSize(); y++) {
for (int y = 0; y < sudoku.getSize(); y++) { for (int x = 0; x < currentSudoku.getSize(); x++) {
for (int x = 0; x < sudoku.getSize(); x++) {
if (x > 0) if (x > 0)
ImGui.sameLine(); ImGui.sameLine();
int index = y * sudoku.getSize() + x; int index = y * currentSudoku.getSize() + x;
Cell cell = sudoku.getCell(x, y); Cell cell = currentSudoku.getCell(x, y);
int symbol = cell.getSymbolIndex(); int symbol = cell.getSymbolIndex();
Color blockColor = colorPalette.get(cell.getBlock()); Color blockColor = colorPalette.get(cell.getBlock());
ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f)); ImGui.pushStyleVar(ImGuiStyleVar.SelectableTextAlign, new ImVec2(0.5f, 0.5f));
@@ -73,7 +77,6 @@ public class SudokuRenderer {
} }
} }
renderPopup(); renderPopup();
ImGui.end();
} }
} }

View File

@@ -14,8 +14,14 @@ public abstract class BaseView {
public void cleanResources() {} public void cleanResources() {}
public void closeMenu(int count) {
for (int i = 0; i < count; i++) {
this.stateMachine.popState();
}
}
public void closeMenu() { public void closeMenu() {
this.stateMachine.popState(); closeMenu(1);
} }
protected void renderReturnButton() { protected void renderReturnButton() {

View File

@@ -0,0 +1,38 @@
package gui.menu;
import gui.SudokuRenderer;
import imgui.ImGui;
import network.client.Client;
import network.server.Server;
import sudoku.MultiDoku;
public class MultiPlayerDokuView extends BaseView{
private final Client client;
private final Server server;
private final SudokuRenderer sudokuRenderer;
public MultiPlayerDokuView(StateMachine stateMachine, Client client, Server server) {
super(stateMachine);
this.client = client;
this.server = server;
this.sudokuRenderer = new SudokuRenderer(this.client.getGame().getDoku());
this.client.onDisconnect.connect(this::onDisconnect);
}
public void onDisconnect() {
if (server == null) {
closeMenu();
}
}
@Override
public void render() {
this.sudokuRenderer.render();
if (ImGui.button("Quitter")) {
this.client.stop();
this.closeMenu(3);
}
}
}

View File

@@ -4,6 +4,8 @@ import game.Player;
import imgui.ImGui; import imgui.ImGui;
import network.client.Client; import network.client.Client;
import network.server.Server; import network.server.Server;
import sudoku.MultiDoku;
import sudoku.SudokuFactory;
public class MultiPlayerView extends BaseView { public class MultiPlayerView extends BaseView {
@@ -15,6 +17,7 @@ public class MultiPlayerView extends BaseView {
this.client = client; this.client = client;
this.server = server; this.server = server;
this.client.onDisconnect.connect(this::onDisconnect); this.client.onDisconnect.connect(this::onDisconnect);
this.client.onGameStarted.connect(() -> this.stateMachine.pushState(new MultiPlayerDokuView(stateMachine, client, server)));
} }
@Override @Override
@@ -32,7 +35,9 @@ public class MultiPlayerView extends BaseView {
ImGui.text("En attente de l'administrateur du serveur ..."); ImGui.text("En attente de l'administrateur du serveur ...");
} else { } else {
if (ImGui.button("Démarrer")) { if (ImGui.button("Démarrer")) {
// start the game // temp
MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(5);
this.server.startGame(doku);
} }
} }
} }

View File

@@ -12,6 +12,8 @@ import network.protocol.packets.KeepAlivePacket;
import network.protocol.packets.LoginPacket; import network.protocol.packets.LoginPacket;
import network.protocol.packets.PlayerJoinPacket; import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket; import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
import sudoku.io.SudokuSerializer;
public class ClientConnexion extends Connexion { public class ClientConnexion extends Connexion {
@@ -65,4 +67,10 @@ public class ClientConnexion extends Connexion {
this.client.getGame().removePlayer(packet.getPlayer()); 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.LoginPacket;
import network.protocol.packets.PlayerJoinPacket; import network.protocol.packets.PlayerJoinPacket;
import network.protocol.packets.PlayerLeavePacket; import network.protocol.packets.PlayerLeavePacket;
import network.protocol.packets.StartGamePacket;
public interface PacketVisitor { public interface PacketVisitor {
@@ -19,5 +20,6 @@ public interface PacketVisitor {
void visitPacket(LoginPacket packet); void visitPacket(LoginPacket packet);
void visitPacket(PlayerJoinPacket packet); void visitPacket(PlayerJoinPacket packet);
void visitPacket(PlayerLeavePacket packet); void visitPacket(PlayerLeavePacket packet);
void visitPacket(StartGamePacket packet);
} }

View File

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