mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Compare commits
44 Commits
ReversiML
...
b2e4e7d810
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2e4e7d810 | ||
|
|
4b99cfe6af | ||
|
|
484cf1dcd3 | ||
|
|
5ee2f2187e | ||
|
|
854744cefa | ||
|
|
f3e2de2c8f | ||
|
|
b6f1b046b1 | ||
|
|
43a39295dd | ||
|
|
e5ce3aa2d9 | ||
|
|
ad7831b0e1 | ||
|
|
4ef0d53d3c | ||
|
|
ea82ccae45 | ||
|
|
990cf1675c | ||
|
|
d91e3a7457 | ||
|
|
fd357fd27d | ||
|
|
816a0b953e | ||
|
|
b0c7c74346 | ||
|
|
ea179eb0e2 | ||
|
|
61f03fab26 | ||
|
|
2fbda6f14d | ||
|
|
0d564283fb | ||
|
|
f24ca88246 | ||
|
|
c8e2c3747e | ||
|
|
8849515af6 | ||
|
|
1b2733c0be | ||
|
|
f205669b41 | ||
|
|
4608135ee5 | ||
|
|
27a35c8117 | ||
|
|
b1224ba6af | ||
|
|
a7b9484aa4 | ||
|
|
75c4e55da6 | ||
|
|
d5223c6cd1 | ||
|
|
730bd5c3dc | ||
|
|
296641b82e | ||
|
|
8dccabe37b | ||
|
|
dd73d1810a | ||
|
|
e2be973c9a | ||
|
|
75af655b49 | ||
|
|
c6d95479af | ||
|
|
72d5989d24 | ||
|
|
4ea458c92e | ||
|
|
134c9a2fd8 | ||
|
|
a00d25f24a | ||
|
|
406ad713f4 |
@@ -1,13 +1,6 @@
|
|||||||
package org.toop;
|
package org.toop;
|
||||||
|
|
||||||
import org.toop.app.App;
|
import org.toop.app.App;
|
||||||
import org.toop.framework.audio.*;
|
|
||||||
import org.toop.framework.networking.NetworkingClientEventListener;
|
|
||||||
import org.toop.framework.networking.NetworkingClientManager;
|
|
||||||
import org.toop.framework.resource.ResourceLoader;
|
|
||||||
import org.toop.framework.resource.ResourceManager;
|
|
||||||
import org.toop.framework.resource.resources.MusicAsset;
|
|
||||||
import org.toop.framework.resource.resources.SoundEffectAsset;
|
|
||||||
|
|
||||||
public final class Main {
|
public final class Main {
|
||||||
static void main(String[] args) {
|
static void main(String[] args) {
|
||||||
|
|||||||
@@ -1,18 +1,20 @@
|
|||||||
package org.toop.app;
|
package org.toop.app;
|
||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.input.KeyCode;
|
||||||
import org.toop.Main;
|
import javafx.scene.input.KeyEvent;
|
||||||
|
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
import org.toop.app.widget.Widget;
|
|
||||||
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;
|
||||||
import org.toop.app.widget.display.SongDisplay;
|
import org.toop.app.widget.display.SongDisplay;
|
||||||
|
import org.toop.app.widget.popup.EscapePopup;
|
||||||
import org.toop.app.widget.popup.QuitPopup;
|
import org.toop.app.widget.popup.QuitPopup;
|
||||||
import org.toop.app.widget.view.MainView;
|
import org.toop.app.widget.view.MainView;
|
||||||
import org.toop.framework.audio.*;
|
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.networking.NetworkingClientEventListener;
|
import org.toop.framework.networking.NetworkingClientEventListener;
|
||||||
import org.toop.framework.networking.NetworkingClientManager;
|
import org.toop.framework.networking.NetworkingClientManager;
|
||||||
import org.toop.framework.resource.ResourceLoader;
|
import org.toop.framework.resource.ResourceLoader;
|
||||||
@@ -30,6 +32,8 @@ import javafx.scene.Scene;
|
|||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public final class App extends Application {
|
public final class App extends Application {
|
||||||
private static Stage stage;
|
private static Stage stage;
|
||||||
private static Scene scene;
|
private static Scene scene;
|
||||||
@@ -37,8 +41,6 @@ public final class App extends Application {
|
|||||||
private static int height;
|
private static int height;
|
||||||
private static int width;
|
private static int width;
|
||||||
|
|
||||||
private static boolean isQuitting;
|
|
||||||
|
|
||||||
public static void run(String[] args) {
|
public static void run(String[] args) {
|
||||||
launch(args);
|
launch(args);
|
||||||
}
|
}
|
||||||
@@ -78,10 +80,10 @@ public final class App extends Application {
|
|||||||
App.width = (int)stage.getWidth();
|
App.width = (int)stage.getWidth();
|
||||||
App.height = (int)stage.getHeight();
|
App.height = (int)stage.getHeight();
|
||||||
|
|
||||||
App.isQuitting = false;
|
|
||||||
|
|
||||||
AppSettings.applySettings();
|
AppSettings.applySettings();
|
||||||
|
|
||||||
|
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
|
||||||
);
|
);
|
||||||
@@ -130,6 +132,33 @@ public final class App extends Application {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setKeybinds(StackPane root) {
|
||||||
|
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
||||||
|
if (event.getCode() == KeyCode.ESCAPE) {
|
||||||
|
escapePopup();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void escapePopup() {
|
||||||
|
|
||||||
|
if ( WidgetContainer.getCurrentView() == null
|
||||||
|
|| WidgetContainer.getCurrentView() instanceof MainView) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Objects.requireNonNull(
|
||||||
|
WidgetContainer.find(widget -> widget instanceof QuitPopup || widget instanceof EscapePopup)
|
||||||
|
).isEmpty()) {
|
||||||
|
WidgetContainer.removeFirst(QuitPopup.class);
|
||||||
|
WidgetContainer.removeFirst(EscapePopup.class);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EscapePopup escPopup = new EscapePopup();
|
||||||
|
escPopup.show(Pos.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
private void setOnLoadingSuccess(LoadingWidget loading) {
|
private void setOnLoadingSuccess(LoadingWidget loading) {
|
||||||
loading.setOnSuccess(() -> {
|
loading.setOnSuccess(() -> {
|
||||||
initSystems();
|
initSystems();
|
||||||
@@ -140,17 +169,29 @@ public final class App extends Application {
|
|||||||
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
|
WidgetContainer.add(Pos.BOTTOM_RIGHT, new SongDisplay());
|
||||||
stage.setOnCloseRequest(event -> {
|
stage.setOnCloseRequest(event -> {
|
||||||
event.consume();
|
event.consume();
|
||||||
startQuit();
|
|
||||||
|
if (WidgetContainer.getAllWidgets().stream().anyMatch(e -> e instanceof QuitPopup)) return;
|
||||||
|
|
||||||
|
QuitPopup a = new QuitPopup();
|
||||||
|
a.show(Pos.CENTER);
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initSystems() { // TODO Move to better place
|
private void initSystems() { // TODO Move to better place
|
||||||
new Thread(() -> new NetworkingClientEventListener(new NetworkingClientManager())).start();
|
new Thread(() -> new NetworkingClientEventListener(
|
||||||
|
GlobalEventBus.get(),
|
||||||
|
new NetworkingClientManager(GlobalEventBus.get()))
|
||||||
|
).start();
|
||||||
|
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
MusicManager<MusicAsset> musicManager =
|
MusicManager<MusicAsset> musicManager =
|
||||||
new MusicManager<>(ResourceManager.getAllOfTypeAndRemoveWrapper(MusicAsset.class), true);
|
new MusicManager<>(
|
||||||
|
GlobalEventBus.get(),
|
||||||
|
ResourceManager.getAllOfTypeAndRemoveWrapper(MusicAsset.class),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
SoundEffectManager<SoundEffectAsset> soundEffectManager =
|
SoundEffectManager<SoundEffectAsset> soundEffectManager =
|
||||||
new SoundEffectManager<>(ResourceManager.getAllOfType(SoundEffectAsset.class));
|
new SoundEffectManager<>(ResourceManager.getAllOfType(SoundEffectAsset.class));
|
||||||
@@ -162,6 +203,7 @@ public final class App extends Application {
|
|||||||
.registerManager(VolumeControl.MUSIC, musicManager);
|
.registerManager(VolumeControl.MUSIC, musicManager);
|
||||||
|
|
||||||
new AudioEventListener<>(
|
new AudioEventListener<>(
|
||||||
|
GlobalEventBus.get(),
|
||||||
musicManager,
|
musicManager,
|
||||||
soundEffectManager,
|
soundEffectManager,
|
||||||
audioVolumeManager
|
audioVolumeManager
|
||||||
@@ -177,19 +219,6 @@ public final class App extends Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startQuit() {
|
|
||||||
if (isQuitting) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
WidgetContainer.add(Pos.CENTER, new QuitPopup());
|
|
||||||
isQuitting = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void stopQuit() {
|
|
||||||
isQuitting = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void quit() {
|
public static void quit() {
|
||||||
stage.close();
|
stage.close();
|
||||||
System.exit(0); // TODO: This is like dropping a nuke
|
System.exit(0); // TODO: This is like dropping a nuke
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.toop.app.widget.popup.ErrorPopup;
|
|||||||
import org.toop.app.widget.popup.SendChallengePopup;
|
import org.toop.app.widget.popup.SendChallengePopup;
|
||||||
import org.toop.app.widget.view.ServerView;
|
import org.toop.app.widget.view.ServerView;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
import org.toop.framework.networking.clients.TournamentNetworkingClient;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
@@ -32,7 +33,6 @@ 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 {
|
||||||
// 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;
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ public final class Server {
|
|||||||
|
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
|
|
||||||
private EventFlow eventFlow = new EventFlow();
|
private EventFlow connectFlow;
|
||||||
|
|
||||||
public static GameInformation.Type gameToType(String game) {
|
public static GameInformation.Type gameToType(String game) {
|
||||||
if (game.equalsIgnoreCase("tic-tac-toe")) {
|
if (game.equalsIgnoreCase("tic-tac-toe")) {
|
||||||
@@ -89,78 +89,85 @@ public final class Server {
|
|||||||
Primitive.text("connecting"), 0, 0, reconnectAttempts, true, true
|
Primitive.text("connecting"), 0, 0, reconnectAttempts, true, true
|
||||||
);
|
);
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionNext(loading);
|
WidgetContainer.getCurrentView().transitionNextCustom(loading, "disconnect", this::disconnect);
|
||||||
|
|
||||||
var a = new EventFlow()
|
var a = new EventFlow()
|
||||||
.addPostEvent(NetworkEvents.StartClient.class,
|
.addPostEvent(NetworkEvents.StartClient.class,
|
||||||
new TournamentNetworkingClient(),
|
new TournamentNetworkingClient(GlobalEventBus.get()),
|
||||||
new NetworkingConnector(ip, parsedPort, reconnectAttempts, 1, TimeUnit.SECONDS)
|
new NetworkingConnector(ip, parsedPort, reconnectAttempts, 1, TimeUnit.SECONDS)
|
||||||
);
|
);
|
||||||
|
|
||||||
loading.setOnFailure(() -> {
|
loading.setOnFailure(() -> {
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
if (WidgetContainer.getCurrentView() == loading) WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
a.unsubscribe("connecting");
|
a.unsubscribeAll();
|
||||||
a.unsubscribe("startclient");
|
|
||||||
WidgetContainer.add(
|
WidgetContainer.add(
|
||||||
Pos.CENTER,
|
Pos.CENTER,
|
||||||
new ErrorPopup(AppContext.getString("connecting-failed") + " " + ip + ":" + port)
|
new ErrorPopup(AppContext.getString("connecting-failed") + " " + ip + ":" + port)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
a.onResponse(NetworkEvents.CreatedIdForClient.class, e -> clientId = e.clientId(), true);
|
||||||
|
|
||||||
|
a.onResponse(NetworkEvents.StartClientResponse.class, e -> {
|
||||||
if (!e.successful()) {
|
if (!e.successful()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WidgetContainer.getCurrentView().transitionPrevious();
|
primary = new ServerView(user, this::sendChallenge);
|
||||||
|
WidgetContainer.getCurrentView().transitionNextCustom(primary, "disconnect", this::disconnect);
|
||||||
|
|
||||||
a.unsubscribe("connecting");
|
a.unsubscribe("connecting");
|
||||||
a.unsubscribe("startclient");
|
a.unsubscribe("startclient");
|
||||||
|
|
||||||
this.user = user;
|
this.user = user;
|
||||||
clientId = e.clientId();
|
|
||||||
|
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.SendLogin(clientId, user)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.SendLogin(clientId, user)).postEvent();
|
||||||
|
|
||||||
primary = new ServerView(user, this::sendChallenge, this::disconnect);
|
|
||||||
WidgetContainer.getCurrentView().transitionNext(primary);
|
|
||||||
|
|
||||||
startPopulateScheduler();
|
startPopulateScheduler();
|
||||||
populateGameList();
|
populateGameList();
|
||||||
|
|
||||||
|
primary.removeViewFromPreviousChain(loading);
|
||||||
|
|
||||||
}, false, "startclient")
|
}, false, "startclient")
|
||||||
.listen(
|
.listen(
|
||||||
NetworkEvents.ConnectTry.class,
|
NetworkEvents.ConnectTry.class,
|
||||||
e -> Platform.runLater(
|
e -> {
|
||||||
() -> {
|
if (clientId != e.clientId()) return;
|
||||||
try {
|
Platform.runLater(
|
||||||
loading.setAmount(e.amount());
|
() -> {
|
||||||
if (e.amount() >= loading.getMaxAmount()) {
|
try {
|
||||||
loading.triggerFailure();
|
loading.setAmount(e.amount());
|
||||||
}
|
if (e.amount() >= loading.getMaxAmount()) {
|
||||||
} catch (Exception ex) {
|
loading.triggerFailure();
|
||||||
throw new RuntimeException(ex);
|
}
|
||||||
}
|
} catch (Exception ex) {
|
||||||
}
|
throw new RuntimeException(ex);
|
||||||
),
|
}
|
||||||
false, "connecting"
|
}
|
||||||
)
|
);
|
||||||
|
},
|
||||||
|
false, "connecting"
|
||||||
|
)
|
||||||
.postEvent();
|
.postEvent();
|
||||||
|
|
||||||
eventFlow.listen(NetworkEvents.ChallengeResponse.class, this::handleReceivedChallenge, false)
|
a.listen(NetworkEvents.ChallengeResponse.class, this::handleReceivedChallenge, false, "challenge")
|
||||||
.listen(NetworkEvents.GameMatchResponse.class, this::handleMatchResponse, false)
|
.listen(NetworkEvents.GameMatchResponse.class, this::handleMatchResponse, false, "match-response")
|
||||||
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false)
|
.listen(NetworkEvents.GameResultResponse.class, this::handleGameResult, false, "game-result")
|
||||||
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false)
|
.listen(NetworkEvents.GameMoveResponse.class, this::handleReceivedMove, false, "game-move")
|
||||||
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false);
|
.listen(NetworkEvents.YourTurnResponse.class, this::handleYourTurn, false, "your-turn");
|
||||||
|
|
||||||
|
connectFlow = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendChallenge(String opponent) {
|
private void sendChallenge(String opponent) {
|
||||||
if (!isPolling) return;
|
if (!isPolling) return;
|
||||||
|
|
||||||
new SendChallengePopup(this, opponent, (playerInformation, gameType) -> {
|
var a = new SendChallengePopup(this, opponent, (playerInformation, gameType) -> {
|
||||||
new EventFlow().addPostEvent(new NetworkEvents.SendChallenge(clientId, opponent, gameType)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.SendChallenge(clientId, opponent, gameType)).postEvent();
|
||||||
isSingleGame.set(true);
|
isSingleGame.set(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
a.show(Pos.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMatchResponse(NetworkEvents.GameMatchResponse response) {
|
private void handleMatchResponse(NetworkEvents.GameMatchResponse response) {
|
||||||
@@ -200,7 +207,7 @@ public final class Server {
|
|||||||
|
|
||||||
switch (type){
|
switch (type){
|
||||||
case TICTACTOE ->{
|
case TICTACTOE ->{
|
||||||
players[myTurn] = new ArtificialPlayer<>(new TicTacToeAIR(), user);
|
players[myTurn] = new ArtificialPlayer<>(new TicTacToeAIR(9), user);
|
||||||
}
|
}
|
||||||
case REVERSI ->{
|
case REVERSI ->{
|
||||||
players[myTurn] = new ArtificialPlayer<>(new ReversiAIR(), user);
|
players[myTurn] = new ArtificialPlayer<>(new ReversiAIR(), user);
|
||||||
@@ -250,11 +257,13 @@ public final class Server {
|
|||||||
String challengerName = extractQuotedValue(response.challengerName());
|
String challengerName = extractQuotedValue(response.challengerName());
|
||||||
String gameType = extractQuotedValue(response.gameType());
|
String gameType = extractQuotedValue(response.gameType());
|
||||||
final String finalGameType = gameType;
|
final String finalGameType = gameType;
|
||||||
new ChallengePopup(challengerName, gameType, (playerInformation) -> {
|
var a = new ChallengePopup(challengerName, gameType, (playerInformation) -> {
|
||||||
final int challengeId = Integer.parseInt(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);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
a.show(Pos.CENTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMessage(String message) {
|
private void sendMessage(String message) {
|
||||||
@@ -265,7 +274,9 @@ public final class Server {
|
|||||||
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
new EventFlow().addPostEvent(new NetworkEvents.CloseClient(clientId)).postEvent();
|
||||||
isPolling = false;
|
isPolling = false;
|
||||||
stopScheduler();
|
stopScheduler();
|
||||||
primary.transitionPrevious();
|
connectFlow.unsubscribeAll();
|
||||||
|
|
||||||
|
WidgetContainer.getCurrentView().transitionPrevious();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void forfeitGame() {
|
private void forfeitGame() {
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public class ReversiController extends AbstractGameController<ReversiR> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
animation.play();
|
animation.play();
|
||||||
primary.nextPlayer(true, getCurrentPlayer().getName(), game.getCurrentTurn() == 0 ? "X" : "O", getPlayer((game.getCurrentTurn() + 1) % 2).getName());
|
primary.nextPlayer(true, getCurrentPlayer().getName(), game.getCurrentTurn() == 0 ? "X" : "O", getPlayer((game.getCurrentTurn() + 1) % 2).getName(), 'R');
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public class TicTacToeController extends AbstractGameController<TicTacToeR> {
|
|||||||
public void updateUI() {
|
public void updateUI() {
|
||||||
canvas.clearAll();
|
canvas.clearAll();
|
||||||
// TODO: wtf is even this pile of poop temp fix
|
// TODO: wtf is even this pile of poop temp fix
|
||||||
primary.nextPlayer(true, getCurrentPlayer().getName(), game.getCurrentTurn() == 0 ? "X" : "O", getPlayer((game.getCurrentTurn() + 1) % 2).getName());
|
primary.nextPlayer(true, getCurrentPlayer().getName(), game.getCurrentTurn() == 0 ? "X" : "O", getPlayer((game.getCurrentTurn() + 1) % 2).getName(), 'T');
|
||||||
drawMoves();
|
drawMoves();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,19 +2,27 @@ package org.toop.app.widget;
|
|||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
public interface Widget {
|
public interface Widget {
|
||||||
|
Logger logger = LogManager.getLogger(Widget.class);
|
||||||
|
|
||||||
Node getNode();
|
Node getNode();
|
||||||
|
|
||||||
default void show(Pos position) {
|
default void show(Pos position) {
|
||||||
|
logger.debug("Showing Widget: {} at position: {}", this.getNode(), position.toString());
|
||||||
WidgetContainer.add(position, this);
|
WidgetContainer.add(position, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
default void hide() {
|
default void hide() {
|
||||||
|
logger.debug("Hiding Widget: {}", this.getNode());
|
||||||
WidgetContainer.remove(this);
|
WidgetContainer.remove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
default void replace(Pos position, Widget widget) {
|
default void replace(Pos position, Widget widget) {
|
||||||
|
logger.debug("Replacing Widget: {}, with widget: {}, to position: {}",
|
||||||
|
this.getNode(), widget.getNode(), position.toString());
|
||||||
widget.show(position);
|
widget.show(position);
|
||||||
hide();
|
hide();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package org.toop.app.widget;
|
package org.toop.app.widget;
|
||||||
|
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.scene.Node;
|
||||||
import org.toop.app.widget.complex.PopupWidget;
|
import org.toop.app.widget.complex.PopupWidget;
|
||||||
import org.toop.app.widget.complex.ViewWidget;
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
|
|
||||||
@@ -7,6 +9,10 @@ import javafx.application.Platform;
|
|||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public final class WidgetContainer {
|
public final class WidgetContainer {
|
||||||
private static StackPane root;
|
private static StackPane root;
|
||||||
private static ViewWidget currentView;
|
private static ViewWidget currentView;
|
||||||
@@ -38,7 +44,7 @@ public final class WidgetContainer {
|
|||||||
root.getChildren().addFirst(view.getNode());
|
root.getChildren().addFirst(view.getNode());
|
||||||
currentView = view;
|
currentView = view;
|
||||||
} else if (widget instanceof PopupWidget popup) {
|
} else if (widget instanceof PopupWidget popup) {
|
||||||
currentView.add(Pos.CENTER, popup);
|
currentView.add(Pos.CENTER, (Widget) popup);
|
||||||
} else {
|
} else {
|
||||||
root.getChildren().add(widget.getNode());
|
root.getChildren().add(widget.getNode());
|
||||||
}
|
}
|
||||||
@@ -52,13 +58,61 @@ public final class WidgetContainer {
|
|||||||
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
if (widget instanceof PopupWidget popup) {
|
if (widget instanceof PopupWidget popup) {
|
||||||
currentView.remove(popup);
|
currentView.remove((Widget) popup);
|
||||||
} else {
|
} else {
|
||||||
root.getChildren().remove(widget.getNode());
|
root.getChildren().remove(widget.getNode());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void remove(Class<? extends Widget> widgetClass) {
|
||||||
|
if (root == null || currentView == null) return;
|
||||||
|
|
||||||
|
Platform.runLater(() ->
|
||||||
|
currentView.getChildren().removeIf(widget -> widget.getClass().isAssignableFrom(widgetClass))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void removeFirst(Class<? extends Widget> widgetClass) {
|
||||||
|
if (root == null || currentView == null) return;
|
||||||
|
|
||||||
|
Platform.runLater(() -> {
|
||||||
|
for (Node widget : currentView.getChildren()) {
|
||||||
|
if (widgetClass.isAssignableFrom(widget.getClass())) {
|
||||||
|
currentView.getChildren().remove(widget);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Widget> find(Class<? extends Widget> widgetClass) {
|
||||||
|
if (root == null || currentView == null) return null;
|
||||||
|
|
||||||
|
return getAllWidgets()
|
||||||
|
.stream()
|
||||||
|
.filter(widget -> widget.getClass().isAssignableFrom(widgetClass))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Widget> find(Predicate<Widget> predicate) {
|
||||||
|
if (root == null || currentView == null) return null;
|
||||||
|
|
||||||
|
return getAllWidgets()
|
||||||
|
.stream()
|
||||||
|
.filter(predicate)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Widget findFirst(Class<? extends Widget> widgetClass) {
|
||||||
|
if (root == null || currentView == null) return null;
|
||||||
|
|
||||||
|
return getAllWidgets()
|
||||||
|
.stream()
|
||||||
|
.filter(widget -> widget.getClass().isAssignableFrom(widgetClass))
|
||||||
|
.findFirst().orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
public static ViewWidget getCurrentView() {
|
public static ViewWidget getCurrentView() {
|
||||||
return currentView;
|
return currentView;
|
||||||
}
|
}
|
||||||
@@ -74,4 +128,22 @@ public final class WidgetContainer {
|
|||||||
currentView = view;
|
currentView = view;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Widget> getAllWidgets() {
|
||||||
|
final List<Widget> children = new ArrayList<>();
|
||||||
|
|
||||||
|
for (var child : root.getChildren()) {
|
||||||
|
if (child instanceof Widget widget) {
|
||||||
|
children.add(widget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var child : currentView.getNode().getChildren()) {
|
||||||
|
if (child instanceof Widget widget) {
|
||||||
|
children.add(widget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return children;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package org.toop.app.widget.complex;
|
|||||||
|
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
|
import javafx.scene.Node;
|
||||||
import javafx.scene.control.Control;
|
import javafx.scene.control.Control;
|
||||||
import javafx.scene.control.ProgressBar;
|
import javafx.scene.control.ProgressBar;
|
||||||
import javafx.scene.control.ProgressIndicator;
|
import javafx.scene.control.ProgressIndicator;
|
||||||
@@ -144,11 +145,11 @@ public class LoadingWidget extends ViewWidget implements Update { // TODO make o
|
|||||||
|
|
||||||
if (successTrigger.call()) {
|
if (successTrigger.call()) {
|
||||||
triggerSuccess();
|
triggerSuccess();
|
||||||
this.remove(this);
|
this.remove((Node) this);
|
||||||
return;
|
return;
|
||||||
} else if (failureTrigger.call()) {
|
} else if (failureTrigger.call()) {
|
||||||
triggerFailure();
|
triggerFailure();
|
||||||
this.remove(this);
|
this.remove((Node) this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ import org.toop.app.widget.Primitive;
|
|||||||
|
|
||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
import javafx.scene.text.Text;
|
||||||
|
|
||||||
public class PlayerInfoWidget {
|
public class PlayerInfoWidget {
|
||||||
private final GameInformation.Player information;
|
private final GameInformation.Player information;
|
||||||
private final VBox container;
|
private final VBox container;
|
||||||
|
private Text playerName;
|
||||||
|
private boolean hasSet;
|
||||||
|
|
||||||
public PlayerInfoWidget(GameInformation.Player information) {
|
public PlayerInfoWidget(GameInformation.Player information) {
|
||||||
this.information = information;
|
this.information = information;
|
||||||
@@ -16,6 +19,7 @@ public class PlayerInfoWidget {
|
|||||||
buildToggle().getNode(),
|
buildToggle().getNode(),
|
||||||
buildContent()
|
buildContent()
|
||||||
);
|
);
|
||||||
|
this.playerName = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ToggleWidget buildToggle() {
|
private ToggleWidget buildToggle() {
|
||||||
@@ -33,51 +37,69 @@ public class PlayerInfoWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Node buildContent() {
|
private Node buildContent() {
|
||||||
if (information.isHuman) {
|
if (information.isHuman) {
|
||||||
var nameInput = new LabeledInputWidget(
|
var nameInput = new LabeledInputWidget(
|
||||||
"name",
|
"name",
|
||||||
"enter-your-name",
|
"enter-your-name",
|
||||||
information.name,
|
information.name,
|
||||||
newName -> information.name = newName
|
newName -> information.name = newName
|
||||||
);
|
);
|
||||||
|
|
||||||
return nameInput.getNode();
|
return nameInput.getNode();
|
||||||
} else {
|
} else {
|
||||||
if (information.name == null || information.name.isEmpty()) {
|
var AIBox = Primitive.vbox(
|
||||||
information.name = "Pism Bot";
|
makeAIButton(0, 1, "zwartepiet"),
|
||||||
}
|
makeAIButton(2, 1, "sinterklaas"),
|
||||||
|
makeAIButton(9, 1, "santa")
|
||||||
|
);
|
||||||
|
|
||||||
var playerName = Primitive.text("");
|
this.playerName = Primitive.text("");
|
||||||
playerName.setText(information.name);
|
playerName.setText(information.name);
|
||||||
|
|
||||||
var nameDisplay = Primitive.vbox(
|
var nameDisplay = Primitive.vbox(
|
||||||
Primitive.text("name"),
|
Primitive.text("name"),
|
||||||
playerName
|
playerName
|
||||||
);
|
);
|
||||||
|
|
||||||
var difficultySlider = new LabeledSliderWidget(
|
if (!hasSet) {
|
||||||
"computer-difficulty",
|
doDefault();
|
||||||
0, 5,
|
hasSet = true;
|
||||||
information.computerDifficulty,
|
}
|
||||||
newVal -> information.computerDifficulty = newVal
|
|
||||||
);
|
|
||||||
|
|
||||||
var thinkTimeSlider = new LabeledSliderWidget(
|
return Primitive.vbox(
|
||||||
"computer-think-time",
|
AIBox,
|
||||||
0, 5,
|
nameDisplay
|
||||||
information.computerThinkTime,
|
);
|
||||||
newVal -> information.computerThinkTime = newVal
|
|
||||||
);
|
|
||||||
|
|
||||||
return Primitive.vbox(
|
}
|
||||||
nameDisplay,
|
}
|
||||||
difficultySlider.getNode(),
|
|
||||||
thinkTimeSlider.getNode()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node getNode() {
|
public Node getNode() {
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Node makeAIButton(int depth, int thinktime, String name) {
|
||||||
|
return Primitive.button(name, () -> {
|
||||||
|
information.name = getName(name);
|
||||||
|
information.computerDifficulty = depth;
|
||||||
|
information.computerThinkTime = thinktime;
|
||||||
|
this.playerName.setText(getName(name));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getName(String name) {
|
||||||
|
return switch (name) {
|
||||||
|
case "sinterklaas" -> "Sint. R. Klaas";
|
||||||
|
case "zwartepiet" -> "Zwarte Piet";
|
||||||
|
case "santa" -> "Santa";
|
||||||
|
default -> "Default";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doDefault() {
|
||||||
|
information.name = getName("zwartepiet");
|
||||||
|
information.computerDifficulty = 0;
|
||||||
|
information.computerThinkTime = 1;
|
||||||
|
this.playerName.setText(getName("zwartepiet"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -7,22 +7,19 @@ import javafx.geometry.Pos;
|
|||||||
import javafx.scene.Node;
|
import javafx.scene.Node;
|
||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
|
|
||||||
public abstract class StackWidget implements Widget {
|
public abstract class StackWidget extends StackPane implements Widget {
|
||||||
private final StackPane container;
|
|
||||||
|
|
||||||
public StackWidget(String cssClass) {
|
public StackWidget(String cssClass) {
|
||||||
container = new StackPane();
|
this.getStyleClass().add(cssClass);
|
||||||
container.getStyleClass().add(cssClass);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(Pos position, Node node) {
|
public void add(Pos position, Node node) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
if (container.getChildren().contains(node)) {
|
if (this.getChildren().contains(node)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
StackPane.setAlignment(node, position);
|
StackPane.setAlignment(node, position);
|
||||||
container.getChildren().add(node);
|
this.getChildren().add(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +29,7 @@ public abstract class StackWidget implements Widget {
|
|||||||
|
|
||||||
public void remove(Node node) {
|
public void remove(Node node) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
container.getChildren().remove(node);
|
this.getChildren().remove(node);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +38,7 @@ public abstract class StackWidget implements Widget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Node getNode() {
|
public StackPane getNode() {
|
||||||
return container;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,6 +37,19 @@ public abstract class ViewWidget extends StackWidget {
|
|||||||
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(backButton));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void transitionNextCustom(ViewWidget view, String key, Runnable runnable) {
|
||||||
|
view.previous = this;
|
||||||
|
|
||||||
|
replace(Pos.CENTER, view);
|
||||||
|
|
||||||
|
var customButton = Primitive.button(key, () -> {
|
||||||
|
runnable.run();
|
||||||
|
view.transitionPrevious();
|
||||||
|
});
|
||||||
|
|
||||||
|
view.add(Pos.BOTTOM_LEFT, Primitive.vbox(customButton));
|
||||||
|
}
|
||||||
|
|
||||||
public void transitionPrevious() {
|
public void transitionPrevious() {
|
||||||
if (previous == null) {
|
if (previous == null) {
|
||||||
return;
|
return;
|
||||||
@@ -46,6 +59,38 @@ public abstract class ViewWidget extends StackWidget {
|
|||||||
previous = null;
|
previous = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeIndexFromPreviousChain(int index) {
|
||||||
|
ViewWidget view = this;
|
||||||
|
|
||||||
|
while (index > 0 && view != null) {
|
||||||
|
index--;
|
||||||
|
|
||||||
|
if (index == 0) {
|
||||||
|
if (view.previous != null && view.previous.previous != null) {
|
||||||
|
view.previous = view.previous.previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
view = view.previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeViewFromPreviousChain(ViewWidget view) {
|
||||||
|
ViewWidget prev = previous;
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
while (prev != null) {
|
||||||
|
index++;
|
||||||
|
|
||||||
|
if (prev == view) {
|
||||||
|
removeIndexFromPreviousChain(index);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev = prev.previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void reload(ViewWidget view) {
|
public void reload(ViewWidget view) {
|
||||||
view.previous = previous;
|
view.previous = previous;
|
||||||
replace(Pos.CENTER, view);
|
replace(Pos.CENTER, view);
|
||||||
|
|||||||
@@ -49,11 +49,11 @@ public class SongDisplay extends VBox implements Widget {
|
|||||||
previousButton.getStyleClass().setAll("previous-button");
|
previousButton.getStyleClass().setAll("previous-button");
|
||||||
|
|
||||||
skipButton.setOnAction( event -> {
|
skipButton.setOnAction( event -> {
|
||||||
GlobalEventBus.post(new AudioEvents.SkipMusic());
|
GlobalEventBus.get().post(new AudioEvents.SkipMusic());
|
||||||
});
|
});
|
||||||
|
|
||||||
pauseButton.setOnAction(event -> {
|
pauseButton.setOnAction(event -> {
|
||||||
GlobalEventBus.post(new AudioEvents.PauseMusic());
|
GlobalEventBus.get().post(new AudioEvents.PauseMusic());
|
||||||
if (pauseButton.getText().equals("⏸")) {
|
if (pauseButton.getText().equals("⏸")) {
|
||||||
pauseButton.setText("▶");
|
pauseButton.setText("▶");
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ public class SongDisplay extends VBox implements Widget {
|
|||||||
});
|
});
|
||||||
|
|
||||||
previousButton.setOnAction( event -> {
|
previousButton.setOnAction( event -> {
|
||||||
GlobalEventBus.post(new AudioEvents.PreviousMusic());
|
GlobalEventBus.get().post(new AudioEvents.PreviousMusic());
|
||||||
});
|
});
|
||||||
|
|
||||||
HBox control = new HBox(10, previousButton, pauseButton, skipButton);
|
HBox control = new HBox(10, previousButton, pauseButton, skipButton);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.toop.app.widget.complex.PopupWidget;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
public final class ChallengePopup extends PopupWidget {
|
public final class ChallengePopup extends PopupWidget {
|
||||||
private final GameInformation.Player playerInformation;
|
private final GameInformation.Player playerInformation;
|
||||||
@@ -28,19 +29,22 @@ public final class ChallengePopup extends PopupWidget {
|
|||||||
private void setupLayout() {
|
private void setupLayout() {
|
||||||
var challengeText = Primitive.text("you-were-challenged-by");
|
var challengeText = Primitive.text("you-were-challenged-by");
|
||||||
|
|
||||||
var challengerHeader = Primitive.header("");
|
var challengerHeader = Primitive.header(challenger, false);
|
||||||
challengerHeader.setText(challenger);
|
|
||||||
|
|
||||||
var gameText = Primitive.text("to-a-game-of");
|
var toAGameOfText = Primitive.text("to-a-game-of");
|
||||||
gameText.setText(gameText.getText() + " " + game);
|
var gameHeader = Primitive.header(game, false);
|
||||||
|
|
||||||
var acceptButton = Primitive.button("accept", () -> onAccept.accept(playerInformation));
|
var acceptButton = Primitive.button("accept", () -> {
|
||||||
|
onAccept.accept(playerInformation);
|
||||||
|
this.hide();
|
||||||
|
});
|
||||||
var denyButton = Primitive.button("deny", () -> hide());
|
var denyButton = Primitive.button("deny", () -> hide());
|
||||||
|
|
||||||
var leftSection = Primitive.vbox(
|
var leftSection = Primitive.vbox(
|
||||||
challengeText,
|
challengeText,
|
||||||
challengerHeader,
|
challengerHeader,
|
||||||
gameText,
|
toAGameOfText,
|
||||||
|
gameHeader,
|
||||||
Primitive.separator(),
|
Primitive.separator(),
|
||||||
Primitive.hbox(
|
Primitive.hbox(
|
||||||
acceptButton,
|
acceptButton,
|
||||||
|
|||||||
48
app/src/main/java/org/toop/app/widget/popup/EscapePopup.java
Normal file
48
app/src/main/java/org/toop/app/widget/popup/EscapePopup.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package org.toop.app.widget.popup;
|
||||||
|
|
||||||
|
import javafx.geometry.Pos;
|
||||||
|
import javafx.scene.Node;
|
||||||
|
import org.toop.app.widget.Primitive;
|
||||||
|
import org.toop.app.widget.Widget;
|
||||||
|
import org.toop.app.widget.WidgetContainer;
|
||||||
|
import org.toop.app.widget.complex.PopupWidget;
|
||||||
|
import org.toop.app.widget.complex.ViewWidget;
|
||||||
|
import org.toop.app.widget.view.GameView;
|
||||||
|
import org.toop.app.widget.view.OptionsView;
|
||||||
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class EscapePopup extends PopupWidget {
|
||||||
|
public EscapePopup() {
|
||||||
|
ViewWidget currentView = WidgetContainer.getCurrentView();
|
||||||
|
ArrayList<Node> nodes = new ArrayList<>();
|
||||||
|
|
||||||
|
nodes.add(Primitive.button("Continue", this::hide, false)); // TODO, localize
|
||||||
|
|
||||||
|
if (!(currentView.getClass().isAssignableFrom(OptionsView.class))) {
|
||||||
|
var opt = Primitive.button("options", () -> {
|
||||||
|
hide();
|
||||||
|
WidgetContainer.getCurrentView().transitionNext(new OptionsView());
|
||||||
|
});
|
||||||
|
nodes.add(opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentView.getClass().isAssignableFrom(GameView.class)) {
|
||||||
|
Widget tut = AppContext.currentTutorial();
|
||||||
|
if (tut != null) {
|
||||||
|
nodes.add(Primitive.button("tutorialstring", () -> {
|
||||||
|
WidgetContainer.getCurrentView().add(Pos.CENTER, tut);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes.add(Primitive.button("quit", () -> {
|
||||||
|
hide();
|
||||||
|
WidgetContainer.add(Pos.CENTER, new QuitPopup());
|
||||||
|
}));
|
||||||
|
|
||||||
|
add(Pos.CENTER, Primitive.vbox(nodes.toArray(new Node[0])));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,14 +15,12 @@ public class QuitPopup extends PopupWidget {
|
|||||||
});
|
});
|
||||||
|
|
||||||
confirmWidget.addButton("no", () -> {
|
confirmWidget.addButton("no", () -> {
|
||||||
App.stopQuit();
|
|
||||||
hide();
|
hide();
|
||||||
});
|
});
|
||||||
|
|
||||||
add(Pos.CENTER, confirmWidget);
|
add(Pos.CENTER, confirmWidget);
|
||||||
|
|
||||||
setOnPop(() -> {
|
setOnPop(() -> {
|
||||||
App.stopQuit();
|
|
||||||
hide();
|
hide();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public final class SendChallengePopup extends PopupWidget {
|
|||||||
// --- Left side: challenge text and buttons ---
|
// --- Left side: challenge text and buttons ---
|
||||||
var challengeText = Primitive.text("challenge");
|
var challengeText = Primitive.text("challenge");
|
||||||
|
|
||||||
var opponentHeader = Primitive.header(opponent);
|
var opponentHeader = Primitive.header(opponent, false);
|
||||||
|
|
||||||
var gameText = Primitive.text("to-a-game-of");
|
var gameText = Primitive.text("to-a-game-of");
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ public final class SendChallengePopup extends PopupWidget {
|
|||||||
|
|
||||||
var sendButton = Primitive.button(
|
var sendButton = Primitive.button(
|
||||||
"send",
|
"send",
|
||||||
() -> onSend.accept(playerInformation, gameChoice.getValue())
|
() -> { onSend.accept(playerInformation, gameChoice.getValue()); this.hide(); }
|
||||||
);
|
);
|
||||||
|
|
||||||
var cancelButton = Primitive.button("cancel", () -> hide());
|
var cancelButton = Primitive.button("cancel", () -> hide());
|
||||||
|
|||||||
@@ -59,8 +59,6 @@ public class BaseTutorialWidget extends PopupWidget implements Updatable {
|
|||||||
var x = Primitive.vbox(imagery, tutorialText);
|
var x = Primitive.vbox(imagery, tutorialText);
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(x, w));
|
add(Pos.CENTER, Primitive.vbox(x, w));
|
||||||
|
|
||||||
WidgetContainer.add(Pos.CENTER, this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,34 +1,43 @@
|
|||||||
package org.toop.app.widget.view;
|
package org.toop.app.widget.view;
|
||||||
|
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import javafx.scene.shape.Circle;
|
||||||
|
import javafx.scene.text.Font;
|
||||||
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 org.toop.app.widget.popup.GameOverPopup;
|
import org.toop.app.widget.popup.GameOverPopup;
|
||||||
|
|
||||||
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.TextField;
|
import javafx.scene.control.TextField;
|
||||||
import javafx.scene.text.Text;
|
import javafx.scene.text.Text;
|
||||||
import org.toop.app.widget.tutorial.BaseTutorialWidget;
|
|
||||||
import org.toop.app.widget.tutorial.Connect4TutorialWidget;
|
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;
|
||||||
|
|
||||||
public final class GameView extends ViewWidget {
|
public final class GameView extends ViewWidget {
|
||||||
private final Text currentPlayerHeader;
|
private final Text playerHeader;
|
||||||
private final Text currentMoveHeader;
|
private final Text turnHeader;
|
||||||
private final Text nextPlayerHeader;
|
private final Text player1Header;
|
||||||
|
private final Text player2Header;
|
||||||
|
private Circle player1Icon;
|
||||||
|
private Circle player2Icon;
|
||||||
private final Button forfeitButton;
|
private final Button forfeitButton;
|
||||||
private final Button exitButton;
|
private final Button exitButton;
|
||||||
private final Button tutorialButton;
|
|
||||||
private final TextField chatInput;
|
private final TextField chatInput;
|
||||||
|
private final Text keyThingy;
|
||||||
|
private boolean hasSet = false;
|
||||||
|
|
||||||
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage, String gameType) {
|
public GameView(Runnable onForfeit, Runnable onExit, Consumer<String> onMessage, String gameType) {
|
||||||
currentPlayerHeader = Primitive.header("");
|
playerHeader = Primitive.header("");
|
||||||
currentMoveHeader = Primitive.header("");
|
turnHeader = Primitive.header("");
|
||||||
nextPlayerHeader = Primitive.header("");
|
keyThingy = Primitive.text("turnof");
|
||||||
|
player1Header = Primitive.header("");
|
||||||
|
player2Header = Primitive.header("");
|
||||||
|
player1Icon = new Circle();
|
||||||
|
player2Icon = new Circle();
|
||||||
|
|
||||||
if (onForfeit != null) {
|
if (onForfeit != null) {
|
||||||
forfeitButton = Primitive.button("forfeit", () -> onForfeit.run());
|
forfeitButton = Primitive.button("forfeit", () -> onForfeit.run());
|
||||||
@@ -51,32 +60,27 @@ public final class GameView extends ViewWidget {
|
|||||||
chatInput = null;
|
chatInput = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(gameType) {
|
switch (gameType) {
|
||||||
case "TicTacToe":
|
case "TicTacToe":
|
||||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new TicTacToeTutorialWidget(() -> {})); break;
|
AppContext.setCurrentTutorial(new TicTacToeTutorialWidget(() -> {}));
|
||||||
|
break;
|
||||||
case "Reversi":
|
case "Reversi":
|
||||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new ReversiTutorialWidget(() -> {})); break;
|
AppContext.setCurrentTutorial(new ReversiTutorialWidget(() -> {}));
|
||||||
|
break;
|
||||||
case "Connect4":
|
case "Connect4":
|
||||||
this.tutorialButton = Primitive.button("tutorialstring", () -> new Connect4TutorialWidget(() -> {})); break;
|
AppContext.setCurrentTutorial(new Connect4TutorialWidget(() -> {}));
|
||||||
default:
|
break;
|
||||||
this.tutorialButton = null; break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setupLayout();
|
setupLayout();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupLayout() {
|
private void setupLayout() {
|
||||||
var playerInfo = Primitive.vbox(
|
var turnInfo = Primitive.vbox(
|
||||||
currentPlayerHeader,
|
turnHeader
|
||||||
Primitive.hbox(
|
);
|
||||||
Primitive.separator(),
|
|
||||||
currentMoveHeader,
|
|
||||||
Primitive.separator()
|
|
||||||
),
|
|
||||||
nextPlayerHeader
|
|
||||||
);
|
|
||||||
|
|
||||||
add(Pos.TOP_RIGHT, playerInfo);
|
add(Pos.TOP_CENTER, turnInfo);
|
||||||
|
|
||||||
var buttons = Primitive.vbox(
|
var buttons = Primitive.vbox(
|
||||||
forfeitButton,
|
forfeitButton,
|
||||||
@@ -88,27 +92,88 @@ public final class GameView extends ViewWidget {
|
|||||||
if (chatInput != null) {
|
if (chatInput != null) {
|
||||||
add(Pos.BOTTOM_RIGHT, Primitive.vbox(chatInput));
|
add(Pos.BOTTOM_RIGHT, Primitive.vbox(chatInput));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tutorialButton != null) {
|
|
||||||
add(Pos.TOP_LEFT, tutorialButton);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer) {
|
public void nextPlayer(boolean isMe, String currentPlayer, String currentMove, String nextPlayer, char GameType) {
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
currentPlayerHeader.setText(currentPlayer);
|
if (!(hasSet)) {
|
||||||
currentMoveHeader.setText(currentMove);
|
playerHeader.setText(currentPlayer + " vs. " + nextPlayer);
|
||||||
nextPlayerHeader.setText(nextPlayer);
|
hasSet = true;
|
||||||
|
setPlayerHeaders(isMe, currentPlayer, nextPlayer, GameType);
|
||||||
if (isMe) {
|
}
|
||||||
currentPlayerHeader.getStyleClass().add("my-turn");
|
//TODO idk if theres any way to check this? only EN uses 's and the rest doesnt. if theres a better way to do this pls let me know
|
||||||
} else {
|
if (AppContext.getLocale().toLanguageTag().equals("en")) {
|
||||||
currentPlayerHeader.getStyleClass().remove("my-turn");
|
turnHeader.setText(currentPlayer + keyThingy.getText());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gameOver(boolean iWon, String winner) {
|
public void gameOver(boolean iWon, String winner) {
|
||||||
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) {
|
||||||
|
if (GameType == 'T') {
|
||||||
|
if (isMe) {
|
||||||
|
player1Header.setText("X: " + currentPlayer);
|
||||||
|
player2Header.setText("O: " + nextPlayer);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
player1Header.setText("X: " + nextPlayer);
|
||||||
|
player2Header.setText("O: " + currentPlayer);
|
||||||
|
}
|
||||||
|
setPlayerInfoTTT();
|
||||||
|
}
|
||||||
|
else if (GameType == 'R') {
|
||||||
|
if (isMe) {
|
||||||
|
player1Header.setText(currentPlayer);
|
||||||
|
player2Header.setText(nextPlayer);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
player1Header.setText(nextPlayer);
|
||||||
|
player2Header.setText(currentPlayer);
|
||||||
|
}
|
||||||
|
setPlayerInfoReversi();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setPlayerInfoTTT() {
|
||||||
|
var playerInfo = Primitive.vbox(
|
||||||
|
playerHeader,
|
||||||
|
Primitive.separator(),
|
||||||
|
player1Header,
|
||||||
|
player2Header
|
||||||
|
);
|
||||||
|
|
||||||
|
add(Pos.TOP_RIGHT, playerInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setPlayerInfoReversi() {
|
||||||
|
var player1box = Primitive.hbox(
|
||||||
|
player1Icon,
|
||||||
|
player1Header
|
||||||
|
);
|
||||||
|
|
||||||
|
player1box.getStyleClass().add("hboxspacing");
|
||||||
|
|
||||||
|
var player2box = Primitive.hbox(
|
||||||
|
player2Icon,
|
||||||
|
player2Header
|
||||||
|
);
|
||||||
|
|
||||||
|
player2box.getStyleClass().add("hboxspacing");
|
||||||
|
|
||||||
|
var playerInfo = Primitive.vbox(
|
||||||
|
playerHeader,
|
||||||
|
Primitive.separator(),
|
||||||
|
player1box,
|
||||||
|
player2box
|
||||||
|
);
|
||||||
|
|
||||||
|
player1Icon.setRadius(player1Header.fontProperty().map(Font::getSize).getValue());
|
||||||
|
player2Icon.setRadius(player2Header.fontProperty().map(Font::getSize).getValue());
|
||||||
|
player1Icon.setFill(Color.BLACK);
|
||||||
|
player2Icon.setFill(Color.WHITE);
|
||||||
|
add(Pos.TOP_RIGHT, playerInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,7 @@ import org.toop.app.gameControllers.AbstractGameController;
|
|||||||
import org.toop.app.gameControllers.ReversiController;
|
import org.toop.app.gameControllers.ReversiController;
|
||||||
import org.toop.app.gameControllers.TicTacToeController;
|
import org.toop.app.gameControllers.TicTacToeController;
|
||||||
import org.toop.framework.gameFramework.model.player.Player;
|
import org.toop.framework.gameFramework.model.player.Player;
|
||||||
|
import org.toop.game.games.tictactoe.TicTacToeAIRSleep;
|
||||||
import org.toop.game.players.ArtificialPlayer;
|
import org.toop.game.players.ArtificialPlayer;
|
||||||
import org.toop.game.players.LocalPlayer;
|
import org.toop.game.players.LocalPlayer;
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
@@ -52,12 +53,12 @@ public class LocalMultiplayerView extends ViewWidget {
|
|||||||
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 TicTacToeAIR(), information.players[0].name);
|
players[0] = new ArtificialPlayer<>(new TicTacToeAIRSleep(information.players[0].computerDifficulty, information.players[1].computerThinkTime), information.players[0].name);
|
||||||
}
|
}
|
||||||
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 TicTacToeAIR(), information.players[1].name);
|
players[1] = new ArtificialPlayer<>(new TicTacToeAIRSleep(information.players[1].computerDifficulty, information.players[1].computerThinkTime), information.players[1].name);
|
||||||
}
|
}
|
||||||
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
if (AppSettings.getSettings().getTutorialFlag() && AppSettings.getSettings().getFirstTTT()) {
|
||||||
new ShowEnableTutorialWidget(
|
new ShowEnableTutorialWidget(
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.toop.app.App;
|
|||||||
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 javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
|
import org.toop.app.widget.popup.QuitPopup;
|
||||||
|
|
||||||
public class MainView extends ViewWidget {
|
public class MainView extends ViewWidget {
|
||||||
public MainView() {
|
public MainView() {
|
||||||
@@ -24,7 +25,8 @@ public class MainView extends ViewWidget {
|
|||||||
});
|
});
|
||||||
|
|
||||||
var quitButton = Primitive.button("quit", () -> {
|
var quitButton = Primitive.button("quit", () -> {
|
||||||
App.startQuit();
|
var a = new QuitPopup();
|
||||||
|
a.show(Pos.CENTER);
|
||||||
});
|
});
|
||||||
|
|
||||||
add(Pos.CENTER, Primitive.vbox(
|
add(Pos.CENTER, Primitive.vbox(
|
||||||
|
|||||||
@@ -14,14 +14,12 @@ import javafx.scene.control.ListView;
|
|||||||
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 Runnable onDisconnect;
|
|
||||||
|
|
||||||
private final ListView<Button> listView;
|
private final ListView<Button> listView;
|
||||||
|
|
||||||
public ServerView(String user, Consumer<String> onPlayerClicked, Runnable onDisconnect) {
|
public ServerView(String user, Consumer<String> onPlayerClicked) {
|
||||||
this.user = user;
|
this.user = user;
|
||||||
this.onPlayerClicked = onPlayerClicked;
|
this.onPlayerClicked = onPlayerClicked;
|
||||||
this.onDisconnect = onDisconnect;
|
|
||||||
|
|
||||||
this.listView = new ListView<>();
|
this.listView = new ListView<>();
|
||||||
|
|
||||||
@@ -40,7 +38,6 @@ public final class ServerView extends ViewWidget {
|
|||||||
add(Pos.CENTER, playerListSection);
|
add(Pos.CENTER, playerListSection);
|
||||||
|
|
||||||
var disconnectButton = Primitive.button("disconnect", () -> {
|
var disconnectButton = Primitive.button("disconnect", () -> {
|
||||||
onDisconnect.run();
|
|
||||||
transitionPrevious();
|
transitionPrevious();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.util.MissingResourceException;
|
|||||||
|
|
||||||
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.widget.tutorial.BaseTutorialWidget;
|
||||||
import org.toop.framework.resource.ResourceManager;
|
import org.toop.framework.resource.ResourceManager;
|
||||||
import org.toop.framework.resource.resources.LocalizationAsset;
|
import org.toop.framework.resource.resources.LocalizationAsset;
|
||||||
|
|
||||||
@@ -16,11 +17,13 @@ import javafx.beans.property.ObjectProperty;
|
|||||||
import javafx.beans.property.SimpleObjectProperty;
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
|
||||||
public class AppContext {
|
public class AppContext {
|
||||||
|
private static final Logger logger = LogManager.getLogger(AppContext.class);
|
||||||
|
|
||||||
private static final LocalizationAsset localization = ResourceManager.get("localization");
|
private static final LocalizationAsset localization = ResourceManager.get("localization");
|
||||||
private static Locale locale = Locale.forLanguageTag("en");
|
private static Locale locale = Locale.forLanguageTag("en");
|
||||||
|
|
||||||
private static final ObjectProperty<Locale> localeProperty = new SimpleObjectProperty<>(locale);
|
private static final ObjectProperty<Locale> localeProperty = new SimpleObjectProperty<>(locale);
|
||||||
private static final Logger logger = LogManager.getLogger(AppContext.class);
|
private static BaseTutorialWidget tutorialWidget;
|
||||||
|
|
||||||
public static LocalizationAsset getLocalization() {
|
public static LocalizationAsset getLocalization() {
|
||||||
return localization;
|
return localization;
|
||||||
@@ -73,4 +76,12 @@ public class AppContext {
|
|||||||
public static StringBinding bindToKey(String key) {
|
public static StringBinding bindToKey(String key) {
|
||||||
return bindToKey(key, true);
|
return bindToKey(key, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setCurrentTutorial(BaseTutorialWidget tutorial) {
|
||||||
|
AppContext.tutorialWidget = tutorial;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BaseTutorialWidget currentTutorial() {
|
||||||
|
return AppContext.tutorialWidget;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -85,6 +85,11 @@ reversi4=\u0627\u0644\u0644\u0627\u0639\u0628 \u0627\u0644\u0630\u064a \u064a\u0
|
|||||||
tutorialstring=\u0627\u0644\u062f\u0631\u0633 \u0627\u0644\u062a\u0648\u0636\u064a\u062d\u064a
|
tutorialstring=\u0627\u0644\u062f\u0631\u0633 \u0627\u0644\u062a\u0648\u0636\u064a\u062d\u064a
|
||||||
startgame=\u0627\u0628\u062f\u0623 \u0627\u0644\u0644\u0639\u0628\u0629!
|
startgame=\u0627\u0628\u062f\u0623 \u0627\u0644\u0644\u0639\u0628\u0629!
|
||||||
goback=\u0627\u0631\u062c\u0639
|
goback=\u0627\u0631\u062c\u0639
|
||||||
|
turnof=\u062F\u0648\u0631\u0647
|
||||||
|
zwartepiet=\u0633\u0647\u0644: Zwarte Piet
|
||||||
|
sinterklaas=\u0645\u062a\u0648\u0633\u0637: Sint R. Klaas
|
||||||
|
santa=\u0635\u0639\u0628: Santa
|
||||||
|
|
||||||
|
|
||||||
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629
|
arabic=\u0627\u0644\u0639\u0631\u0628\u064a\u0629
|
||||||
chinese=\u4e2d\u6587 (\u0627\u0644\u0635\u064a\u0646\u064a\u0629)
|
chinese=\u4e2d\u6587 (\u0627\u0644\u0635\u064a\u0646\u064a\u0629)
|
||||||
|
|||||||
@@ -87,6 +87,10 @@ reversi4=Der Spieler, der am Ende die meisten Steine auf dem Brett hat, gewinnt.
|
|||||||
tutorialstring=Tutorial
|
tutorialstring=Tutorial
|
||||||
startgame=Spiel starten!
|
startgame=Spiel starten!
|
||||||
goback=Zur<EFBFBD>ck
|
goback=Zur<EFBFBD>ck
|
||||||
|
turnof=ist dran
|
||||||
|
zwartepiet=Leicht: Zwarte Piet
|
||||||
|
sinterklaas=Mittel: Sint R. Klaas
|
||||||
|
santa=Schwer: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ error=Error
|
|||||||
exit=Exit
|
exit=Exit
|
||||||
forfeit=Forfeit
|
forfeit=Forfeit
|
||||||
fullscreen=Fullscreen
|
fullscreen=Fullscreen
|
||||||
|
game=REPLACE ME
|
||||||
game-over=Game Over
|
game-over=Game Over
|
||||||
general=General
|
general=General
|
||||||
high-contrast=High contrast
|
high-contrast=High contrast
|
||||||
@@ -88,7 +89,10 @@ reversi4=The player who wins at the end of the game is the one who has the most
|
|||||||
tutorialstring=Tutorial
|
tutorialstring=Tutorial
|
||||||
startgame=Start game!
|
startgame=Start game!
|
||||||
goback=Go back
|
goback=Go back
|
||||||
|
turnof='s turn
|
||||||
|
zwartepiet=Easy: Zwarte Piet
|
||||||
|
sinterklaas=Medium: Sint R. Klaas
|
||||||
|
santa=Hard:Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -86,7 +86,10 @@ reversi4=El jugador que gane al final del juego es quien tenga m
|
|||||||
tutorialstring=Tutorial
|
tutorialstring=Tutorial
|
||||||
startgame=\u00a1Iniciar juego!
|
startgame=\u00a1Iniciar juego!
|
||||||
goback=Volver
|
goback=Volver
|
||||||
|
turnof=le toca
|
||||||
|
zwartepiet=F\u00e1cil: Zwarte Piet
|
||||||
|
sinterklaas=Medio: Sint R. Klaas
|
||||||
|
santa=Dif\u00edcil: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -86,6 +86,10 @@ reversi4=Le joueur qui a le plus de pions
|
|||||||
tutorialstring=Tutoriel
|
tutorialstring=Tutoriel
|
||||||
startgame=D\u00e9marrer le jeu!
|
startgame=D\u00e9marrer le jeu!
|
||||||
goback=Retour
|
goback=Retour
|
||||||
|
turnof=\u00E0 son tour
|
||||||
|
zwartepiet=Facile: Zwarte Piet
|
||||||
|
sinterklaas=Moyen : Sint R. Klaas
|
||||||
|
santa=Difficile: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -86,6 +86,10 @@ reversi4=\u0916\u0941\u092f \u0915\u093f \u0915\u0940 \u0928\u093f\u092e\u0940 \
|
|||||||
tutorialstring=\u0924\u0942\u091f\u0949\u0930\u093f\u092f\u0932
|
tutorialstring=\u0924\u0942\u091f\u0949\u0930\u093f\u092f\u0932
|
||||||
startgame=\u0916\u0947\u0932 \u0936\u0941\u0930\u0942 \u0915\u0930\u0947\u0902!
|
startgame=\u0916\u0947\u0932 \u0936\u0941\u0930\u0942 \u0915\u0930\u0947\u0902!
|
||||||
goback=\u0935\u093e\u092a\u0938 \u091c\u093e\u090f\u0901
|
goback=\u0935\u093e\u092a\u0938 \u091c\u093e\u090f\u0901
|
||||||
|
turnof=\u0915\u0940 \u092C\u093E\u0930\u0940
|
||||||
|
zwartepiet=\u0905\u0938\u093e\u0928: Zwarte Piet
|
||||||
|
sinterklaas=\u092e\u0927\u094d\u092f\u092e: Sint R. Klaas
|
||||||
|
santa=\u0915\u0924\u093f\u0928: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=Il giocatore che alla fine del gioco ha pi
|
|||||||
tutorialstring=Tutorial
|
tutorialstring=Tutorial
|
||||||
startgame=Avvia il gioco!
|
startgame=Avvia il gioco!
|
||||||
goback=Indietro
|
goback=Indietro
|
||||||
|
turnof=\u00E8 il suo turno
|
||||||
|
zwartepiet=Facile: Zwarte Piet
|
||||||
|
sinterklaas=Medio: Sint R. Klaas
|
||||||
|
santa=Difficile: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=\u672c\u6b21\u306b\u30dc\u30fc\u30c9\u4e0a\u3067\u6700\u591a\u306e\u8ca
|
|||||||
tutorialstring=\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb
|
tutorialstring=\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb
|
||||||
startgame=\u30b2\u30fc\u30e0\u3092\u958b\u59cb\uff01
|
startgame=\u30b2\u30fc\u30e0\u3092\u958b\u59cb\uff01
|
||||||
goback=\u623b\u308b
|
goback=\u623b\u308b
|
||||||
|
turnof=\u306E\u756A
|
||||||
|
zwartepiet=\u7c21\u5358: Zwarte Piet
|
||||||
|
sinterklaas=\u4e2d\u7d1a: Sint R. Klaas
|
||||||
|
santa=\u96e3\u3057\u3044: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=\uacbd\uc6b0 \uc5d0\uc11c \ucd5c\ub300 \ud648\uc744 \uac00\uc838\ub294
|
|||||||
tutorialstring=\ud14c\ud2b8\ub9ad
|
tutorialstring=\ud14c\ud2b8\ub9ad
|
||||||
startgame=\uac8c\uc784 \uc2dc\uc791!
|
startgame=\uac8c\uc784 \uc2dc\uc791!
|
||||||
goback=\ub4a4\ub85c \uac00\uae30
|
goback=\ub4a4\ub85c \uac00\uae30
|
||||||
|
turnof=\uC758 \uCC28\uB840
|
||||||
|
zwartepiet=\uc218\uc601: Zwarte Piet
|
||||||
|
sinterklaas=\ubcf4\ud1b5: Sint R. Klaas
|
||||||
|
santa=\uc5d0\uc18c: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=De speler die aan het einde van het spel de meeste stukken op het bord
|
|||||||
tutorialstring=Tutorial
|
tutorialstring=Tutorial
|
||||||
startgame=Spel starten!
|
startgame=Spel starten!
|
||||||
goback=Ga terug
|
goback=Ga terug
|
||||||
|
turnof=is aan de beurt
|
||||||
|
zwartepiet=Makkelijk: Zwarte Piet
|
||||||
|
sinterklaas=Gemiddeld: Sint R. Klaas
|
||||||
|
santa=Moeilijk: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=\u0418043 \u0433043 \u0440043 \u043e043 \u043a043 \u043e043 \u0442043 \
|
|||||||
tutorialstring=\u0423\u0447\u0435\u0431\u043d\u0438\u043a
|
tutorialstring=\u0423\u0447\u0435\u0431\u043d\u0438\u043a
|
||||||
startgame=\u041d\u0430\u0447\u0430\u0442\u044c \u0438\u0433\u0440\u0443!
|
startgame=\u041d\u0430\u0447\u0430\u0442\u044c \u0438\u0433\u0440\u0443!
|
||||||
goback=\u041d\u0430\u0437\u0430\u0434
|
goback=\u041d\u0430\u0437\u0430\u0434
|
||||||
|
turnof=\u0445\u043E\u0434\u0438\u0442
|
||||||
|
zwartepiet=\u041b\u0435\u0433\u043a\u043e: Zwarte Piet
|
||||||
|
sinterklaas=\u0421\u0440\u0435\u0434\u043d\u0438\u0439: Sint R. Klaas
|
||||||
|
santa=\u0421\u043b\u043e\u0436\u043d\u043e: Santa
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -85,6 +85,10 @@ reversi4=\u672c\u6e38\u620f\u7ed3\u675f\u65f6\u8d62\u5f97\u6ee1\u8fc7\u76d8\u976
|
|||||||
tutorialstring=\u6559\u7a0b
|
tutorialstring=\u6559\u7a0b
|
||||||
startgame=\u5f00\u59cb\u6e38\u620f\uff01
|
startgame=\u5f00\u59cb\u6e38\u620f\uff01
|
||||||
goback=\u8fd4\u56de
|
goback=\u8fd4\u56de
|
||||||
|
turnof=\u7684\u56DE\u5408
|
||||||
|
zwartepiet=\u7b80\u5355: Zwarte Piet
|
||||||
|
sinterklaas=\u4e2d\u7b49: Sint R. Klaas
|
||||||
|
santa=\u56f0\u96be: Santa
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@@ -166,7 +166,7 @@
|
|||||||
-fx-effect: dropshadow(gaussian, #88cc8899, 5, 0, 0, 1);
|
-fx-effect: dropshadow(gaussian, #88cc8899, 5, 0, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-turn {
|
.text.my-turn {
|
||||||
-fx-fill: #e05656;
|
-fx-fill: #e05656;
|
||||||
-fx-font-weight: bold;
|
-fx-font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -164,7 +164,7 @@
|
|||||||
-fx-effect: dropshadow(gaussian, #70e070cc, 6, 0, 0, 2);
|
-fx-effect: dropshadow(gaussian, #70e070cc, 6, 0, 0, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-turn {
|
.text.my-turn {
|
||||||
-fx-fill: #ff4b4b;
|
-fx-fill: #ff4b4b;
|
||||||
-fx-font-weight: bold;
|
-fx-font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,11 @@
|
|||||||
-fx-spacing: 14;
|
-fx-spacing: 14;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hboxspacing {
|
||||||
|
-fx-padding: 2;
|
||||||
|
-fx-spacing: 10;
|
||||||
|
}
|
||||||
|
|
||||||
.current-player {
|
.current-player {
|
||||||
-fx-font-size: 32px;
|
-fx-font-size: 32px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -166,7 +166,7 @@
|
|||||||
-fx-effect: dropshadow(gaussian, #aad3aa99, 4, 0, 0, 1);
|
-fx-effect: dropshadow(gaussian, #aad3aa99, 4, 0, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-turn {
|
.text.my-turn {
|
||||||
-fx-fill: #d14b4b;
|
-fx-fill: #d14b4b;
|
||||||
-fx-font-weight: bold;
|
-fx-font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,11 @@
|
|||||||
-fx-spacing: 10;
|
-fx-spacing: 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hboxspacing {
|
||||||
|
-fx-padding: 2;
|
||||||
|
-fx-spacing: 10;
|
||||||
|
}
|
||||||
|
|
||||||
.current-player {
|
.current-player {
|
||||||
-fx-font-size: 24px;
|
-fx-font-size: 24px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,11 @@
|
|||||||
-fx-spacing: 6;
|
-fx-spacing: 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.hboxspacing {
|
||||||
|
-fx-padding: 2;
|
||||||
|
-fx-spacing: 10;
|
||||||
|
}
|
||||||
|
|
||||||
.current-player {
|
.current-player {
|
||||||
-fx-font-size: 16px;
|
-fx-font-size: 16px;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,25 +6,29 @@ import org.toop.framework.audio.interfaces.SoundEffectManager;
|
|||||||
import org.toop.framework.audio.interfaces.VolumeManager;
|
import org.toop.framework.audio.interfaces.VolumeManager;
|
||||||
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.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.resource.types.AudioResource;
|
import org.toop.framework.resource.types.AudioResource;
|
||||||
|
|
||||||
public class AudioEventListener<T extends AudioResource, K extends AudioResource> {
|
public class AudioEventListener<T extends AudioResource, K extends AudioResource> {
|
||||||
|
private final EventBus eventBus;
|
||||||
private final MusicManager<T> musicManager;
|
private final MusicManager<T> musicManager;
|
||||||
private final SoundEffectManager<K> soundEffectManager;
|
private final SoundEffectManager<K> soundEffectManager;
|
||||||
private final VolumeManager audioVolumeManager;
|
private final VolumeManager audioVolumeManager;
|
||||||
|
|
||||||
public AudioEventListener(
|
public AudioEventListener(
|
||||||
|
EventBus eventBus,
|
||||||
MusicManager<T> musicManager,
|
MusicManager<T> musicManager,
|
||||||
SoundEffectManager<K> soundEffectManager,
|
SoundEffectManager<K> soundEffectManager,
|
||||||
VolumeManager audioVolumeManager
|
VolumeManager audioVolumeManager
|
||||||
) {
|
) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.musicManager = musicManager;
|
this.musicManager = musicManager;
|
||||||
this.soundEffectManager = soundEffectManager;
|
this.soundEffectManager = soundEffectManager;
|
||||||
this.audioVolumeManager = audioVolumeManager;
|
this.audioVolumeManager = audioVolumeManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AudioEventListener<?, ?> initListeners(String buttonSoundToPlay) {
|
public AudioEventListener<?, ?> initListeners(String buttonSoundToPlay) {
|
||||||
new EventFlow()
|
new EventFlow(eventBus)
|
||||||
.listen(AudioEvents.StopAudioManager.class, this::handleStopMusicManager, false)
|
.listen(AudioEvents.StopAudioManager.class, this::handleStopMusicManager, false)
|
||||||
.listen(AudioEvents.PlayEffect.class, this::handlePlaySound, false)
|
.listen(AudioEvents.PlayEffect.class, this::handlePlaySound, false)
|
||||||
.listen(AudioEvents.SkipMusic.class, this::handleSkipSong, false)
|
.listen(AudioEvents.SkipMusic.class, this::handleSkipSong, false)
|
||||||
@@ -73,7 +77,7 @@ public class AudioEventListener<T extends AudioResource, K extends AudioResource
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleGetVolume(AudioEvents.GetVolume event) {
|
private void handleGetVolume(AudioEvents.GetVolume event) {
|
||||||
GlobalEventBus.postAsync(new AudioEvents.GetVolumeResponse(
|
eventBus.post(new AudioEvents.GetVolumeResponse(
|
||||||
audioVolumeManager.getVolume(event.controlType()),
|
audioVolumeManager.getVolume(event.controlType()),
|
||||||
event.identifier()));
|
event.identifier()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import org.toop.framework.audio.events.AudioEvents;
|
|||||||
import org.toop.framework.dispatch.interfaces.Dispatcher;
|
import org.toop.framework.dispatch.interfaces.Dispatcher;
|
||||||
import org.toop.framework.dispatch.JavaFXDispatcher;
|
import org.toop.framework.dispatch.JavaFXDispatcher;
|
||||||
import org.toop.annotations.TestsOnly;
|
import org.toop.annotations.TestsOnly;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.eventbus.GlobalEventBus;
|
|
||||||
import org.toop.framework.resource.types.AudioResource;
|
import org.toop.framework.resource.types.AudioResource;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -18,6 +17,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class MusicManager<T extends AudioResource> implements org.toop.framework.audio.interfaces.MusicManager<T> {
|
public class MusicManager<T extends AudioResource> implements org.toop.framework.audio.interfaces.MusicManager<T> {
|
||||||
private static final Logger logger = LogManager.getLogger(MusicManager.class);
|
private static final Logger logger = LogManager.getLogger(MusicManager.class);
|
||||||
|
|
||||||
|
private final EventBus eventBus;
|
||||||
private final List<T> backgroundMusic = new ArrayList<>();
|
private final List<T> backgroundMusic = new ArrayList<>();
|
||||||
private final Dispatcher dispatcher;
|
private final Dispatcher dispatcher;
|
||||||
private final List<T> resources;
|
private final List<T> resources;
|
||||||
@@ -27,7 +27,8 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
|
|||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
|
|
||||||
|
|
||||||
public MusicManager(List<T> resources, boolean shuffleMusic) {
|
public MusicManager(EventBus eventbus, List<T> resources, boolean shuffleMusic) {
|
||||||
|
this.eventBus = eventbus;
|
||||||
this.dispatcher = new JavaFXDispatcher();
|
this.dispatcher = new JavaFXDispatcher();
|
||||||
this.resources = resources;
|
this.resources = resources;
|
||||||
// Shuffle if wanting to shuffle
|
// Shuffle if wanting to shuffle
|
||||||
@@ -40,7 +41,8 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
|
|||||||
* {@code @TestsOnly} DO NOT USE
|
* {@code @TestsOnly} DO NOT USE
|
||||||
*/
|
*/
|
||||||
@TestsOnly
|
@TestsOnly
|
||||||
public MusicManager(List<T> resources, Dispatcher dispatcher) {
|
public MusicManager(EventBus eventBus, List<T> resources, Dispatcher dispatcher) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.dispatcher = dispatcher;
|
this.dispatcher = dispatcher;
|
||||||
this.resources = new ArrayList<>(resources);
|
this.resources = new ArrayList<>(resources);
|
||||||
backgroundMusic.addAll(resources);
|
backgroundMusic.addAll(resources);
|
||||||
@@ -124,7 +126,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
|
|||||||
Runnable currentMusicTask = new Runnable() {
|
Runnable currentMusicTask = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
GlobalEventBus.post(new AudioEvents.PlayingMusic(track.getName(), track.currentPosition(), track.duration()));
|
eventBus.post(new AudioEvents.PlayingMusic(track.getName(), track.currentPosition(), track.duration()));
|
||||||
scheduler.schedule(this, 1, TimeUnit.SECONDS);
|
scheduler.schedule(this, 1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,18 +2,10 @@ package org.toop.framework.audio;
|
|||||||
|
|
||||||
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.resource.ResourceManager;
|
|
||||||
import org.toop.framework.resource.ResourceMeta;
|
import org.toop.framework.resource.ResourceMeta;
|
||||||
import org.toop.framework.resource.resources.BaseResource;
|
import org.toop.framework.resource.resources.BaseResource;
|
||||||
import org.toop.framework.resource.resources.MusicAsset;
|
|
||||||
import org.toop.framework.resource.resources.SoundEffectAsset;
|
|
||||||
import org.toop.framework.resource.types.AudioResource;
|
import org.toop.framework.resource.types.AudioResource;
|
||||||
|
|
||||||
import javax.sound.sampled.Clip;
|
|
||||||
import javax.sound.sampled.LineEvent;
|
|
||||||
import javax.sound.sampled.LineUnavailableException;
|
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|||||||
@@ -13,10 +13,13 @@ import org.toop.framework.SnowflakeGenerator;
|
|||||||
import org.toop.framework.eventbus.events.EventType;
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
||||||
import org.toop.framework.eventbus.events.UniqueEvent;
|
import org.toop.framework.eventbus.events.UniqueEvent;
|
||||||
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
|
import org.toop.framework.eventbus.subscriber.DefaultSubscriber;
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* EventFlow is a utility class for creating, posting, and optionally subscribing to events in a
|
* EventFlow is a utility class for creating, posting, and optionally subscribing to events in a
|
||||||
* type-safe and chainable manner. It is designed to work with the {@link GlobalEventBus}.
|
* type-safe and chainable manner. It is designed to work with the {@link EventBus}.
|
||||||
*
|
*
|
||||||
* <p>This class supports automatic UUID assignment for {@link UniqueEvent} events, and
|
* <p>This class supports automatic UUID assignment for {@link UniqueEvent} events, and
|
||||||
* allows filtering subscribers so they only respond to events with a specific UUID. All
|
* allows filtering subscribers so they only respond to events with a specific UUID. All
|
||||||
@@ -31,6 +34,8 @@ public class EventFlow {
|
|||||||
/** Cache of constructor handles for event classes to avoid repeated reflection lookups. */
|
/** Cache of constructor handles for event classes to avoid repeated reflection lookups. */
|
||||||
private static final Map<Class<?>, MethodHandle> CONSTRUCTOR_CACHE = new ConcurrentHashMap<>();
|
private static final Map<Class<?>, MethodHandle> CONSTRUCTOR_CACHE = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private final EventBus eventBus;
|
||||||
|
|
||||||
/** Automatically assigned UUID for {@link UniqueEvent} events. */
|
/** Automatically assigned UUID for {@link UniqueEvent} events. */
|
||||||
private long eventSnowflake = -1;
|
private long eventSnowflake = -1;
|
||||||
|
|
||||||
@@ -38,13 +43,19 @@ public class EventFlow {
|
|||||||
private EventType event = null;
|
private EventType event = null;
|
||||||
|
|
||||||
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
||||||
private final List<ListenerHandler<?>> listeners = new ArrayList<>();
|
private final List<Subscriber<?, ?>> listeners = new ArrayList<>();
|
||||||
|
|
||||||
/** Holds the results returned from the subscribed event, if any. */
|
/** Holds the results returned from the subscribed event, if any. */
|
||||||
private Map<String, ?> result = null;
|
private Map<String, ?> result = null;
|
||||||
|
|
||||||
/** Empty constructor (event must be added via {@link #addPostEvent(Class, Object...)}). */
|
/** Empty constructor (event must be added via {@link #addPostEvent(Class, Object...)}). */
|
||||||
public EventFlow() {}
|
public EventFlow(EventBus eventBus) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EventFlow() {
|
||||||
|
this.eventBus = GlobalEventBus.get();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -145,21 +156,19 @@ public class EventFlow {
|
|||||||
|
|
||||||
action.accept(eventClass);
|
action.accept(eventClass);
|
||||||
|
|
||||||
if (unsubscribeAfterSuccess) unsubscribe(id);
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
|
|
||||||
this.result = eventClass.result();
|
this.result = eventClass.result();
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO Remove casts
|
var subscriber = new DefaultSubscriber<>(
|
||||||
var listener = new ListenerHandler<>(
|
|
||||||
id,
|
|
||||||
name,
|
name,
|
||||||
(Class<ResponseToUniqueEvent>) event,
|
event,
|
||||||
(Consumer<ResponseToUniqueEvent>) newAction
|
newAction
|
||||||
);
|
);
|
||||||
|
|
||||||
GlobalEventBus.subscribe(listener);
|
eventBus.subscribe(subscriber);
|
||||||
this.listeners.add(listener);
|
this.listeners.add(subscriber);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +236,7 @@ public class EventFlow {
|
|||||||
TT typedEvent = (TT) uuidEvent;
|
TT typedEvent = (TT) uuidEvent;
|
||||||
action.accept(typedEvent);
|
action.accept(typedEvent);
|
||||||
|
|
||||||
if (unsubscribeAfterSuccess) unsubscribe(id);
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
|
|
||||||
this.result = typedEvent.result();
|
this.result = typedEvent.result();
|
||||||
} catch (ClassCastException _) {
|
} catch (ClassCastException _) {
|
||||||
@@ -239,14 +248,13 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new ListenerHandler<>(
|
var listener = new DefaultSubscriber<>(
|
||||||
id,
|
|
||||||
name,
|
name,
|
||||||
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
||||||
newAction
|
newAction
|
||||||
);
|
);
|
||||||
|
|
||||||
GlobalEventBus.subscribe(listener);
|
eventBus.subscribe(listener);
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -284,17 +292,16 @@ public class EventFlow {
|
|||||||
Consumer<TT> newAction = eventc -> {
|
Consumer<TT> newAction = eventc -> {
|
||||||
action.accept(eventc);
|
action.accept(eventc);
|
||||||
|
|
||||||
if (unsubscribeAfterSuccess) unsubscribe(id);
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new ListenerHandler<>(
|
var listener = new DefaultSubscriber<>(
|
||||||
id,
|
|
||||||
name,
|
name,
|
||||||
event,
|
event,
|
||||||
newAction
|
newAction
|
||||||
);
|
);
|
||||||
|
|
||||||
GlobalEventBus.subscribe(listener);
|
eventBus.subscribe(listener);
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -362,7 +369,7 @@ public class EventFlow {
|
|||||||
try {
|
try {
|
||||||
TT typedEvent = (TT) nonUuidEvent;
|
TT typedEvent = (TT) nonUuidEvent;
|
||||||
action.accept(typedEvent);
|
action.accept(typedEvent);
|
||||||
if (unsubscribeAfterSuccess) unsubscribe(id);
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
} catch (ClassCastException _) {
|
} catch (ClassCastException _) {
|
||||||
throw new ClassCastException(
|
throw new ClassCastException(
|
||||||
"Cannot cast "
|
"Cannot cast "
|
||||||
@@ -371,14 +378,13 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new ListenerHandler<>(
|
var listener = new DefaultSubscriber<>(
|
||||||
id,
|
|
||||||
name,
|
name,
|
||||||
eventClass,
|
eventClass,
|
||||||
newAction
|
newAction
|
||||||
);
|
);
|
||||||
|
|
||||||
GlobalEventBus.subscribe(listener);
|
eventBus.subscribe(listener);
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -401,15 +407,18 @@ public class EventFlow {
|
|||||||
* Posts the event added through {@link #addPostEvent}.
|
* Posts the event added through {@link #addPostEvent}.
|
||||||
*/
|
*/
|
||||||
public EventFlow postEvent() {
|
public EventFlow postEvent() {
|
||||||
GlobalEventBus.post(this.event);
|
eventBus.post(this.event);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Posts the event added through {@link #addPostEvent} asynchronously.
|
* Posts the event added through {@link #addPostEvent} asynchronously.
|
||||||
|
*
|
||||||
|
* @deprecated use {@link #postEvent()} instead.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public EventFlow asyncPostEvent() {
|
public EventFlow asyncPostEvent() {
|
||||||
GlobalEventBus.postAsync(this.event);
|
eventBus.post(this.event);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,28 +426,12 @@ public class EventFlow {
|
|||||||
*
|
*
|
||||||
* Unsubscribe from an event.
|
* Unsubscribe from an event.
|
||||||
*
|
*
|
||||||
* @param listenerObject The listener object to remove and unsubscribe.
|
* @param action The listener object to remove and unsubscribe.
|
||||||
*/
|
*/
|
||||||
public void unsubscribe(Object listenerObject) {
|
public void unsubscribe(Consumer<?> action) {
|
||||||
this.listeners.removeIf(handler -> {
|
this.listeners.removeIf(handler -> {
|
||||||
if (handler.getListener() == listenerObject) {
|
if (handler.getAction().equals(action)) {
|
||||||
GlobalEventBus.unsubscribe(handler);
|
eventBus.unsubscribe(handler);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Unsubscribe from an event.
|
|
||||||
*
|
|
||||||
* @param listenerId The id given to the {@link ListenerHandler}.
|
|
||||||
*/
|
|
||||||
public void unsubscribe(long listenerId) {
|
|
||||||
this.listeners.removeIf(handler -> {
|
|
||||||
if (handler.getId() == listenerId) {
|
|
||||||
GlobalEventBus.unsubscribe(handler);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -452,8 +445,8 @@ public class EventFlow {
|
|||||||
*/
|
*/
|
||||||
public void unsubscribe(String name) {
|
public void unsubscribe(String name) {
|
||||||
this.listeners.removeIf(handler -> {
|
this.listeners.removeIf(handler -> {
|
||||||
if (handler.getName().equals(name)) {
|
if (handler.getId().equals(name)) {
|
||||||
GlobalEventBus.unsubscribe(handler);
|
eventBus.unsubscribe(handler);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -465,7 +458,7 @@ public class EventFlow {
|
|||||||
*/
|
*/
|
||||||
public void unsubscribeAll() {
|
public void unsubscribeAll() {
|
||||||
listeners.removeIf(handler -> {
|
listeners.removeIf(handler -> {
|
||||||
GlobalEventBus.unsubscribe(handler);
|
eventBus.unsubscribe(handler);
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -503,8 +496,8 @@ public class EventFlow {
|
|||||||
*
|
*
|
||||||
* @return Copy of the list of listeners.
|
* @return Copy of the list of listeners.
|
||||||
*/
|
*/
|
||||||
public ListenerHandler[] getListeners() {
|
public Subscriber<?, ?>[] getListeners() {
|
||||||
return listeners.toArray(new ListenerHandler[0]);
|
return listeners.toArray(new Subscriber[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,193 +1,45 @@
|
|||||||
package org.toop.framework.eventbus;
|
package org.toop.framework.eventbus;
|
||||||
|
|
||||||
import com.lmax.disruptor.*;
|
|
||||||
import com.lmax.disruptor.dsl.Disruptor;
|
|
||||||
import com.lmax.disruptor.dsl.ProducerType;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.toop.framework.eventbus.bus.DisruptorEventBus;
|
||||||
import org.toop.framework.eventbus.events.EventType;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.eventbus.events.UniqueEvent;
|
import org.toop.framework.eventbus.holder.AsyncArraySubscriberStore;
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
/**
|
public class GlobalEventBus implements EventBus {
|
||||||
* GlobalEventBus backed by the LMAX Disruptor for ultra-low latency, high-throughput event
|
private static final EventBus INSTANCE = new DisruptorEventBus(
|
||||||
* publishing.
|
LogManager.getLogger(DisruptorEventBus.class),
|
||||||
*/
|
new AsyncArraySubscriberStore()
|
||||||
public final class GlobalEventBus {
|
);
|
||||||
private static final Logger logger = LogManager.getLogger(GlobalEventBus.class);
|
|
||||||
|
|
||||||
/** Map of event class to type-specific listeners. */
|
|
||||||
private static final Map<Class<?>, CopyOnWriteArrayList<ListenerHandler<?>>>
|
|
||||||
LISTENERS = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/** Map of event class to Snowflake-ID-specific listeners. */
|
|
||||||
private static final Map<
|
|
||||||
Class<?>, ConcurrentHashMap<Long, Consumer<? extends UniqueEvent>>>
|
|
||||||
UUID_LISTENERS = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/** Disruptor ring buffer size (must be power of two). */
|
|
||||||
private static final int RING_BUFFER_SIZE = 1024 * 64;
|
|
||||||
|
|
||||||
/** Disruptor instance. */
|
|
||||||
private static final Disruptor<EventHolder> DISRUPTOR;
|
|
||||||
|
|
||||||
/** Ring buffer used for publishing events. */
|
|
||||||
private static final RingBuffer<EventHolder> RING_BUFFER;
|
|
||||||
|
|
||||||
static {
|
|
||||||
ThreadFactory threadFactory =
|
|
||||||
r -> {
|
|
||||||
Thread t = new Thread(r, "EventBus-Disruptor");
|
|
||||||
t.setDaemon(true);
|
|
||||||
return t;
|
|
||||||
};
|
|
||||||
|
|
||||||
DISRUPTOR =
|
|
||||||
new Disruptor<>(
|
|
||||||
EventHolder::new,
|
|
||||||
RING_BUFFER_SIZE,
|
|
||||||
threadFactory,
|
|
||||||
ProducerType.MULTI,
|
|
||||||
new BusySpinWaitStrategy());
|
|
||||||
|
|
||||||
DISRUPTOR.handleEventsWith(
|
|
||||||
(holder, seq, endOfBatch) -> {
|
|
||||||
if (holder.event != null) {
|
|
||||||
dispatchEvent(holder.event);
|
|
||||||
holder.event = null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
DISRUPTOR.start();
|
|
||||||
RING_BUFFER = DISRUPTOR.getRingBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Prevent instantiation. */
|
|
||||||
private GlobalEventBus() {}
|
private GlobalEventBus() {}
|
||||||
|
|
||||||
/** Wrapper used inside the ring buffer. */
|
public static EventBus get() {
|
||||||
private static class EventHolder {
|
return INSTANCE;
|
||||||
EventType event;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
@Override
|
||||||
// Subscription
|
public void subscribe(Subscriber<?, ?> listener) {
|
||||||
// ------------------------------------------------------------------------
|
INSTANCE.subscribe(listener);
|
||||||
public static <T extends EventType> void subscribe(ListenerHandler<T> listener) {
|
|
||||||
logger.debug("Subscribing to {}: {}", listener.getListenerClass().getSimpleName(), listener.getListener().getClass().getSimpleName());
|
|
||||||
LISTENERS.computeIfAbsent(listener.getListenerClass(), _ -> new CopyOnWriteArrayList<>()).add(listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
@Override
|
||||||
public static <T extends UniqueEvent> void subscribeById(
|
public void unsubscribe(Subscriber<?, ?> listener) {
|
||||||
Class<T> eventClass, long eventId, Consumer<T> listener) {
|
INSTANCE.unsubscribe(listener);
|
||||||
UUID_LISTENERS
|
|
||||||
.computeIfAbsent(eventClass, _ -> new ConcurrentHashMap<>())
|
|
||||||
.put(eventId, listener);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void unsubscribe(ListenerHandler<?> listener) {
|
@Override
|
||||||
logger.debug("Unsubscribing from {}: {}", listener.getListenerClass().getSimpleName(), listener.getListener().getClass().getSimpleName());
|
public <T> void post(T event) {
|
||||||
LISTENERS.getOrDefault(listener.getListenerClass(), new CopyOnWriteArrayList<>())
|
INSTANCE.post(event);
|
||||||
.remove(listener);
|
|
||||||
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
@Override
|
||||||
public static <T extends UniqueEvent> void unsubscribeById(
|
public void shutdown() {
|
||||||
Class<T> eventClass, long eventId) {
|
INSTANCE.shutdown();
|
||||||
Map<Long, Consumer<? extends UniqueEvent>> map = UUID_LISTENERS.get(eventClass);
|
|
||||||
if (map != null) map.remove(eventId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
@Override
|
||||||
// Posting
|
public void reset() {
|
||||||
// ------------------------------------------------------------------------
|
INSTANCE.reset();
|
||||||
public static <T extends EventType> void post(T event) {
|
|
||||||
dispatchEvent(event); // synchronous
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T extends EventType> void postAsync(T event) {
|
|
||||||
long seq = RING_BUFFER.next();
|
|
||||||
try {
|
|
||||||
EventHolder holder = RING_BUFFER.get(seq);
|
|
||||||
holder.event = event;
|
|
||||||
} finally {
|
|
||||||
RING_BUFFER.publish(seq);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static <T extends EventType> void callListener(ListenerHandler<?> raw, EventType event) {
|
|
||||||
ListenerHandler<T> handler = (ListenerHandler<T>) raw;
|
|
||||||
Consumer<T> listener = handler.getListener();
|
|
||||||
|
|
||||||
T casted = (T) event;
|
|
||||||
|
|
||||||
listener.accept(casted);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static void dispatchEvent(EventType event) {
|
|
||||||
Class<?> clazz = event.getClass();
|
|
||||||
|
|
||||||
logger.debug("Triggered event: {}", event.getClass().getSimpleName());
|
|
||||||
|
|
||||||
CopyOnWriteArrayList<ListenerHandler<?>> classListeners = LISTENERS.get(clazz);
|
|
||||||
if (classListeners != null) {
|
|
||||||
for (ListenerHandler<?> listener : classListeners) {
|
|
||||||
try {
|
|
||||||
callListener(listener, event);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CopyOnWriteArrayList<ListenerHandler<?>> genericListeners = LISTENERS.get(Object.class);
|
|
||||||
if (genericListeners != null) {
|
|
||||||
for (ListenerHandler<?> listener : genericListeners) {
|
|
||||||
try {
|
|
||||||
callListener(listener, event);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event instanceof UniqueEvent snowflakeEvent) {
|
|
||||||
Map<Long, Consumer<? extends UniqueEvent>> map = UUID_LISTENERS.get(clazz);
|
|
||||||
if (map != null) {
|
|
||||||
Consumer<UniqueEvent> listener =
|
|
||||||
(Consumer<UniqueEvent>) map.remove(snowflakeEvent.getIdentifier());
|
|
||||||
if (listener != null) {
|
|
||||||
try {
|
|
||||||
listener.accept(snowflakeEvent);
|
|
||||||
} catch (Throwable ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
// Lifecycle
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
public static void shutdown() {
|
|
||||||
DISRUPTOR.shutdown();
|
|
||||||
LISTENERS.clear();
|
|
||||||
UUID_LISTENERS.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void reset() {
|
|
||||||
LISTENERS.clear();
|
|
||||||
UUID_LISTENERS.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<Class<?>, CopyOnWriteArrayList<ListenerHandler<?>>> getAllListeners() {
|
|
||||||
return LISTENERS;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
package org.toop.framework.eventbus;
|
|
||||||
|
|
||||||
import org.toop.framework.SnowflakeGenerator;
|
|
||||||
import org.toop.framework.eventbus.events.EventType;
|
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public class ListenerHandler<T extends EventType> {
|
|
||||||
private final long id;
|
|
||||||
private final String name;
|
|
||||||
private final Class<T> clazz;
|
|
||||||
private final Consumer<T> listener;
|
|
||||||
|
|
||||||
public ListenerHandler(long id, String name, Class<T> clazz, Consumer<T> listener) {
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.clazz = clazz;
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ListenerHandler(String name, Class<T> clazz, Consumer<T> listener) {
|
|
||||||
this(SnowflakeGenerator.nextId(), name, clazz, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ListenerHandler(long id, Class<T> clazz, Consumer<T> listener) {
|
|
||||||
this(id, String.valueOf(id), clazz, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ListenerHandler(Class<T> clazz, Consumer<T> listener) {
|
|
||||||
this(SnowflakeGenerator.nextId(), clazz, listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Consumer<T> getListener() {
|
|
||||||
return listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<T> getListenerClass() {
|
|
||||||
return clazz;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package org.toop.framework.eventbus.bus;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
import org.toop.framework.eventbus.holder.SubscriberStore;
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class DefaultEventBus implements EventBus {
|
||||||
|
private final Logger logger;
|
||||||
|
private final SubscriberStore eventsHolder;
|
||||||
|
|
||||||
|
public DefaultEventBus(Logger logger, SubscriberStore eventsHolder) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.eventsHolder = eventsHolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void subscribe(Subscriber<?, ?> subscriber) {
|
||||||
|
eventsHolder.add(subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unsubscribe(Subscriber<?, ?> subscriber) {
|
||||||
|
eventsHolder.remove(subscriber);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> void post(T event) {
|
||||||
|
Class<T> eventType = (Class<T>) event.getClass();
|
||||||
|
var subs = eventsHolder.get(eventType);
|
||||||
|
if (subs != null) {
|
||||||
|
for (Subscriber<?, ?> subscriber : subs) {
|
||||||
|
Class<T> eventClass = (Class<T>) subscriber.getEvent();
|
||||||
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.getAction();
|
||||||
|
|
||||||
|
action.accept((EventType) eventClass.cast(event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
eventsHolder.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
eventsHolder.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
package org.toop.framework.eventbus.bus;
|
||||||
|
|
||||||
|
import com.lmax.disruptor.BusySpinWaitStrategy;
|
||||||
|
import com.lmax.disruptor.RingBuffer;
|
||||||
|
import com.lmax.disruptor.dsl.Disruptor;
|
||||||
|
import com.lmax.disruptor.dsl.ProducerType;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
import org.toop.framework.eventbus.holder.SubscriberStore;
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadFactory;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class DisruptorEventBus implements EventBus {
|
||||||
|
/** Wrapper used inside the ring buffer. */
|
||||||
|
private static class EventHolder<T> {
|
||||||
|
T event;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Logger logger;
|
||||||
|
private final SubscriberStore eventsHolder;
|
||||||
|
|
||||||
|
private final Disruptor<EventHolder<?>> disruptor;
|
||||||
|
private final RingBuffer<EventHolder<?>> ringBuffer;
|
||||||
|
|
||||||
|
public DisruptorEventBus(Logger logger, SubscriberStore eventsHolder) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.eventsHolder = eventsHolder;
|
||||||
|
|
||||||
|
ThreadFactory threadFactory =
|
||||||
|
r -> {
|
||||||
|
Thread t = new Thread(r, "EventBus-Disruptor");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
};
|
||||||
|
|
||||||
|
disruptor = getEventHolderDisruptor(threadFactory);
|
||||||
|
|
||||||
|
disruptor.start();
|
||||||
|
this.ringBuffer = disruptor.getRingBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Disruptor<EventHolder<?>> getEventHolderDisruptor(ThreadFactory threadFactory) {
|
||||||
|
int RING_BUFFER_SIZE = 1024 * 64;
|
||||||
|
Disruptor<EventHolder<?>> disruptor = new Disruptor<>(
|
||||||
|
EventHolder::new,
|
||||||
|
RING_BUFFER_SIZE,
|
||||||
|
threadFactory,
|
||||||
|
ProducerType.MULTI,
|
||||||
|
new BusySpinWaitStrategy());
|
||||||
|
|
||||||
|
disruptor.handleEventsWith(
|
||||||
|
(holder, _, _) -> {
|
||||||
|
if (holder.event != null) {
|
||||||
|
dispatchEvent(holder.event);
|
||||||
|
holder.event = null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return disruptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void subscribe(Subscriber<?, ?> listener) {
|
||||||
|
eventsHolder.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unsubscribe(Subscriber<?, ?> listener) {
|
||||||
|
eventsHolder.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> void post(T event) {
|
||||||
|
long seq = ringBuffer.next();
|
||||||
|
try {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
EventHolder<T> holder = (EventHolder<T>) ringBuffer.get(seq);
|
||||||
|
holder.event = event;
|
||||||
|
} finally {
|
||||||
|
ringBuffer.publish(seq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
disruptor.shutdown();
|
||||||
|
eventsHolder.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
eventsHolder.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void dispatchEvent(T event) {
|
||||||
|
var classListeners = eventsHolder.get(event.getClass());
|
||||||
|
if (classListeners != null) {
|
||||||
|
for (Subscriber<?, ?> listener : classListeners) {
|
||||||
|
try {
|
||||||
|
callListener(listener, event);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("Exception while handling event: {}", event, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private <T> void callListener(Subscriber<?, ?> subscriber, T event) {
|
||||||
|
Class<T> eventClass = (Class<T>) subscriber.getEvent();
|
||||||
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.getAction();
|
||||||
|
|
||||||
|
action.accept((EventType) eventClass.cast(event));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.toop.framework.eventbus.bus;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
public interface EventBus {
|
||||||
|
void subscribe(Subscriber<?, ?> subscriber);
|
||||||
|
void unsubscribe(Subscriber<?, ?> subscriber);
|
||||||
|
<T> void post(T event);
|
||||||
|
void shutdown();
|
||||||
|
void reset();
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package org.toop.framework.eventbus.holder;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class AsyncArraySubscriberStore implements SubscriberStore {
|
||||||
|
|
||||||
|
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
||||||
|
|
||||||
|
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> listeners =
|
||||||
|
new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Subscriber<?, ?> sub) {
|
||||||
|
listeners.compute(sub.getEvent(), (_, arr) -> {
|
||||||
|
if (arr == null || arr.length == 0) {
|
||||||
|
return new Subscriber<?, ?>[]{sub};
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = arr.length;
|
||||||
|
Subscriber<?, ?>[] newArr = new Subscriber[len + 1];
|
||||||
|
System.arraycopy(arr, 0, newArr, 0, len);
|
||||||
|
newArr[len] = sub;
|
||||||
|
return newArr;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
|
listeners.computeIfPresent(sub.getEvent(), (_, arr) -> {
|
||||||
|
int len = arr.length;
|
||||||
|
|
||||||
|
if (len == 1) {
|
||||||
|
return arr[0].equals(sub) ? null : arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int keep = 0;
|
||||||
|
for (Subscriber<?, ?> s : arr) {
|
||||||
|
if (!s.equals(sub)) keep++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keep == len) {
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
if (keep == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Subscriber<?, ?>[] newArr = new Subscriber[keep];
|
||||||
|
int i = 0;
|
||||||
|
for (Subscriber<?, ?> s : arr) {
|
||||||
|
if (!s.equals(sub)) {
|
||||||
|
newArr[i++] = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newArr;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Subscriber<?, ?>[] get(Class<?> event) {
|
||||||
|
return listeners.getOrDefault(event, EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
listeners.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package org.toop.framework.eventbus.holder;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
public class AsyncSubscriberStore implements SubscriberStore {
|
||||||
|
private final ConcurrentHashMap<Class<?>, ConcurrentLinkedQueue<Subscriber<?, ?>>> queues = new ConcurrentHashMap<>();
|
||||||
|
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> snapshots = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Subscriber<?, ?> sub) {
|
||||||
|
queues.computeIfAbsent(sub.getEvent(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
||||||
|
rebuildSnapshot(sub.getEvent());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
|
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(sub.getEvent());
|
||||||
|
if (queue != null) {
|
||||||
|
queue.remove(sub);
|
||||||
|
rebuildSnapshot(sub.getEvent());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Subscriber<?, ?>[] get(Class<?> event) {
|
||||||
|
return snapshots.getOrDefault(event, new Subscriber[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
queues.clear();
|
||||||
|
snapshots.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void rebuildSnapshot(Class<?> event) {
|
||||||
|
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(event);
|
||||||
|
if (queue != null) {
|
||||||
|
snapshots.put(event, queue.toArray(new Subscriber[0]));
|
||||||
|
} else {
|
||||||
|
snapshots.put(event, new Subscriber[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package org.toop.framework.eventbus.holder;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
public interface SubscriberStore {
|
||||||
|
void add(Subscriber<?, ?> subscriber);
|
||||||
|
void remove(Subscriber<?, ?> subscriber);
|
||||||
|
Subscriber<?, ?>[] get(Class<?> event);
|
||||||
|
void reset();
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package org.toop.framework.eventbus.holder;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
public class SyncSubscriberStore implements SubscriberStore {
|
||||||
|
private final Map<Class<?>, List<Subscriber<?, ?>>> LISTENERS = new ConcurrentHashMap<>();
|
||||||
|
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Subscriber<?, ?> sub) {
|
||||||
|
LISTENERS.computeIfAbsent(sub.getEvent(), _ -> new ArrayList<>()).add(sub);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
|
LISTENERS.getOrDefault(sub.getEvent(), new ArrayList<>()).remove(sub);
|
||||||
|
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Subscriber<?, ?>[] get(Class<?> event) {
|
||||||
|
List<Subscriber<?, ?>> list = LISTENERS.get(event);
|
||||||
|
if (list == null || list.isEmpty()) return EMPTY;
|
||||||
|
return list.toArray(EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
LISTENERS.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class DefaultSubscriber<T, K> implements Subscriber<K, T> {
|
||||||
|
private final K id;
|
||||||
|
private final Class<T> event;
|
||||||
|
private final Consumer<T> action;
|
||||||
|
|
||||||
|
public DefaultSubscriber(K id, Class<T> eventClass, Consumer<T> action) {
|
||||||
|
this.id = id;
|
||||||
|
this.event = eventClass;
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public K getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<T> getEvent() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Consumer<T> getAction() {
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public interface Subscriber<T, K> {
|
||||||
|
T getId();
|
||||||
|
Class<K> getEvent();
|
||||||
|
Consumer<K> getAction();
|
||||||
|
}
|
||||||
@@ -4,19 +4,20 @@ 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.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.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);
|
||||||
|
|
||||||
private final NetworkingClientManager clientManager;
|
private final NetworkingClientManager clientManager;
|
||||||
|
|
||||||
/** Starts a connection manager, to manage, connections. */
|
/** Starts a connection manager, to manage, connections. */
|
||||||
public NetworkingClientEventListener(NetworkingClientManager clientManager) {
|
public NetworkingClientEventListener(EventBus eventBus, NetworkingClientManager clientManager) {
|
||||||
this.clientManager = clientManager;
|
this.clientManager = clientManager;
|
||||||
new EventFlow()
|
new EventFlow(eventBus)
|
||||||
.listen(NetworkEvents.StartClient.class, this::handleStartClient, false)
|
.listen(NetworkEvents.StartClient.class, this::handleStartClient, false)
|
||||||
.listen(NetworkEvents.SendCommand.class, this::handleCommand, false)
|
.listen(NetworkEvents.SendCommand.class, this::handleCommand, false)
|
||||||
.listen(NetworkEvents.SendLogin.class, this::handleSendLogin, false)
|
.listen(NetworkEvents.SendLogin.class, this::handleSendLogin, false)
|
||||||
@@ -40,6 +41,7 @@ public class NetworkingClientEventListener {
|
|||||||
|
|
||||||
void handleStartClient(NetworkEvents.StartClient event) {
|
void handleStartClient(NetworkEvents.StartClient event) {
|
||||||
long clientId = SnowflakeGenerator.nextId();
|
long clientId = SnowflakeGenerator.nextId();
|
||||||
|
new EventFlow().addPostEvent(new NetworkEvents.CreatedIdForClient(clientId, event.identifier())).postEvent();
|
||||||
clientManager.startClient(
|
clientManager.startClient(
|
||||||
clientId,
|
clientId,
|
||||||
event.networkingClient(),
|
event.networkingClient(),
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
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.GlobalEventBus;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.events.NetworkEvents;
|
import org.toop.framework.networking.events.NetworkEvents;
|
||||||
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
import org.toop.framework.networking.exceptions.ClientNotFoundException;
|
||||||
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
||||||
@@ -17,9 +18,13 @@ import org.toop.framework.networking.types.NetworkingConnector;
|
|||||||
|
|
||||||
public class NetworkingClientManager implements org.toop.framework.networking.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 Map<Long, NetworkingClient> networkClients = new ConcurrentHashMap<>();
|
private final Map<Long, NetworkingClient> networkClients = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public NetworkingClientManager() {}
|
public NetworkingClientManager(EventBus eventBus) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
}
|
||||||
|
|
||||||
private void connectHelper(
|
private void connectHelper(
|
||||||
long id,
|
long id,
|
||||||
@@ -28,8 +33,16 @@ public class NetworkingClientManager implements org.toop.framework.networking.in
|
|||||||
Runnable onSuccess,
|
Runnable onSuccess,
|
||||||
Runnable onFailure
|
Runnable onFailure
|
||||||
) {
|
) {
|
||||||
|
|
||||||
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
|
EventFlow closeEvent = new EventFlow()
|
||||||
|
.listen(
|
||||||
|
NetworkEvents.CloseClient.class,
|
||||||
|
e -> {
|
||||||
|
if (e.clientId() == id) scheduler.shutdownNow();
|
||||||
|
}, "close");
|
||||||
|
|
||||||
Runnable connectTask = new Runnable() {
|
Runnable connectTask = new Runnable() {
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
|
|
||||||
@@ -46,7 +59,7 @@ public class NetworkingClientManager implements org.toop.framework.networking.in
|
|||||||
nClient.connect(id, nConnector.host(), nConnector.port());
|
nClient.connect(id, nConnector.host(), nConnector.port());
|
||||||
networkClients.put(id, nClient);
|
networkClients.put(id, nClient);
|
||||||
logger.info("New client started successfully for {}:{}", nConnector.host(), nConnector.port());
|
logger.info("New client started successfully for {}:{}", nConnector.host(), nConnector.port());
|
||||||
GlobalEventBus.post(new NetworkEvents.ConnectTry(id, attempts, nConnector.reconnectAttempts(), true));
|
eventBus.post(new NetworkEvents.ConnectTry(id, attempts, nConnector.reconnectAttempts(), true));
|
||||||
onSuccess.run();
|
onSuccess.run();
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
} catch (CouldNotConnectException e) {
|
} catch (CouldNotConnectException e) {
|
||||||
@@ -54,17 +67,17 @@ public class NetworkingClientManager implements org.toop.framework.networking.in
|
|||||||
if (attempts < nConnector.reconnectAttempts()) {
|
if (attempts < nConnector.reconnectAttempts()) {
|
||||||
logger.warn("Could not connect to {}:{}. Retrying in {} {}",
|
logger.warn("Could not connect to {}:{}. Retrying in {} {}",
|
||||||
nConnector.host(), nConnector.port(), nConnector.timeout(), nConnector.timeUnit());
|
nConnector.host(), nConnector.port(), nConnector.timeout(), nConnector.timeUnit());
|
||||||
GlobalEventBus.post(new NetworkEvents.ConnectTry(id, attempts, nConnector.reconnectAttempts(), false));
|
eventBus.post(new NetworkEvents.ConnectTry(id, attempts, nConnector.reconnectAttempts(), false));
|
||||||
scheduler.schedule(this, nConnector.timeout(), nConnector.timeUnit());
|
scheduler.schedule(this, nConnector.timeout(), nConnector.timeUnit());
|
||||||
} else {
|
} else {
|
||||||
logger.error("Failed to start client for {}:{} after {} attempts", nConnector.host(), nConnector.port(), attempts);
|
logger.error("Failed to start client for {}:{} after {} attempts", nConnector.host(), nConnector.port(), attempts);
|
||||||
GlobalEventBus.post(new NetworkEvents.ConnectTry(id, -1, nConnector.reconnectAttempts(), false));
|
eventBus.post(new NetworkEvents.ConnectTry(id, -1, nConnector.reconnectAttempts(), false));
|
||||||
onFailure.run();
|
onFailure.run();
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Unexpected exception during startClient", e);
|
logger.error("Unexpected exception during startClient", e);
|
||||||
GlobalEventBus.post(new NetworkEvents.ConnectTry(id, -1, nConnector.reconnectAttempts(), false));
|
eventBus.post(new NetworkEvents.ConnectTry(id, -1, nConnector.reconnectAttempts(), false));
|
||||||
onFailure.run();
|
onFailure.run();
|
||||||
scheduler.shutdown();
|
scheduler.shutdown();
|
||||||
}
|
}
|
||||||
@@ -72,6 +85,8 @@ public class NetworkingClientManager implements org.toop.framework.networking.in
|
|||||||
};
|
};
|
||||||
|
|
||||||
scheduler.schedule(connectTask, 0, TimeUnit.MILLISECONDS);
|
scheduler.schedule(connectTask, 0, TimeUnit.MILLISECONDS);
|
||||||
|
//
|
||||||
|
// closeEvent.unsubscribe("close");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import io.netty.handler.codec.string.StringEncoder;
|
|||||||
import io.netty.util.CharsetUtil;
|
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.networking.exceptions.CouldNotConnectException;
|
import org.toop.framework.networking.exceptions.CouldNotConnectException;
|
||||||
import org.toop.framework.networking.handlers.NetworkingGameClientHandler;
|
import org.toop.framework.networking.handlers.NetworkingGameClientHandler;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClient;
|
import org.toop.framework.networking.interfaces.NetworkingClient;
|
||||||
@@ -19,9 +20,13 @@ import java.net.InetSocketAddress;
|
|||||||
|
|
||||||
public class TournamentNetworkingClient implements NetworkingClient {
|
public class TournamentNetworkingClient implements NetworkingClient {
|
||||||
private static final Logger logger = LogManager.getLogger(TournamentNetworkingClient.class);
|
private static final Logger logger = LogManager.getLogger(TournamentNetworkingClient.class);
|
||||||
|
|
||||||
|
private final EventBus eventBus;
|
||||||
private Channel channel;
|
private Channel channel;
|
||||||
|
|
||||||
public TournamentNetworkingClient() {}
|
public TournamentNetworkingClient(EventBus eventBus) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InetSocketAddress getAddress() {
|
public InetSocketAddress getAddress() {
|
||||||
@@ -40,7 +45,7 @@ public class TournamentNetworkingClient implements NetworkingClient {
|
|||||||
new ChannelInitializer<SocketChannel>() {
|
new ChannelInitializer<SocketChannel>() {
|
||||||
@Override
|
@Override
|
||||||
public void initChannel(SocketChannel ch) {
|
public void initChannel(SocketChannel ch) {
|
||||||
NetworkingGameClientHandler handler = new NetworkingGameClientHandler(clientId);
|
NetworkingGameClientHandler handler = new NetworkingGameClientHandler(eventBus, clientId);
|
||||||
|
|
||||||
ChannelPipeline pipeline = ch.pipeline();
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
pipeline.addLast(new LineBasedFrameDecoder(1024)); // split at \n
|
pipeline.addLast(new LineBasedFrameDecoder(1024)); // split at \n
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.*;
|
|||||||
import java.util.concurrent.CompletableFuture;
|
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.events.*;
|
import org.toop.framework.eventbus.events.*;
|
||||||
import org.toop.framework.networking.interfaces.NetworkingClient;
|
import org.toop.framework.networking.interfaces.NetworkingClient;
|
||||||
import org.toop.framework.networking.types.NetworkingConnector;
|
import org.toop.framework.networking.types.NetworkingConnector;
|
||||||
@@ -11,7 +12,7 @@ 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.
|
||||||
* <p>
|
* <p>
|
||||||
* These events are used in conjunction with the {@link org.toop.framework.eventbus.GlobalEventBus}
|
* These events are used in conjunction with the {@link GlobalEventBus}
|
||||||
* and {@link org.toop.framework.eventbus.EventFlow} to communicate between components
|
* and {@link org.toop.framework.eventbus.EventFlow} to communicate between components
|
||||||
* such as networking clients, managers, and listeners.
|
* such as networking clients, managers, and listeners.
|
||||||
* </p>
|
* </p>
|
||||||
@@ -166,6 +167,10 @@ public class NetworkEvents extends EventsBase {
|
|||||||
long identifier)
|
long identifier)
|
||||||
implements UniqueEvent {}
|
implements UniqueEvent {}
|
||||||
|
|
||||||
|
public record CreatedIdForClient(long clientId, long identifier) implements ResponseToUniqueEvent {}
|
||||||
|
|
||||||
|
public record ConnectTry(long clientId, int amount, int maxAmount, boolean success) implements GenericEvent {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response confirming that a client has been successfully started.
|
* Response confirming that a client has been successfully started.
|
||||||
* <p>
|
* <p>
|
||||||
@@ -181,8 +186,6 @@ public class NetworkEvents extends EventsBase {
|
|||||||
public record StartClientResponse(long clientId, boolean successful, long identifier)
|
public record StartClientResponse(long clientId, boolean successful, long identifier)
|
||||||
implements ResponseToUniqueEvent {}
|
implements ResponseToUniqueEvent {}
|
||||||
|
|
||||||
public record ConnectTry(long clientId, int amount, int maxAmount, boolean success) implements GenericEvent {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests reconnection of an existing client using its previous configuration.
|
* Requests reconnection of an existing client using its previous configuration.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -8,15 +8,17 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
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.EventFlow;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.networking.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);
|
||||||
|
|
||||||
|
private final EventBus eventBus;
|
||||||
private final long connectionId;
|
private final long connectionId;
|
||||||
|
|
||||||
public NetworkingGameClientHandler(long connectionId) {
|
public NetworkingGameClientHandler(EventBus eventBus, long connectionId) {
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.connectionId = connectionId;
|
this.connectionId = connectionId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,9 +42,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
"Received SVR message from server-{}, data: {}",
|
"Received SVR message from server-{}, data: {}",
|
||||||
ctx.channel().remoteAddress(),
|
ctx.channel().remoteAddress(),
|
||||||
msg);
|
msg);
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.ServerResponse(this.connectionId));
|
||||||
.addPostEvent(new NetworkEvents.ServerResponse(this.connectionId))
|
|
||||||
.asyncPostEvent();
|
|
||||||
parseServerReturn(rec);
|
parseServerReturn(rec);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -113,11 +113,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.map(m -> m.group(1).trim())
|
.map(m -> m.group(1).trim())
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.GameMoveResponse(this.connectionId, msg[0], msg[1], msg[2]));
|
||||||
.addPostEvent(
|
|
||||||
new NetworkEvents.GameMoveResponse(
|
|
||||||
this.connectionId, msg[0], msg[1], msg[2]))
|
|
||||||
.asyncPostEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void gameWinConditionHandler(String rec) {
|
private void gameWinConditionHandler(String rec) {
|
||||||
@@ -128,9 +124,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.findFirst()
|
.findFirst()
|
||||||
.orElse("");
|
.orElse("");
|
||||||
|
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.GameResultResponse(this.connectionId, condition));
|
||||||
.addPostEvent(new NetworkEvents.GameResultResponse(this.connectionId, condition))
|
|
||||||
.asyncPostEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void gameChallengeHandler(String rec) {
|
private void gameChallengeHandler(String rec) {
|
||||||
@@ -145,17 +139,9 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
if (isCancelled)
|
if (isCancelled)
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.GameResultResponse(this.connectionId, msg[0]));
|
||||||
.addPostEvent(
|
|
||||||
new NetworkEvents.ChallengeCancelledResponse(
|
|
||||||
this.connectionId, msg[0]))
|
|
||||||
.asyncPostEvent();
|
|
||||||
else
|
else
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.ChallengeResponse(this.connectionId, msg[0], msg[1], msg[2]));
|
||||||
.addPostEvent(
|
|
||||||
new NetworkEvents.ChallengeResponse(
|
|
||||||
this.connectionId, msg[0], msg[1], msg[2]))
|
|
||||||
.asyncPostEvent();
|
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
logger.error("Array out of bounds for: {}", rec, e);
|
logger.error("Array out of bounds for: {}", rec, e);
|
||||||
}
|
}
|
||||||
@@ -171,11 +157,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
// [0] playerToMove, [1] gameType, [2] opponent
|
// [0] playerToMove, [1] gameType, [2] opponent
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.GameMatchResponse(this.connectionId, msg[0], msg[1], msg[2]));
|
||||||
.addPostEvent(
|
|
||||||
new NetworkEvents.GameMatchResponse(
|
|
||||||
this.connectionId, msg[0], msg[1], msg[2]))
|
|
||||||
.asyncPostEvent();
|
|
||||||
} catch (ArrayIndexOutOfBoundsException e) {
|
} catch (ArrayIndexOutOfBoundsException e) {
|
||||||
logger.error("Array out of bounds for: {}", rec, e);
|
logger.error("Array out of bounds for: {}", rec, e);
|
||||||
}
|
}
|
||||||
@@ -190,9 +172,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.toString()
|
.toString()
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.YourTurnResponse(this.connectionId, msg));
|
||||||
.addPostEvent(new NetworkEvents.YourTurnResponse(this.connectionId, msg))
|
|
||||||
.asyncPostEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playerlistHandler(String rec) {
|
private void playerlistHandler(String rec) {
|
||||||
@@ -203,9 +183,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.map(m -> m.group(1).trim())
|
.map(m -> m.group(1).trim())
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.PlayerlistResponse(this.connectionId, players));
|
||||||
.addPostEvent(new NetworkEvents.PlayerlistResponse(this.connectionId, players))
|
|
||||||
.asyncPostEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void gamelistHandler(String rec) {
|
private void gamelistHandler(String rec) {
|
||||||
@@ -216,9 +194,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
.map(m -> m.group(1).trim())
|
.map(m -> m.group(1).trim())
|
||||||
.toArray(String[]::new);
|
.toArray(String[]::new);
|
||||||
|
|
||||||
new EventFlow()
|
eventBus.post(new NetworkEvents.GamelistResponse(this.connectionId, gameTypes));
|
||||||
.addPostEvent(new NetworkEvents.GamelistResponse(this.connectionId, gameTypes))
|
|
||||||
.asyncPostEvent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void helpHandler(String rec) {
|
private void helpHandler(String rec) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.toop.framework.audio;
|
|||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.toop.framework.dispatch.interfaces.Dispatcher;
|
import org.toop.framework.dispatch.interfaces.Dispatcher;
|
||||||
|
import org.toop.framework.eventbus.GlobalEventBus;
|
||||||
import org.toop.framework.resource.resources.BaseResource;
|
import org.toop.framework.resource.resources.BaseResource;
|
||||||
import org.toop.framework.resource.types.AudioResource;
|
import org.toop.framework.resource.types.AudioResource;
|
||||||
|
|
||||||
@@ -94,7 +95,7 @@ public class MusicManagerTest {
|
|||||||
|
|
||||||
List<MockAudioResource> resources = List.of(track1, track2, track3);
|
List<MockAudioResource> resources = List.of(track1, track2, track3);
|
||||||
|
|
||||||
manager = new MusicManager<>(resources, dispatcher);
|
manager = new MusicManager<>(GlobalEventBus.get(), resources, dispatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -188,7 +189,7 @@ public class MusicManagerTest {
|
|||||||
manyTracks.add(new MockAudioResource("track" + i));
|
manyTracks.add(new MockAudioResource("track" + i));
|
||||||
}
|
}
|
||||||
|
|
||||||
MusicManager<MockAudioResource> multiManager = new MusicManager<>(manyTracks, dispatcher);
|
MusicManager<MockAudioResource> multiManager = new MusicManager<>(GlobalEventBus.get(), manyTracks, dispatcher);
|
||||||
|
|
||||||
for (int i = 0; i < manyTracks.size() - 1; i++) {
|
for (int i = 0; i < manyTracks.size() - 1; i++) {
|
||||||
multiManager.play();
|
multiManager.play();
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ public final class ReversiR extends AbstractGame<ReversiR> {
|
|||||||
// TODO: Don't hardcore for two players :)
|
// TODO: Don't hardcore for two players :)
|
||||||
public record Score(int player1Score, int player2Score) {}
|
public record Score(int player1Score, int player2Score) {}
|
||||||
|
|
||||||
public ReversiR(Player<ReversiR>[] players) {
|
public ReversiR(Player<ReversiR>[] players) {
|
||||||
super(8, 8, 2, players);
|
super(8, 8, 2, players);
|
||||||
addStartPieces();
|
addStartPieces();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ReversiR(ReversiR other) {
|
public ReversiR(ReversiR other) {
|
||||||
super(other);
|
super(other);
|
||||||
@@ -53,8 +53,8 @@ public final class ReversiR extends AbstractGame<ReversiR> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int[] getLegalMoves() {
|
public int[] getLegalMoves() {
|
||||||
final ArrayList<Integer> legalMoves = new ArrayList<>();
|
final ArrayList<Integer> legalMoves = new ArrayList<>();
|
||||||
int[][] boardGrid = makeBoardAGrid();
|
int[][] boardGrid = makeBoardAGrid();
|
||||||
int currentPlayer = this.getCurrentTurn();
|
int currentPlayer = this.getCurrentTurn();
|
||||||
@@ -67,7 +67,7 @@ public final class ReversiR extends AbstractGame<ReversiR> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return legalMoves.stream().mapToInt(Integer::intValue).toArray();
|
return legalMoves.stream().mapToInt(Integer::intValue).toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Point> getAdjacentCells(int[][] boardGrid) {
|
private Set<Point> getAdjacentCells(int[][] boardGrid) {
|
||||||
Set<Point> possibleCells = new HashSet<>();
|
Set<Point> possibleCells = new HashSet<>();
|
||||||
@@ -76,7 +76,7 @@ public final class ReversiR extends AbstractGame<ReversiR> {
|
|||||||
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ //orthogonally and diagonally
|
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ //orthogonally and diagonally
|
||||||
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
|
int newX = point.x + deltaColumn, newY = point.y + deltaRow;
|
||||||
if (deltaColumn == 0 && deltaRow == 0 //continue if out of bounds
|
if (deltaColumn == 0 && deltaRow == 0 //continue if out of bounds
|
||||||
|| !isOnBoard(newX, newY)) {
|
|| !isOnBoard(newX, newY)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty
|
if (boardGrid[newY][newX] == EMPTY) { //check if the cell is empty
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import org.toop.framework.gameFramework.GameState;
|
|||||||
* opening or when no clear best move is found.
|
* opening or when no clear best move is found.
|
||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
|
public class TicTacToeAIR extends AbstractAI<TicTacToeR> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines the best move for the given Tic-Tac-Toe game state.
|
* Determines the best move for the given Tic-Tac-Toe game state.
|
||||||
@@ -27,8 +27,14 @@ public final class TicTacToeAIR extends AbstractAI<TicTacToeR> {
|
|||||||
* @param depth the depth of lookahead for evaluating moves (non-negative)
|
* @param depth the depth of lookahead for evaluating moves (non-negative)
|
||||||
* @return the index of the best move, or -1 if no moves are available
|
* @return the index of the best move, or -1 if no moves are available
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
private int depth;
|
||||||
|
|
||||||
|
public TicTacToeAIR(int depth) {
|
||||||
|
this.depth = depth;
|
||||||
|
}
|
||||||
|
|
||||||
public int getMove(TicTacToeR game) {
|
public int getMove(TicTacToeR game) {
|
||||||
int depth = 9;
|
|
||||||
assert game != null;
|
assert game != null;
|
||||||
final int[] legalMoves = game.getLegalMoves();
|
final int[] legalMoves = game.getLegalMoves();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package org.toop.game.games.tictactoe;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class TicTacToeAIRSleep extends TicTacToeAIR {
|
||||||
|
|
||||||
|
private int thinkTime;
|
||||||
|
|
||||||
|
public TicTacToeAIRSleep(int depth, int thinkTime) {
|
||||||
|
super(depth);
|
||||||
|
this.thinkTime = thinkTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMove(TicTacToeR game) {
|
||||||
|
int score = super.getMove(game);
|
||||||
|
try {
|
||||||
|
Random random = new Random();
|
||||||
|
Thread.sleep(this.thinkTime * 1000L + random.nextInt(1000));
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
|
|
||||||
final class TicTacToeAIRTest {
|
final class TicTacToeAIRTest {
|
||||||
|
|
||||||
private final TicTacToeAIR ai = new TicTacToeAIR();
|
private final TicTacToeAIR ai = new TicTacToeAIR(9);
|
||||||
|
|
||||||
// Helper: play multiple moves in sequence on a fresh board
|
// Helper: play multiple moves in sequence on a fresh board
|
||||||
private TicTacToeR playSequence(int... moves) {
|
private TicTacToeR playSequence(int... moves) {
|
||||||
|
|||||||
Reference in New Issue
Block a user