mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Compare commits
11 Commits
8867d5a1ea
...
UnitTests
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
153e6343e1 | ||
|
|
e5e79bb9b0 | ||
|
|
a982345791 | ||
|
|
d1176f2151 | ||
|
|
39df1be6ee | ||
|
|
4b80072042 | ||
|
|
e6e931b565 | ||
|
|
e5ad144783 | ||
|
|
550ee5874b | ||
|
|
1ae79daef0 | ||
|
|
cd8eb99559 |
3
.idea/inspectionProfiles/Project_Default.xml
generated
3
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -4,6 +4,9 @@
|
|||||||
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,org.toop.framework.audio.AudioPlayer,play,java.util.Map,remove,java.util.concurrent.Executors,newSingleThreadScheduledExecutor" />
|
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.lang.foreign.Arena,ofAuto,java.lang.foreign.Arena,global,org.toop.framework.audio.AudioPlayer,play,java.util.Map,remove,java.util.concurrent.Executors,newSingleThreadScheduledExecutor" />
|
||||||
</inspection_tool>
|
</inspection_tool>
|
||||||
|
<inspection_tool class="ThrowablePrintStackTrace" enabled="true" level="WARNING" enabled_by_default="true">
|
||||||
|
<scope name="Tests" level="WARNING" enabled="false" />
|
||||||
|
</inspection_tool>
|
||||||
<inspection_tool class="WriteOnlyObject" enabled="false" level="WARNING" enabled_by_default="false" />
|
<inspection_tool class="WriteOnlyObject" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
||||||
@@ -1,14 +1,10 @@
|
|||||||
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;
|
||||||
@@ -20,8 +16,8 @@ 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.connection.NetworkingClientEventListener;
|
import org.toop.framework.networking.NetworkingClientEventListener;
|
||||||
import org.toop.framework.networking.connection.NetworkingClientManager;
|
import org.toop.framework.networking.NetworkingClientManager;
|
||||||
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;
|
||||||
@@ -31,6 +27,12 @@ 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.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@@ -84,7 +86,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
|
||||||
@@ -92,7 +94,7 @@ public final class App extends Application {
|
|||||||
|
|
||||||
WidgetContainer.setCurrentView(loading);
|
WidgetContainer.setCurrentView(loading);
|
||||||
|
|
||||||
setOnLoadingSuccess(loading);
|
setOnLoadingSuccess(loading);
|
||||||
|
|
||||||
EventFlow loadingFlow = new EventFlow();
|
EventFlow loadingFlow = new EventFlow();
|
||||||
|
|
||||||
@@ -135,12 +137,13 @@ 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,13 +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.connection.clients.TournamentNetworkingClient;
|
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
||||||
import org.toop.framework.networking.connection.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
import org.toop.framework.game.players.ArtificialPlayer;
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.framework.game.players.OnlinePlayer;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.framework.game.players.RandomAI;
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
import org.toop.framework.networking.server.gateway.NettyGatewayServer;
|
import org.toop.game.players.OnlinePlayer;
|
||||||
|
import org.toop.game.players.ai.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -31,8 +32,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public final class Server {
|
public final class Server {
|
||||||
private NettyGatewayServer nettyGatewayServer;
|
// TODO: Keep track of listeners. Remove them on Server connection close so reference is deleted.
|
||||||
|
|
||||||
private String user = "";
|
private String user = "";
|
||||||
private long clientId = -1;
|
private long clientId = -1;
|
||||||
|
|
||||||
@@ -60,13 +60,10 @@ public final class Server {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server(String ip, String port, String user) {
|
|
||||||
this(ip, port, user, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server has to deal with ALL network related listen events. This "server" can then interact with the manager to make stuff happen.
|
// Server has to deal with ALL network related listen events. This "server" can then interact with the manager to make stuff happen.
|
||||||
// This prevents data races where events get sent to the game manager but the manager isn't ready yet.
|
// This prevents data races where events get sent to the game manager but the manager isn't ready yet.
|
||||||
public Server(String ip, String port, String user, NettyGatewayServer nettyGatewayServer) {
|
public Server(String ip, String port, String user) {
|
||||||
if (ip.split("\\.").length < 4) {
|
if (ip.split("\\.").length < 4) {
|
||||||
new ErrorPopup("\"" + ip + "\" " + AppContext.getString("is-not-a-valid-ip-address"));
|
new ErrorPopup("\"" + ip + "\" " + AppContext.getString("is-not-a-valid-ip-address"));
|
||||||
return;
|
return;
|
||||||
@@ -86,8 +83,6 @@ public final class Server {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.nettyGatewayServer = nettyGatewayServer;
|
|
||||||
|
|
||||||
final int reconnectAttempts = 10;
|
final int reconnectAttempts = 10;
|
||||||
|
|
||||||
LoadingWidget loading = new LoadingWidget(
|
LoadingWidget loading = new LoadingWidget(
|
||||||
@@ -118,7 +113,7 @@ public final class Server {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
primary = new ServerView(user, this::sendChallenge, clientId);
|
primary = new ServerView(user, this::sendChallenge);
|
||||||
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
||||||
|
|
||||||
a.unsubscribe("connecting");
|
a.unsubscribe("connecting");
|
||||||
@@ -159,8 +154,7 @@ public final class Server {
|
|||||||
.listen(NetworkEvents.GameMatchResponse.class, this::handleMatchResponse, false, "match-response")
|
.listen(NetworkEvents.GameMatchResponse.class, this::handleMatchResponse, false, "match-response")
|
||||||
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false, "game-result")
|
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false, "game-result")
|
||||||
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false, "game-move")
|
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false, "game-move")
|
||||||
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false, "your-turn")
|
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false, "your-turn");
|
||||||
.listen(NetworkEvents.ClosedConnection.class, this::closedConnection, false, "closed-connection");
|
|
||||||
|
|
||||||
connectFlow = a;
|
connectFlow = a;
|
||||||
}
|
}
|
||||||
@@ -220,22 +214,21 @@ public final class Server {
|
|||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TICTACTOE ->{
|
case TICTACTOE ->{
|
||||||
Player[] players = new Player[2];
|
Player<BitboardTicTacToe>[] 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(), user);
|
players[myTurn] = new ArtificialPlayer<>(new RandomAI<BitboardTicTacToe>(), user);
|
||||||
gameController = new TicTacToeBitController(players);
|
gameController = new TicTacToeBitController(players);
|
||||||
}
|
}
|
||||||
case REVERSI -> {
|
case REVERSI -> {
|
||||||
Player[] players = new Player[2];
|
Player<BitboardReversi>[] 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(), user);
|
players[myTurn] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(), user);
|
||||||
gameController = new ReversiBitController(players);}
|
gameController = new ReversiBitController(players, false);}
|
||||||
default -> new ErrorPopup("Unsupported game type.");
|
default -> new ErrorPopup("Unsupported game type.");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gameController != null) {
|
if (gameController != null){
|
||||||
primary.reEnableButton();
|
|
||||||
gameController.start();
|
gameController.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,7 +263,7 @@ public final class Server {
|
|||||||
String gameType = extractQuotedValue(response.gameType());
|
String gameType = extractQuotedValue(response.gameType());
|
||||||
final String finalGameType = gameType;
|
final String finalGameType = gameType;
|
||||||
var a = new ChallengePopup(challengerName, gameType, (playerInformation) -> {
|
var a = new ChallengePopup(challengerName, gameType, (playerInformation) -> {
|
||||||
final long challengeId = Long.parseLong(response.challengeId().replaceAll("\\D", ""));
|
final int challengeId = Integer.parseInt(response.challengeId().replaceAll("\\D", ""));
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientId, challengeId)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.SendAcceptChallenge(clientId, challengeId)).postEvent();
|
||||||
isSingleGame.set(true);
|
isSingleGame.set(true);
|
||||||
});
|
});
|
||||||
@@ -288,27 +281,9 @@ public final class Server {
|
|||||||
stopScheduler();
|
stopScheduler();
|
||||||
connectFlow.unsubscribeAll();
|
connectFlow.unsubscribeAll();
|
||||||
|
|
||||||
if (nettyGatewayServer != null) {
|
|
||||||
nettyGatewayServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void closedConnection(NetworkEvents.ClosedConnection e) {
|
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
|
||||||
isPolling = false;
|
|
||||||
stopScheduler();
|
|
||||||
connectFlow.unsubscribeAll();
|
|
||||||
|
|
||||||
if (nettyGatewayServer != null) {
|
|
||||||
nettyGatewayServer.stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
|
||||||
WidgetContainer.add(Pos.CENTER, new ErrorPopup("Server closed connection."));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void forfeitGame() {
|
private void forfeitGame() {
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.SendForfeit(clientId)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.SendForfeit(clientId)).postEvent();
|
||||||
}
|
}
|
||||||
@@ -355,9 +330,7 @@ public final class Server {
|
|||||||
|
|
||||||
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
private void gamesListFromServerHandler(NetworkEvents.GamelistResponse event) {
|
||||||
gameList.clear();
|
gameList.clear();
|
||||||
var gl = List.of(event.gamelist());
|
gameList.addAll(List.of(event.gamelist()));
|
||||||
gameList.addAll(gl);
|
|
||||||
primary.updateGameList(gl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateGameList() {
|
public void populateGameList() {
|
||||||
|
|||||||
@@ -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 implements GameCanvas {
|
public abstract class BitGameCanvas<T extends TurnBasedGame<T>> implements GameCanvas<T> {
|
||||||
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 &&
|
||||||
@@ -108,6 +108,7 @@ public abstract class BitGameCanvas implements GameCanvas {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void render() {
|
private void render() {
|
||||||
graphics.setFill(backgroundColor);
|
graphics.setFill(backgroundColor);
|
||||||
graphics.fillRect(0, 0, width, height);
|
graphics.fillRect(0, 0, width, height);
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.toop.app.canvas;
|
||||||
|
|
||||||
|
public interface BitLegalMoveDrawer {
|
||||||
|
void showLegalMove(long move, int currentPlayerIndex);
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
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 extends GameDrawer{
|
public interface GameCanvas<T extends TurnBasedGame<T>> extends GameDrawer<T>{
|
||||||
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 {
|
public interface GameDrawer<T extends TurnBasedGame<T>> {
|
||||||
void redraw(TurnBasedGame gameCopy);
|
void redraw(T gameCopy);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,17 @@ 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.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
|
|
||||||
public class ReversiBitCanvas extends BitGameCanvas {
|
public class ReversiBitCanvas extends BitGameCanvas<BitboardReversi> implements BitLegalMoveDrawer {
|
||||||
public ReversiBitCanvas() {
|
|
||||||
|
private final boolean switchColors;
|
||||||
|
private final boolean local;
|
||||||
|
|
||||||
|
public ReversiBitCanvas(boolean switchColors, boolean local) {
|
||||||
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);
|
||||||
|
this.switchColors = switchColors;
|
||||||
|
this.local = local;
|
||||||
canvas.setOnMouseMoved(event -> {
|
canvas.setOnMouseMoved(event -> {
|
||||||
double mouseX = event.getX();
|
double mouseX = event.getX();
|
||||||
double mouseY = event.getY();
|
double mouseY = event.getY();
|
||||||
@@ -30,10 +36,42 @@ public class ReversiBitCanvas extends BitGameCanvas {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redraw(TurnBasedGame gameCopy) {
|
public void redraw(BitboardReversi gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
long[] board = gameCopy.getBoard();
|
long[] board = gameCopy.getBoard();
|
||||||
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
if (switchColors && local) {
|
||||||
loopOverBoard(board[1], (i) -> drawDot(Color.BLACK, i));
|
loopOverBoard(board[0], (i) -> drawDot(Color.BLACK, i));
|
||||||
|
loopOverBoard(board[1], (i) -> drawDot(Color.WHITE, i));
|
||||||
|
} else {
|
||||||
|
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
||||||
|
loopOverBoard(board[1], (i) -> drawDot(Color.BLACK, i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showLegalMove(long move, int currentPlayerIndex) {
|
||||||
|
int idx = Long.numberOfTrailingZeros(move);
|
||||||
|
drawLegalMove(idx, currentPlayerIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void drawLegalMove(int cell, int player) {
|
||||||
|
Color innerColor;
|
||||||
|
if (switchColors) {
|
||||||
|
if (player == 0) {
|
||||||
|
innerColor = new Color(0.0f, 0.0f, 0.0f, 0.6f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
innerColor = new Color(1.0f, 1.0f, 1.0f, 0.75f);
|
||||||
|
}
|
||||||
|
this.drawInnerDot(innerColor, cell, false);
|
||||||
|
} else {
|
||||||
|
if (player == 1) {
|
||||||
|
innerColor = new Color(0.0f, 0.0f, 0.0f, 0.6f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
innerColor = new Color(1.0f, 1.0f, 1.0f, 0.75f);
|
||||||
|
}
|
||||||
|
this.drawInnerDot(innerColor, cell, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ 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.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
|
|
||||||
public class TicTacToeBitCanvas extends BitGameCanvas{
|
import java.util.Arrays;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class TicTacToeBitCanvas extends BitGameCanvas<BitboardTicTacToe>{
|
||||||
public TicTacToeBitCanvas() {
|
public TicTacToeBitCanvas() {
|
||||||
super(
|
super(
|
||||||
Color.GRAY,
|
Color.GRAY,
|
||||||
@@ -19,7 +22,7 @@ public class TicTacToeBitCanvas extends BitGameCanvas{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void redraw(TurnBasedGame gameCopy) {
|
public void redraw(BitboardTicTacToe gameCopy) {
|
||||||
clearAll();
|
clearAll();
|
||||||
drawMoves(gameCopy.getBoard());
|
drawMoves(gameCopy.getBoard());
|
||||||
}
|
}
|
||||||
@@ -27,7 +30,6 @@ public class TicTacToeBitCanvas extends BitGameCanvas{
|
|||||||
private void drawMoves(long[] gameBoard){
|
private void drawMoves(long[] gameBoard){
|
||||||
loopOverBoard(gameBoard[0], (i) -> drawX(Color.RED, i));
|
loopOverBoard(gameBoard[0], (i) -> drawX(Color.RED, i));
|
||||||
loopOverBoard(gameBoard[1], (i) -> drawO(Color.BLUE, i));
|
loopOverBoard(gameBoard[1], (i) -> drawO(Color.BLUE, i));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import javafx.geometry.Pos;
|
|||||||
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.app.canvas.GameCanvas;
|
import org.toop.app.canvas.GameCanvas;
|
||||||
|
import org.toop.app.canvas.BitLegalMoveDrawer;
|
||||||
import org.toop.app.widget.WidgetContainer;
|
import org.toop.app.widget.WidgetContainer;
|
||||||
import org.toop.app.widget.view.GameView;
|
import org.toop.app.widget.view.GameView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
@@ -15,10 +16,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.connection.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.game.players.LocalPlayer;
|
import org.toop.game.players.LocalPlayer;
|
||||||
|
|
||||||
public class GenericGameController implements GameController {
|
public class GenericGameController<T extends TurnBasedGame<T>, C extends GameCanvas<T>> implements GameController {
|
||||||
protected final EventFlow eventFlow = new EventFlow();
|
protected final EventFlow eventFlow = new EventFlow();
|
||||||
|
|
||||||
// Logger for logging
|
// Logger for logging
|
||||||
@@ -27,18 +28,22 @@ public class GenericGameController implements GameController {
|
|||||||
// Reference to gameView view
|
// Reference to gameView view
|
||||||
protected final GameView gameView;
|
protected final GameView gameView;
|
||||||
|
|
||||||
// Reference to game canvas
|
// Reference to String gametype
|
||||||
protected final GameCanvas canvas;
|
protected final String gameType;
|
||||||
|
|
||||||
protected final TurnBasedGame game; // Reference to game instance
|
// Reference to game canvas
|
||||||
|
protected final C canvas;
|
||||||
|
|
||||||
|
protected final TurnBasedGame<T> 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 canvas, TurnBasedGame game, ThreadBehaviour gameThreadBehaviour, String gameType) {
|
public GenericGameController(C canvas, T game, ThreadBehaviour gameThreadBehaviour, String gameType) {
|
||||||
logger.info("Creating: " + this.getClass());
|
logger.info("Creating: " + this.getClass());
|
||||||
|
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.game = game;
|
this.game = game;
|
||||||
|
this.gameType = gameType;
|
||||||
this.gameThreadBehaviour = gameThreadBehaviour;
|
this.gameThreadBehaviour = gameThreadBehaviour;
|
||||||
|
|
||||||
// Tell thread how to send moves
|
// Tell thread how to send moves
|
||||||
@@ -55,9 +60,7 @@ public class GenericGameController implements GameController {
|
|||||||
// 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 -> {
|
.listen(GUIEvents.PlayerAttemptedMove.class, event -> {if (getCurrentPlayer() instanceof LocalPlayer<T> lp){lp.setLastMove(event.move());}}, false);
|
||||||
if (getCurrentPlayer() instanceof LocalPlayer lp){lp.setMove(event.move());}
|
|
||||||
}, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start(){
|
public void start(){
|
||||||
@@ -72,7 +75,7 @@ public class GenericGameController implements GameController {
|
|||||||
gameThreadBehaviour.stop();
|
gameThreadBehaviour.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getCurrentPlayer(){
|
public Player<T> getCurrentPlayer(){
|
||||||
return game.getPlayer(getCurrentPlayerIndex());
|
return game.getPlayer(getCurrentPlayerIndex());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,13 +96,13 @@ public class GenericGameController implements GameController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onGameFinish(GUIEvents.GameEnded event){
|
private void onGameFinish(GUIEvents.GameEnded event){
|
||||||
logger.info("OnlineTurnBasedGame Finished");
|
logger.info("Game Finished");
|
||||||
String name = event.winner() == -1 ? null : getPlayer(event.winner()).getName();
|
String name = event.winner() == -1 ? null : getPlayer(event.winner()).getName();
|
||||||
gameView.gameOver(event.winOrTie(), name);
|
gameView.gameOver(event.winOrTie(), name);
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayer(int player){
|
public Player<T> 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");
|
||||||
@@ -138,5 +141,21 @@ public class GenericGameController implements GameController {
|
|||||||
@Override
|
@Override
|
||||||
public void updateUI() {
|
public void updateUI() {
|
||||||
canvas.redraw(game.deepCopy());
|
canvas.redraw(game.deepCopy());
|
||||||
|
gameView.updatePlayerInfo(
|
||||||
|
true,
|
||||||
|
getCurrentPlayer().getName(),
|
||||||
|
game.getCurrentTurn() == 0 ? "X" : "O",
|
||||||
|
getPlayer((game.getCurrentTurn() + 1 ) % 2).getName(),
|
||||||
|
this.gameType
|
||||||
|
);
|
||||||
|
// draw legal moves for bit canvasses
|
||||||
|
if (canvas instanceof BitLegalMoveDrawer) {
|
||||||
|
long movesLoop = game.getLegalMoves();
|
||||||
|
while (movesLoop != 0) {
|
||||||
|
long move = 1L << Long.numberOfTrailingZeros(movesLoop);
|
||||||
|
((BitLegalMoveDrawer) canvas).showLegalMove(move, getCurrentPlayerIndex());
|
||||||
|
movesLoop &= movesLoop - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
package org.toop.app.gameControllers;
|
package org.toop.app.gameControllers;
|
||||||
|
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
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.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.reversi.BitboardReversi;
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.framework.game.players.OnlinePlayer;
|
import org.toop.game.players.LocalPlayer;
|
||||||
|
import org.toop.game.players.OnlinePlayer;
|
||||||
|
|
||||||
public class ReversiBitController extends GenericGameController {
|
public class ReversiBitController extends GenericGameController<BitboardReversi, ReversiBitCanvas> {
|
||||||
public ReversiBitController(Player[] players) {
|
public ReversiBitController(Player<BitboardReversi>[] players, boolean switchColors) {
|
||||||
BitboardReversi game = new BitboardReversi();
|
BitboardReversi game = new BitboardReversi(players);
|
||||||
game.init(players);
|
ThreadBehaviour thread = new LocalThreadBehaviour<>(game);
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
for (Player<BitboardReversi> player : players) {
|
||||||
for (Player player : players) {
|
if (player instanceof OnlinePlayer<BitboardReversi>) {
|
||||||
if (player instanceof OnlinePlayer){
|
thread = new OnlineThreadBehaviour<>(game);
|
||||||
thread = new OnlineThreadBehaviour(game);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super(new ReversiBitCanvas(), game, thread, "Reversi");
|
super(new ReversiBitCanvas(switchColors, players[0] instanceof LocalPlayer<BitboardReversi> || players[1] instanceof LocalPlayer<BitboardReversi>), 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.framework.game.gameThreads.LocalThreadBehaviour;
|
import org.toop.game.gameThreads.LocalFixedRateThreadBehaviour;
|
||||||
import org.toop.framework.game.gameThreads.OnlineThreadBehaviour;
|
import org.toop.game.gameThreads.LocalThreadBehaviour;
|
||||||
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.game.gameThreads.OnlineThreadBehaviour;
|
||||||
import org.toop.framework.game.players.OnlinePlayer;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
|
import org.toop.game.players.OnlinePlayer;
|
||||||
|
|
||||||
public class TicTacToeBitController extends GenericGameController {
|
public class TicTacToeBitController extends GenericGameController<BitboardTicTacToe, TicTacToeBitCanvas > {
|
||||||
public TicTacToeBitController(Player[] players) {
|
public TicTacToeBitController(Player<BitboardTicTacToe>[] players) {
|
||||||
BitboardTicTacToe game = new BitboardTicTacToe();
|
BitboardTicTacToe game = new BitboardTicTacToe(players);
|
||||||
game.init(players);
|
ThreadBehaviour thread = new LocalThreadBehaviour<>(game);
|
||||||
ThreadBehaviour thread = new LocalThreadBehaviour(game);
|
for (Player<BitboardTicTacToe> player : players) {
|
||||||
for (Player player : players) {
|
if (player instanceof OnlinePlayer<BitboardTicTacToe>){
|
||||||
if (player instanceof OnlinePlayer){
|
thread = new OnlineThreadBehaviour<>(game);
|
||||||
thread = new OnlineThreadBehaviour(game);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
super(new TicTacToeBitCanvas(), game, thread , "TicTacToe");
|
super(new TicTacToeBitCanvas(), game, thread , "TicTacToe");
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public final class Primitive {
|
|||||||
return imageView;
|
return imageView;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Button button(String key, Runnable onAction, boolean localize, boolean disableOnClick) {
|
public static Button button(String key, Runnable onAction, boolean localize) {
|
||||||
var button = new Button();
|
var button = new Button();
|
||||||
button.getStyleClass().add("button");
|
button.getStyleClass().add("button");
|
||||||
|
|
||||||
@@ -75,7 +75,6 @@ public final class Primitive {
|
|||||||
|
|
||||||
if (onAction != null) {
|
if (onAction != null) {
|
||||||
button.setOnAction(_ -> {
|
button.setOnAction(_ -> {
|
||||||
if (disableOnClick) button.setDisable(true);
|
|
||||||
onAction.run();
|
onAction.run();
|
||||||
playButtonSound();
|
playButtonSound();
|
||||||
});
|
});
|
||||||
@@ -84,8 +83,8 @@ public final class Primitive {
|
|||||||
return button;
|
return button;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Button button(String key, Runnable onAction, boolean disableOnClick) {
|
public static Button button(String key, Runnable onAction) {
|
||||||
return button(key, onAction, true, disableOnClick);
|
return button(key, onAction, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TextField input(String promptKey, String text, Consumer<String> onValueChanged, boolean localize) {
|
public static TextField input(String promptKey, String text, Consumer<String> onValueChanged, boolean localize) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public class ConfirmWidget implements Widget {
|
|||||||
|
|
||||||
public void addButton(String key, Runnable onClick) {
|
public void addButton(String key, Runnable onClick) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
var button = Primitive.button(key, onClick, false);
|
var button = Primitive.button(key, onClick);
|
||||||
buttonsContainer.getChildren().add(button);
|
buttonsContainer.getChildren().add(button);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public class PlayerInfoWidget {
|
|||||||
information.computerDifficulty = depth;
|
information.computerDifficulty = depth;
|
||||||
information.computerThinkTime = thinktime;
|
information.computerThinkTime = thinktime;
|
||||||
this.playerName.setText(getName(name));
|
this.playerName.setText(getName(name));
|
||||||
}, false);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getName(String name) {
|
private String getName(String name) {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public abstract class ViewWidget extends StackWidget {
|
|||||||
|
|
||||||
var backButton = Primitive.button("back", () -> {
|
var backButton = Primitive.button("back", () -> {
|
||||||
view.transitionPrevious();
|
view.transitionPrevious();
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ public abstract class ViewWidget extends StackWidget {
|
|||||||
var customButton = Primitive.button(key, () -> {
|
var customButton = Primitive.button(key, () -> {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
view.transitionPrevious();
|
view.transitionPrevious();
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(customButton));
|
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(customButton));
|
||||||
}
|
}
|
||||||
@@ -97,7 +97,7 @@ public abstract class ViewWidget extends StackWidget {
|
|||||||
|
|
||||||
var backButton = Primitive.button("back", () -> {
|
var backButton = Primitive.button("back", () -> {
|
||||||
view.transitionPrevious();
|
view.transitionPrevious();
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ public final class ChallengePopup extends PopupWidget {
|
|||||||
var acceptButton = Primitive.button("accept", () -> {
|
var acceptButton = Primitive.button("accept", () -> {
|
||||||
onAccept.accept(playerInformation);
|
onAccept.accept(playerInformation);
|
||||||
this.hide();
|
this.hide();
|
||||||
}, false);
|
});
|
||||||
var denyButton = Primitive.button("deny", () -> hide(), false);
|
var denyButton = Primitive.button("deny", () -> hide());
|
||||||
|
|
||||||
var leftSection = Primitive.vbox(
|
var leftSection = Primitive.vbox(
|
||||||
challengeText,
|
challengeText,
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ public class EscapePopup extends PopupWidget {
|
|||||||
ViewWidget currentView = WidgetContainer.getCurrentView();
|
ViewWidget currentView = WidgetContainer.getCurrentView();
|
||||||
ArrayList<Node> nodes = new ArrayList<>();
|
ArrayList<Node> nodes = new ArrayList<>();
|
||||||
|
|
||||||
nodes.add(Primitive.button("Continue", this::hide, false, false)); // TODO, localize
|
nodes.add(Primitive.button("Continue", this::hide, false)); // TODO, localize
|
||||||
|
|
||||||
if (!(currentView.getClass().isAssignableFrom(OptionsView.class))) {
|
if (!(currentView.getClass().isAssignableFrom(OptionsView.class))) {
|
||||||
var opt = Primitive.button("options", () -> {
|
var opt = Primitive.button("options", () -> {
|
||||||
hide();
|
hide();
|
||||||
WidgetContainer.getCurrentView().transitionNext(new OptionsView());
|
WidgetContainer.getCurrentView().transitionNext(new OptionsView());
|
||||||
}, false);
|
});
|
||||||
nodes.add(opt);
|
nodes.add(opt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,14 +33,14 @@ public class EscapePopup extends PopupWidget {
|
|||||||
if (tut != null) {
|
if (tut != null) {
|
||||||
nodes.add(Primitive.button("tutorialstring", () -> {
|
nodes.add(Primitive.button("tutorialstring", () -> {
|
||||||
WidgetContainer.getCurrentView().add(Pos.CENTER, tut);
|
WidgetContainer.getCurrentView().add(Pos.CENTER, tut);
|
||||||
}, false));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
nodes.add(Primitive.button("quit", () -> {
|
nodes.add(Primitive.button("quit", () -> {
|
||||||
hide();
|
hide();
|
||||||
WidgetContainer.add(Pos.CENTER, new QuitPopup());
|
WidgetContainer.add(Pos.CENTER, new QuitPopup());
|
||||||
}, false));
|
}));
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(nodes.toArray(new Node[0])));
|
add(Pos.CENTER, Primitive.vbox(nodes.toArray(new Node[0])));
|
||||||
|
|
||||||
|
|||||||
@@ -62,9 +62,9 @@ public final class SendChallengePopup extends PopupWidget {
|
|||||||
var sendButton = Primitive.button(
|
var sendButton = Primitive.button(
|
||||||
"send",
|
"send",
|
||||||
() -> { onSend.accept(playerInformation, gameChoice.getValue()); this.hide(); }
|
() -> { onSend.accept(playerInformation, gameChoice.getValue()); this.hide(); }
|
||||||
, false);
|
);
|
||||||
|
|
||||||
var cancelButton = Primitive.button("cancel", () -> hide(), false);
|
var cancelButton = Primitive.button("cancel", () -> hide());
|
||||||
|
|
||||||
var leftSection = Primitive.vbox(
|
var leftSection = Primitive.vbox(
|
||||||
challengeText,
|
challengeText,
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ public class BaseTutorialWidget extends PopupWidget implements Updatable {
|
|||||||
this.pages = pages;
|
this.pages = pages;
|
||||||
this.nextScreen = nextScreen;
|
this.nextScreen = nextScreen;
|
||||||
|
|
||||||
previousButton = Primitive.button("goback", () -> { update(false); this.hide(); }, false);
|
previousButton = Primitive.button("goback", () -> { update(false); this.hide(); });
|
||||||
nextButton = Primitive.button(">", () -> update(true), false);
|
nextButton = Primitive.button(">", () -> update(true));
|
||||||
|
|
||||||
var w = Primitive.hbox(
|
var w = Primitive.hbox(
|
||||||
previousButton,
|
previousButton,
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ public class ShowEnableTutorialWidget extends PopupWidget {
|
|||||||
|
|
||||||
public ShowEnableTutorialWidget(Runnable tutorial, Runnable nextScreen, Runnable appSettingsSetter) {
|
public ShowEnableTutorialWidget(Runnable tutorial, Runnable nextScreen, Runnable appSettingsSetter) {
|
||||||
var a = Primitive.hbox(
|
var a = Primitive.hbox(
|
||||||
Primitive.button("ok", () -> { appSettingsSetter.run(); tutorial.run(); this.hide(); }, false),
|
Primitive.button("ok", () -> { appSettingsSetter.run(); tutorial.run(); this.hide(); }),
|
||||||
Primitive.button("no", () -> { appSettingsSetter.run(); nextScreen.run(); this.hide(); }, false),
|
Primitive.button("no", () -> { appSettingsSetter.run(); nextScreen.run(); this.hide(); }),
|
||||||
Primitive.button("never", () -> { AppSettings.getSettings().setTutorialFlag(false); nextScreen.run(); this.hide(); }, false)
|
Primitive.button("never", () -> { AppSettings.getSettings().setTutorialFlag(false); nextScreen.run(); this.hide(); })
|
||||||
);
|
);
|
||||||
|
|
||||||
var txt = Primitive.text("tutorial");
|
var txt = Primitive.text("tutorial");
|
||||||
|
|||||||
@@ -16,14 +16,15 @@ import org.toop.app.widget.tutorial.Connect4TutorialWidget;
|
|||||||
import org.toop.app.widget.tutorial.ReversiTutorialWidget;
|
import org.toop.app.widget.tutorial.ReversiTutorialWidget;
|
||||||
import org.toop.app.widget.tutorial.TicTacToeTutorialWidget;
|
import org.toop.app.widget.tutorial.TicTacToeTutorialWidget;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
import org.toop.local.AppSettings;
|
||||||
|
|
||||||
public final class GameView extends ViewWidget {
|
public final class GameView extends ViewWidget {
|
||||||
private final Text playerHeader;
|
private final Text playerHeader;
|
||||||
private final Text turnHeader;
|
private final Text turnHeader;
|
||||||
private final Text player1Header;
|
private final Text player1Header;
|
||||||
private final Text player2Header;
|
private final Text player2Header;
|
||||||
private Circle player1Icon;
|
private final Circle player1Icon;
|
||||||
private Circle player2Icon;
|
private final Circle player2Icon;
|
||||||
private final Button forfeitButton;
|
private final Button forfeitButton;
|
||||||
private final Button exitButton;
|
private final Button exitButton;
|
||||||
private final TextField chatInput;
|
private final TextField chatInput;
|
||||||
@@ -40,7 +41,7 @@ public final class GameView extends ViewWidget {
|
|||||||
player2Icon = new Circle();
|
player2Icon = new Circle();
|
||||||
|
|
||||||
if (onForfeit != null) {
|
if (onForfeit != null) {
|
||||||
forfeitButton = Primitive.button("forfeit", () -> onForfeit.run(), false);
|
forfeitButton = Primitive.button("forfeit", () -> onForfeit.run());
|
||||||
} else {
|
} else {
|
||||||
forfeitButton = null;
|
forfeitButton = null;
|
||||||
}
|
}
|
||||||
@@ -48,7 +49,7 @@ public final class GameView extends ViewWidget {
|
|||||||
exitButton = Primitive.button("exit", () -> {
|
exitButton = Primitive.button("exit", () -> {
|
||||||
onExit.run();
|
onExit.run();
|
||||||
transitionPrevious();
|
transitionPrevious();
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
if (onMessage != null) {
|
if (onMessage != null) {
|
||||||
chatInput = Primitive.input("enter-your-message", "", null);
|
chatInput = Primitive.input("enter-your-message", "", null);
|
||||||
@@ -94,7 +95,7 @@ public final class GameView extends ViewWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer, char GameType) {
|
public void updatePlayerInfo(boolean isMe, String currentPlayer, String currentMove, String nextPlayer, String GameType) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
if (!(hasSet)) {
|
if (!(hasSet)) {
|
||||||
playerHeader.setText(currentPlayer + " vs. " + nextPlayer);
|
playerHeader.setText(currentPlayer + " vs. " + nextPlayer);
|
||||||
@@ -112,8 +113,8 @@ public final class GameView extends ViewWidget {
|
|||||||
new GameOverPopup(iWon, winner).show(Pos.CENTER);
|
new GameOverPopup(iWon, winner).show(Pos.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setPlayerHeaders(boolean isMe, String currentPlayer, String nextPlayer, char GameType) {
|
private void setPlayerHeaders(boolean isMe, String currentPlayer, String nextPlayer, String GameType) {
|
||||||
if (GameType == 'T') {
|
if (GameType.equals("TicTacToe")) {
|
||||||
if (isMe) {
|
if (isMe) {
|
||||||
player1Header.setText("X: " + currentPlayer);
|
player1Header.setText("X: " + currentPlayer);
|
||||||
player2Header.setText("O: " + nextPlayer);
|
player2Header.setText("O: " + nextPlayer);
|
||||||
@@ -124,15 +125,16 @@ public final class GameView extends ViewWidget {
|
|||||||
}
|
}
|
||||||
setPlayerInfoTTT();
|
setPlayerInfoTTT();
|
||||||
}
|
}
|
||||||
else if (GameType == 'R') {
|
else if (GameType.equals("Reversi")) {
|
||||||
if (isMe) {
|
boolean swap = AppSettings.getSettings().getSwitchReversi();
|
||||||
player1Header.setText(currentPlayer);
|
if (!swap) {
|
||||||
player2Header.setText(nextPlayer);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
player1Header.setText(nextPlayer);
|
player1Header.setText(nextPlayer);
|
||||||
player2Header.setText(currentPlayer);
|
player2Header.setText(currentPlayer);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
player1Header.setText(currentPlayer);
|
||||||
|
player2Header.setText(nextPlayer);
|
||||||
|
}
|
||||||
setPlayerInfoReversi();
|
setPlayerInfoReversi();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,8 +168,8 @@ public final class GameView extends ViewWidget {
|
|||||||
var playerInfo = Primitive.vbox(
|
var playerInfo = Primitive.vbox(
|
||||||
playerHeader,
|
playerHeader,
|
||||||
Primitive.separator(),
|
Primitive.separator(),
|
||||||
player1box,
|
player2box,
|
||||||
player2box
|
player1box
|
||||||
);
|
);
|
||||||
|
|
||||||
player1Icon.setRadius(player1Header.fontProperty().map(Font::getSize).getValue());
|
player1Icon.setRadius(player1Header.fontProperty().map(Font::getSize).getValue());
|
||||||
|
|||||||
@@ -6,17 +6,17 @@ 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.framework.game.games.reversi.BitboardReversi;
|
import org.toop.game.games.reversi.BitboardReversi;
|
||||||
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||||
import org.toop.framework.game.players.ArtificialPlayer;
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
import org.toop.framework.game.players.LocalPlayer;
|
import org.toop.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.framework.game.players.MiniMaxAI;
|
import org.toop.game.players.ai.MiniMaxAI;
|
||||||
import org.toop.framework.game.players.RandomAI;
|
import org.toop.game.players.ai.RandomAI;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
@@ -52,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(), "Random AI");
|
players[0] = new ArtificialPlayer<>(new RandomAI<BitboardTicTacToe>(), "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(9), "MiniMax AI");
|
players[1] = new ArtificialPlayer<>(new MiniMaxAI<BitboardTicTacToe>(9), "MiniMax AI");
|
||||||
}
|
}
|
||||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
||||||
new ShowEnableTutorialWidget(
|
new ShowEnableTutorialWidget(
|
||||||
@@ -79,35 +79,36 @@ public class LocalMultiplayerView extends ViewWidget {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case REVERSI:
|
case REVERSI:
|
||||||
|
boolean swap = AppSettings.getSettings().getSwitchReversi();
|
||||||
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(), "Random AI");
|
players[0] = new ArtificialPlayer<>(new RandomAI<BitboardReversi>(), "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(6), "MiniMax");
|
players[1] = new ArtificialPlayer<>(new MiniMaxAI<BitboardReversi>(6), "MiniMax");
|
||||||
}
|
}
|
||||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
|
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstReversi()) {
|
||||||
new ShowEnableTutorialWidget(
|
new ShowEnableTutorialWidget(
|
||||||
() -> new ReversiTutorialWidget(() -> {
|
() -> new ReversiTutorialWidget(() -> {
|
||||||
gameController = new ReversiBitController(players);
|
gameController = new ReversiBitController(players, swap);
|
||||||
gameController.start();
|
gameController.start();
|
||||||
}),
|
}),
|
||||||
() -> Platform.runLater(() -> {
|
() -> Platform.runLater(() -> {
|
||||||
gameController = new ReversiBitController(players);
|
gameController = new ReversiBitController(players, swap);
|
||||||
gameController.start();
|
gameController.start();
|
||||||
}),
|
}),
|
||||||
() -> AppSettings.getSettings().setFirstReversi(false)
|
() -> AppSettings.getSettings().setFirstReversi(false)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
gameController = new ReversiBitController(players);
|
gameController = new ReversiBitController(players, swap);
|
||||||
gameController.start();
|
gameController.start();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var playerSection = setupPlayerSections();
|
var playerSection = setupPlayerSections();
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ public class LocalView extends ViewWidget {
|
|||||||
public LocalView() {
|
public LocalView() {
|
||||||
var ticTacToeButton = Primitive.button("tic-tac-toe", () -> {
|
var ticTacToeButton = Primitive.button("tic-tac-toe", () -> {
|
||||||
transitionNext(new LocalMultiplayerView(GameInformation.Type.TICTACTOE));
|
transitionNext(new LocalMultiplayerView(GameInformation.Type.TICTACTOE));
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var reversiButton = Primitive.button("reversi", () -> {
|
var reversiButton = Primitive.button("reversi", () -> {
|
||||||
transitionNext(new LocalMultiplayerView(GameInformation.Type.REVERSI));
|
transitionNext(new LocalMultiplayerView(GameInformation.Type.REVERSI));
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(
|
add(Pos.CENTER, Primitive.vbox(
|
||||||
ticTacToeButton,
|
ticTacToeButton,
|
||||||
|
|||||||
@@ -9,24 +9,24 @@ public class MainView extends ViewWidget {
|
|||||||
public MainView() {
|
public MainView() {
|
||||||
var localButton = Primitive.button("local", () -> {
|
var localButton = Primitive.button("local", () -> {
|
||||||
transitionNext(new LocalView());
|
transitionNext(new LocalView());
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var onlineButton = Primitive.button("online", () -> {
|
var onlineButton = Primitive.button("online", () -> {
|
||||||
transitionNext(new OnlineView());
|
transitionNext(new OnlineView());
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var creditsButton = Primitive.button("credits", () -> {
|
var creditsButton = Primitive.button("credits", () -> {
|
||||||
transitionNext(new CreditsView());
|
transitionNext(new CreditsView());
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var optionsButton = Primitive.button("options", () -> {
|
var optionsButton = Primitive.button("options", () -> {
|
||||||
transitionNext(new OptionsView());
|
transitionNext(new OptionsView());
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var quitButton = Primitive.button("quit", () -> {
|
var quitButton = Primitive.button("quit", () -> {
|
||||||
var a = new QuitPopup();
|
var a = new QuitPopup();
|
||||||
a.show(Pos.CENTER);
|
a.show(Pos.CENTER);
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(
|
add(Pos.CENTER, Primitive.vbox(
|
||||||
localButton,
|
localButton,
|
||||||
|
|||||||
@@ -6,14 +6,6 @@ import org.toop.app.widget.complex.LabeledInputWidget;
|
|||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import org.toop.framework.game.games.reversi.BitboardReversi;
|
|
||||||
import org.toop.framework.game.games.tictactoe.BitboardTicTacToe;
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.gateway.NettyGatewayServer;
|
|
||||||
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
public class OnlineView extends ViewWidget {
|
public class OnlineView extends ViewWidget {
|
||||||
public OnlineView() {
|
public OnlineView() {
|
||||||
@@ -29,31 +21,7 @@ public class OnlineView extends ViewWidget {
|
|||||||
serverPortInput.getValue(),
|
serverPortInput.getValue(),
|
||||||
playerNameInput.getValue()
|
playerNameInput.getValue()
|
||||||
);
|
);
|
||||||
}, false);
|
});
|
||||||
|
|
||||||
var localHostButton = Primitive.button("host!", () -> {
|
|
||||||
|
|
||||||
var tps = new TurnBasedGameTypeStore();
|
|
||||||
tps.register("tic-tac-toe", BitboardTicTacToe::new);
|
|
||||||
tps.register("reversi", BitboardReversi::new);
|
|
||||||
|
|
||||||
var a = new NettyGatewayServer(6666, tps, Duration.ofSeconds(10));
|
|
||||||
|
|
||||||
new Thread(() -> {
|
|
||||||
try {
|
|
||||||
a.start();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
new Server(
|
|
||||||
"127.0.0.1",
|
|
||||||
"6666",
|
|
||||||
"host",
|
|
||||||
a
|
|
||||||
);
|
|
||||||
}, false, false);
|
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(
|
add(Pos.CENTER, Primitive.vbox(
|
||||||
serverInformationHeader,
|
serverInformationHeader,
|
||||||
@@ -64,9 +32,7 @@ public class OnlineView extends ViewWidget {
|
|||||||
playerNameInput.getNode(),
|
playerNameInput.getNode(),
|
||||||
Primitive.separator(),
|
Primitive.separator(),
|
||||||
|
|
||||||
connectButton,
|
connectButton
|
||||||
Primitive.separator(),
|
|
||||||
localHostButton
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,13 +149,22 @@ public class OptionsView extends ViewWidget {
|
|||||||
"small", "medium", "large"
|
"small", "medium", "large"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
var swapReversiColors = new ToggleWidget(
|
||||||
|
"reversinotswapped", "reversiswapped",
|
||||||
|
AppSettings.getSettings().getSwitchReversi(),
|
||||||
|
swap -> {
|
||||||
|
AppSettings.getSettings().setSwitchReversi(swap);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return Primitive.vbox(
|
return Primitive.vbox(
|
||||||
Primitive.header("style"),
|
Primitive.header("style"),
|
||||||
Primitive.separator(),
|
Primitive.separator(),
|
||||||
|
|
||||||
themeWidget.getNode(),
|
themeWidget.getNode(),
|
||||||
layoutWidget.getNode()
|
layoutWidget.getNode(),
|
||||||
|
Primitive.text("reversiswap"),
|
||||||
|
swapReversiColors.getNode()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,37 +1,26 @@
|
|||||||
package org.toop.app.widget.view;
|
package org.toop.app.widget.view;
|
||||||
|
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.css.converter.StringConverter;
|
|
||||||
import javafx.scene.control.ComboBox;
|
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.Button;
|
import javafx.scene.control.Button;
|
||||||
import javafx.scene.control.ListView;
|
import javafx.scene.control.ListView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
|
||||||
import org.toop.framework.networking.connection.events.NetworkEvents;
|
|
||||||
|
|
||||||
public final class ServerView extends ViewWidget {
|
public final class ServerView extends ViewWidget {
|
||||||
private final String user;
|
private final String user;
|
||||||
private final Consumer<String> onPlayerClicked;
|
private final Consumer<String> onPlayerClicked;
|
||||||
private final long clientId;
|
|
||||||
|
|
||||||
private final ComboBox<String> gameList;
|
|
||||||
private final ListView<Button> listView;
|
private final ListView<Button> listView;
|
||||||
private Button subscribeButton;
|
|
||||||
|
|
||||||
public ServerView(String user, Consumer<String> onPlayerClicked, long clientId) {
|
public ServerView(String user, Consumer<String> onPlayerClicked) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.onPlayerClicked = onPlayerClicked;
|
this.onPlayerClicked = onPlayerClicked;
|
||||||
this.clientId = clientId;
|
|
||||||
|
|
||||||
this.gameList = new ComboBox<>();
|
|
||||||
this.listView = new ListView<>();
|
this.listView = new ListView<>();
|
||||||
|
|
||||||
setupLayout();
|
setupLayout();
|
||||||
@@ -40,26 +29,17 @@ public final class ServerView extends ViewWidget {
|
|||||||
private void setupLayout() {
|
private void setupLayout() {
|
||||||
var playerHeader = Primitive.header(user, false);
|
var playerHeader = Primitive.header(user, false);
|
||||||
|
|
||||||
subscribeButton = Primitive.button(
|
|
||||||
"subscribe",
|
|
||||||
() -> new EventFlow().addPostEvent(new NetworkEvents.SendSubscribe(clientId, gameList.getValue())).postEvent(),
|
|
||||||
false,
|
|
||||||
true
|
|
||||||
); // TODO localize
|
|
||||||
|
|
||||||
var subscribe = Primitive.hbox(gameList, subscribeButton);
|
|
||||||
|
|
||||||
var playerListSection = Primitive.vbox(
|
var playerListSection = Primitive.vbox(
|
||||||
playerHeader,
|
playerHeader,
|
||||||
Primitive.separator(),
|
Primitive.separator(),
|
||||||
subscribe,
|
|
||||||
listView
|
listView
|
||||||
);
|
);
|
||||||
|
|
||||||
add(Pos.CENTER, playerListSection);
|
add(Pos.CENTER, playerListSection);
|
||||||
|
|
||||||
var disconnectButton = Primitive.button(
|
var disconnectButton = Primitive.button("disconnect", () -> {
|
||||||
"disconnect", () -> transitionPrevious(), false);
|
transitionPrevious();
|
||||||
|
});
|
||||||
|
|
||||||
add(Pos.BOTTOM_LEFT, Primitive.vbox(disconnectButton));
|
add(Pos.BOTTOM_LEFT, Primitive.vbox(disconnectButton));
|
||||||
}
|
}
|
||||||
@@ -69,21 +49,9 @@ public final class ServerView extends ViewWidget {
|
|||||||
listView.getItems().clear();
|
listView.getItems().clear();
|
||||||
|
|
||||||
for (String player : players) {
|
for (String player : players) {
|
||||||
var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player), false, false);
|
var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player), false);
|
||||||
listView.getItems().add(playerButton);
|
listView.getItems().add(playerButton);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateGameList(List<String> games) {
|
|
||||||
Platform.runLater(() -> {
|
|
||||||
gameList.getItems().clear();
|
|
||||||
gameList.setItems(FXCollections.observableArrayList(games));
|
|
||||||
gameList.getSelectionModel().select(0);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reEnableButton() {
|
|
||||||
subscribeButton.setDisable(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -5,18 +5,16 @@ import org.toop.framework.audio.VolumeControl;
|
|||||||
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.resource.ResourceManager;
|
import org.toop.framework.resource.ResourceManager;
|
||||||
import org.toop.framework.resource.ResourceMeta;
|
|
||||||
import org.toop.framework.resource.resources.SettingsAsset;
|
import org.toop.framework.resource.resources.SettingsAsset;
|
||||||
import org.toop.framework.settings.Settings;
|
import org.toop.framework.settings.Settings;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class AppSettings {
|
public class AppSettings {
|
||||||
private static SettingsAsset settingsAsset;
|
private static SettingsAsset settingsAsset;
|
||||||
|
|
||||||
public static void applySettings() {
|
public static void applySettings() {
|
||||||
settingsAsset = getPath();
|
settingsAsset = getSettingsAsset();
|
||||||
if (!settingsAsset.isLoaded()) {
|
if (!settingsAsset.isLoaded()) {
|
||||||
settingsAsset.load();
|
settingsAsset.load();
|
||||||
}
|
}
|
||||||
@@ -45,26 +43,9 @@ public class AppSettings {
|
|||||||
.postEvent();
|
.postEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SettingsAsset getPath() {
|
public static SettingsAsset getSettingsAsset() {
|
||||||
if (settingsAsset == null) {
|
if (settingsAsset == null) {
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
settingsAsset = SettingsAsset.getPath();
|
||||||
String basePath;
|
|
||||||
|
|
||||||
if (os.contains("win")) {
|
|
||||||
basePath = System.getenv("APPDATA");
|
|
||||||
if (basePath == null) {
|
|
||||||
basePath = System.getProperty("user.home");
|
|
||||||
}
|
|
||||||
} else if (os.contains("mac")) {
|
|
||||||
basePath = System.getProperty("user.home") + "/Library/Application Support";
|
|
||||||
} else {
|
|
||||||
basePath = System.getProperty("user.home") + "/.config";
|
|
||||||
}
|
|
||||||
|
|
||||||
File settingsFile =
|
|
||||||
new File(basePath + File.separator + "ISY1" + File.separator + "settings.json");
|
|
||||||
// this.settingsAsset = new SettingsAsset(settingsFile);
|
|
||||||
ResourceManager.addAsset(new ResourceMeta<>("settings.json", new SettingsAsset(settingsFile)));
|
|
||||||
}
|
}
|
||||||
return ResourceManager.get("settings.json");
|
return ResourceManager.get("settings.json");
|
||||||
}
|
}
|
||||||
@@ -110,5 +91,6 @@ public class AppSettings {
|
|||||||
settingsAsset.setMusicVolume(15);
|
settingsAsset.setMusicVolume(15);
|
||||||
settingsAsset.setTutorialFlag(true);
|
settingsAsset.setTutorialFlag(true);
|
||||||
settingsAsset.setLayoutSize("medium");
|
settingsAsset.setLayoutSize("medium");
|
||||||
|
settingsAsset.setSwitchReversi(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,6 +89,9 @@ turnof=\u062F\u0648\u0631\u0647
|
|||||||
zwartepiet=\u0633\u0647\u0644: Zwarte Piet
|
zwartepiet=\u0633\u0647\u0644: Zwarte Piet
|
||||||
sinterklaas=\u0645\u062a\u0648\u0633\u0637: Sint R. Klaas
|
sinterklaas=\u0645\u062a\u0648\u0633\u0637: Sint R. Klaas
|
||||||
santa=\u0635\u0639\u0628: Santa
|
santa=\u0635\u0639\u0628: Santa
|
||||||
|
reversiswapped=\u0645\u0628\u062F\u0651\u064E\u0644
|
||||||
|
reversinotswapped=\u063A\u064A\u0631\u0020\u0645\u0628\u062F\u0651\u064E\u0644
|
||||||
|
reversiswap=\u0647\u0644\u0020\u062A\u0631\u064A\u062F\u0020\u062A\u0628\u062F\u064A\u0644\u0020\u0623\u0644\u0648\u0627\u0646\u0020\u0627\u0644\u0631\u064A\u0641\u064E\u0631\u0633\u064A\u061F\u0020\u0645\u062D\u0644\u064A\u0020\u0641\u0642\u0637\u002E
|
||||||
|
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629
|
||||||
|
|||||||
@@ -91,6 +91,9 @@ turnof=ist dran
|
|||||||
zwartepiet=Leicht: Zwarte Piet
|
zwartepiet=Leicht: Zwarte Piet
|
||||||
sinterklaas=Mittel: Sint R. Klaas
|
sinterklaas=Mittel: Sint R. Klaas
|
||||||
santa=Schwer: Santa
|
santa=Schwer: Santa
|
||||||
|
reversiswapped=Getauscht
|
||||||
|
reversinotswapped=Nicht getauscht
|
||||||
|
reversiswap=M\u00F6chten Sie die Reversi-Farben tauschen? Nur lokal.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabisch)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabisch)
|
||||||
chinese=\u4e2d\u6587 (Chinesisch)
|
chinese=\u4e2d\u6587 (Chinesisch)
|
||||||
|
|||||||
@@ -93,6 +93,9 @@ turnof='s turn
|
|||||||
zwartepiet=Easy: Zwarte Piet
|
zwartepiet=Easy: Zwarte Piet
|
||||||
sinterklaas=Medium: Sint R. Klaas
|
sinterklaas=Medium: Sint R. Klaas
|
||||||
santa=Hard:Santa
|
santa=Hard:Santa
|
||||||
|
reversiswapped=Swapped
|
||||||
|
reversinotswapped=Not Swapped
|
||||||
|
reversiswap=Do you want to swap the Reversi colors? Local only.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabic)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabic)
|
||||||
chinese=\u4e2d\u6587 (Chinese)
|
chinese=\u4e2d\u6587 (Chinese)
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ turnof=le toca
|
|||||||
zwartepiet=F\u00e1cil: Zwarte Piet
|
zwartepiet=F\u00e1cil: Zwarte Piet
|
||||||
sinterklaas=Medio: Sint R. Klaas
|
sinterklaas=Medio: Sint R. Klaas
|
||||||
santa=Dif\u00edcil: Santa
|
santa=Dif\u00edcil: Santa
|
||||||
|
reversiswapped=Cambiado
|
||||||
|
reversinotswapped=No cambiado
|
||||||
|
reversiswap=\u00BFDeseas intercambiar los colores de Reversi? Solo local.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Ar\u00e1bigo)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Ar\u00e1bigo)
|
||||||
chinese=\u4e2d\u6587 (Chino)
|
chinese=\u4e2d\u6587 (Chino)
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ turnof=\u00E0 son tour
|
|||||||
zwartepiet=Facile: Zwarte Piet
|
zwartepiet=Facile: Zwarte Piet
|
||||||
sinterklaas=Moyen : Sint R. Klaas
|
sinterklaas=Moyen : Sint R. Klaas
|
||||||
santa=Difficile: Santa
|
santa=Difficile: Santa
|
||||||
|
reversiswapped=\u00C9chang\u00E9
|
||||||
|
reversinotswapped=Non \u00E9chang\u00E9
|
||||||
|
reversiswap=Voulez-vous \u00E9changer les couleurs de Reversi ? Local uniquement.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabe)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabe)
|
||||||
chinese=\u4e2d\u6587 (Chinois)
|
chinese=\u4e2d\u6587 (Chinois)
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ turnof=\u0915\u0940 \u092C\u093E\u0930\u0940
|
|||||||
zwartepiet=\u0905\u0938\u093e\u0928: Zwarte Piet
|
zwartepiet=\u0905\u0938\u093e\u0928: Zwarte Piet
|
||||||
sinterklaas=\u092e\u0927\u094d\u092f\u092e: Sint R. Klaas
|
sinterklaas=\u092e\u0927\u094d\u092f\u092e: Sint R. Klaas
|
||||||
santa=\u0915\u0924\u093f\u0928: Santa
|
santa=\u0915\u0924\u093f\u0928: Santa
|
||||||
|
reversiswapped=\u092C\u0926\u0932\u093E\u0020\u0939\u0941\u0906
|
||||||
|
reversinotswapped=\u0928\u0939\u0940\u0902\u0020\u092C\u0926\u0932\u093E\u0020\u0939\u0941\u0906
|
||||||
|
reversiswap=\u0915\u094D\u092F\u093E\u0020\u0906\u092A\u0020\u0930\u093F\u0935\u0930\u094D\u0938\u0940\u0020\u0915\u0947\u0020\u0930\u0902\u0917\u0020\u092C\u0926\u0932\u0928\u093E\u0020\u091A\u093E\u0939\u0924\u0947\u0020\u0939\u0948\u0902?\u0020\u0915\u0947\u0935\u0932\u0020\u0938\u094D\u0925\u093E\u0928\u0940\u092F\u002E
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0905\u0930\u092c\u0940)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0905\u0930\u092c\u0940)
|
||||||
chinese=\u4e2d\u6587 (\u091a\u0940\u0928\u0940)
|
chinese=\u4e2d\u6587 (\u091a\u0940\u0928\u0940)
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ turnof=\u00E8 il suo turno
|
|||||||
zwartepiet=Facile: Zwarte Piet
|
zwartepiet=Facile: Zwarte Piet
|
||||||
sinterklaas=Medio: Sint R. Klaas
|
sinterklaas=Medio: Sint R. Klaas
|
||||||
santa=Difficile: Santa
|
santa=Difficile: Santa
|
||||||
|
reversiswapped=Scambiato
|
||||||
|
reversinotswapped=Non scambiato
|
||||||
|
reversiswap=Vuoi scambiare i colori di Reversi? Solo locale.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabo)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabo)
|
||||||
chinese=\u4e2d\u6587 (Cinese)
|
chinese=\u4e2d\u6587 (Cinese)
|
||||||
|
|||||||
@@ -89,6 +89,10 @@ turnof=\u306E\u756A
|
|||||||
zwartepiet=\u7c21\u5358: Zwarte Piet
|
zwartepiet=\u7c21\u5358: Zwarte Piet
|
||||||
sinterklaas=\u4e2d\u7d1a: Sint R. Klaas
|
sinterklaas=\u4e2d\u7d1a: Sint R. Klaas
|
||||||
santa=\u96e3\u3057\u3044: Santa
|
santa=\u96e3\u3057\u3044: Santa
|
||||||
|
reversiswapped=\u5165\u308C\u66FF\u3048\u6E08\u307F
|
||||||
|
reversinotswapped=\u672A\u5165\u308C\u66FF\u3048
|
||||||
|
reversiswap=\u30EA\u30D0\u30FC\u30B7\u306E\u8272\u3092\u5165\u308C\u66FF\u3048\u307E\u3059\u304B?\u0020\u30ED\u30FC\u30AB\u30EB\u306E\u307F\u3067\u3059\u002E
|
||||||
|
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u30a2\u30e9\u30d3\u30a2\u8a9e)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u30a2\u30e9\u30d3\u30a2\u8a9e)
|
||||||
chinese=\u4e2d\u6587 (\u4e2d\u6587)
|
chinese=\u4e2d\u6587 (\u4e2d\u6587)
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ turnof=\uC758 \uCC28\uB840
|
|||||||
zwartepiet=\uc218\uc601: Zwarte Piet
|
zwartepiet=\uc218\uc601: Zwarte Piet
|
||||||
sinterklaas=\ubcf4\ud1b5: Sint R. Klaas
|
sinterklaas=\ubcf4\ud1b5: Sint R. Klaas
|
||||||
santa=\uc5d0\uc18c: Santa
|
santa=\uc5d0\uc18c: Santa
|
||||||
|
reversiswapped=\uAD50\uCCB4\uB428
|
||||||
|
reversinotswapped=\uAD50\uCCB4\uB418\uC9C0 \uC54A\uC74C
|
||||||
|
reversiswap=\uB9AC\uBC84\uC2DC \uC0C9\uC0C1\uC744 \uBC14\uAFB8\uC2DC\uACA0\uC2B5\uB2C8\uAE4C?\u0020\uB85C\uCEEC \uC804\uC6A9\u0020\uC785\uB2C8\uB2E4\u002E
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0639\u0631\u0628\u064a\u0629)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0639\u0631\u0628\u064a\u0629)
|
||||||
chinese=\u4e2d\u6587 (\u4e2d\u6587)
|
chinese=\u4e2d\u6587 (\u4e2d\u6587)
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ turnof=is aan de beurt
|
|||||||
zwartepiet=Makkelijk: Zwarte Piet
|
zwartepiet=Makkelijk: Zwarte Piet
|
||||||
sinterklaas=Gemiddeld: Sint R. Klaas
|
sinterklaas=Gemiddeld: Sint R. Klaas
|
||||||
santa=Moeilijk: Santa
|
santa=Moeilijk: Santa
|
||||||
|
reversiswapped=Gewisseld
|
||||||
|
reversinotswapped=Niet gewisseld
|
||||||
|
reversiswap=Wil je de Reversi-kleuren wisselen? Alleen lokaal.
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabisch)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (Arabisch)
|
||||||
chinese=\u4e2d\u6587 (Chinees)
|
chinese=\u4e2d\u6587 (Chinees)
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ turnof=\u0445\u043E\u0434\u0438\u0442
|
|||||||
zwartepiet=\u041b\u0435\u0433\u043a\u043e: Zwarte Piet
|
zwartepiet=\u041b\u0435\u0433\u043a\u043e: Zwarte Piet
|
||||||
sinterklaas=\u0421\u0440\u0435\u0434\u043d\u0438\u0439: Sint R. Klaas
|
sinterklaas=\u0421\u0440\u0435\u0434\u043d\u0438\u0439: Sint R. Klaas
|
||||||
santa=\u0421\u043b\u043e\u0436\u043d\u043e: Santa
|
santa=\u0421\u043b\u043e\u0436\u043d\u043e: Santa
|
||||||
|
reversiswapped=\u041F\u043E\u043C\u0435\u043D\u044F\u043D\u043E
|
||||||
|
reversinotswapped=\u041D\u0435 \u043F\u043E\u043C\u0435\u043D\u044F\u043D\u043E
|
||||||
|
reversiswap=\u0425\u043E\u0442\u0438\u0442\u0435 \u043F\u043E\u043C\u0435\u043D\u044F\u0442\u044C \u0446\u0432\u0435\u0442\u0430 \u0420\u0435\u0432\u0435\u0440\u0441\u0438?\u0020\u0422\u043E\u043B\u044C\u043A\u043E \u043B\u043E\u043A\u0430\u043B\u044C\u043D\u043E\u002E
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0410\u0440\u0430\u0431\u0441\u043a\u0438\u0439)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u0410\u0440\u0430\u0431\u0441\u043a\u0438\u0439)
|
||||||
chinese=\u4e2d\u6587 (\u041a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0439)
|
chinese=\u4e2d\u6587 (\u041a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0439)
|
||||||
|
|||||||
@@ -89,6 +89,9 @@ turnof=\u7684\u56DE\u5408
|
|||||||
zwartepiet=\u7b80\u5355: Zwarte Piet
|
zwartepiet=\u7b80\u5355: Zwarte Piet
|
||||||
sinterklaas=\u4e2d\u7b49: Sint R. Klaas
|
sinterklaas=\u4e2d\u7b49: Sint R. Klaas
|
||||||
santa=\u56f0\u96be: Santa
|
santa=\u56f0\u96be: Santa
|
||||||
|
reversiswapped=\u5DF2\u4EA4\u6362
|
||||||
|
reversinotswapped=\u672A\u4EA4\u6362
|
||||||
|
reversiswap=\u662F\u5426\u8981\u4EA4\u6362\u9ED1\u767D\u68CB\u7684\u989C\u8272\uFF1F\u4EC5\u9650\u672C\u5730\u002E
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u963f\u62c9\u4f2f\u8bed)
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629 (\u963f\u62c9\u4f2f\u8bed)
|
||||||
chinese=\u4e2d\u6587
|
chinese=\u4e2d\u6587
|
||||||
|
|||||||
@@ -147,6 +147,7 @@
|
|||||||
<version>2.42.0</version>
|
<version>2.42.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -1,82 +0,0 @@
|
|||||||
package org.toop.framework.game.gameThreads;
|
|
||||||
|
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
|
||||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.gameFramework.model.game.threadBehaviour.AbstractThreadBehaviour;
|
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
|
||||||
import org.toop.framework.gameFramework.view.GUIEvents;
|
|
||||||
import org.toop.framework.utils.ImmutablePair;
|
|
||||||
import org.toop.framework.utils.Pair;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import static org.toop.framework.gameFramework.GameState.TURN_SKIPPED;
|
|
||||||
import static org.toop.framework.gameFramework.GameState.WIN;
|
|
||||||
|
|
||||||
public class ServerThreadBehaviour extends AbstractThreadBehaviour implements Runnable {
|
|
||||||
private final Consumer<ImmutablePair<String, Integer>> onPlayerMove;
|
|
||||||
private final Consumer<Pair<GameState, Integer>> onGameEnd;
|
|
||||||
/**
|
|
||||||
* Creates a new base behaviour for the specified game.
|
|
||||||
*
|
|
||||||
* @param game the turn-based game to control
|
|
||||||
*/
|
|
||||||
public ServerThreadBehaviour(TurnBasedGame game, Consumer<ImmutablePair<String, Integer>> onPlayerMove, Consumer<Pair<GameState, Integer>> onGameEnd) {
|
|
||||||
this.onPlayerMove = onPlayerMove;
|
|
||||||
this.onGameEnd = onGameEnd;
|
|
||||||
super(game);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyPlayerMove(ImmutablePair<String, Integer> pair) {
|
|
||||||
if (onPlayerMove != null) {
|
|
||||||
onPlayerMove.accept(pair);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyGameEnd(ImmutablePair<GameState, Integer> pair) {
|
|
||||||
if (onGameEnd != null) {
|
|
||||||
onGameEnd.accept(pair);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Starts the game loop in a new thread. */
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
if (isRunning.compareAndSet(false, true)) {
|
|
||||||
new Thread(this).start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Stops the game loop after the current iteration. */
|
|
||||||
@Override
|
|
||||||
public void stop() {
|
|
||||||
isRunning.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (isRunning.get()) {
|
|
||||||
Player currentPlayer = game.getPlayer(game.getCurrentTurn());
|
|
||||||
long move = currentPlayer.getMove(game.deepCopy());
|
|
||||||
PlayResult result = game.play(move);
|
|
||||||
|
|
||||||
GameState state = result.state();
|
|
||||||
notifyPlayerMove(new ImmutablePair<>(currentPlayer.getName(), Long.numberOfTrailingZeros(move)));
|
|
||||||
|
|
||||||
switch (state) {
|
|
||||||
case WIN, DRAW -> {
|
|
||||||
isRunning.set(false);
|
|
||||||
notifyGameEnd(new ImmutablePair<>(state, game.getWinner()));
|
|
||||||
}
|
|
||||||
case NORMAL, TURN_SKIPPED -> { /* continue normally */ }
|
|
||||||
default -> {
|
|
||||||
logger.error("Unexpected state {}", state);
|
|
||||||
isRunning.set(false);
|
|
||||||
throw new RuntimeException("Unknown state: " + state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
package org.toop.framework.game.players;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.player.*;
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a player controlled by an AI in a game.
|
|
||||||
* <p>
|
|
||||||
* This player uses an {@link AbstractAI} instance to determine its moves. The generic
|
|
||||||
* parameter {@code T} specifies the type of {@link GameR} the AI can handle.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param <T> the specific type of game this AI player can play
|
|
||||||
*/
|
|
||||||
public class ArtificialPlayer extends AbstractPlayer {
|
|
||||||
|
|
||||||
/** The AI instance used to calculate moves. */
|
|
||||||
private final AI ai;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new ArtificialPlayer using the specified AI.
|
|
||||||
*
|
|
||||||
* @param ai the AI instance that determines moves for this player
|
|
||||||
*/
|
|
||||||
public ArtificialPlayer(AI ai, String name) {
|
|
||||||
super(name);
|
|
||||||
this.ai = ai;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArtificialPlayer(ArtificialPlayer other) {
|
|
||||||
super(other);
|
|
||||||
this.ai = other.ai.deepCopy();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determines the next move for this player using its AI.
|
|
||||||
* <p>
|
|
||||||
* This method overrides {@link AbstractPlayer#getMove(GameR)}. Because the AI is
|
|
||||||
* typed to {@code T}, a runtime cast is required. It is the caller's
|
|
||||||
* responsibility to ensure that {@code gameCopy} is of type {@code T}.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @param gameCopy a copy of the current game state
|
|
||||||
* @return the integer representing the chosen move
|
|
||||||
* @throws ClassCastException if {@code gameCopy} is not of type {@code T}
|
|
||||||
*/
|
|
||||||
public long getMove(TurnBasedGame gameCopy) {
|
|
||||||
return ai.getMove(gameCopy);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ArtificialPlayer deepCopy() {
|
|
||||||
return new ArtificialPlayer(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
package org.toop.framework.game.players;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
public class LocalPlayer extends AbstractPlayer {
|
|
||||||
// Future can be used with event system, IF unsubscribeAfterSuccess works...
|
|
||||||
// private CompletableFuture<Integer> LastMove = new CompletableFuture<>();
|
|
||||||
|
|
||||||
private CompletableFuture<Long> LastMove;
|
|
||||||
|
|
||||||
public LocalPlayer(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public LocalPlayer(LocalPlayer other) {
|
|
||||||
super(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getMove(TurnBasedGame gameCopy) {
|
|
||||||
return getValidMove(gameCopy);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMove(long move) {
|
|
||||||
LastMove.complete(move);
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: helper function, would like to replace to get rid of this method
|
|
||||||
public static boolean contains(int[] array, int value){
|
|
||||||
for (int i : array) if (i == value) return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long getMove2(TurnBasedGame gameCopy) {
|
|
||||||
LastMove = new CompletableFuture<>();
|
|
||||||
long move = 0;
|
|
||||||
try {
|
|
||||||
move = LastMove.get();
|
|
||||||
System.out.println(Long.toBinaryString(move));
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
// TODO: Add proper logging.
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected long getValidMove(TurnBasedGame gameCopy){
|
|
||||||
// Get this player's valid moves
|
|
||||||
long validMoves = gameCopy.getLegalMoves();
|
|
||||||
// Make sure provided move is valid
|
|
||||||
// TODO: Limit amount of retries?
|
|
||||||
// TODO: Stop copying game so many times
|
|
||||||
long move = getMove2(gameCopy.deepCopy());
|
|
||||||
while ((validMoves & move) == 0) {
|
|
||||||
System.out.println("Not a valid move, try again");
|
|
||||||
move = getMove2(gameCopy.deepCopy());
|
|
||||||
}
|
|
||||||
return move;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public LocalPlayer deepCopy() {
|
|
||||||
return new LocalPlayer(this.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/*public void register() {
|
|
||||||
// Listening to PlayerAttemptedMove
|
|
||||||
new EventFlow().listen(GUIEvents.PlayerAttemptedMove.class, event -> {
|
|
||||||
if (!LastMove.isDone()) {
|
|
||||||
LastMove.complete(event.move()); // complete the future
|
|
||||||
}
|
|
||||||
}, true); // auto-unsubscribe
|
|
||||||
}
|
|
||||||
|
|
||||||
// This blocks until the next move arrives
|
|
||||||
public int take() throws ExecutionException, InterruptedException {
|
|
||||||
int move = LastMove.get(); // blocking
|
|
||||||
LastMove = new CompletableFuture<>(); // reset for next move
|
|
||||||
return move;
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package org.toop.framework.game.players;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a player controlled remotely or over a network.
|
|
||||||
* <p>
|
|
||||||
* This class extends {@link AbstractPlayer} and can be used to implement game logic
|
|
||||||
* where moves are provided by an external source (e.g., another user or a server).
|
|
||||||
* Currently, this class is a placeholder and does not implement move logic.
|
|
||||||
* </p>
|
|
||||||
*/
|
|
||||||
public class OnlinePlayer extends AbstractPlayer {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new OnlinePlayer.
|
|
||||||
* <p>
|
|
||||||
* Currently, no additional initialization is performed. Subclasses or
|
|
||||||
* future implementations should provide mechanisms to receive moves from
|
|
||||||
* an external source.
|
|
||||||
*/
|
|
||||||
public OnlinePlayer(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OnlinePlayer(OnlinePlayer other) {
|
|
||||||
super(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Player deepCopy() {
|
|
||||||
return new OnlinePlayer(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package org.toop.framework.game.players;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.gameFramework.model.player.AbstractPlayer;
|
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
public class ServerPlayer extends AbstractPlayer {
|
|
||||||
private NettyClient client;
|
|
||||||
private CompletableFuture<Long> lastMove;
|
|
||||||
|
|
||||||
public ServerPlayer(NettyClient client) {
|
|
||||||
super(client.name());
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMove(long move) {
|
|
||||||
lastMove.complete(move);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Player deepCopy() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getMove(TurnBasedGame game) {
|
|
||||||
lastMove = new CompletableFuture<>();
|
|
||||||
System.out.println("Sending yourturn");
|
|
||||||
client.send("SVR GAME YOURTURN {TURNMESSAGE: \"<bericht voor deze beurt>\"}\n");
|
|
||||||
try {
|
|
||||||
return lastMove.get();
|
|
||||||
} catch (InterruptedException | ExecutionException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,13 +4,13 @@ package org.toop.framework.gameFramework;
|
|||||||
* Represents the current state of a turn-based game.
|
* Represents the current state of a turn-based game.
|
||||||
*/
|
*/
|
||||||
public enum GameState {
|
public enum GameState {
|
||||||
/** OnlineTurnBasedGame is ongoing and no special condition applies. */
|
/** Game is ongoing and no special condition applies. */
|
||||||
NORMAL,
|
NORMAL,
|
||||||
|
|
||||||
/** OnlineTurnBasedGame ended in a draw. */
|
/** Game ended in a draw. */
|
||||||
DRAW,
|
DRAW,
|
||||||
|
|
||||||
/** OnlineTurnBasedGame ended with a win for a player. */
|
/** Game ended with a win for a player. */
|
||||||
WIN,
|
WIN,
|
||||||
|
|
||||||
/** Next player's turn was skipped. */
|
/** Next player's turn was skipped. */
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
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.connection.events.NetworkEvents;
|
import org.toop.framework.networking.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 {
|
public interface PlayerProvider<T extends TurnBasedGame<T>> {
|
||||||
Player getPlayer(int index);
|
Player<T> getPlayer(int index);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
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,9 +1,6 @@
|
|||||||
package org.toop.framework.gameFramework.model.game;
|
package org.toop.framework.gameFramework.model.game;
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
public interface TurnBasedGame<T extends TurnBasedGame<T>> extends Playable, DeepCopyable<T>, PlayerProvider<T>, BoardProvider {
|
||||||
|
|
||||||
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 implements ThreadBehaviour {
|
public abstract class AbstractThreadBehaviour<T extends TurnBasedGame<T>> 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 TurnBasedGame game;
|
protected final T 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 implements ThreadBehaviour {
|
|||||||
*
|
*
|
||||||
* @param game the turn-based game to control
|
* @param game the turn-based game to control
|
||||||
*/
|
*/
|
||||||
public AbstractThreadBehaviour(TurnBasedGame game) {
|
public AbstractThreadBehaviour(T 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 extends MoveProvider, DeepCopyable<AI> {
|
public interface AI<T extends TurnBasedGame<T>> extends MoveProvider<T>, DeepCopyable<AI<T>> {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 implements AI {
|
public abstract class AbstractAI<T extends TurnBasedGame<T>> implements AI<T> {
|
||||||
// Concrete AI implementations should override findBestMove(T game, int depth)
|
// Concrete AI implementations should override findBestMove(T game, int depth)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,47 +5,66 @@ import org.apache.logging.log4j.Logger;
|
|||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class representing a player in a game.
|
* Base class for players in a turn-based game.
|
||||||
* <p>
|
*
|
||||||
* Players are entities that can make moves based on the current state of a game.
|
* @param <T> the game type
|
||||||
* player types, such as human players or AI players.
|
|
||||||
* </p>
|
|
||||||
* <p>
|
|
||||||
* Subclasses should override the {@link #getMove(GameR)} method to provide
|
|
||||||
* specific move logic.
|
|
||||||
* </p>
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractPlayer implements Player {
|
public abstract class AbstractPlayer<T extends TurnBasedGame<T>> implements Player<T> {
|
||||||
private final Logger logger = LogManager.getLogger(this.getClass());
|
|
||||||
|
|
||||||
|
private final Logger logger = LogManager.getLogger(this.getClass());
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new player with the given name.
|
||||||
|
*
|
||||||
|
* @param name the player name
|
||||||
|
*/
|
||||||
protected AbstractPlayer(String name) {
|
protected AbstractPlayer(String name) {
|
||||||
System.out.println("Player " + name + " has been created");
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AbstractPlayer(AbstractPlayer other) {
|
/**
|
||||||
|
* Creates a copy of another player.
|
||||||
|
*
|
||||||
|
* @param other the player to copy
|
||||||
|
*/
|
||||||
|
protected AbstractPlayer(AbstractPlayer<T> other) {
|
||||||
this.name = other.name;
|
this.name = other.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines the next move based on the provided game state.
|
* Gets the player's move for the given game state.
|
||||||
|
* A deep copy is provided so the player cannot modify the real state.
|
||||||
* <p>
|
* <p>
|
||||||
* The default implementation throws an {@link UnsupportedOperationException},
|
* This method uses the Template Method Pattern: it defines the fixed
|
||||||
* indicating that concrete subclasses must override this method to provide
|
* algorithm and delegates the variable part to {@link #determineMove(T)}.
|
||||||
* actual move logic.
|
|
||||||
* </p>
|
|
||||||
*
|
*
|
||||||
* @param gameCopy a snapshot of the current game state
|
* @param game the current game
|
||||||
* @return an integer representing the chosen move
|
* @return the chosen move
|
||||||
* @throws UnsupportedOperationException if the method is not overridden
|
|
||||||
*/
|
*/
|
||||||
public long getMove(TurnBasedGame gameCopy) {
|
public final long getMove(T game) {
|
||||||
logger.error("Method getMove not implemented.");
|
return determineMove(game.deepCopy());
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String getName(){
|
|
||||||
|
/**
|
||||||
|
* Determines the player's move using a safe copy of the game.
|
||||||
|
* <p>
|
||||||
|
* This method is called by {@link #getMove(T)} and should contain
|
||||||
|
* the player's strategy for choosing a move.
|
||||||
|
*
|
||||||
|
* @param gameCopy a deep copy of the game
|
||||||
|
* @return the chosen move
|
||||||
|
*/
|
||||||
|
protected abstract long determineMove(T gameCopy);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the player's name.
|
||||||
|
*
|
||||||
|
* @return the name
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
public interface MoveProvider<T extends TurnBasedGame<T>> {
|
||||||
long getMove(TurnBasedGame game);
|
long getMove(T game);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
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 extends NameProvider, MoveProvider, DeepCopyable<Player> {}
|
public interface Player<T extends TurnBasedGame<T>> extends NameProvider, MoveProvider<T>, DeepCopyable<Player<T>> {
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package org.toop.framework.networking.connection;
|
package org.toop.framework.networking;
|
||||||
|
|
||||||
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.connection.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.connection.interfaces.NetworkingClientManager;
|
import org.toop.framework.networking.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.connection;
|
package org.toop.framework.networking;
|
||||||
|
|
||||||
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.connection.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.connection.interfaces.NetworkingClient;
|
import org.toop.framework.networking.interfaces.NetworkingClient;
|
||||||
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
|
|
||||||
public class NetworkingClientManager implements org.toop.framework.networking.connection.interfaces.NetworkingClientManager {
|
public class NetworkingClientManager implements org.toop.framework.networking.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.connection.clients;
|
package org.toop.framework.networking.clients;
|
||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.*;
|
||||||
@@ -12,10 +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.connection.events.NetworkEvents;
|
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;
|
||||||
|
|
||||||
@@ -24,7 +23,6 @@ public class TournamentNetworkingClient implements NetworkingClient {
|
|||||||
|
|
||||||
private final EventBus eventBus;
|
private final EventBus eventBus;
|
||||||
private Channel channel;
|
private Channel channel;
|
||||||
private long clientId;
|
|
||||||
|
|
||||||
public TournamentNetworkingClient(EventBus eventBus) {
|
public TournamentNetworkingClient(EventBus eventBus) {
|
||||||
this.eventBus = eventBus;
|
this.eventBus = eventBus;
|
||||||
@@ -37,7 +35,6 @@ public class TournamentNetworkingClient implements NetworkingClient {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void connect(long clientId, String host, int port) throws CouldNotConnectException {
|
public void connect(long clientId, String host, int port) throws CouldNotConnectException {
|
||||||
this.clientId = clientId;
|
|
||||||
try {
|
try {
|
||||||
Bootstrap bootstrap = new Bootstrap();
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
|
||||||
@@ -78,7 +75,6 @@ public class TournamentNetworkingClient implements NetworkingClient {
|
|||||||
logger.info("Connection {} sent message: '{}' ", this.channel.remoteAddress(), literalMsg);
|
logger.info("Connection {} sent message: '{}' ", this.channel.remoteAddress(), literalMsg);
|
||||||
} else {
|
} else {
|
||||||
logger.warn("Cannot send message: '{}', connection inactive. ", literalMsg);
|
logger.warn("Cannot send message: '{}', connection inactive. ", literalMsg);
|
||||||
eventBus.post(new NetworkEvents.ClosedConnection(clientId));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package org.toop.framework.networking.connection.types;
|
|
||||||
|
|
||||||
public record ServerMessage(String message) {}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.connection.events;
|
package org.toop.framework.networking.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.connection.interfaces.NetworkingClient;
|
import org.toop.framework.networking.interfaces.NetworkingClient;
|
||||||
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines all event types related to the networking subsystem.
|
* Defines all event types related to the networking subsystem.
|
||||||
@@ -102,7 +102,7 @@ public class NetworkEvents extends EventsBase {
|
|||||||
implements GenericEvent {}
|
implements GenericEvent {}
|
||||||
|
|
||||||
/** Requests to accept an existing challenge. */
|
/** Requests to accept an existing challenge. */
|
||||||
public record SendAcceptChallenge(long clientId, long challengeId)
|
public record SendAcceptChallenge(long clientId, int challengeId)
|
||||||
implements GenericEvent {}
|
implements GenericEvent {}
|
||||||
|
|
||||||
/** Requests to forfeit the current game. */
|
/** Requests to forfeit the current game. */
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.connection.exceptions;
|
package org.toop.framework.networking.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.connection.exceptions;
|
package org.toop.framework.networking.exceptions;
|
||||||
|
|
||||||
public class CouldNotConnectException extends RuntimeException {
|
public class CouldNotConnectException extends RuntimeException {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.connection.exceptions;
|
package org.toop.framework.networking.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.connection.handlers;
|
package org.toop.framework.networking.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.connection.events.NetworkEvents;
|
import org.toop.framework.networking.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.connection.interfaces;
|
package org.toop.framework.networking.interfaces;
|
||||||
|
|
||||||
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package org.toop.framework.networking.connection.interfaces;
|
package org.toop.framework.networking.interfaces;
|
||||||
|
|
||||||
import org.toop.framework.networking.connection.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.connection.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.connection.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
|
|
||||||
public interface NetworkingClientManager {
|
public interface NetworkingClientManager {
|
||||||
void startClient(
|
void startClient(
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package org.toop.framework.networking.server;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface GameServer<GAMETYPE, CLIENT, CHALLENGEIDTYPE> {
|
|
||||||
void startGame(String gameType, CLIENT... clients);
|
|
||||||
|
|
||||||
void addClient(CLIENT client);
|
|
||||||
void removeClient(CLIENT client);
|
|
||||||
|
|
||||||
void challengeClient(String fromClientName, String toClientName, String gameTypeKey);
|
|
||||||
void acceptChallenge(CHALLENGEIDTYPE challengeId);
|
|
||||||
|
|
||||||
void subscribeClient(String clientName, String gameTypeKey);
|
|
||||||
void unsubscribeClient(String clientName);
|
|
||||||
|
|
||||||
List<String> gameTypes();
|
|
||||||
List<OnlineGame<GAMETYPE>> ongoingGames();
|
|
||||||
|
|
||||||
List<CLIENT> onlineUsers();
|
|
||||||
void shutdown();
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package org.toop.framework.networking.server;
|
|
||||||
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
|
|
||||||
public interface OnlineGame<T> {
|
|
||||||
long id();
|
|
||||||
T game();
|
|
||||||
NettyClient[] users();
|
|
||||||
void start();
|
|
||||||
}
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
package org.toop.framework.networking.server;
|
|
||||||
|
|
||||||
import org.toop.framework.game.gameThreads.ServerThreadBehaviour;
|
|
||||||
import org.toop.framework.gameFramework.GameState;
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
|
|
||||||
public class OnlineTurnBasedGame implements OnlineGame<TurnBasedGame> {
|
|
||||||
|
|
||||||
private long id;
|
|
||||||
private NettyClient[] clients;
|
|
||||||
private TurnBasedGame game;
|
|
||||||
private ServerThreadBehaviour gameThread;
|
|
||||||
|
|
||||||
public OnlineTurnBasedGame(TurnBasedGame game, NettyClient... clients) {
|
|
||||||
this.game = game;
|
|
||||||
this.gameThread = new ServerThreadBehaviour(
|
|
||||||
game,
|
|
||||||
(pair) -> notifyMoveMade(pair.getLeft(), pair.getRight()),
|
|
||||||
(pair) -> notifyGameEnd(pair.getLeft(), pair.getRight())
|
|
||||||
);
|
|
||||||
this.clients = clients;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyMoveMade(String speler, int move){
|
|
||||||
for (NettyClient client : clients) {
|
|
||||||
client.send(String.format("SVR GAME MOVE {PLAYER: \"%s\", MOVE: \"%s\", DETAILS: \"<reactie spel op zet>\"}\n", speler, move));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void notifyGameEnd(GameState state, int winner){
|
|
||||||
if (state == GameState.DRAW){
|
|
||||||
for (NettyClient client : clients) {
|
|
||||||
client.send(String.format("SVR GAME DRAW {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
clients[winner].send(String.format("SVR GAME WIN {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
|
||||||
clients[(winner + 1)%2].send(String.format("SVR GAME LOSS {PLAYERONESCORE: \"<score speler1>\", PLAYERTWOSCORE: \"<score speler2>\", COMMENT: \"NettyClient disconnected\"}\n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long id() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public org.toop.framework.gameFramework.model.game.TurnBasedGame game() {
|
|
||||||
return game;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NettyClient[] users() {
|
|
||||||
return clients;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start(){
|
|
||||||
this.gameThread.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,258 +0,0 @@
|
|||||||
package org.toop.framework.networking.server;
|
|
||||||
|
|
||||||
import org.toop.framework.game.players.ServerPlayer;
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.challenges.gamechallenge.GameChallenge;
|
|
||||||
import org.toop.framework.networking.server.challenges.gamechallenge.GameChallengeTimer;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
import org.toop.framework.networking.server.stores.ClientStore;
|
|
||||||
import org.toop.framework.networking.server.stores.TurnBasedGameStore;
|
|
||||||
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
|
|
||||||
import org.toop.framework.utils.ImmutablePair;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public class Server implements GameServer<TurnBasedGame, NettyClient, Long> {
|
|
||||||
|
|
||||||
final private TurnBasedGameTypeStore gameTypesStore;
|
|
||||||
final private ClientStore<Long, NettyClient> clientStore;
|
|
||||||
final private List<GameChallenge> gameChallenges = new CopyOnWriteArrayList<>();
|
|
||||||
final private TurnBasedGameStore gameStore;
|
|
||||||
|
|
||||||
final private ConcurrentHashMap<String, List<String>> subscriptions; // TODO move to own store / manager
|
|
||||||
|
|
||||||
final private Duration challengeDuration;
|
|
||||||
final private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
|
||||||
|
|
||||||
public Server(
|
|
||||||
Duration challengeDuration,
|
|
||||||
TurnBasedGameTypeStore turnBasedGameTypeStore,
|
|
||||||
ClientStore<Long, NettyClient> clientStore,
|
|
||||||
TurnBasedGameStore gameStore
|
|
||||||
|
|
||||||
) {
|
|
||||||
this.gameTypesStore = turnBasedGameTypeStore;
|
|
||||||
this.challengeDuration = challengeDuration;
|
|
||||||
this.clientStore = clientStore;
|
|
||||||
this.gameStore = gameStore;
|
|
||||||
this.subscriptions = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
scheduler.schedule(this::serverTask, 0, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addClient(NettyClient client) {
|
|
||||||
clientStore.add(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeClient(NettyClient client) {
|
|
||||||
clientStore.remove(client.id());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> gameTypes() {
|
|
||||||
return new ArrayList<>(gameTypesStore.all().keySet());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<OnlineGame<TurnBasedGame>> ongoingGames() {
|
|
||||||
return gameStore.all().stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void challengeClient(String fromUser, String toUser, String gameType) {
|
|
||||||
|
|
||||||
NettyClient from = getUser(fromUser);
|
|
||||||
if (from == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!gameTypesStore.all().containsKey(gameType)) {
|
|
||||||
from.send("ERR gametype not found \n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
NettyClient to = getUser(toUser);
|
|
||||||
if (to == null) {
|
|
||||||
from.send("ERR user not found \n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var ch = new GameChallenge(from, to, gameType, new GameChallengeTimer(challengeDuration));
|
|
||||||
|
|
||||||
to.send(
|
|
||||||
"SVR GAME CHALLENGE {CHALLENGER: \"%s\", CHALLENGENUMBER: \"%s\", GAMETYPE: \"%s\"} \n"
|
|
||||||
.formatted(from.name(), ch.id(), gameType)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!isValidChallenge(ch)) {
|
|
||||||
warnUserExpiredChallenge(from, ch.id());
|
|
||||||
ch.forceExpire();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
gameChallenges.addLast(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void acceptChallenge(Long challengeId) {
|
|
||||||
for (var challenge : gameChallenges) {
|
|
||||||
if (challenge.id() == challengeId) {
|
|
||||||
startGame(challenge.acceptChallenge(), challenge.getUsers());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void subscribeClient(String clientName, String gameTypeKey) {
|
|
||||||
|
|
||||||
if (!gameTypesStore.all().containsKey(gameTypeKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
subscriptions.forEach((_, clientNames) -> clientNames.remove(clientName));
|
|
||||||
subscriptions.computeIfAbsent(
|
|
||||||
gameTypeKey,
|
|
||||||
_ -> new ArrayList<>())
|
|
||||||
.add(clientName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unsubscribeClient(String clientName) {
|
|
||||||
subscriptions.forEach((_, clientNames) -> clientNames.remove(clientName));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startGame(String gameType, NettyClient... clients) {
|
|
||||||
IO.println("------------------------------------------");
|
|
||||||
|
|
||||||
IO.println("USERS: " + clients.length + " " + Arrays.stream(clients).toList().toString());
|
|
||||||
|
|
||||||
if (!gameTypesStore.all().containsKey(gameType)) return;
|
|
||||||
|
|
||||||
IO.println("------------------------------------------");
|
|
||||||
|
|
||||||
try {
|
|
||||||
ServerPlayer[] players = new ServerPlayer[clients.length];
|
|
||||||
var game = new OnlineTurnBasedGame(gameTypesStore.create(gameType), clients);
|
|
||||||
|
|
||||||
for (int i = 0; i < clients.length; i++) {
|
|
||||||
players[i] = new ServerPlayer(clients[i]);
|
|
||||||
clients[i].addGame(new ImmutablePair<>(game, players[i]));
|
|
||||||
}
|
|
||||||
System.out.println("Starting OnlineTurnBasedGame");
|
|
||||||
|
|
||||||
game.game().init(players);
|
|
||||||
gameStore.add(game);
|
|
||||||
|
|
||||||
clients[0].send(String.format("SVR GAME MATCH {PLAYERTOMOVE: \"%s\", GAMETYPE: \"%s\", OPPONENT: \"%s\"}\n",
|
|
||||||
clients[0].name(),
|
|
||||||
gameType,
|
|
||||||
clients[1].name()));
|
|
||||||
clients[1].send(String.format("SVR GAME MATCH {PLAYERTOMOVE: \"%s\", GAMETYPE: \"%s\", OPPONENT: \"%s\"}\n",
|
|
||||||
clients[0].name(),
|
|
||||||
gameType,
|
|
||||||
clients[0].name()));
|
|
||||||
game.start();
|
|
||||||
} catch (Exception e) {
|
|
||||||
IO.println("ERROR: Failed to start OnlineTurnBasedGame");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<NettyClient> onlineUsers() {
|
|
||||||
return clientStore.all().stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
|
||||||
scheduler.shutdown();
|
|
||||||
gameChallenges.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void serverTask() {
|
|
||||||
checkChallenges();
|
|
||||||
checkSubscriptions();
|
|
||||||
scheduler.schedule(this::serverTask, 500, TimeUnit.MILLISECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkChallenges() {
|
|
||||||
for (int i = gameChallenges.size() - 1; i >= 0; i--) {
|
|
||||||
var challenge = gameChallenges.get(i);
|
|
||||||
|
|
||||||
if (isValidChallenge(challenge)) continue;
|
|
||||||
|
|
||||||
if (challenge.isExpired()) {
|
|
||||||
if (!challenge.isChallengeAccepted()) Arrays.stream(challenge.getUsers())
|
|
||||||
.forEach(user -> warnUserExpiredChallenge(user, challenge.id()));
|
|
||||||
|
|
||||||
gameChallenges.remove(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void checkSubscriptions() {
|
|
||||||
if (subscriptions.isEmpty()) return;
|
|
||||||
|
|
||||||
List<String> keys = List.copyOf(subscriptions.keySet());
|
|
||||||
Random ran = new Random();
|
|
||||||
|
|
||||||
for (String key : keys) {
|
|
||||||
List<String> userNames = subscriptions.get(key);
|
|
||||||
if (userNames.size() < 2) continue;
|
|
||||||
|
|
||||||
while (userNames.size() > 1) {
|
|
||||||
int left = ran.nextInt(userNames.size());
|
|
||||||
int right;
|
|
||||||
do {
|
|
||||||
right = ran.nextInt(userNames.size());
|
|
||||||
} while (left == right);
|
|
||||||
|
|
||||||
String userLeft = userNames.get(left);
|
|
||||||
String userRight = userNames.get(right);
|
|
||||||
|
|
||||||
int first = Math.max(left, right);
|
|
||||||
int second = Math.min(left, right);
|
|
||||||
userNames.remove(first);
|
|
||||||
userNames.remove(second);
|
|
||||||
|
|
||||||
startGame(key, getUser(userLeft), getUser(userRight));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private NettyClient getUser(String username) {
|
|
||||||
return clientStore.all().stream().filter(e -> e.name().equalsIgnoreCase(username)).findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
private NettyClient getUser(long id) {
|
|
||||||
return clientStore.get(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void warnUserExpiredChallenge(NettyClient client, long challengeId) {
|
|
||||||
client.send("SVR GAME CHALLENGE CANCELLED {CHALLENGENUMBER: \"" + challengeId + "\"}" + "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isValidChallenge(GameChallenge gameChallenge) { // TODO move to challenge class
|
|
||||||
for (var user : gameChallenge.getUsers()) {
|
|
||||||
if (clientStore.get(user.id()) == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user.game() != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gameChallenge.isExpired()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.challenges.gamechallenge;
|
|
||||||
|
|
||||||
import org.toop.framework.SnowflakeGenerator;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
import org.toop.framework.utils.SimpleTimer;
|
|
||||||
|
|
||||||
public class GameChallenge {
|
|
||||||
private final long id = SnowflakeGenerator.nextId(); // I don't need this, but the tournament server uses it...
|
|
||||||
|
|
||||||
private final NettyClient from;
|
|
||||||
private final NettyClient to;
|
|
||||||
private final String gameType;
|
|
||||||
private final SimpleTimer timer;
|
|
||||||
|
|
||||||
private boolean isChallengeAccepted = false;
|
|
||||||
|
|
||||||
public GameChallenge(NettyClient from, NettyClient to, String gameType, SimpleTimer timer) {
|
|
||||||
this.from = from;
|
|
||||||
this.to = to;
|
|
||||||
this.gameType = gameType;
|
|
||||||
this.timer = timer;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long id() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public NettyClient[] getUsers() {
|
|
||||||
return new NettyClient[]{from, to};
|
|
||||||
}
|
|
||||||
|
|
||||||
public void forceExpire() {
|
|
||||||
timer.forceExpire();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String acceptChallenge() {
|
|
||||||
isChallengeAccepted = true;
|
|
||||||
timer.forceExpire();
|
|
||||||
return gameType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isChallengeAccepted() {
|
|
||||||
return isChallengeAccepted;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isExpired() {
|
|
||||||
return timer.isExpired();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.challenges.gamechallenge;
|
|
||||||
|
|
||||||
import org.toop.framework.utils.SimpleTimer;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
public class GameChallengeTimer implements SimpleTimer {
|
|
||||||
|
|
||||||
private final Instant createdAt;
|
|
||||||
private final Duration timeout;
|
|
||||||
|
|
||||||
private boolean isExpired = false;
|
|
||||||
|
|
||||||
public GameChallengeTimer(Duration duration) {
|
|
||||||
this.createdAt = Instant.now();
|
|
||||||
this.timeout = duration;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void forceExpire() {
|
|
||||||
this.isExpired = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isExpired() {
|
|
||||||
if (this.isExpired) return true;
|
|
||||||
return Instant.now().isAfter(createdAt.plus(timeout));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public long secondsRemaining() {
|
|
||||||
return Duration.between(Instant.now(), createdAt.plus(timeout)).toSeconds();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.client;
|
|
||||||
|
|
||||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
|
||||||
import org.toop.framework.utils.Pair;
|
|
||||||
|
|
||||||
public interface Client<G, P> {
|
|
||||||
long id();
|
|
||||||
|
|
||||||
String name();
|
|
||||||
void setName(String name);
|
|
||||||
|
|
||||||
OnlineTurnBasedGame game();
|
|
||||||
P player();
|
|
||||||
|
|
||||||
void addGame(Pair<G, P> gamePair);
|
|
||||||
void clearGame();
|
|
||||||
|
|
||||||
void send(String message);
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.client;
|
|
||||||
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import org.toop.framework.game.players.ServerPlayer;
|
|
||||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
|
||||||
import org.toop.framework.utils.Pair;
|
|
||||||
|
|
||||||
public class NettyClient implements Client<OnlineTurnBasedGame, ServerPlayer> {
|
|
||||||
final private long id;
|
|
||||||
private ChannelHandlerContext ctx;
|
|
||||||
private String name;
|
|
||||||
private Pair<OnlineTurnBasedGame, ServerPlayer> gamePair;
|
|
||||||
|
|
||||||
public NettyClient(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 addGame(Pair<OnlineTurnBasedGame, ServerPlayer> gamePair) {
|
|
||||||
if (this.gamePair == null) {
|
|
||||||
this.gamePair = gamePair;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void clearGame() {
|
|
||||||
this.gamePair = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public OnlineTurnBasedGame game() {
|
|
||||||
if (this.gamePair == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return this.gamePair.getLeft();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ServerPlayer player() {
|
|
||||||
return this.gamePair.getRight();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void send(String message) {
|
|
||||||
IO.println(message);
|
|
||||||
ctx.channel().writeAndFlush(message + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCtx(ChannelHandlerContext ctx) {
|
|
||||||
this.ctx = ctx;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.connectionHandler;
|
|
||||||
|
|
||||||
import org.toop.framework.networking.server.client.Client;
|
|
||||||
|
|
||||||
public interface ClientSession<G, P> {
|
|
||||||
Client<G, P> client();
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.connectionHandler;
|
|
||||||
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
|
||||||
import io.netty.channel.SimpleChannelInboundHandler;
|
|
||||||
import org.toop.framework.game.players.ServerPlayer;
|
|
||||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
import org.toop.framework.networking.server.handlers.Handler;
|
|
||||||
import org.toop.framework.networking.server.parsing.ParsedMessage;
|
|
||||||
import org.toop.framework.networking.server.Server;
|
|
||||||
import org.toop.framework.networking.server.client.Client;
|
|
||||||
import org.toop.framework.networking.server.parsing.Parser;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
public class NettyClientSession extends SimpleChannelInboundHandler<String> implements ClientSession<OnlineTurnBasedGame, ServerPlayer> {
|
|
||||||
|
|
||||||
private final NettyClient client;
|
|
||||||
private final Server server;
|
|
||||||
private final Handler<ParsedMessage> handler;
|
|
||||||
|
|
||||||
public NettyClientSession(NettyClient client, Server server, Handler<ParsedMessage> handler) {
|
|
||||||
this.client = client;
|
|
||||||
this.server = server;
|
|
||||||
this.handler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Client<OnlineTurnBasedGame, ServerPlayer> client() {
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelActive(ChannelHandlerContext ctx) {
|
|
||||||
ctx.writeAndFlush("Welcome " + client.id() + " please login" + "\n");
|
|
||||||
|
|
||||||
client.setCtx(ctx);
|
|
||||||
server.addClient(client); // TODO set correct name on login
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
|
|
||||||
|
|
||||||
IO.println(msg);
|
|
||||||
|
|
||||||
ParsedMessage p = Parser.parse(msg);
|
|
||||||
if (p == null) return;
|
|
||||||
|
|
||||||
IO.println(p.command() + " " + Arrays.toString(p.args()));
|
|
||||||
|
|
||||||
handler.handle(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
|
||||||
cause.printStackTrace();
|
|
||||||
ctx.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void channelInactive(ChannelHandlerContext ctx) {
|
|
||||||
server.removeClient(client);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.gateway;
|
|
||||||
|
|
||||||
public interface GatewayServer {
|
|
||||||
void start() throws Exception;
|
|
||||||
void stop();
|
|
||||||
int port();
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.gateway;
|
|
||||||
|
|
||||||
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.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
import org.toop.framework.networking.server.connectionHandler.NettyClientSession;
|
|
||||||
import org.toop.framework.networking.server.Server;
|
|
||||||
import org.toop.framework.networking.server.handlers.MessageHandler;
|
|
||||||
import org.toop.framework.networking.server.stores.NettyClientStore;
|
|
||||||
import org.toop.framework.networking.server.stores.TurnBasedGameStore;
|
|
||||||
import org.toop.framework.networking.server.stores.TurnBasedGameTypeStore;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
|
||||||
|
|
||||||
public class NettyGatewayServer implements GatewayServer {
|
|
||||||
private final int port;
|
|
||||||
private final Server gs;
|
|
||||||
|
|
||||||
ChannelFuture future;
|
|
||||||
EventLoopGroup bossGroup;
|
|
||||||
EventLoopGroup workerGroup;
|
|
||||||
|
|
||||||
public NettyGatewayServer(
|
|
||||||
int port,
|
|
||||||
TurnBasedGameTypeStore turnBasedGameTypeStore,
|
|
||||||
Duration challengeDuration
|
|
||||||
) {
|
|
||||||
this.port = port;
|
|
||||||
this.gs = new Server(
|
|
||||||
challengeDuration,
|
|
||||||
turnBasedGameTypeStore,
|
|
||||||
new NettyClientStore(new ConcurrentHashMap<>()),
|
|
||||||
new TurnBasedGameStore(new CopyOnWriteArrayList<>())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() throws InterruptedException {
|
|
||||||
|
|
||||||
bossGroup = new NioEventLoopGroup(1);
|
|
||||||
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();
|
|
||||||
NettyClient client = new NettyClient(userid, ""+userid);
|
|
||||||
pipeline.addLast(new NettyClientSession(client, gs, new MessageHandler(gs, client)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
future = bootstrap.bind(port).sync();
|
|
||||||
|
|
||||||
future.channel().closeFuture().sync();
|
|
||||||
} finally {
|
|
||||||
bossGroup.shutdownGracefully();
|
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void stop() {
|
|
||||||
if (future == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
future.channel().close();
|
|
||||||
bossGroup.shutdownGracefully();
|
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
|
|
||||||
future = null;
|
|
||||||
bossGroup = null;
|
|
||||||
workerGroup = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int port() {
|
|
||||||
return port;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.handlers;
|
|
||||||
|
|
||||||
public interface Handler<T> {
|
|
||||||
void handle(T message);
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.handlers;
|
|
||||||
|
|
||||||
import org.toop.framework.game.players.ServerPlayer;
|
|
||||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.Server;
|
|
||||||
import org.toop.framework.networking.server.client.Client;
|
|
||||||
import org.toop.framework.networking.server.parsing.ParsedMessage;
|
|
||||||
import org.toop.framework.utils.Utils;
|
|
||||||
|
|
||||||
public class MessageHandler implements Handler<ParsedMessage> {
|
|
||||||
|
|
||||||
private final Server server;
|
|
||||||
private final Client<OnlineTurnBasedGame, ServerPlayer> client;
|
|
||||||
|
|
||||||
public MessageHandler(Server server, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
this.server = server;
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handle(ParsedMessage message) {
|
|
||||||
switch (message.command()) {
|
|
||||||
case "ping" -> client.send("PONG");
|
|
||||||
case "login" -> handleLogin(message, client);
|
|
||||||
case "get" -> handleGet(message, client);
|
|
||||||
case "subscribe" -> handleSubscribe(message, client);
|
|
||||||
case "move" -> handleMove(message, client);
|
|
||||||
case "challenge" -> handleChallenge(message, client);
|
|
||||||
case "message" -> handleMessage(message, client);
|
|
||||||
case "help" -> handleHelp(message, client);
|
|
||||||
default -> client.send("ERROR Unknown command");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DO NOT INVERT
|
|
||||||
private boolean hasArgs(String... args) {
|
|
||||||
if (args.length < 1) {
|
|
||||||
client.send("ERR not enough arguments");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleLogin(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
if (!hasArgs(p.args())) return;
|
|
||||||
|
|
||||||
client.setName(p.args()[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleSubscribe(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
if (!hasArgs(p.args())) return;
|
|
||||||
|
|
||||||
server.subscribeClient(client.name(), p.args()[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleHelp(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleMessage(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleGet(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
if (!hasArgs(p.args())) return;
|
|
||||||
|
|
||||||
switch (p.args()[0]) {
|
|
||||||
case "playerlist" -> {
|
|
||||||
var names = server.onlineUsers().stream().map(Client::name).iterator();
|
|
||||||
client.send("SVR PLAYERLIST " + Utils.returnQuotedString(names));
|
|
||||||
}
|
|
||||||
case "gamelist" -> {
|
|
||||||
var names = server.gameTypes().stream().iterator();
|
|
||||||
client.send("SVR GAMELIST " + Utils.returnQuotedString(names));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleChallenge(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
if (!hasArgs(p.args())) return;
|
|
||||||
if (p.args().length < 2) return;
|
|
||||||
|
|
||||||
if (p.args()[0].equalsIgnoreCase("accept")) {
|
|
||||||
try {
|
|
||||||
long id = Long.parseLong(p.args()[1]);
|
|
||||||
|
|
||||||
if (id <= 0) {
|
|
||||||
client.send("ERR id must be a positive number");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.acceptChallenge(id);
|
|
||||||
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
client.send("ERR id is not a valid number or too big");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
server.challengeClient(client.name(), p.args()[0], p.args()[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleMove(ParsedMessage p, Client<OnlineTurnBasedGame, ServerPlayer> client) {
|
|
||||||
if(!hasArgs(p.args())) return;
|
|
||||||
|
|
||||||
// TODO check if not number
|
|
||||||
client.player().setMove(1L << Integer.parseInt(p.args()[0]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.parsing;
|
|
||||||
|
|
||||||
public record ParsedMessage(String command, String... args) {}
|
|
||||||
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.parsing;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Parser {
|
|
||||||
public static 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
import org.toop.framework.game.players.ServerPlayer;
|
|
||||||
import org.toop.framework.networking.server.OnlineTurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.client.Client;
|
|
||||||
|
|
||||||
public interface ClientStore<ID, T extends Client<OnlineTurnBasedGame, ServerPlayer>> extends Store<ID, T> {}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
public interface GameStore<T, K> extends Store<T, K> {}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
import org.toop.framework.networking.server.client.NettyClient;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class NettyClientStore implements ClientStore<Long, NettyClient> {
|
|
||||||
final private Map<Long, NettyClient> users;
|
|
||||||
|
|
||||||
public NettyClientStore(Map<Long, NettyClient> usersMap) {
|
|
||||||
this.users = usersMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void add(NettyClient adding) {
|
|
||||||
users.putIfAbsent(adding.id(), adding);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove(Long remover) {
|
|
||||||
users.remove(remover);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public NettyClient get(Long getter) {
|
|
||||||
return users.get(getter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<NettyClient> all() {
|
|
||||||
return users.values();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
public interface Store<IDENTIFIER, STORED> {
|
|
||||||
void add(STORED adding);
|
|
||||||
void remove(IDENTIFIER remover);
|
|
||||||
STORED get(IDENTIFIER getter);
|
|
||||||
Collection<STORED> all();
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
import org.toop.framework.networking.server.OnlineGame;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class TurnBasedGameStore implements GameStore<OnlineGame<TurnBasedGame>, OnlineGame<TurnBasedGame>> {
|
|
||||||
|
|
||||||
private List<OnlineGame<TurnBasedGame>> gameList;
|
|
||||||
|
|
||||||
public TurnBasedGameStore(List<OnlineGame<TurnBasedGame>> initGameList) {
|
|
||||||
this.gameList = initGameList;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void add(OnlineGame<TurnBasedGame> adding) {
|
|
||||||
gameList.addLast(adding);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void remove(OnlineGame<TurnBasedGame> remover) {
|
|
||||||
gameList.remove(remover);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public OnlineGame<TurnBasedGame> get(OnlineGame<TurnBasedGame> getter) {
|
|
||||||
return gameList.stream().filter(game->game.equals(getter)).findFirst().orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<OnlineGame<TurnBasedGame>> all() {
|
|
||||||
return gameList;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package org.toop.framework.networking.server.stores;
|
|
||||||
|
|
||||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
public class TurnBasedGameTypeStore {
|
|
||||||
|
|
||||||
private final Map<String, Supplier<? extends TurnBasedGame>> gameFactories = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
public TurnBasedGameTypeStore() {}
|
|
||||||
|
|
||||||
public void register(String key, Supplier<? extends TurnBasedGame> factory) {
|
|
||||||
gameFactories.put(key, factory);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregister(String key) {
|
|
||||||
gameFactories.remove(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TurnBasedGame create(String key) {
|
|
||||||
Supplier<? extends TurnBasedGame> factory = gameFactories.get(key);
|
|
||||||
if (factory == null) throw new IllegalArgumentException("Unknown game type: " + key);
|
|
||||||
return factory.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Supplier<? extends TurnBasedGame>> all() {
|
|
||||||
return Map.copyOf(gameFactories);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.networking.connection.types;
|
package org.toop.framework.networking.types;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package org.toop.framework.networking.connection.types;
|
package org.toop.framework.networking.types;
|
||||||
|
|
||||||
public record ServerCommand(long clientId, String command) {}
|
public record ServerCommand(long clientId, String command) {}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package org.toop.framework.networking.types;
|
||||||
|
|
||||||
|
public record ServerMessage(String message) {}
|
||||||
@@ -82,6 +82,10 @@ public class MusicAsset extends BaseResource implements LoadableResource, AudioR
|
|||||||
getMediaPlayer().pause();
|
getMediaPlayer().pause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getVolume() {
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
getMediaPlayer().stop();
|
getMediaPlayer().stop();
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package org.toop.framework.resource.resources;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.toop.framework.resource.ResourceManager;
|
||||||
|
import org.toop.framework.resource.ResourceMeta;
|
||||||
import org.toop.framework.settings.Settings;
|
import org.toop.framework.settings.Settings;
|
||||||
|
|
||||||
public class SettingsAsset extends JsonAsset<Settings> {
|
public class SettingsAsset extends JsonAsset<Settings> {
|
||||||
@@ -54,6 +57,10 @@ public class SettingsAsset extends JsonAsset<Settings> {
|
|||||||
return getContent().firstReversi;
|
return getContent().firstReversi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getSwitchReversi() {
|
||||||
|
return getContent().switchReversi;
|
||||||
|
}
|
||||||
|
|
||||||
public void setVolume(int volume) {
|
public void setVolume(int volume) {
|
||||||
getContent().volume = volume;
|
getContent().volume = volume;
|
||||||
save();
|
save();
|
||||||
@@ -108,4 +115,51 @@ public class SettingsAsset extends JsonAsset<Settings> {
|
|||||||
getContent().firstReversi = firstReversi;
|
getContent().firstReversi = firstReversi;
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSwitchReversi(boolean switchReversi) {
|
||||||
|
getContent().switchReversi = switchReversi;
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(Settings settings) {
|
||||||
|
setVolume(settings.volume);
|
||||||
|
setFxVolume(settings.fxVolume);
|
||||||
|
setMusicVolume(settings.musicVolume);
|
||||||
|
setLocale(settings.locale);
|
||||||
|
setFullscreen(settings.fullScreen);
|
||||||
|
setTheme(settings.theme);
|
||||||
|
setLayoutSize(settings.layoutSize);
|
||||||
|
setTutorialFlag(settings.showTutorials);
|
||||||
|
setFirstTTT(settings.firstTTT);
|
||||||
|
setFirstConnect4(settings.firstConnect4);
|
||||||
|
setFirstReversi(settings.firstReversi);
|
||||||
|
setSwitchReversi(settings.switchReversi);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SettingsAsset getPath() {
|
||||||
|
SettingsAsset settingsAsset;
|
||||||
|
try {
|
||||||
|
settingsAsset = ResourceManager.get("settings.json");
|
||||||
|
} catch (Exception e) {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
String basePath;
|
||||||
|
|
||||||
|
if (os.contains("win")) {
|
||||||
|
basePath = System.getenv("APPDATA");
|
||||||
|
if (basePath == null) {
|
||||||
|
basePath = System.getProperty("user.home");
|
||||||
|
}
|
||||||
|
} else if (os.contains("mac")) {
|
||||||
|
basePath = System.getProperty("user.home") + "/Library/Application Support";
|
||||||
|
} else {
|
||||||
|
basePath = System.getProperty("user.home") + "/.config";
|
||||||
|
}
|
||||||
|
|
||||||
|
File settingsFile =
|
||||||
|
new File(basePath + File.separator + "ISY1" + File.separator + "settings.json");
|
||||||
|
settingsAsset = new SettingsAsset(settingsFile);
|
||||||
|
ResourceManager.addAsset(new ResourceMeta<>("settings.json", settingsAsset));
|
||||||
|
}
|
||||||
|
return settingsAsset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,5 +12,5 @@ public class Settings {
|
|||||||
public Boolean firstReversi;
|
public Boolean firstReversi;
|
||||||
public Boolean firstTTT;
|
public Boolean firstTTT;
|
||||||
public Boolean firstConnect4;
|
public Boolean firstConnect4;
|
||||||
|
public boolean switchReversi = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
package org.toop.framework.utils;
|
|
||||||
|
|
||||||
public class ImmutablePair<T, K> implements Pair<T,K> {
|
|
||||||
final T left;
|
|
||||||
final K right;
|
|
||||||
|
|
||||||
public ImmutablePair(T left, K right) {
|
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getLeft() {
|
|
||||||
return left;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public K getRight() {
|
|
||||||
return right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package org.toop.framework.utils;
|
|
||||||
|
|
||||||
public class MutablePair<T, K> implements Pair<T,K> {
|
|
||||||
T left;
|
|
||||||
K right;
|
|
||||||
|
|
||||||
public MutablePair(T left, K right) {
|
|
||||||
this.left = left;
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public T getLeft() {
|
|
||||||
return left;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLeft(T left) {
|
|
||||||
this.left = left;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public K getRight() {
|
|
||||||
return right;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRight(K right) {
|
|
||||||
this.right = right;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
package org.toop.framework.utils;
|
|
||||||
|
|
||||||
public interface Pair<T, K> {
|
|
||||||
T getLeft();
|
|
||||||
K getRight();
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user