mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Compare commits
7 Commits
UnitTests
...
a60c702306
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a60c702306 | ||
|
|
c30c118c04 | ||
| 4b8edf1585 | |||
| fa9c2ce32b | |||
|
|
2599f9fa40 | ||
|
|
84e411fa38 | ||
|
|
66cb000fad |
@@ -1,10 +1,14 @@
|
|||||||
package org.toop.app;
|
package org.toop.app;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
import javafx.geometry.Pos;
|
||||||
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.input.KeyCodeCombination;
|
import javafx.scene.input.KeyCodeCombination;
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
|
import javafx.scene.layout.StackPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
import org.toop.app.widget.WidgetContainer;
|
import org.toop.app.widget.WidgetContainer;
|
||||||
import org.toop.app.widget.complex.LoadingWidget;
|
import org.toop.app.widget.complex.LoadingWidget;
|
||||||
@@ -16,8 +20,13 @@ import org.toop.framework.audio.*;
|
|||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.eventbus.GlobalEventBus;
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.networking.NetworkingClientEventListener;
|
import org.toop.framework.game.BitboardGame;
|
||||||
import org.toop.framework.networking.NetworkingClientManager;
|
import org.toop.framework.game.games.reversi.BitboardReversi;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.networking.connection.NetworkingClientEventListener;
|
||||||
|
import org.toop.framework.networking.connection.NetworkingClientManager;
|
||||||
|
import org.toop.framework.networking.server.GameDefinition;
|
||||||
|
import org.toop.framework.networking.server.MasterServer;
|
||||||
import org.toop.framework.resource.ResourceLoader;
|
import org.toop.framework.resource.ResourceLoader;
|
||||||
import org.toop.framework.resource.ResourceManager;
|
import org.toop.framework.resource.ResourceManager;
|
||||||
import org.toop.framework.resource.events.AssetLoaderEvents;
|
import org.toop.framework.resource.events.AssetLoaderEvents;
|
||||||
@@ -27,13 +36,8 @@ import org.toop.framework.resource.resources.SoundEffectAsset;
|
|||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
import org.toop.local.AppSettings;
|
import org.toop.local.AppSettings;
|
||||||
|
|
||||||
import javafx.application.Application;
|
|
||||||
import javafx.geometry.Pos;
|
|
||||||
import javafx.scene.Scene;
|
|
||||||
import javafx.scene.layout.StackPane;
|
|
||||||
import javafx.stage.Stage;
|
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
@@ -86,7 +90,7 @@ public final class App extends Application {
|
|||||||
|
|
||||||
AppSettings.applySettings();
|
AppSettings.applySettings();
|
||||||
|
|
||||||
setKeybinds(root);
|
setKeybinds(root);
|
||||||
|
|
||||||
LoadingWidget loading = new LoadingWidget(Primitive.text(
|
LoadingWidget loading = new LoadingWidget(Primitive.text(
|
||||||
"Loading...", false), 0, 0, Integer.MAX_VALUE, false, false // Just set a high default
|
"Loading...", false), 0, 0, Integer.MAX_VALUE, false, false // Just set a high default
|
||||||
@@ -137,13 +141,12 @@ public final class App extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stage.show();
|
stage.show();
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void setKeybinds(StackPane root) {
|
private void setKeybinds(StackPane root) {
|
||||||
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
||||||
if (event.getCode() == KeyCode.ESCAPE) {
|
if (event.getCode() == KeyCode.ESCAPE) {
|
||||||
escapePopup();
|
escapePopup();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
stage.setFullScreenExitKeyCombination(
|
stage.setFullScreenExitKeyCombination(
|
||||||
|
|||||||
@@ -14,14 +14,14 @@ import org.toop.framework.eventbus.EventFlow;
|
|||||||
import org.toop.framework.gameFramework.controller.GameController;
|
import org.toop.framework.gameFramework.controller.GameController;
|
||||||
import org.toop.framework.eventbus.GlobalEventBus;
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
import org.toop.framework.networking.connection.clients.TournamentNetworkingClient;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
||||||
import org.toop.game.games.reversi.BitboardReversi;
|
import org.toop.framework.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.game.players.ArtificialPlayer;
|
import org.toop.framework.game.players.ArtificialPlayer;
|
||||||
import org.toop.game.players.OnlinePlayer;
|
import org.toop.framework.game.players.OnlinePlayer;
|
||||||
import org.toop.game.players.RandomAI;
|
import org.toop.framework.game.players.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -214,15 +214,15 @@ public final class Server {
|
|||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TICTACTOE ->{
|
case TICTACTOE ->{
|
||||||
Player<BitboardTicTacToe>[] players = new Player[2];
|
Player[] players = new Player[2];
|
||||||
players[(myTurn + 1) % 2] = new OnlinePlayer<>(response.opponent());
|
players[(myTurn + 1) % 2] = new OnlinePlayer(response.opponent());
|
||||||
players[myTurn] = new ArtificialPlayer<>(new RandomAI<BitboardTicTacToe>(), user);
|
players[myTurn] = new ArtificialPlayer(new RandomAI(), user);
|
||||||
gameController = new TicTacToeBitController(players);
|
gameController = new TicTacToeBitController(players);
|
||||||
}
|
}
|
||||||
case REVERSI -> {
|
case REVERSI -> {
|
||||||
Player<BitboardReversi>[] players = new Player[2];
|
Player[] players = new Player[2];
|
||||||
players[(myTurn + 1) % 2] = new OnlinePlayer<>(response.opponent());
|
players[(myTurn + 1) % 2] = new OnlinePlayer(response.opponent());
|
||||||
players[myTurn] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(), user);
|
players[myTurn] = new ArtificialPlayer(new RandomAI(), user);
|
||||||
gameController = new ReversiBitController(players);}
|
gameController = new ReversiBitController(players);}
|
||||||
default -> new ErrorPopup("Unsupported game type.");
|
default -> new ErrorPopup("Unsupported game type.");
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import org.toop.framework.gameFramework.view.GUIEvents;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public abstract class BitGameCanvas<T extends TurnBasedGame<T>> implements GameCanvas<T> {
|
public abstract class BitGameCanvas implements GameCanvas {
|
||||||
protected record Cell(float x, float y, float width, float height) {
|
protected record Cell(float x, float y, float width, float height) {
|
||||||
public boolean isInside(double x, double y) {
|
public boolean isInside(double x, double y) {
|
||||||
return x >= this.x && x <= this.x + width &&
|
return x >= this.x && x <= this.x + width &&
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package org.toop.app.canvas;
|
package org.toop.app.canvas;
|
||||||
|
|
||||||
import javafx.scene.canvas.Canvas;
|
import javafx.scene.canvas.Canvas;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
|
|
||||||
public interface GameCanvas<T extends TurnBasedGame<T>> extends GameDrawer<T>{
|
public interface GameCanvas extends GameDrawer{
|
||||||
Canvas getCanvas();
|
Canvas getCanvas();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package org.toop.app.canvas;
|
|||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
public interface GameDrawer<T extends TurnBasedGame<T>> {
|
public interface GameDrawer {
|
||||||
void redraw(T gameCopy);
|
void redraw(TurnBasedGame gameCopy);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,9 @@ package org.toop.app.canvas;
|
|||||||
|
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import org.toop.app.App;
|
import org.toop.app.App;
|
||||||
import org.toop.game.games.reversi.BitboardReversi;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
import java.util.Arrays;
|
public class ReversiBitCanvas extends BitGameCanvas {
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi> {
|
|
||||||
public ReversiBitCanvas() {
|
public ReversiBitCanvas() {
|
||||||
super(Color.GRAY, new Color(0f, 0.4f, 0.2f, 1f), (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, 8, 8, 5, true);
|
super(Color.GRAY, new Color(0f, 0.4f, 0.2f, 1f), (App.getHeight() / 4) * 3, (App.getHeight() / 4) * 3, 8, 8, 5, true);
|
||||||
canvas.setOnMouseMoved(event -> {
|
canvas.setOnMouseMoved(event -> {
|
||||||
@@ -33,7 +30,7 @@ public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redraw(BitboardReversi gameCopy) {
|
public void redraw(TurnBasedGame gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
long[] board = gameCopy.getBoard();
|
long[] board = gameCopy.getBoard();
|
||||||
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
||||||
|
|||||||
@@ -2,12 +2,9 @@ package org.toop.app.canvas;
|
|||||||
|
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
import org.toop.app.App;
|
import org.toop.app.App;
|
||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
import java.util.Arrays;
|
public class TicTacToeBitCanvas extends BitGameCanvas{
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
|
||||||
public TicTacToeBitCanvas() {
|
public TicTacToeBitCanvas() {
|
||||||
super(
|
super(
|
||||||
Color.GRAY,
|
Color.GRAY,
|
||||||
@@ -22,7 +19,7 @@ public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redraw(BitboardTicTacToe gameCopy) {
|
public void redraw(TurnBasedGame gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
drawMoves(gameCopy.getBoard());
|
drawMoves(gameCopy.getBoard());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
import org.toop.framework.gameFramework.view.GUIEvents;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
import org.toop.game.players.LocalPlayer;
|
import org.toop.framework.game.players.LocalPlayer;
|
||||||
|
|
||||||
public class GenericGameController<T extends TurnBasedGame<T>> implements GameController {
|
public class GenericGameController implements GameController {
|
||||||
protected final EventFlow eventFlow = new EventFlow();
|
protected final EventFlow eventFlow = new EventFlow();
|
||||||
|
|
||||||
// Logger for logging
|
// Logger for logging
|
||||||
@@ -28,13 +28,13 @@ public class GenericGameController<T extends TurnBasedGame<T>> implements GameCo
|
|||||||
protected final GameView gameView;
|
protected final GameView gameView;
|
||||||
|
|
||||||
// Reference to game canvas
|
// Reference to game canvas
|
||||||
protected final GameCanvas<T> canvas;
|
protected final GameCanvas canvas;
|
||||||
|
|
||||||
protected final TurnBasedGame<T> game; // Reference to game instance
|
protected final TurnBasedGame game; // Reference to game instance
|
||||||
private final ThreadBehaviour gameThreadBehaviour;
|
private final ThreadBehaviour gameThreadBehaviour;
|
||||||
|
|
||||||
// TODO: Change gameType to automatically happen with either dependency injection or something else.
|
// TODO: Change gameType to automatically happen with either dependency injection or something else.
|
||||||
public GenericGameController(GameCanvas<T> canvas, T game, ThreadBehaviour gameThreadBehaviour, String gameType) {
|
public GenericGameController(GameCanvas canvas, TurnBasedGame game, ThreadBehaviour gameThreadBehaviour, String gameType) {
|
||||||
logger.info("Creating: " + this.getClass());
|
logger.info("Creating: " + this.getClass());
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
@@ -55,7 +55,9 @@ public class GenericGameController<T extends TurnBasedGame<T>> implements GameCo
|
|||||||
// Listen to updates
|
// Listen to updates
|
||||||
eventFlow
|
eventFlow
|
||||||
.listen(GUIEvents.GameEnded.class, this::onGameFinish, false)
|
.listen(GUIEvents.GameEnded.class, this::onGameFinish, false)
|
||||||
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer<T> lp){lp.setMove(event.move());}}, false);
|
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {
|
||||||
|
if (getCurrentPlayer() instanceof LocalPlayer lp){lp.setMove(event.move());}
|
||||||
|
}, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(){
|
public void start(){
|
||||||
@@ -70,7 +72,7 @@ public class GenericGameController<T extends TurnBasedGame<T>> implements GameCo
|
|||||||
gameThreadBehaviour.stop();
|
gameThreadBehaviour.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player<T> getCurrentPlayer(){
|
public Player getCurrentPlayer(){
|
||||||
return game.getPlayer(getCurrentPlayerIndex());
|
return game.getPlayer(getCurrentPlayerIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +99,7 @@ public class GenericGameController<T extends TurnBasedGame<T>> implements GameCo
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player<T> getPlayer(int player){
|
public Player getPlayer(int player){
|
||||||
if (player < 0 || player >= 2){ // TODO: Make game turn player count
|
if (player < 0 || player >= 2){ // TODO: Make game turn player count
|
||||||
logger.error("Invalid player index");
|
logger.error("Invalid player index");
|
||||||
throw new IllegalArgumentException("player out of range");
|
throw new IllegalArgumentException("player out of range");
|
||||||
|
|||||||
@@ -3,18 +3,19 @@ package org.toop.app.gameControllers;
|
|||||||
import org.toop.app.canvas.ReversiBitCanvas;
|
import org.toop.app.canvas.ReversiBitCanvas;
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.gameThreads.LocalThreadBehaviour;
|
import org.toop.framework.game.gameThreads.LocalThreadBehaviour;
|
||||||
import org.toop.game.gameThreads.OnlineThreadBehaviour;
|
import org.toop.framework.game.gameThreads.OnlineThreadBehaviour;
|
||||||
import org.toop.game.games.reversi.BitboardReversi;
|
import org.toop.framework.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.game.players.OnlinePlayer;
|
import org.toop.framework.game.players.OnlinePlayer;
|
||||||
|
|
||||||
public class ReversiBitController extends GenericGameController<BitboardReversi> {
|
public class ReversiBitController extends GenericGameController {
|
||||||
public ReversiBitController(Player<BitboardReversi>[] players) {
|
public ReversiBitController(Player[] players) {
|
||||||
BitboardReversi game = new BitboardReversi(players);
|
BitboardReversi game = new BitboardReversi();
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour<>(game);
|
game.init(players);
|
||||||
for (Player<BitboardReversi> player : players) {
|
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
||||||
if (player instanceof OnlinePlayer<BitboardReversi>){
|
for (Player player : players) {
|
||||||
thread = new OnlineThreadBehaviour<>(game);
|
if (player instanceof OnlinePlayer){
|
||||||
|
thread = new OnlineThreadBehaviour(game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super(new ReversiBitCanvas(), game, thread, "Reversi");
|
super(new ReversiBitCanvas(), game, thread, "Reversi");
|
||||||
|
|||||||
@@ -3,19 +3,19 @@ package org.toop.app.gameControllers;
|
|||||||
import org.toop.app.canvas.TicTacToeBitCanvas;
|
import org.toop.app.canvas.TicTacToeBitCanvas;
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.ThreadBehaviour;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.gameThreads.LocalFixedRateThreadBehaviour;
|
import org.toop.framework.game.gameThreads.LocalThreadBehaviour;
|
||||||
import org.toop.game.gameThreads.LocalThreadBehaviour;
|
import org.toop.framework.game.gameThreads.OnlineThreadBehaviour;
|
||||||
import org.toop.game.gameThreads.OnlineThreadBehaviour;
|
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.framework.game.players.OnlinePlayer;
|
||||||
import org.toop.game.players.OnlinePlayer;
|
|
||||||
|
|
||||||
public class TicTacToeBitController extends GenericGameController<BitboardTicTacToe> {
|
public class TicTacToeBitController extends GenericGameController {
|
||||||
public TicTacToeBitController(Player<BitboardTicTacToe>[] players) {
|
public TicTacToeBitController(Player[] players) {
|
||||||
BitboardTicTacToe game = new BitboardTicTacToe(players);
|
BitboardTicTacToe game = new BitboardTicTacToe();
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour<>(game);
|
game.init(players);
|
||||||
for (Player<BitboardTicTacToe> player : players) {
|
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
||||||
if (player instanceof OnlinePlayer<BitboardTicTacToe>){
|
for (Player player : players) {
|
||||||
thread = new OnlineThreadBehaviour<>(game);
|
if (player instanceof OnlinePlayer){
|
||||||
|
thread = new OnlineThreadBehaviour(game);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super(new TicTacToeBitCanvas(), game, thread , "TicTacToe");
|
super(new TicTacToeBitCanvas(), game, thread , "TicTacToe");
|
||||||
|
|||||||
@@ -2,24 +2,21 @@ package org.toop.app.widget.view;
|
|||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import org.toop.app.GameInformation;
|
import org.toop.app.GameInformation;
|
||||||
import org.toop.app.canvas.ReversiBitCanvas;
|
|
||||||
import org.toop.app.canvas.TicTacToeBitCanvas;
|
|
||||||
import org.toop.app.gameControllers.GenericGameController;
|
|
||||||
import org.toop.app.gameControllers.ReversiBitController;
|
import org.toop.app.gameControllers.ReversiBitController;
|
||||||
import org.toop.app.gameControllers.TicTacToeBitController;
|
import org.toop.app.gameControllers.TicTacToeBitController;
|
||||||
import org.toop.framework.gameFramework.controller.GameController;
|
import org.toop.framework.gameFramework.controller.GameController;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.games.reversi.BitboardReversi;
|
import org.toop.framework.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.game.players.ArtificialPlayer;
|
import org.toop.framework.game.players.ArtificialPlayer;
|
||||||
import org.toop.game.players.LocalPlayer;
|
import org.toop.framework.game.players.LocalPlayer;
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
import org.toop.app.widget.complex.PlayerInfoWidget;
|
import org.toop.app.widget.complex.PlayerInfoWidget;
|
||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
import org.toop.app.widget.popup.ErrorPopup;
|
import org.toop.app.widget.popup.ErrorPopup;
|
||||||
import org.toop.app.widget.tutorial.*;
|
import org.toop.app.widget.tutorial.*;
|
||||||
import org.toop.game.players.MiniMaxAI;
|
import org.toop.framework.game.players.MiniMaxAI;
|
||||||
import org.toop.game.players.RandomAI;
|
import org.toop.framework.game.players.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
@@ -27,9 +24,6 @@ import javafx.scene.control.ScrollPane;
|
|||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
import org.toop.local.AppSettings;
|
import org.toop.local.AppSettings;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public class LocalMultiplayerView extends ViewWidget {
|
public class LocalMultiplayerView extends ViewWidget {
|
||||||
private final GameInformation information;
|
private final GameInformation information;
|
||||||
|
|
||||||
@@ -58,14 +52,14 @@ public class LocalMultiplayerView extends ViewWidget {
|
|||||||
switch (information.type) {
|
switch (information.type) {
|
||||||
case TICTACTOE:
|
case TICTACTOE:
|
||||||
if (information.players[0].isHuman) {
|
if (information.players[0].isHuman) {
|
||||||
players[0] = new LocalPlayer<>(information.players[0].name);
|
players[0] = new LocalPlayer(information.players[0].name);
|
||||||
} else {
|
} else {
|
||||||
players[0] = new ArtificialPlayer<>(new RandomAI<BitboardTicTacToe>(), "Random AI");
|
players[0] = new ArtificialPlayer(new RandomAI(), "Random AI");
|
||||||
}
|
}
|
||||||
if (information.players[1].isHuman) {
|
if (information.players[1].isHuman) {
|
||||||
players[1] = new LocalPlayer<>(information.players[1].name);
|
players[1] = new LocalPlayer(information.players[1].name);
|
||||||
} else {
|
} else {
|
||||||
players[1] = new ArtificialPlayer<>(new MiniMaxAI<BitboardTicTacToe>(9), "MiniMax AI");
|
players[1] = new ArtificialPlayer(new MiniMaxAI(9), "MiniMax AI");
|
||||||
}
|
}
|
||||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
||||||
new ShowEnableTutorialWidget(
|
new ShowEnableTutorialWidget(
|
||||||
@@ -86,14 +80,14 @@ public class LocalMultiplayerView extends ViewWidget {
|
|||||||
break;
|
break;
|
||||||
case REVERSI:
|
case REVERSI:
|
||||||
if (information.players[0].isHuman) {
|
if (information.players[0].isHuman) {
|
||||||
players[0] = new LocalPlayer<>(information.players[0].name);
|
players[0] = new LocalPlayer(information.players[0].name);
|
||||||
} else {
|
} else {
|
||||||
players[0] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(), "Random AI");
|
players[0] = new ArtificialPlayer(new RandomAI(), "Random AI");
|
||||||
}
|
}
|
||||||
if (information.players[1].isHuman) {
|
if (information.players[1].isHuman) {
|
||||||
players[1] = new LocalPlayer<>(information.players[1].name);
|
players[1] = new LocalPlayer(information.players[1].name);
|
||||||
} else {
|
} else {
|
||||||
players[1] = new ArtificialPlayer<>(new MiniMaxAI<BitboardReversi>(6), "MiniMax");
|
players[1] = new ArtificialPlayer(new MiniMaxAI(6), "MiniMax");
|
||||||
}
|
}
|
||||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
|
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
|
||||||
new ShowEnableTutorialWidget(
|
new ShowEnableTutorialWidget(
|
||||||
|
|||||||
@@ -147,7 +147,6 @@
|
|||||||
<version>2.42.0</version>
|
<version>2.42.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -1,39 +1,44 @@
|
|||||||
package org.toop.game;
|
package org.toop.framework.game;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
|
||||||
|
|
||||||
// There is AI performance to be gained by getting rid of non-primitives and thus speeding up deepCopy
|
// There is AI performance to be gained by getting rid of non-primitives and thus speeding up deepCopy
|
||||||
public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBasedGame<T> {
|
public abstract class BitboardGame implements TurnBasedGame {
|
||||||
private final int columnSize;
|
private final int columnSize;
|
||||||
private final int rowSize;
|
private final int rowSize;
|
||||||
|
|
||||||
private Player<T>[] players;
|
private Player[] players;
|
||||||
|
|
||||||
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
// long is 64 bits. Every game has a limit of 64 cells maximum.
|
||||||
private final long[] playerBitboard;
|
private long[] playerBitboard;
|
||||||
private int currentTurn = 0;
|
private int currentTurn = 0;
|
||||||
|
private int playerCount;
|
||||||
|
|
||||||
public BitboardGame(int columnSize, int rowSize, int playerCount, Player<T>[] players) {
|
public BitboardGame(int columnSize, int rowSize, int playerCount) {
|
||||||
this.columnSize = columnSize;
|
this.columnSize = columnSize;
|
||||||
this.rowSize = rowSize;
|
this.rowSize = rowSize;
|
||||||
this.players = players;
|
this.playerCount = playerCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
this.players = players;
|
||||||
this.playerBitboard = new long[playerCount];
|
this.playerBitboard = new long[playerCount];
|
||||||
|
|
||||||
Arrays.fill(playerBitboard, 0L);
|
Arrays.fill(playerBitboard, 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitboardGame(BitboardGame<T> other) {
|
public BitboardGame(BitboardGame other) {
|
||||||
this.columnSize = other.columnSize;
|
this.columnSize = other.columnSize;
|
||||||
this.rowSize = other.rowSize;
|
this.rowSize = other.rowSize;
|
||||||
|
|
||||||
this.playerBitboard = other.playerBitboard.clone();
|
this.playerBitboard = other.playerBitboard.clone();
|
||||||
this.currentTurn = other.currentTurn;
|
this.currentTurn = other.currentTurn;
|
||||||
this.players = Arrays.stream(other.players)
|
this.players = Arrays.stream(other.players)
|
||||||
.map(Player<T>::deepCopy)
|
.map(Player::deepCopy)
|
||||||
.toArray(Player[]::new);
|
.toArray(Player[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +66,7 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
|
|||||||
return getCurrentPlayerIndex();
|
return getCurrentPlayerIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player<T> getPlayer(int index) {return players[index];}
|
public Player getPlayer(int index) {return players[index];}
|
||||||
|
|
||||||
public int getCurrentPlayerIndex() {
|
public int getCurrentPlayerIndex() {
|
||||||
return currentTurn % playerBitboard.length;
|
return currentTurn % playerBitboard.length;
|
||||||
@@ -71,7 +76,7 @@ public abstract class BitboardGame<T extends BitboardGame<T>> implements TurnBas
|
|||||||
return (currentTurn + 1) % playerBitboard.length;
|
return (currentTurn + 1) % playerBitboard.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player<T> getCurrentPlayer(){
|
public Player getCurrentPlayer(){
|
||||||
return players[getCurrentPlayerIndex()];
|
return players[getCurrentPlayerIndex()];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package org.toop.game;
|
package org.toop.framework.game;
|
||||||
// TODO: Remove this, only used in ReversiCanvas. Needs to not
|
// TODO: Remove this, only used in ReversiCanvas. Needs to not
|
||||||
public record Move(int position, char value) {}
|
public record Move(int position, char value) {}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.gameThreads;
|
package org.toop.framework.game.gameThreads;
|
||||||
|
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
@@ -16,7 +16,7 @@ import java.util.function.Consumer;
|
|||||||
* Runs a separate thread that executes game turns at a fixed frequency (default 60 updates/sec),
|
* Runs a separate thread that executes game turns at a fixed frequency (default 60 updates/sec),
|
||||||
* applying player moves, updating the game state, and dispatching UI events.
|
* applying player moves, updating the game state, and dispatching UI events.
|
||||||
*/
|
*/
|
||||||
public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
|
public class LocalFixedRateThreadBehaviour extends AbstractThreadBehaviour implements Runnable {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -24,7 +24,7 @@ public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends A
|
|||||||
*
|
*
|
||||||
* @param game the game instance
|
* @param game the game instance
|
||||||
*/
|
*/
|
||||||
public LocalFixedRateThreadBehaviour(T game) {
|
public LocalFixedRateThreadBehaviour(TurnBasedGame game) {
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ public class LocalFixedRateThreadBehaviour<T extends TurnBasedGame<T>> extends A
|
|||||||
if (now >= nextUpdate) {
|
if (now >= nextUpdate) {
|
||||||
nextUpdate += UPDATE_INTERVAL;
|
nextUpdate += UPDATE_INTERVAL;
|
||||||
|
|
||||||
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
|
Player currentPlayer = game.getPlayer(game.getCurrentTurn());
|
||||||
long move = currentPlayer.getMove(game.deepCopy());
|
long move = currentPlayer.getMove(game.deepCopy());
|
||||||
PlayResult result = game.play(move);
|
PlayResult result = game.play(move);
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.gameThreads;
|
package org.toop.framework.game.gameThreads;
|
||||||
|
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
||||||
@@ -16,14 +16,14 @@ import java.util.function.Consumer;
|
|||||||
* Repeatedly gets the current player's move, applies it to the game,
|
* Repeatedly gets the current player's move, applies it to the game,
|
||||||
* updates the UI, and stops when the game ends or {@link #stop()} is called.
|
* updates the UI, and stops when the game ends or {@link #stop()} is called.
|
||||||
*/
|
*/
|
||||||
public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements Runnable {
|
public class LocalThreadBehaviour extends AbstractThreadBehaviour implements Runnable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new behaviour for a local turn-based game.
|
* Creates a new behaviour for a local turn-based game.
|
||||||
*
|
*
|
||||||
* @param game the game instance
|
* @param game the game instance
|
||||||
*/
|
*/
|
||||||
public LocalThreadBehaviour(T game) {
|
public LocalThreadBehaviour(TurnBasedGame game) {
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ public class LocalThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractTh
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (isRunning.get()) {
|
while (isRunning.get()) {
|
||||||
Player<T> currentPlayer = game.getPlayer(game.getCurrentTurn());
|
Player currentPlayer = game.getPlayer(game.getCurrentTurn());
|
||||||
long move = currentPlayer.getMove(game.deepCopy());
|
long move = currentPlayer.getMove(game.deepCopy());
|
||||||
PlayResult result = game.play(move);
|
PlayResult result = game.play(move);
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.gameThreads;
|
package org.toop.framework.game.gameThreads;
|
||||||
|
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
||||||
@@ -6,7 +6,7 @@ import org.toop.framework.gameFramework.view.GUIEvents;
|
|||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
|
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.players.OnlinePlayer;
|
import org.toop.framework.game.players.OnlinePlayer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles online multiplayer game logic.
|
* Handles online multiplayer game logic.
|
||||||
@@ -14,17 +14,17 @@ import org.toop.game.players.OnlinePlayer;
|
|||||||
* Reacts to server events, sending moves and updating the game state
|
* Reacts to server events, sending moves and updating the game state
|
||||||
* for the local player while receiving moves from other players.
|
* for the local player while receiving moves from other players.
|
||||||
*/
|
*/
|
||||||
public class OnlineThreadBehaviour<T extends TurnBasedGame<T>> extends AbstractThreadBehaviour<T> implements SupportsOnlinePlay {
|
public class OnlineThreadBehaviour extends AbstractThreadBehaviour implements SupportsOnlinePlay {
|
||||||
/**
|
/**
|
||||||
* Creates behaviour and sets the first local player
|
* Creates behaviour and sets the first local player
|
||||||
* (non-online player) from the given array.
|
* (non-online player) from the given array.
|
||||||
*/
|
*/
|
||||||
public OnlineThreadBehaviour(T game) {
|
public OnlineThreadBehaviour(TurnBasedGame game) {
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Finds the first non-online player in the array. */
|
/** Finds the first non-online player in the array. */
|
||||||
private int getFirstNotOnlinePlayer(Player<T>[] players) {
|
private int getFirstNotOnlinePlayer(Player[] players) {
|
||||||
for (int i = 0; i < players.length; i++) {
|
for (int i = 0; i < players.length; i++) {
|
||||||
if (!(players[i] instanceof OnlinePlayer)) {
|
if (!(players[i] instanceof OnlinePlayer)) {
|
||||||
return i;
|
return i;
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.toop.game.gameThreads;
|
package org.toop.framework.game.gameThreads;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Online thread behaviour that adds a fixed delay before processing
|
* Online thread behaviour that adds a fixed delay before processing
|
||||||
@@ -10,14 +9,14 @@ import org.toop.framework.networking.events.NetworkEvents;
|
|||||||
* This is identical to {@link OnlineThreadBehaviour}, but inserts a
|
* This is identical to {@link OnlineThreadBehaviour}, but inserts a
|
||||||
* short sleep before delegating to the base implementation.
|
* short sleep before delegating to the base implementation.
|
||||||
*/
|
*/
|
||||||
public class OnlineWithSleepThreadBehaviour<T extends TurnBasedGame<T>> extends OnlineThreadBehaviour<T> {
|
public class OnlineWithSleepThreadBehaviour extends OnlineThreadBehaviour {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the behaviour and forwards the players to the base class.
|
* Creates the behaviour and forwards the players to the base class.
|
||||||
*
|
*
|
||||||
* @param game the online-capable turn-based game
|
* @param game the online-capable turn-based game
|
||||||
*/
|
*/
|
||||||
public OnlineWithSleepThreadBehaviour(T game) {
|
public OnlineWithSleepThreadBehaviour(TurnBasedGame game) {
|
||||||
super(game);
|
super(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,19 +1,20 @@
|
|||||||
package org.toop.game.games.reversi;
|
package org.toop.framework.game.games.reversi;
|
||||||
|
|
||||||
|
import org.toop.framework.game.BitboardGame;
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.BitboardGame;
|
import org.toop.framework.game.BitboardGame;
|
||||||
|
|
||||||
public class BitboardReversi extends BitboardGame<BitboardReversi> {
|
public class BitboardReversi extends BitboardGame {
|
||||||
|
|
||||||
public record Score(int black, int white) {}
|
public record Score(int black, int white) {}
|
||||||
|
|
||||||
private final long notAFile = 0xfefefefefefefefeL;
|
private final long notAFile = 0xfefefefefefefefeL;
|
||||||
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
private final long notHFile = 0x7f7f7f7f7f7f7f7fL;
|
||||||
|
|
||||||
public BitboardReversi(Player<BitboardReversi>[] players) {
|
public BitboardReversi() {
|
||||||
super(8, 8, 2, players);
|
super(8, 8, 2);
|
||||||
|
|
||||||
// Black (player 0)
|
// Black (player 0)
|
||||||
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
|
setPlayerBitboard(0, (1L << (3 + 4 * 8)) | (1L << (4 + 3 * 8)));
|
||||||
@@ -22,6 +23,11 @@ public class BitboardReversi extends BitboardGame<BitboardReversi> {
|
|||||||
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8)));
|
setPlayerBitboard(1, (1L << (3 + 3 * 8)) | (1L << (4 + 4 * 8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
super.init(players);
|
||||||
|
}
|
||||||
|
|
||||||
public BitboardReversi(BitboardReversi other) {
|
public BitboardReversi(BitboardReversi other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package org.toop.game.games.tictactoe;
|
package org.toop.framework.game.games.tictactoe;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.game.BitboardGame;
|
import org.toop.framework.game.BitboardGame;
|
||||||
|
|
||||||
public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
|
public class BitboardTicTacToe extends BitboardGame {
|
||||||
private final long[] winningLines = {
|
private final long[] winningLines = {
|
||||||
0b111000000L, // top row
|
0b111000000L, // top row
|
||||||
0b000111000L, // middle row
|
0b000111000L, // middle row
|
||||||
@@ -17,9 +17,15 @@ public class BitboardTicTacToe extends BitboardGame<BitboardTicTacToe> {
|
|||||||
0b001010100L // anti-diagonal
|
0b001010100L // anti-diagonal
|
||||||
};
|
};
|
||||||
|
|
||||||
public BitboardTicTacToe(Player<BitboardTicTacToe>[] players) {
|
public BitboardTicTacToe() {
|
||||||
super(3, 3, 2, players);
|
super(3, 3, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
super.init(players);
|
||||||
|
}
|
||||||
|
|
||||||
public BitboardTicTacToe(BitboardTicTacToe other) {
|
public BitboardTicTacToe(BitboardTicTacToe other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.framework.game.players;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.player.*;
|
import org.toop.framework.gameFramework.model.player.*;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
@@ -12,22 +12,22 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|||||||
*
|
*
|
||||||
* @param <T> the specific type of game this AI player can play
|
* @param <T> the specific type of game this AI player can play
|
||||||
*/
|
*/
|
||||||
public class ArtificialPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class ArtificialPlayer extends AbstractPlayer {
|
||||||
|
|
||||||
/** The AI instance used to calculate moves. */
|
/** The AI instance used to calculate moves. */
|
||||||
private final AI<T> ai;
|
private final AI ai;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new ArtificialPlayer using the specified AI.
|
* Constructs a new ArtificialPlayer using the specified AI.
|
||||||
*
|
*
|
||||||
* @param ai the AI instance that determines moves for this player
|
* @param ai the AI instance that determines moves for this player
|
||||||
*/
|
*/
|
||||||
public ArtificialPlayer(AI<T> ai, String name) {
|
public ArtificialPlayer(AI ai, String name) {
|
||||||
super(name);
|
super(name);
|
||||||
this.ai = ai;
|
this.ai = ai;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArtificialPlayer(ArtificialPlayer<T> other) {
|
public ArtificialPlayer(ArtificialPlayer other) {
|
||||||
super(other);
|
super(other);
|
||||||
this.ai = other.ai.deepCopy();
|
this.ai = other.ai.deepCopy();
|
||||||
}
|
}
|
||||||
@@ -44,12 +44,12 @@ public class ArtificialPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer
|
|||||||
* @return the integer representing the chosen move
|
* @return the integer representing the chosen move
|
||||||
* @throws ClassCastException if {@code gameCopy} is not of type {@code T}
|
* @throws ClassCastException if {@code gameCopy} is not of type {@code T}
|
||||||
*/
|
*/
|
||||||
public long getMove(T gameCopy) {
|
public long getMove(TurnBasedGame gameCopy) {
|
||||||
return ai.getMove(gameCopy);
|
return ai.getMove(gameCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ArtificialPlayer<T> deepCopy() {
|
public ArtificialPlayer deepCopy() {
|
||||||
return new ArtificialPlayer<T>(this);
|
return new ArtificialPlayer(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.framework.game.players;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class LocalPlayer extends AbstractPlayer {
|
||||||
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
||||||
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
||||||
|
|
||||||
@@ -17,12 +16,12 @@ public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
|||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocalPlayer(LocalPlayer<T> other) {
|
public LocalPlayer(LocalPlayer other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getMove(T gameCopy) {
|
public long getMove(TurnBasedGame gameCopy) {
|
||||||
return getValidMove(gameCopy);
|
return getValidMove(gameCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +35,7 @@ public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getMove2(T gameCopy) {
|
private long getMove2(TurnBasedGame gameCopy) {
|
||||||
LastMove = new CompletableFuture<>();
|
LastMove = new CompletableFuture<>();
|
||||||
long move = 0;
|
long move = 0;
|
||||||
try {
|
try {
|
||||||
@@ -49,7 +48,7 @@ public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
|||||||
return move;
|
return move;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected long getValidMove(T gameCopy){
|
protected long getValidMove(TurnBasedGame gameCopy){
|
||||||
// Get this player's valid moves
|
// Get this player's valid moves
|
||||||
long validMoves = gameCopy.getLegalMoves();
|
long validMoves = gameCopy.getLegalMoves();
|
||||||
// Make sure provided move is valid
|
// Make sure provided move is valid
|
||||||
@@ -64,8 +63,8 @@ public class LocalPlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LocalPlayer<T> deepCopy() {
|
public LocalPlayer deepCopy() {
|
||||||
return new LocalPlayer<T>(this.getName());
|
return new LocalPlayer(this.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public void register() {
|
/*public void register() {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.framework.game.players;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
import org.toop.framework.gameFramework.GameState;
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
@@ -9,7 +9,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
public class MiniMaxAI extends AbstractAI {
|
||||||
|
|
||||||
private final int maxDepth;
|
private final int maxDepth;
|
||||||
private final Random random = new Random();
|
private final Random random = new Random();
|
||||||
@@ -18,17 +18,17 @@ public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
|||||||
this.maxDepth = depth;
|
this.maxDepth = depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MiniMaxAI(MiniMaxAI<T> other) {
|
public MiniMaxAI(MiniMaxAI other) {
|
||||||
this.maxDepth = other.maxDepth;
|
this.maxDepth = other.maxDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MiniMaxAI<T> deepCopy() {
|
public MiniMaxAI deepCopy() {
|
||||||
return new MiniMaxAI<>(this);
|
return new MiniMaxAI(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getMove(T game) {
|
public long getMove(TurnBasedGame game) {
|
||||||
long legalMoves = game.getLegalMoves();
|
long legalMoves = game.getLegalMoves();
|
||||||
if (legalMoves == 0) return 0;
|
if (legalMoves == 0) return 0;
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
|||||||
long movesLoop = legalMoves;
|
long movesLoop = legalMoves;
|
||||||
while (movesLoop != 0) {
|
while (movesLoop != 0) {
|
||||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||||
T copy = game.deepCopy();
|
TurnBasedGame copy = game.deepCopy();
|
||||||
PlayResult result = copy.play(move);
|
PlayResult result = copy.play(move);
|
||||||
|
|
||||||
int score;
|
int score;
|
||||||
@@ -75,7 +75,7 @@ public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
|||||||
* @param beta Beta value
|
* @param beta Beta value
|
||||||
* @return score of the position
|
* @return score of the position
|
||||||
*/
|
*/
|
||||||
private int getMoveScore(T game, int depth, boolean maximizing, int aiPlayer, int alpha, int beta) {
|
private int getMoveScore(TurnBasedGame game, int depth, boolean maximizing, int aiPlayer, int alpha, int beta) {
|
||||||
long legalMoves = game.getLegalMoves();
|
long legalMoves = game.getLegalMoves();
|
||||||
|
|
||||||
// Terminal state
|
// Terminal state
|
||||||
@@ -95,7 +95,7 @@ public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
|||||||
|
|
||||||
while (movesLoop != 0) {
|
while (movesLoop != 0) {
|
||||||
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||||
T copy = game.deepCopy();
|
TurnBasedGame copy = game.deepCopy();
|
||||||
PlayResult result = copy.play(move);
|
PlayResult result = copy.play(move);
|
||||||
|
|
||||||
int score;
|
int score;
|
||||||
@@ -130,7 +130,7 @@ public class MiniMaxAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
|||||||
* @param aiPlayer AI's player index
|
* @param aiPlayer AI's player index
|
||||||
* @return heuristic score
|
* @return heuristic score
|
||||||
*/
|
*/
|
||||||
private int evaluateBoard(T game, int aiPlayer) {
|
private int evaluateBoard(TurnBasedGame game, int aiPlayer) {
|
||||||
long[] board = game.getBoard();
|
long[] board = game.getBoard();
|
||||||
int aiCount = 0;
|
int aiCount = 0;
|
||||||
int opponentCount = 0;
|
int opponentCount = 0;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.framework.game.players;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
||||||
@@ -12,7 +12,7 @@ import org.toop.framework.gameFramework.model.player.Player;
|
|||||||
* Currently, this class is a placeholder and does not implement move logic.
|
* Currently, this class is a placeholder and does not implement move logic.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T> {
|
public class OnlinePlayer extends AbstractPlayer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new OnlinePlayer.
|
* Constructs a new OnlinePlayer.
|
||||||
@@ -25,12 +25,12 @@ public class OnlinePlayer<T extends TurnBasedGame<T>> extends AbstractPlayer<T>
|
|||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OnlinePlayer(OnlinePlayer<T> other) {
|
public OnlinePlayer(OnlinePlayer other) {
|
||||||
super(other);
|
super(other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Player<T> deepCopy() {
|
public Player deepCopy() {
|
||||||
return new OnlinePlayer<>(this);
|
return new OnlinePlayer(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.game.players;
|
package org.toop.framework.game.players;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
import org.toop.framework.gameFramework.model.player.AbstractAI;
|
||||||
@@ -6,19 +6,19 @@ import org.toop.framework.gameFramework.model.player.AbstractAI;
|
|||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
|
||||||
public class RandomAI<T extends TurnBasedGame<T>> extends AbstractAI<T> {
|
public class RandomAI extends AbstractAI {
|
||||||
|
|
||||||
public RandomAI() {
|
public RandomAI() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RandomAI<T> deepCopy() {
|
public RandomAI deepCopy() {
|
||||||
return new RandomAI<T>();
|
return new RandomAI();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getMove(T game) {
|
public long getMove(TurnBasedGame game) {
|
||||||
long legalMoves = game.getLegalMoves();
|
long legalMoves = game.getLegalMoves();
|
||||||
int move = new Random().nextInt(Long.bitCount(legalMoves));
|
int move = new Random().nextInt(Long.bitCount(legalMoves));
|
||||||
return nthBitIndex(legalMoves, move);
|
return nthBitIndex(legalMoves, move);
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package org.toop.framework.gameFramework.controller;
|
package org.toop.framework.gameFramework.controller;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.SupportsOnlinePlay;
|
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
|
import org.toop.framework.gameFramework.model.game.threadBehaviour.Controllable;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
|
|
||||||
public interface GameController extends Controllable, UpdatesGameUI {
|
public interface GameController extends Controllable, UpdatesGameUI {
|
||||||
/** Called when it is this player's turn to make a move. */
|
/** Called when it is this player's turn to make a move. */
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package org.toop.framework.gameFramework.model.game;
|
|||||||
|
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
public interface PlayerProvider<T extends TurnBasedGame<T>> {
|
public interface PlayerProvider {
|
||||||
Player<T> getPlayer(int index);
|
Player getPlayer(int index);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.toop.framework.gameFramework.model.game;
|
package org.toop.framework.gameFramework.model.game;
|
||||||
|
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for games that support online multiplayer play.
|
* Interface for games that support online multiplayer play.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.toop.framework.gameFramework.model.game;
|
package org.toop.framework.gameFramework.model.game;
|
||||||
|
|
||||||
public interface TurnBasedGame<T extends TurnBasedGame<T>> extends Playable, DeepCopyable<T>, PlayerProvider<T>, BoardProvider {
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
|
public interface TurnBasedGame extends Playable, PlayerProvider, BoardProvider, DeepCopyable<TurnBasedGame> {
|
||||||
|
void init(Player[] players);
|
||||||
int getCurrentTurn();
|
int getCurrentTurn();
|
||||||
int getPlayerCount();
|
int getPlayerCount();
|
||||||
int getWinner();
|
int getWinner();
|
||||||
|
|||||||
@@ -16,14 +16,14 @@ import java.util.function.Consumer;
|
|||||||
* a running flag, a game reference, and a logger.
|
* a running flag, a game reference, and a logger.
|
||||||
* Subclasses implement the actual game-loop logic.
|
* Subclasses implement the actual game-loop logic.
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implements ThreadBehaviour {
|
public abstract class AbstractThreadBehaviour implements ThreadBehaviour {
|
||||||
private LongPairConsumer onSendMove;
|
private LongPairConsumer onSendMove;
|
||||||
private Runnable onUpdateUI;
|
private Runnable onUpdateUI;
|
||||||
/** Indicates whether the game loop or event processing is active. */
|
/** Indicates whether the game loop or event processing is active. */
|
||||||
protected final AtomicBoolean isRunning = new AtomicBoolean();
|
protected final AtomicBoolean isRunning = new AtomicBoolean();
|
||||||
|
|
||||||
/** The game instance controlled by this behaviour. */
|
/** The game instance controlled by this behaviour. */
|
||||||
protected final T game;
|
protected final TurnBasedGame game;
|
||||||
|
|
||||||
/** Logger for the subclass to report errors or debug info. */
|
/** Logger for the subclass to report errors or debug info. */
|
||||||
protected final Logger logger = LogManager.getLogger(this.getClass());
|
protected final Logger logger = LogManager.getLogger(this.getClass());
|
||||||
@@ -33,7 +33,7 @@ public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> implem
|
|||||||
*
|
*
|
||||||
* @param game the turn-based game to control
|
* @param game the turn-based game to control
|
||||||
*/
|
*/
|
||||||
public AbstractThreadBehaviour(T game) {
|
public AbstractThreadBehaviour(TurnBasedGame game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ package org.toop.framework.gameFramework.model.player;
|
|||||||
import org.toop.framework.gameFramework.model.game.DeepCopyable;
|
import org.toop.framework.gameFramework.model.game.DeepCopyable;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
public interface AI<T extends TurnBasedGame<T>> extends MoveProvider<T>, DeepCopyable<AI<T>> {
|
public interface AI extends MoveProvider, DeepCopyable<AI> {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,6 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|||||||
*
|
*
|
||||||
* @param <T> the specific type of game this AI can play, extending {@link GameR}
|
* @param <T> the specific type of game this AI can play, extending {@link GameR}
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractAI<T extends TurnBasedGame<T>> implements AI<T> {
|
public abstract class AbstractAI implements AI {
|
||||||
// Concrete AI implementations should override findBestMove(T game, int depth)
|
// Concrete AI implementations should override findBestMove(T game, int depth)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|||||||
* specific move logic.
|
* specific move logic.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Player<T> {
|
public abstract class AbstractPlayer implements Player {
|
||||||
private final Logger logger = LogManager.getLogger(this.getClass());
|
private final Logger logger = LogManager.getLogger(this.getClass());
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
@@ -24,7 +24,7 @@ public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Play
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AbstractPlayer(AbstractPlayer<T> other) {
|
protected AbstractPlayer(AbstractPlayer other) {
|
||||||
this.name = other.name;
|
this.name = other.name;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -39,7 +39,7 @@ public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Play
|
|||||||
* @return an integer representing the chosen move
|
* @return an integer representing the chosen move
|
||||||
* @throws UnsupportedOperationException if the method is not overridden
|
* @throws UnsupportedOperationException if the method is not overridden
|
||||||
*/
|
*/
|
||||||
public long getMove(T gameCopy) {
|
public long getMove(TurnBasedGame gameCopy) {
|
||||||
logger.error("Method getMove not implemented.");
|
logger.error("Method getMove not implemented.");
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package org.toop.framework.gameFramework.model.player;
|
|||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
public interface MoveProvider<T extends TurnBasedGame<T>> {
|
public interface MoveProvider {
|
||||||
long getMove(T game);
|
long getMove(TurnBasedGame game);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
package org.toop.framework.gameFramework.model.player;
|
package org.toop.framework.gameFramework.model.player;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.DeepCopyable;
|
import org.toop.framework.gameFramework.model.game.DeepCopyable;
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
|
|
||||||
public interface Player<T extends TurnBasedGame<T>> extends NameProvider, MoveProvider<T>, DeepCopyable<Player<T>> {
|
public interface Player extends NameProvider, MoveProvider, DeepCopyable<Player> {}
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package org.toop.framework.networking;
|
package org.toop.framework.networking.connection;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.SnowflakeGenerator;
|
import org.toop.framework.SnowflakeGenerator;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClientManager;
|
import org.toop.framework.networking.connection.interfaces.NetworkingClientManager;
|
||||||
|
|
||||||
public class NetworkingClientEventListener {
|
public class NetworkingClientEventListener {
|
||||||
private static final Logger logger = LogManager.getLogger(NetworkingClientEventListener.class);
|
private static final Logger logger = LogManager.getLogger(NetworkingClientEventListener.class);
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking;
|
package org.toop.framework.networking.connection;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -10,13 +10,13 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClient;
|
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
||||||
|
|
||||||
public class NetworkingClientManager implements org.toop.framework.networking.interfaces.NetworkingClientManager {
|
public class NetworkingClientManager implements org.toop.framework.networking.connection.interfaces.NetworkingClientManager {
|
||||||
private static final Logger logger = LogManager.getLogger(NetworkingClientManager.class);
|
private static final Logger logger = LogManager.getLogger(NetworkingClientManager.class);
|
||||||
|
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.clients;
|
package org.toop.framework.networking.connection.clients;
|
||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.*;
|
||||||
@@ -12,9 +12,9 @@ import io.netty.util.CharsetUtil;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.handlers.NetworkingGameClientHandler;
|
import org.toop.framework.networking.connection.handlers.NetworkingGameClientHandler;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClient;
|
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.events;
|
package org.toop.framework.networking.connection.events;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -6,8 +6,8 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
import org.toop.annotations.AutoResponseResult;
|
import org.toop.annotations.AutoResponseResult;
|
||||||
import org.toop.framework.eventbus.GlobalEventBus;
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.eventbus.events.*;
|
import org.toop.framework.eventbus.events.*;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClient;
|
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines all event types related to the networking subsystem.
|
* Defines all event types related to the networking subsystem.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.exceptions;
|
package org.toop.framework.networking.connection.exceptions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thrown when an operation is attempted on a networking client
|
* Thrown when an operation is attempted on a networking client
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.exceptions;
|
package org.toop.framework.networking.connection.exceptions;
|
||||||
|
|
||||||
public class CouldNotConnectException extends RuntimeException {
|
public class CouldNotConnectException extends RuntimeException {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.exceptions;
|
package org.toop.framework.networking.connection.exceptions;
|
||||||
|
|
||||||
public class NetworkingInitializationException extends RuntimeException {
|
public class NetworkingInitializationException extends RuntimeException {
|
||||||
public NetworkingInitializationException(String message, Throwable cause) {
|
public NetworkingInitializationException(String message, Throwable cause) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.handlers;
|
package org.toop.framework.networking.connection.handlers;
|
||||||
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
@@ -9,7 +9,7 @@ import java.util.regex.Pattern;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
|
|
||||||
public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
||||||
private static final Logger logger = LogManager.getLogger(NetworkingGameClientHandler.class);
|
private static final Logger logger = LogManager.getLogger(NetworkingGameClientHandler.class);
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package org.toop.framework.networking.interfaces;
|
package org.toop.framework.networking.connection.interfaces;
|
||||||
|
|
||||||
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package org.toop.framework.networking.interfaces;
|
package org.toop.framework.networking.connection.interfaces;
|
||||||
|
|
||||||
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
||||||
|
|
||||||
public interface NetworkingClientManager {
|
public interface NetworkingClientManager {
|
||||||
void startClient(
|
void startClient(
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.types;
|
package org.toop.framework.networking.connection.types;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package org.toop.framework.networking.types;
|
package org.toop.framework.networking.connection.types;
|
||||||
|
|
||||||
public record ServerCommand(long clientId, String command) {}
|
public record ServerCommand(long clientId, String command) {}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package org.toop.framework.networking.connection.types;
|
||||||
|
|
||||||
|
public record ServerMessage(String message) {}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
|
public class Game implements OnlineGame<TurnBasedGame> {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private User[] users;
|
||||||
|
private TurnBasedGame game;
|
||||||
|
|
||||||
|
public Game(TurnBasedGame game, User... users) {
|
||||||
|
this.game = game;
|
||||||
|
this.users = users;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long id() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TurnBasedGame game() {
|
||||||
|
return game;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User[] users() {
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
public class GameDefinition<T> {
|
||||||
|
private final String name;
|
||||||
|
private final Class<T> game;
|
||||||
|
|
||||||
|
public GameDefinition(String name, Class<T> game) {
|
||||||
|
this.name = name;
|
||||||
|
this.game = game;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T create(String... users) {
|
||||||
|
try {
|
||||||
|
return game.getDeclaredConstructor().newInstance(users);
|
||||||
|
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public interface GameServer {
|
||||||
|
// List<?> gameTypes();
|
||||||
|
// List<?> ongoingGames();
|
||||||
|
// void startGame(String gameType, User... users);
|
||||||
|
// String[] onlineUsers();
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
|
import io.netty.channel.*;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.nio.NioIoHandler;
|
||||||
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import io.netty.handler.codec.LineBasedFrameDecoder;
|
||||||
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
|
import io.netty.handler.logging.LogLevel;
|
||||||
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
|
import org.toop.framework.SnowflakeGenerator;
|
||||||
|
import org.toop.framework.game.BitboardGame;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class MasterServer {
|
||||||
|
private final int port;
|
||||||
|
public final Server gs;
|
||||||
|
|
||||||
|
public MasterServer(int port, Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
||||||
|
this.port = port;
|
||||||
|
this.gs = new Server(gameTypes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() throws InterruptedException {
|
||||||
|
|
||||||
|
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
||||||
|
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
ServerBootstrap bootstrap = new ServerBootstrap();
|
||||||
|
bootstrap.group(workerGroup);
|
||||||
|
bootstrap.channel(NioServerSocketChannel.class);
|
||||||
|
bootstrap.option(ChannelOption.SO_BACKLOG, 128);
|
||||||
|
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
|
||||||
|
bootstrap.handler(new LoggingHandler(LogLevel.INFO));
|
||||||
|
bootstrap.childHandler(
|
||||||
|
new ChannelInitializer<NioSocketChannel>() {
|
||||||
|
@Override
|
||||||
|
protected void initChannel(NioSocketChannel ch) {
|
||||||
|
|
||||||
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
|
|
||||||
|
pipeline.addLast(new LineBasedFrameDecoder(8192));
|
||||||
|
pipeline.addLast(new StringDecoder());
|
||||||
|
pipeline.addLast(new StringEncoder());
|
||||||
|
|
||||||
|
long userid = SnowflakeGenerator.nextId();
|
||||||
|
User user = new User(userid, ""+userid);
|
||||||
|
pipeline.addLast(new ServerHandler(user, gs));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
ChannelFuture future = bootstrap.bind(port).sync();
|
||||||
|
System.out.println("MasterServer listening on port " + port);
|
||||||
|
|
||||||
|
future.channel().closeFuture().sync();
|
||||||
|
} finally {
|
||||||
|
bossGroup.shutdownGracefully();
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public interface MessageStore {
|
||||||
|
void add(String message);
|
||||||
|
String get();
|
||||||
|
void reset();
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public interface OnlineGame<T> {
|
||||||
|
long id();
|
||||||
|
T game();
|
||||||
|
User[] users();
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public record ParsedMessage(String command, String... args) {}
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public class Parser {
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
public interface ServableGame {
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import org.toop.framework.game.BitboardGame;
|
||||||
|
import org.toop.framework.game.players.LocalPlayer;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class Server implements GameServer {
|
||||||
|
|
||||||
|
final private Map<String, Class<? extends TurnBasedGame>> gameTypes;
|
||||||
|
final private List<OnlineGame<TurnBasedGame>> games = new ArrayList<>();
|
||||||
|
final private Map<Long, ServerUser> users = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public Server(Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
||||||
|
this.gameTypes = gameTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addUser(ServerUser user) {
|
||||||
|
users.putIfAbsent(user.id(), user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeUser(ServerUser user) {
|
||||||
|
users.remove(user.id());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] gameTypes() {
|
||||||
|
return gameTypes.keySet().toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<OnlineGame<TurnBasedGame>> ongoingGames() {
|
||||||
|
return games;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startGame(String gameType, User... users) {
|
||||||
|
if (!gameTypes.containsKey(gameType)) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
Player[] players = new Player[users.length];
|
||||||
|
for (int i = 0; i < users.length; i++) {
|
||||||
|
players[i] = new LocalPlayer(users[i].name());
|
||||||
|
}
|
||||||
|
|
||||||
|
var game = new Game(gameTypes.get(gameType).getDeclaredConstructor().newInstance(), users);
|
||||||
|
game.game().init(players);
|
||||||
|
games.addLast(game);
|
||||||
|
|
||||||
|
} catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] onlineUsers() {
|
||||||
|
return users.values().stream().map(ServerUser::name).toArray(String[]::new);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.SimpleChannelInboundHandler;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||||
|
|
||||||
|
private final User user;
|
||||||
|
private final Server server;
|
||||||
|
|
||||||
|
public ServerHandler(User user, Server server) {
|
||||||
|
this.user = user;
|
||||||
|
this.server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) {
|
||||||
|
ctx.writeAndFlush("WELCOME " + user.id() + "\n");
|
||||||
|
|
||||||
|
user.setCtx(ctx);
|
||||||
|
server.addUser(user); // TODO set correct name on login
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
|
||||||
|
ParsedMessage p = parse(msg);
|
||||||
|
if (p == null) return;
|
||||||
|
|
||||||
|
IO.println(p.command() + " " + Arrays.toString(p.args()));
|
||||||
|
|
||||||
|
switch (p.command()) {
|
||||||
|
case "ping" -> ctx.writeAndFlush("PONG\n");
|
||||||
|
case "login" -> handleLogin(p);
|
||||||
|
case "get" -> handleGet(p);
|
||||||
|
case "subscribe" -> handleSubscribe(p);
|
||||||
|
case "move" -> handleMove(p);
|
||||||
|
case "challenge" -> handleChallenge(p);
|
||||||
|
case "message" -> handleMessage(p);
|
||||||
|
case "help" -> handleHelp(p);
|
||||||
|
default -> ctx.writeAndFlush("ERROR Unknown command\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean allowedArgs(String... args) {
|
||||||
|
if (args.length < 1) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleLogin(ParsedMessage p) {
|
||||||
|
|
||||||
|
if (!allowedArgs(p.args())) return;
|
||||||
|
|
||||||
|
user.setName(p.args()[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleGet(ParsedMessage p) {
|
||||||
|
if (!allowedArgs(p.args())) return;
|
||||||
|
|
||||||
|
switch (p.args()[0]) {
|
||||||
|
case "playerlist" -> user.ctx().writeAndFlush(Arrays.toString(server.onlineUsers()));
|
||||||
|
case "gamelist" -> user.ctx().writeAndFlush(Arrays.toString(server.gameTypes()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleSubscribe(ParsedMessage p) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleHelp(ParsedMessage p) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMessage(ParsedMessage p) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleChallenge(ParsedMessage p) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMove(ParsedMessage p) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
private ParsedMessage parse(String msg) {
|
||||||
|
// TODO, what if empty string.
|
||||||
|
|
||||||
|
if (msg.isEmpty()) return null;
|
||||||
|
|
||||||
|
msg = msg.trim().toLowerCase();
|
||||||
|
|
||||||
|
List<String> parts = new LinkedList<>(List.of(msg.split(" ")));
|
||||||
|
|
||||||
|
if (parts.size() > 1) {
|
||||||
|
String command = parts.removeFirst();
|
||||||
|
return new ParsedMessage(command, parts.toArray(String[]::new));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new ParsedMessage(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||||
|
cause.printStackTrace();
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelInactive(ChannelHandlerContext ctx) {
|
||||||
|
server.removeUser(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
public class ServerMessageStore implements MessageStore {
|
||||||
|
|
||||||
|
Queue<String> messageQueue;
|
||||||
|
|
||||||
|
public ServerMessageStore(Queue<String> messageQueue) {
|
||||||
|
this.messageQueue = messageQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(String message) {
|
||||||
|
messageQueue.offer(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get() {
|
||||||
|
return messageQueue.poll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
messageQueue.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
public interface ServerUser {
|
||||||
|
long id();
|
||||||
|
String name();
|
||||||
|
void setName(String name);
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
public class User implements ServerUser {
|
||||||
|
final private long id;
|
||||||
|
private String name;
|
||||||
|
private ChannelHandlerContext connectionContext;
|
||||||
|
|
||||||
|
public User(long userId, String name) {
|
||||||
|
this.id = userId;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long id() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String name() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChannelHandlerContext ctx() {
|
||||||
|
return connectionContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCtx(ChannelHandlerContext ctx) {
|
||||||
|
this.connectionContext = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package org.toop.framework.networking.types;
|
|
||||||
|
|
||||||
public record ServerMessage(String message) {}
|
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
//import org.mockito.*;
|
//import org.mockito.*;
|
||||||
//import org.toop.framework.SnowflakeGenerator;
|
//import org.toop.framework.SnowflakeGenerator;
|
||||||
//import org.toop.framework.eventbus.EventFlow;
|
//import org.toop.framework.eventbus.EventFlow;
|
||||||
//import org.toop.framework.networking.events.NetworkEvents;
|
//import org.toop.framework.networking.connection.events.NetworkEvents;
|
||||||
//
|
//
|
||||||
//class NetworkingClientManagerTest {
|
//class NetworkingClientManagerTest {
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//package org.toop.framework.networking.events;
|
//package org.toop.framework.networking.connection.events;
|
||||||
//
|
//
|
||||||
//import static org.junit.jupiter.api.Assertions.*;
|
//import static org.junit.jupiter.api.Assertions.*;
|
||||||
//
|
//
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
package org.toop.framework.networking.server;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||||
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class ServerTest {
|
||||||
|
|
||||||
|
static class TurnBasedGameMock implements TurnBasedGame {
|
||||||
|
private Player[] players;
|
||||||
|
|
||||||
|
public TurnBasedGameMock() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(Player[] players) {
|
||||||
|
this.players = players;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getCurrentTurn() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPlayerCount() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getWinner() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long[] getBoard() {
|
||||||
|
return new long[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TurnBasedGame deepCopy() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLegalMoves() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlayResult play(long move) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Player getPlayer(int index) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Server server;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
|
||||||
|
games.put("tictactoe", TurnBasedGameMock.class);
|
||||||
|
games.put("reversi", TurnBasedGameMock.class);
|
||||||
|
|
||||||
|
server = new Server(games);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGameTypes() {
|
||||||
|
String[] expected = {"tictactoe", "reversi"};
|
||||||
|
String[] actual = server.gameTypes();
|
||||||
|
|
||||||
|
Arrays.sort(expected);
|
||||||
|
Arrays.sort(actual);
|
||||||
|
|
||||||
|
Assertions.assertArrayEquals(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStartGame() {
|
||||||
|
server.startGame("tictactoe", new User(0, "A"), new User(1, "B"));
|
||||||
|
Assertions.assertEquals(1, server.ongoingGames().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user