mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
Code cleanup
This commit is contained in:
@@ -22,6 +22,7 @@ import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.framework.eventbus.GlobalEventBus;
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
import org.toop.framework.game.games.reversi.BitboardReversi;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.framework.networking.connection.NetworkingClientEventListener;
|
||||
import org.toop.framework.networking.connection.NetworkingClientManager;
|
||||
import org.toop.framework.networking.server.GameDefinition;
|
||||
@@ -141,9 +142,9 @@ public final class App extends Application {
|
||||
|
||||
stage.show();
|
||||
|
||||
var games = new ConcurrentHashMap<String, GameDefinition<BitboardGame<?>>>();
|
||||
games.put("tictactoe", new GameDefinition<>("tictactoe", BitboardReversi.class));
|
||||
games.put("reversi", new GameDefinition<>("reversi", BitboardReversi.class));
|
||||
var games = new ConcurrentHashMap<String, Class<? extends TurnBasedGame>>();
|
||||
games.put("tictactoe", BitboardReversi.class);
|
||||
games.put("reversi", BitboardReversi.class);
|
||||
var s = new MasterServer(6666, games);
|
||||
try {
|
||||
s.start();
|
||||
@@ -156,7 +157,7 @@ public final class App extends Application {
|
||||
private void setKeybinds(StackPane root) {
|
||||
root.addEventHandler(KeyEvent.KEY_PRESSED,event -> {
|
||||
if (event.getCode() == KeyCode.ESCAPE) {
|
||||
escapePopup();
|
||||
escapePopup();
|
||||
}
|
||||
});
|
||||
stage.setFullScreenExitKeyCombination(
|
||||
|
||||
@@ -14,7 +14,7 @@ import org.toop.framework.gameFramework.view.GUIEvents;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class BitGameCanvas<T extends TurnBasedGame> implements GameCanvas {
|
||||
public abstract class BitGameCanvas implements GameCanvas {
|
||||
protected record Cell(float x, float y, float width, float height) {
|
||||
public boolean isInside(double x, double y) {
|
||||
return x >= this.x && x <= this.x + width &&
|
||||
|
||||
@@ -3,10 +3,6 @@ package org.toop.app.canvas;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.app.App;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.game.games.reversi.BitboardReversi;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class ReversiBitCanvas extends BitGameCanvas {
|
||||
public ReversiBitCanvas() {
|
||||
@@ -37,7 +33,7 @@ public class ReversiBitCanvas extends BitGameCanvas {
|
||||
public void redraw(TurnBasedGame gameCopy) {
|
||||
clearAll();
|
||||
long[] board = gameCopy.getBoard();
|
||||
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, (int)i));
|
||||
loopOverBoard(board[1], (i) -> drawDot(Color.BLACK, (int)i));
|
||||
loopOverBoard(board[0], (i) -> drawDot(Color.WHITE, i));
|
||||
loopOverBoard(board[1], (i) -> drawDot(Color.BLACK, i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,6 @@ package org.toop.app.canvas;
|
||||
import javafx.scene.paint.Color;
|
||||
import org.toop.app.App;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
import org.toop.game.games.tictactoe.BitboardTicTacToe;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TicTacToeBitCanvas extends BitGameCanvas{
|
||||
public TicTacToeBitCanvas() {
|
||||
@@ -29,8 +25,8 @@ public class TicTacToeBitCanvas extends BitGameCanvas{
|
||||
}
|
||||
|
||||
private void drawMoves(long[] gameBoard){
|
||||
loopOverBoard(gameBoard[0], (i) -> drawX(Color.RED, (int)i));
|
||||
loopOverBoard(gameBoard[1], (i) -> drawO(Color.BLUE, (Integer) i));
|
||||
loopOverBoard(gameBoard[0], (i) -> drawX(Color.RED, i));
|
||||
loopOverBoard(gameBoard[1], (i) -> drawO(Color.BLUE, i));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package org.toop.game.games.reversi;
|
||||
package org.toop.framework.game.games.reversi;
|
||||
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
import org.toop.game.BitboardGame;
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
|
||||
public class BitboardReversi extends BitboardGame {
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package org.toop.game.games.tictactoe;
|
||||
package org.toop.framework.game.games.tictactoe;
|
||||
|
||||
import org.toop.framework.gameFramework.GameState;
|
||||
import org.toop.framework.gameFramework.model.game.PlayResult;
|
||||
import org.toop.framework.gameFramework.model.player.Player;
|
||||
import org.toop.game.BitboardGame;
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
|
||||
public class BitboardTicTacToe extends BitboardGame {
|
||||
private final long[] winningLines = {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
|
||||
public class Game implements OnlineGame {
|
||||
public class Game implements OnlineGame<TurnBasedGame> {
|
||||
|
||||
private long id;
|
||||
private User[] users;
|
||||
private GameDefinition<BitboardGame<?>> game;
|
||||
private TurnBasedGame game;
|
||||
|
||||
public Game(GameDefinition game, User... users) {
|
||||
public Game(TurnBasedGame game, User... users) {
|
||||
this.game = game;
|
||||
this.users = users;
|
||||
}
|
||||
@@ -19,7 +19,7 @@ public class Game implements OnlineGame {
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameDefinition game() {
|
||||
public TurnBasedGame game() {
|
||||
return game;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class GameDefinition<T> {
|
||||
|
||||
@@ -13,6 +13,7 @@ import io.netty.handler.logging.LogLevel;
|
||||
import io.netty.handler.logging.LoggingHandler;
|
||||
import org.toop.framework.SnowflakeGenerator;
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -20,7 +21,7 @@ public class MasterServer {
|
||||
private final int port;
|
||||
public final Server gs;
|
||||
|
||||
public MasterServer(int port, Map<String, GameDefinition<BitboardGame<?>>> gameTypes) {
|
||||
public MasterServer(int port, Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
||||
this.port = port;
|
||||
this.gs = new Server(gameTypes);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
public interface OnlineGame {
|
||||
public interface OnlineGame<T> {
|
||||
long id();
|
||||
GameDefinition game();
|
||||
T game();
|
||||
User[] users();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.game.BitboardGame;
|
||||
import org.toop.framework.gameFramework.model.game.TurnBasedGame;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -9,11 +10,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class Server implements GameServer {
|
||||
|
||||
final private Map<String, GameDefinition<BitboardGame<?>>> gameTypes;
|
||||
public List<OnlineGame> games = new ArrayList<>();
|
||||
final private Map<String, Class<? extends TurnBasedGame>> gameTypes;
|
||||
public List<OnlineGame<TurnBasedGame>> games = new ArrayList<>();
|
||||
final private Map<Long, ServerUser> users = new ConcurrentHashMap<>();
|
||||
|
||||
public Server(Map<String, GameDefinition<BitboardGame<?>>> gameTypes) {
|
||||
public Server(Map<String, Class<? extends TurnBasedGame>> gameTypes) {
|
||||
this.gameTypes = gameTypes;
|
||||
}
|
||||
|
||||
@@ -37,8 +38,8 @@ public class Server implements GameServer {
|
||||
if (!gameTypes.containsKey(gameType)) return;
|
||||
|
||||
try {
|
||||
var game = new Game(gameTypes.get(gameType).create(), users);
|
||||
games.addLast(new Game(game, users));
|
||||
var game = new Game(gameTypes.get(gameType).getDeclaredConstructor().newInstance(), users);
|
||||
games.addLast(game);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
|
||||
public class ServerTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
new Server();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user