Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 05df8a56a7 | |||
| af0ac0ff77 | |||
| 375e0eddbd | |||
| 7b90a81365 |
@@ -9,14 +9,6 @@ Une application de génération et résolution de MultiDoku.
|
||||
- Sudoku saves
|
||||
- Multiplayer
|
||||
|
||||
## Screenshots 🖼
|
||||
|
||||
[[screenshots/menu.png]]
|
||||
|
||||
[[screenshots/solo.png]]
|
||||
|
||||
[[screenshots/multi.png]]
|
||||
|
||||
## Develop ☝🤓
|
||||
|
||||
**Pour plus de détails sur la conception et autres, regarder le 👉 [wiki](https://git.ale-pri.com/Ryuk/Sudoku/wiki)** 👈
|
||||
|
||||
@@ -71,28 +71,24 @@ public class SudokuRenderer {
|
||||
|
||||
private void renderPopup() {
|
||||
if (ImGui.beginPopup("editPopup")) {
|
||||
if (currentCell == null)
|
||||
ImGui.closeCurrentPopup();
|
||||
else {
|
||||
Block block = currentCell.getBlock();
|
||||
int symbolCount = block.getCells().size();
|
||||
for (int i = 0; i < symbolCount; i++) {
|
||||
if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1)
|
||||
ImGui.sameLine();
|
||||
if (currentCell.getSymbolIndex() == i) {
|
||||
if (ImGui.button("X", cellSize)) {
|
||||
currentCell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
this.onCellChange.emit(currentCell);
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
} else {
|
||||
if (ImGui.button(Options.Symboles.getSymbols().get(i), cellSize)) {
|
||||
currentCell.setSymbolIndex(i);
|
||||
this.onCellChange.emit(currentCell);
|
||||
if (this.doku.getDoku().isSolved())
|
||||
this.onResolve.emit();
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
Block block = currentCell.getBlock();
|
||||
int symbolCount = block.getCells().size();
|
||||
for (int i = 0; i < symbolCount; i++) {
|
||||
if ((i + 1) % (int) (Math.sqrt(symbolCount)) != 1)
|
||||
ImGui.sameLine();
|
||||
if (currentCell.getSymbolIndex() == i) {
|
||||
if (ImGui.button("X", cellSize)) {
|
||||
currentCell.setSymbolIndex(Cell.NOSYMBOL);
|
||||
this.onCellChange.emit(currentCell);
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
} else {
|
||||
if (ImGui.button(Options.Symboles.getSymbols().get(i), cellSize)) {
|
||||
currentCell.setSymbolIndex(i);
|
||||
this.onCellChange.emit(currentCell);
|
||||
if (this.doku.getDoku().isSolved())
|
||||
this.onResolve.emit();
|
||||
ImGui.closeCurrentPopup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,54 @@
|
||||
package network;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.PacketVisitor;
|
||||
|
||||
public abstract class Connexion implements PacketVisitor {
|
||||
|
||||
final Socket socket;
|
||||
private final ObjectOutputStream objectOutputStream;
|
||||
private final ConnexionThread connexionThread;
|
||||
|
||||
public Connexion(Socket socket) throws IOException {
|
||||
protected final DatagramSocket socket;
|
||||
protected final InetSocketAddress address;
|
||||
|
||||
public Connexion(DatagramSocket socket, InetSocketAddress address) {
|
||||
this.socket = socket;
|
||||
this.objectOutputStream = new ObjectOutputStream(this.socket.getOutputStream());
|
||||
this.connexionThread = new ConnexionThread(this);
|
||||
this.connexionThread.start();
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public boolean isClosed() {
|
||||
return this.socket.isClosed();
|
||||
public static record ReadInfo(Packet packet, InetSocketAddress address) {}
|
||||
|
||||
public static ReadInfo readPacket(final DatagramSocket socket)
|
||||
throws IOException, ClassNotFoundException {
|
||||
byte[] buffer = new byte[65535];
|
||||
DatagramPacket dataPacket = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(dataPacket);
|
||||
|
||||
InetSocketAddress address = new InetSocketAddress(dataPacket.getAddress(), dataPacket.getPort());
|
||||
|
||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(dataPacket.getData()));
|
||||
Packet packet = (Packet) ois.readObject();
|
||||
return new ReadInfo(packet, address);
|
||||
}
|
||||
|
||||
public synchronized void sendPacket(Packet packet) {
|
||||
public void sendPacket(Packet packet) {
|
||||
try {
|
||||
objectOutputStream.writeObject(packet);
|
||||
objectOutputStream.flush();
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(stream);
|
||||
oos.writeObject(packet);
|
||||
oos.flush();
|
||||
byte[] data = stream.toByteArray();
|
||||
DatagramPacket dataPacket = new DatagramPacket(data, data.length, this.address.getAddress(),
|
||||
this.address.getPort());
|
||||
this.socket.send(dataPacket);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error while sending packet ! " + e.getLocalizedMessage());
|
||||
close();
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.connexionThread.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class ConnexionThread extends Thread {
|
||||
|
||||
private final Connexion connexion;
|
||||
private final ObjectInputStream objectInputStream;
|
||||
|
||||
public ConnexionThread(Connexion connexion) throws IOException {
|
||||
this.connexion = connexion;
|
||||
this.objectInputStream = new ObjectInputStream(this.connexion.socket.getInputStream());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!interrupted()) {
|
||||
try {
|
||||
// System.out.println(objectInputStream.available());
|
||||
Object o = objectInputStream.readObject();
|
||||
if (o instanceof Packet packet) {
|
||||
connexion.visit(packet);
|
||||
}
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
e.printStackTrace();
|
||||
this.connexion.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
objectInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
interrupt();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package network.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
import network.protocol.packets.DisconnectPacket;
|
||||
@@ -21,18 +23,35 @@ import sudoku.io.SudokuSerializer;
|
||||
public class ClientConnexion extends Connexion {
|
||||
|
||||
private final Client client;
|
||||
private final Thread readThread;
|
||||
|
||||
public ClientConnexion(String address, short port, Client client) throws UnknownHostException, IOException {
|
||||
super(new Socket(address, port));
|
||||
super(new DatagramSocket(), new InetSocketAddress(address, port));
|
||||
this.client = client;
|
||||
this.readThread = new Thread(this::readPackets);
|
||||
this.readThread.start();
|
||||
}
|
||||
|
||||
private void readPackets() {
|
||||
while (!Thread.interrupted()) {
|
||||
try {
|
||||
Packet packet = Connexion.readPacket(this.socket).packet();
|
||||
|
||||
visit(packet);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!this.isClosed()) {
|
||||
super.close();
|
||||
if (!this.socket.isClosed()) {
|
||||
sendPacket(new DisconnectPacket(""));
|
||||
client.onDisconnect.emit();
|
||||
this.readThread.interrupt();
|
||||
this.socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import game.Player;
|
||||
import network.protocol.Packet;
|
||||
import network.protocol.packets.EndGamePacket;
|
||||
import network.protocol.packets.StartGamePacket;
|
||||
@@ -17,17 +18,17 @@ import sudoku.structure.MultiDoku;
|
||||
|
||||
public class Server {
|
||||
|
||||
final ServerSocket serverSocket;
|
||||
final List<ServerConnexion> connexions;
|
||||
private final ServerAcceptThread acceptThread;
|
||||
final DatagramSocket serverSocket;
|
||||
final Map<InetSocketAddress, ServerConnexion> connexions;
|
||||
private final ServerReadThread acceptThread;
|
||||
private final ServerLogicThread logicThread;
|
||||
private final Game game;
|
||||
private int nextPlayerId = 0;
|
||||
|
||||
public Server(short port) throws IOException {
|
||||
this.serverSocket = new ServerSocket(port);
|
||||
this.connexions = new ArrayList<>();
|
||||
this.acceptThread = new ServerAcceptThread(this);
|
||||
this.serverSocket = new DatagramSocket(port);
|
||||
this.connexions = new HashMap<>();
|
||||
this.acceptThread = new ServerReadThread(this);
|
||||
this.acceptThread.start();
|
||||
this.logicThread = new ServerLogicThread(this);
|
||||
this.logicThread.start();
|
||||
@@ -35,7 +36,7 @@ public class Server {
|
||||
}
|
||||
|
||||
public void broadcastPacket(Packet packet) {
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.sendPacket(packet);
|
||||
}
|
||||
}
|
||||
@@ -50,8 +51,9 @@ public class Server {
|
||||
}
|
||||
|
||||
private void checkConnexions() {
|
||||
for (var it = connexions.iterator(); it.hasNext();) {
|
||||
ServerConnexion connexion = it.next();
|
||||
for (var it = connexions.entrySet().iterator(); it.hasNext();) {
|
||||
var entry = it.next();
|
||||
ServerConnexion connexion = entry.getValue();
|
||||
if (!connexion.update()) {
|
||||
connexion.close();
|
||||
connexion.nukeConnection();
|
||||
@@ -68,10 +70,10 @@ public class Server {
|
||||
public void stop() {
|
||||
this.acceptThread.cancel();
|
||||
this.logicThread.cancel();
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.nukeConnection();
|
||||
connexion.close();
|
||||
}
|
||||
this.serverSocket.close();
|
||||
}
|
||||
|
||||
public Player addPlayer(String pseudo) {
|
||||
@@ -88,7 +90,7 @@ public class Server {
|
||||
public void startGame(MultiDoku doku, long gameDuration) {
|
||||
Instant now = Instant.now();
|
||||
this.game.startGame(doku, now, gameDuration);
|
||||
for (ServerConnexion connexion : this.connexions) {
|
||||
for (ServerConnexion connexion : this.connexions.values()) {
|
||||
connexion.setSudoku(doku.clone());
|
||||
}
|
||||
broadcastPacket(new StartGamePacket(SudokuSerializer.serializeSudoku(doku).toString(), now, gameDuration));
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ServerAcceptThread extends Thread {
|
||||
|
||||
private final Server server;
|
||||
|
||||
public ServerAcceptThread(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
this.server.serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while(!interrupted()) {
|
||||
Socket newConnection = this.server.serverSocket.accept();
|
||||
ServerConnexion serverConnection = new ServerConnexion(newConnection, this.server);
|
||||
this.server.connexions.add(serverConnection);
|
||||
}
|
||||
} catch(IOException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import game.Game;
|
||||
import game.Player;
|
||||
import game.Game.GameState;
|
||||
import game.Player;
|
||||
import network.Connexion;
|
||||
import network.protocol.packets.ChangeCellPacket;
|
||||
import network.protocol.packets.ConnexionInfoPacket;
|
||||
@@ -29,14 +29,14 @@ public class ServerConnexion extends Connexion {
|
||||
private Player player = null;
|
||||
private MultiDoku doku;
|
||||
|
||||
public ServerConnexion(Socket socket, Server server) throws IOException {
|
||||
super(socket);
|
||||
public ServerConnexion(InetSocketAddress remoteAddress, Server server) throws IOException {
|
||||
super(server.serverSocket, remoteAddress);
|
||||
this.server = server;
|
||||
this.keepAliveHandler = new KeepAliveHandler(this);
|
||||
}
|
||||
|
||||
public boolean update() {
|
||||
if (shouldClose || isClosed())
|
||||
if (shouldClose)
|
||||
return false;
|
||||
return this.keepAliveHandler.update();
|
||||
}
|
||||
@@ -49,15 +49,6 @@ public class ServerConnexion extends Connexion {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (shouldClose)
|
||||
return;
|
||||
super.close();
|
||||
shouldClose = true;
|
||||
System.out.println("[Server] Closing connexion !");
|
||||
}
|
||||
|
||||
private void finishLogin() {
|
||||
// send players that have already joined (excluding this one)
|
||||
for (Player p : this.server.getGame().getPlayers().values()) {
|
||||
@@ -87,7 +78,7 @@ public class ServerConnexion extends Connexion {
|
||||
|
||||
@Override
|
||||
public void visitPacket(DisconnectPacket packet) {
|
||||
close();
|
||||
//TODO: close connexion
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -164,4 +155,8 @@ public class ServerConnexion extends Connexion {
|
||||
this.server.broadcastPacket(new UpdatePlayerScorePacket(player.getId(), player.getRemainingCells()));
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.shouldClose = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
43
app/src/main/java/network/server/ServerReadThread.java
Normal file
43
app/src/main/java/network/server/ServerReadThread.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package network.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import network.Connexion;
|
||||
import network.protocol.Packet;
|
||||
|
||||
public class ServerReadThread extends Thread {
|
||||
|
||||
private final Server server;
|
||||
|
||||
public ServerReadThread(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
this.server.serverSocket.close();
|
||||
interrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!interrupted()) {
|
||||
Connexion.ReadInfo read = Connexion.readPacket(this.server.serverSocket);
|
||||
Packet packet = read.packet();
|
||||
InetSocketAddress address = read.address();
|
||||
|
||||
if (!this.server.connexions.containsKey(address)) {
|
||||
this.server.connexions.put(address, new ServerConnexion(address, server));
|
||||
}
|
||||
|
||||
this.server.connexions.get(address).visit(packet);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 204 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 236 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 242 KiB |
Reference in New Issue
Block a user