Fixed unittests. Formatting

This commit is contained in:
lieght
2025-09-28 21:45:58 +02:00
committed by Bas Antonius de Jong
parent c76b7a800e
commit a94d83292e
25 changed files with 1506 additions and 945 deletions

View File

@@ -13,6 +13,12 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.46.1</version>
</dependency>
<dependency>
<groupId>org.toop</groupId>
<artifactId>pism_framework</artifactId>
@@ -58,6 +64,41 @@
<!-- <fork>true</fork>-->
</configuration>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.46.1</version>
<configuration>
<!-- optional: limit format enforcement to just the files changed by this feature branch -->
<ratchetFrom>origin/main</ratchetFrom>
<formats>
<!-- you can define as many formats as you want, each is independent -->
<format>
<!-- define the files to apply to -->
<includes>
<include>.gitattributes</include>
<include>.gitignore</include>
</includes>
<!-- define the steps to apply to those files -->
<trimTrailingWhitespace/>
<endWithNewline/>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<!-- define a language-specific format -->
<java>
<googleJavaFormat>
<version>1.28.0</version>
<style>AOSP</style> <!-- GOOGLE (2 indents), AOSP (4 indents) -->
<reflowLongStrings>true</reflowLongStrings>
<formatJavadoc>true</formatJavadoc>
</googleJavaFormat>
</java>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,57 +1,86 @@
package org.toop;
import java.util.Arrays;
import org.toop.app.gui.LocalServerSelector;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.framework.networking.NetworkingClientManager;
import org.toop.framework.networking.NetworkingInitializationException;
import org.toop.app.gui.LocalServerSelector;
import java.util.Arrays;
import org.toop.framework.networking.events.NetworkEvents;
public class Main {
static void main(String[] args) {
initSystems();
static void main(String[] args) {
initSystems();
EventFlow a = new EventFlow()
.addPostEvent(
NetworkEvents.StartClient.class,
"127.0.0.1",
7789)
.onResponse(Main::login)
// .onResponse(Main::sendCommand)
// .onResponse(Main::closeClient)
.asyncPostEvent();
EventFlow a =
new EventFlow()
.addPostEvent(NetworkEvents.StartClient.class, "127.0.0.1", 7789)
.onResponse(Main::login)
// .onResponse(Main::sendCommand)
// .onResponse(Main::closeClient)
.asyncPostEvent();
new Thread(() -> {
while (a.getResult() == null) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
}
long clid = (Long) a.getResult().get("clientId");
new EventFlow()
.addPostEvent(new NetworkEvents.SendCommand(clid, "get playerlist"))
.listen(NetworkEvents.PlayerListResponse.class, response -> {
if (response.clientId() == clid) System.out.println(Arrays.toString(response.playerlist()));
})
.asyncPostEvent();
}).start();
new Thread(
() -> {
while (a.getResult() == null) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
long clid = (Long) a.getResult().get("clientId");
new EventFlow()
.addPostEvent(
new NetworkEvents.SendSubscribe(clid, "tic-tac-toe"))
.listen(
NetworkEvents.PlayerlistResponse.class,
response -> {
if (response.clientId() == clid)
System.out.println(
Arrays.toString(response.playerlist()));
})
.listen(
NetworkEvents.ChallengeResponse.class,
response -> {
if (response.clientId() == clid)
System.out.println(response.challengeId());
})
.listen(
NetworkEvents.ChallengeCancelledResponse.class,
response -> {
if (response.clientId() == clid)
System.out.println(response.challengeId());
})
.listen(
NetworkEvents.GamelistResponse.class,
response -> {
if (response.clientId() == clid)
System.out.println(
Arrays.toString(response.gamelist()));
})
.asyncPostEvent();
})
.start();
new Thread(() -> javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new)).start();
}
new Thread(() -> javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new)).start();
}
private static void login(NetworkEvents.StartClientResponse event) {
new Thread(() -> {
try {
Thread.sleep(1000);
new EventFlow()
.addPostEvent(new NetworkEvents.SendCommand(event.clientId(), "login bas"))
.asyncPostEvent();
} catch (InterruptedException e) {}
}).start();
}
private static void login(NetworkEvents.StartClientResponse event) {
new Thread(
() -> {
try {
Thread.sleep(1000);
new EventFlow()
.addPostEvent(
new NetworkEvents.SendCommand(
event.clientId(), "login bas"))
.asyncPostEvent();
} catch (InterruptedException e) {
}
})
.start();
}
private static void initSystems() throws NetworkingInitializationException {
new NetworkingClientManager();
}
}
private static void initSystems() throws NetworkingInitializationException {
new NetworkingClientManager();
}
}

View File

@@ -7,9 +7,9 @@ import javax.swing.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.EventFlow;
import org.toop.framework.networking.NetworkingGameClientHandler;
import org.toop.framework.networking.events.NetworkEvents;
import org.toop.tictactoe.LocalTicTacToe;
import org.toop.framework.networking.NetworkingGameClientHandler;
import org.toop.tictactoe.gui.UIGameBoard;
public class RemoteGameSelector {
@@ -55,36 +55,44 @@ public class RemoteGameSelector {
&& !portTextField.getText().isEmpty()) {
AtomicReference<Long> clientId = new AtomicReference<>();
new EventFlow().addPostEvent(
NetworkEvents.StartClient.class,
(Supplier<NetworkingGameClientHandler>)
new NetworkingGameClientHandler(clientId.get()),
"127.0.0.1",
5001
).onResponse(
NetworkEvents.StartClientResponse.class,
(response) -> {
clientId.set(response.clientId());
}
).asyncPostEvent();
new EventFlow()
.addPostEvent(
NetworkEvents.StartClient.class,
(Supplier<NetworkingGameClientHandler>)
new NetworkingGameClientHandler(clientId.get()),
"127.0.0.1",
5001)
.onResponse(
NetworkEvents.StartClientResponse.class,
(response) -> {
clientId.set(response.clientId());
})
.asyncPostEvent();
// GlobalEventBus.subscribeAndRegister(
// NetworkEvents.ReceivedMessage.class,
// event -> {
// if (event.message().equalsIgnoreCase("ok")) {
// logger.info("received ok from server.");
// } else if (event.message().toLowerCase().startsWith("gameid")) {
// String gameId =
// event.message()
// .toLowerCase()
// .replace("gameid ", "");
// GlobalEventBus.post(
// new NetworkEvents.SendCommand(
// "start_game " + gameId));
// } else {
// logger.info("{}", event.message());
// }
// });
// GlobalEventBus.subscribeAndRegister(
// NetworkEvents.ReceivedMessage.class,
// event -> {
// if
// (event.message().equalsIgnoreCase("ok")) {
// logger.info("received ok from
// server.");
// } else if
// (event.message().toLowerCase().startsWith("gameid")) {
// String gameId =
// event.message()
// .toLowerCase()
// .replace("gameid
// ", "");
// GlobalEventBus.post(
// new
// NetworkEvents.SendCommand(
// "start_game " +
// gameId));
// } else {
// logger.info("{}",
// event.message());
// }
// });
frame.remove(mainMenu);
UIGameBoard ttt = new UIGameBoard(localTicTacToe, this);
localTicTacToe.startThreads();

View File

@@ -1,7 +1,6 @@
package org.toop.tictactoe;
import java.util.concurrent.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.eventbus.EventFlow;
@@ -10,11 +9,6 @@ import org.toop.game.Game;
import org.toop.game.tictactoe.TicTacToe;
import org.toop.game.tictactoe.TicTacToeAI;
import org.toop.tictactoe.gui.UIGameBoard;
import org.toop.framework.networking.NetworkingGameClientHandler;
import java.util.function.Supplier;
import static java.lang.Thread.sleep;
/**
* A representation of a local tic-tac-toe game. Calls are made to a server for information about
@@ -71,24 +65,25 @@ public class LocalTicTacToe { // TODO: Implement runnable
* @param port The port of the server to connect to.
*/
private LocalTicTacToe(String ip, int port) {
// this.receivedMessageListener =
// GlobalEventBus.subscribe(this::receiveMessageAction);
// GlobalEventBus.subscribe(this.receivedMessageListener);
// this.connectionId = this.createConnection(ip, port); TODO: Refactor this
// this.receivedMessageListener =
// GlobalEventBus.subscribe(this::receiveMessageAction);
// GlobalEventBus.subscribe(this.receivedMessageListener);
// this.connectionId = this.createConnection(ip, port); TODO: Refactor this
this.createGame("X", "O");
this.isLocal = false;
//this.executor.submit(this::remoteGameThread);
// this.executor.submit(this::remoteGameThread);
}
private LocalTicTacToe(boolean[] aiFlags) {
this.isAiPlayer = aiFlags; // store who is AI
this.isLocal = true;
//this.executor.submit(this::localGameThread);
// this.executor.submit(this::localGameThread);
}
public void startThreads(){
public void startThreads() {
if (isLocal) {
this.executor.submit(this::localGameThread);
}else {
} else {
this.executor.submit(this::remoteGameThread);
}
}
@@ -124,10 +119,10 @@ public class LocalTicTacToe { // TODO: Implement runnable
state = this.ticTacToe.play(this.moveQueuePlayerA.take());
} else {
Game.Move bestMove = ai.findBestMove(this.ticTacToe, 9);
assert bestMove != null;
assert bestMove != null;
state = this.ticTacToe.play(bestMove);
ui.setCell(bestMove.position(), "X");
state = this.ticTacToe.play(bestMove);
ui.setCell(bestMove.position(), "X");
}
if (state == Game.State.WIN || state == Game.State.DRAW) {
ui.setState(state, "X");
@@ -138,9 +133,9 @@ public class LocalTicTacToe { // TODO: Implement runnable
state = this.ticTacToe.play(this.moveQueuePlayerB.take());
} else {
Game.Move bestMove = ai.findBestMove(this.ticTacToe, 9);
assert bestMove != null;
state = this.ticTacToe.play(bestMove);
ui.setCell(bestMove.position(), "O");
assert bestMove != null;
state = this.ticTacToe.play(bestMove);
ui.setCell(bestMove.position(), "O");
}
if (state == Game.State.WIN || state == Game.State.DRAW) {
ui.setState(state, "O");
@@ -166,8 +161,8 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
public char[] getCurrentBoard() {
//return ticTacToe.getGrid();
return new char[2];
// return ticTacToe.getGrid();
return new char[2];
}
/** End the current game. */
@@ -206,7 +201,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
private void endTheGame() {
this.sendCommand("end_game", this.gameId);
// this.endListeners();
// this.endListeners();
}
private void receiveMessageAction(NetworkEvents.ReceivedMessage receivedMessage) {
@@ -215,8 +210,7 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
try {
logger.info(
"Received message from {}: {}", this.clientId, receivedMessage.message());
logger.info("Received message from {}: {}", this.clientId, receivedMessage.message());
this.receivedQueue.put(receivedMessage.message());
} catch (InterruptedException e) {
logger.error("Error waiting for received Message", e);
@@ -224,12 +218,14 @@ public class LocalTicTacToe { // TODO: Implement runnable
}
private void sendCommand(String... args) {
new EventFlow().addPostEvent(NetworkEvents.SendCommand.class, this.clientId, args).asyncPostEvent();
new EventFlow()
.addPostEvent(NetworkEvents.SendCommand.class, this.clientId, args)
.asyncPostEvent();
}
// private void endListeners() {
// GlobalEventBus.unregister(this.receivedMessageListener);
// } TODO
// private void endListeners() {
// GlobalEventBus.unregister(this.receivedMessageListener);
// } TODO
public void setUIReference(UIGameBoard uiGameBoard) {
this.ui = uiGameBoard;