mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
MVP game client
This commit is contained in:
5
.idea/uiDesigner.xml
generated
5
.idea/uiDesigner.xml
generated
@@ -119,9 +119,12 @@
|
|||||||
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
</item>
|
</item>
|
||||||
|
<item class="org.toop.UI.BackgroundPanel" icon="icon.png" removable="true" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
</group>
|
</group>
|
||||||
</component>
|
</component>
|
||||||
<component name="uidesigner-configuration">
|
<component name="uidesigner-configuration">
|
||||||
<option name="DEFAULT_LAYOUT_MANAGER" value="CardLayout" />
|
<option name="DEFAULT_LAYOUT_MANAGER" value="BorderLayout" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
BIN
img/background.jpg
Normal file
BIN
img/background.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 71 KiB |
@@ -1,7 +1,6 @@
|
|||||||
package org.toop;
|
package org.toop;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Level;
|
import org.toop.UI.LocalServerSelector;
|
||||||
import org.toop.UI.GameSelectorWindow;
|
|
||||||
import org.toop.eventbus.EventRegistry;
|
import org.toop.eventbus.EventRegistry;
|
||||||
import org.toop.eventbus.Events;
|
import org.toop.eventbus.Events;
|
||||||
import org.toop.eventbus.GlobalEventBus;
|
import org.toop.eventbus.GlobalEventBus;
|
||||||
@@ -9,9 +8,7 @@ import org.toop.server.backend.ServerManager;
|
|||||||
|
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.toop.server.backend.tictactoe.TicTacToeServer;
|
|
||||||
import org.toop.server.frontend.ConnectionManager;
|
import org.toop.server.frontend.ConnectionManager;
|
||||||
import org.toop.server.frontend.TcpClient;
|
|
||||||
|
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@@ -20,7 +17,8 @@ public class Main {
|
|||||||
private static boolean running = false;
|
private static boolean running = false;
|
||||||
|
|
||||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
Logging.disableAllLogs();
|
// Logging.disableAllLogs();
|
||||||
|
Logging.disableLogsForClass(EventRegistry.class);
|
||||||
// Logging.enableLogsForClass(ServerManager.class, Level.ALL);
|
// Logging.enableLogsForClass(ServerManager.class, Level.ALL);
|
||||||
// Logging.enableLogsForClass(TicTacToeServer.class, Level.ALL);
|
// Logging.enableLogsForClass(TicTacToeServer.class, Level.ALL);
|
||||||
// Logging.enableLogsForClass(TcpClient.class, Level.ALL);
|
// Logging.enableLogsForClass(TcpClient.class, Level.ALL);
|
||||||
@@ -28,33 +26,17 @@ public class Main {
|
|||||||
initSystems();
|
initSystems();
|
||||||
registerEvents();
|
registerEvents();
|
||||||
|
|
||||||
/*
|
// JFrame frame = new JFrame("Server Settings");
|
||||||
Window window = Window.setup(Window.API.GLFW, "Test", new Window.Size(1280, 720));
|
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
Renderer renderer = Renderer.setup(Renderer.API.OPENGL);
|
// frame.setSize(800, 600);
|
||||||
|
// frame.setLocationRelativeTo(null);
|
||||||
|
// frame.setVisible(true);
|
||||||
|
|
||||||
if (!initEvents()) {
|
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
|
||||||
throw new RuntimeException("A event could not be initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
TcpServer server = new TcpServer(5001);
|
// new Thread(() -> {
|
||||||
Thread serverThread = new Thread(server);
|
// LocalServerSelector window = new LocalServerSelector();
|
||||||
serverThread.start();
|
// }).start();
|
||||||
Server.start("127.0.0.1", "5001");
|
|
||||||
// Testsss.start(""); // Used for testing server.
|
|
||||||
Window.start("");
|
|
||||||
*/
|
|
||||||
|
|
||||||
// ConsoleGui console = new ConsoleGui();
|
|
||||||
//
|
|
||||||
// do {
|
|
||||||
// console.print();
|
|
||||||
// } while (console.next());
|
|
||||||
//
|
|
||||||
// console.print();
|
|
||||||
|
|
||||||
new Thread(() -> {
|
|
||||||
GameSelectorWindow gameSelectorWindow = new GameSelectorWindow();
|
|
||||||
}).start();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
src/main/java/org/toop/UI/BackgroundPanel.java
Normal file
21
src/main/java/org/toop/UI/BackgroundPanel.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package org.toop.UI;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class BackgroundPanel extends JPanel {
|
||||||
|
private Image backgroundImage;
|
||||||
|
|
||||||
|
public void setBackgroundImage(Image image) {
|
||||||
|
this.backgroundImage = image;
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (backgroundImage != null) {
|
||||||
|
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
src/main/java/org/toop/UI/LocalGameSelector.form
Normal file
37
src/main/java/org/toop/UI/LocalGameSelector.form
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.LocalGameSelector">
|
||||||
|
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
|
<constraints>
|
||||||
|
<xy x="20" y="20" width="780" height="600"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="3f7f6" class="javax.swing.JComboBox" binding="gameSelectionComboBox">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<model/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="4700f" class="javax.swing.JButton" binding="startGame">
|
||||||
|
<constraints>
|
||||||
|
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Start game"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="41f79" class="javax.swing.JComboBox" binding="playerTypeSelectionBox">
|
||||||
|
<constraints>
|
||||||
|
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<editable value="false"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
||||||
139
src/main/java/org/toop/UI/LocalGameSelector.java
Normal file
139
src/main/java/org/toop/UI/LocalGameSelector.java
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
package org.toop.UI;
|
||||||
|
|
||||||
|
import jdk.jfr.Event;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import org.toop.Main;
|
||||||
|
import org.toop.eventbus.Events;
|
||||||
|
import org.toop.eventbus.GlobalEventBus;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
public class LocalGameSelector extends JFrame {
|
||||||
|
private static final Logger logger = LogManager.getLogger(LocalGameSelector.class);
|
||||||
|
|
||||||
|
private JPanel panel1;
|
||||||
|
private JComboBox gameSelectionComboBox;
|
||||||
|
private JButton startGame;
|
||||||
|
private JComboBox playerTypeSelectionBox;
|
||||||
|
|
||||||
|
private JPanel cards; // CardLayout panel
|
||||||
|
private CardLayout cardLayout;
|
||||||
|
|
||||||
|
private UIGameBoard tttBoard;
|
||||||
|
|
||||||
|
public LocalGameSelector() {
|
||||||
|
setTitle("Local Game Selector");
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
setSize(1920, 1080);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// Setup CardLayout
|
||||||
|
cardLayout = new CardLayout();
|
||||||
|
cards = new JPanel(cardLayout);
|
||||||
|
setContentPane(cards);
|
||||||
|
|
||||||
|
// --- Main menu panel ---
|
||||||
|
panel1 = new JPanel();
|
||||||
|
panel1.setLayout(new FlowLayout());
|
||||||
|
gameSelectionComboBox = new JComboBox<>();
|
||||||
|
gameSelectionComboBox.addItem("Tic Tac Toe");
|
||||||
|
gameSelectionComboBox.addItem("Reversi");
|
||||||
|
|
||||||
|
playerTypeSelectionBox = new JComboBox<>();
|
||||||
|
playerTypeSelectionBox.addItem("Player vs Player");
|
||||||
|
playerTypeSelectionBox.addItem("Player vs AI");
|
||||||
|
playerTypeSelectionBox.addItem("AI vs Player");
|
||||||
|
|
||||||
|
panel1.add(gameSelectionComboBox);
|
||||||
|
panel1.add(playerTypeSelectionBox);
|
||||||
|
|
||||||
|
startGame = new JButton("Start Game");
|
||||||
|
panel1.add(startGame);
|
||||||
|
|
||||||
|
cards.add(panel1, "MainMenu");
|
||||||
|
|
||||||
|
// Start button action
|
||||||
|
startGame.addActionListener(e -> startGameClicked());
|
||||||
|
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createServer() {
|
||||||
|
CompletableFuture<String> serverIdFuture = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.StartServerRequest("5001", "tictactoe", serverIdFuture)); // TODO: what if 5001 is in use
|
||||||
|
try {
|
||||||
|
return serverIdFuture.get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error getting server ID", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createConnection() {
|
||||||
|
CompletableFuture<String> connectionIdFuture = new CompletableFuture<>();
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.StartConnectionRequest("127.0.0.1", "5001", connectionIdFuture)); // TODO: what if server couldn't be started with port.
|
||||||
|
try {
|
||||||
|
return connectionIdFuture.get();
|
||||||
|
} catch (InterruptedException | ExecutionException e) {
|
||||||
|
logger.error("Error getting connection ID", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startGameClicked() {
|
||||||
|
String playerTypes = (String) playerTypeSelectionBox.getSelectedItem();
|
||||||
|
String selectedGame = (String) gameSelectionComboBox.getSelectedItem();
|
||||||
|
|
||||||
|
String serverId = createServer();
|
||||||
|
String connectionId = createConnection();
|
||||||
|
final String[] gameId = new String[1];
|
||||||
|
|
||||||
|
if ("Player vs AI".equalsIgnoreCase(playerTypes)) {
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", "Player", "AI"));
|
||||||
|
} else if ("AI vs Player".equalsIgnoreCase(playerTypes)) {
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", "Player", "AI"));
|
||||||
|
} else { // Player vs Player is default
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "create_game", "Player1", "Player2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1); // TODO: This is bad, fix later
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.ReceivedMessage.class, event -> {
|
||||||
|
logger.info(event.message());
|
||||||
|
if (event.message().toLowerCase().startsWith("game created successfully")) {
|
||||||
|
String[] parts = event.message().split("\\|");
|
||||||
|
String gameIdPart = parts[1];
|
||||||
|
gameId[0] = gameIdPart.split(" ")[1];
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
latch.await(); // TODO: Bad, fix later
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(connectionId, "START_GAME", gameId[0]));
|
||||||
|
|
||||||
|
if ("Tic Tac Toe".equalsIgnoreCase(selectedGame)) {
|
||||||
|
if (tttBoard == null) {
|
||||||
|
tttBoard = new UIGameBoard("tic tac toe", connectionId, serverId, gameId[0], this);
|
||||||
|
cards.add(tttBoard.getTTTPanel(), "TicTacToe");
|
||||||
|
}
|
||||||
|
cardLayout.show(cards, "TicTacToe");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showMainMenu() {
|
||||||
|
cardLayout.show(cards, "MainMenu");
|
||||||
|
}
|
||||||
|
}
|
||||||
39
src/main/java/org/toop/UI/LocalServerSelector.form
Normal file
39
src/main/java/org/toop/UI/LocalServerSelector.form
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.LocalServerSelector">
|
||||||
|
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
|
<constraints>
|
||||||
|
<xy x="20" y="20" width="500" height="400"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children>
|
||||||
|
<component id="7774a" class="javax.swing.JButton" binding="serverButton" default-binding="true">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Server"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<component id="ca0c8" class="javax.swing.JButton" binding="localButton">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
<properties>
|
||||||
|
<text value="Local"/>
|
||||||
|
</properties>
|
||||||
|
</component>
|
||||||
|
<hspacer id="74dd2">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
<hspacer id="50fd">
|
||||||
|
<constraints>
|
||||||
|
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
</constraints>
|
||||||
|
</hspacer>
|
||||||
|
</children>
|
||||||
|
</grid>
|
||||||
|
</form>
|
||||||
34
src/main/java/org/toop/UI/LocalServerSelector.java
Normal file
34
src/main/java/org/toop/UI/LocalServerSelector.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package org.toop.UI;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class LocalServerSelector {
|
||||||
|
private JPanel panel1;
|
||||||
|
private JButton serverButton;
|
||||||
|
private JButton localButton;
|
||||||
|
private final JFrame frame;
|
||||||
|
|
||||||
|
public LocalServerSelector() {
|
||||||
|
frame = new JFrame("Server Selector");
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
frame.setContentPane(panel1);
|
||||||
|
frame.setSize(1920, 1080);
|
||||||
|
frame.setLocationRelativeTo(null); // Sets to center
|
||||||
|
frame.setVisible(true);
|
||||||
|
|
||||||
|
serverButton.addActionListener(e -> onServerClicked());
|
||||||
|
localButton.addActionListener(e -> onLocalClicked());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onServerClicked() {
|
||||||
|
frame.dispose();
|
||||||
|
new RemoteGameSelector();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onLocalClicked() {
|
||||||
|
frame.dispose();
|
||||||
|
new LocalGameSelector();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.GameSelectorWindow">
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.RemoteGameSelector">
|
||||||
<grid id="27dc6" binding="mainMenu" layout-manager="GridLayoutManager" row-count="10" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
<grid id="27dc6" binding="mainMenu" layout-manager="GridLayoutManager" row-count="10" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
<margin top="10" left="10" bottom="10" right="10"/>
|
<margin top="10" left="10" bottom="10" right="10"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="20" y="20" width="482" height="404"/>
|
<xy x="20" y="20" width="741" height="625"/>
|
||||||
</constraints>
|
</constraints>
|
||||||
<properties>
|
<properties>
|
||||||
<opaque value="true"/>
|
<opaque value="true"/>
|
||||||
@@ -6,14 +6,13 @@ import org.toop.eventbus.GlobalEventBus;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
|
||||||
public class GameSelectorWindow extends JFrame {
|
public class RemoteGameSelector {
|
||||||
private static final Logger logger = LogManager.getLogger(GameSelectorWindow.class);
|
private static final Logger logger = LogManager.getLogger(RemoteGameSelector.class);
|
||||||
|
|
||||||
private JPanel mainMenu;
|
private JPanel mainMenu;
|
||||||
private JTextField nameTextField;
|
private JTextField nameTextField;
|
||||||
@@ -27,7 +26,7 @@ public class GameSelectorWindow extends JFrame {
|
|||||||
private JFrame frame;
|
private JFrame frame;
|
||||||
private JLabel fillAllFields;
|
private JLabel fillAllFields;
|
||||||
|
|
||||||
public GameSelectorWindow() {
|
public RemoteGameSelector() {
|
||||||
gameSelectorBox.addItem("Tic Tac Toe");
|
gameSelectorBox.addItem("Tic Tac Toe");
|
||||||
gameSelectorBox.addItem("Reversi");
|
gameSelectorBox.addItem("Reversi");
|
||||||
//todo get supported games from server and add to gameSelectorBox
|
//todo get supported games from server and add to gameSelectorBox
|
||||||
@@ -107,8 +106,8 @@ public class GameSelectorWindow extends JFrame {
|
|||||||
|
|
||||||
|
|
||||||
frame.remove(mainMenu);
|
frame.remove(mainMenu);
|
||||||
UIGameBoard ttt = new UIGameBoard(gameSelectorBox.getSelectedItem().toString(),this);
|
// UIGameBoard ttt = new UIGameBoard("tic tac toe", "test", "test",this); // TODO: Fix later
|
||||||
frame.add(ttt.getTTTPanel());
|
// frame.add(ttt.getTTTPanel()); // TODO: Fix later
|
||||||
frame.revalidate();
|
frame.revalidate();
|
||||||
frame.repaint();
|
frame.repaint();
|
||||||
} else {
|
} else {
|
||||||
12
src/main/java/org/toop/UI/Services.form
Normal file
12
src/main/java/org/toop/UI/Services.form
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.Services">
|
||||||
|
<grid id="27dc6" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
|
<constraints>
|
||||||
|
<xy x="20" y="20" width="500" height="400"/>
|
||||||
|
</constraints>
|
||||||
|
<properties/>
|
||||||
|
<border type="none"/>
|
||||||
|
<children/>
|
||||||
|
</grid>
|
||||||
|
</form>
|
||||||
6
src/main/java/org/toop/UI/Services.java
Normal file
6
src/main/java/org/toop/UI/Services.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package org.toop.UI;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class Services {
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.UIGameBoard">
|
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.toop.UI.UIGameBoard">
|
||||||
<grid id="27dc6" binding="tttPanel" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
<grid id="27dc6" binding="tttPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
<margin top="10" left="10" bottom="10" right="10"/>
|
<margin top="10" left="10" bottom="10" right="10"/>
|
||||||
<constraints>
|
<constraints>
|
||||||
<xy x="20" y="20" width="500" height="400"/>
|
<xy x="20" y="20" width="500" height="400"/>
|
||||||
@@ -10,38 +10,6 @@
|
|||||||
</properties>
|
</properties>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<children>
|
||||||
<component id="f23e8" class="javax.swing.JLabel" binding="name">
|
|
||||||
<constraints>
|
|
||||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Label"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="4db9d" class="javax.swing.JLabel" binding="ip">
|
|
||||||
<constraints>
|
|
||||||
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Label"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<component id="ac2e0" class="javax.swing.JLabel" binding="gameName">
|
|
||||||
<constraints>
|
|
||||||
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties>
|
|
||||||
<text value="Label"/>
|
|
||||||
</properties>
|
|
||||||
</component>
|
|
||||||
<grid id="f1c82" binding="cellPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
|
|
||||||
<constraints>
|
|
||||||
<grid row="1" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
|
||||||
</constraints>
|
|
||||||
<properties/>
|
|
||||||
<border type="none"/>
|
|
||||||
<children/>
|
|
||||||
</grid>
|
|
||||||
<component id="e1a78" class="javax.swing.JButton" binding="backToMainMenuButton" default-binding="true">
|
<component id="e1a78" class="javax.swing.JButton" binding="backToMainMenuButton" default-binding="true">
|
||||||
<constraints>
|
<constraints>
|
||||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||||
|
|||||||
@@ -1,79 +1,76 @@
|
|||||||
package org.toop.UI;
|
package org.toop.UI;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
|
||||||
import org.apache.logging.log4j.Logger;
|
|
||||||
import org.toop.Main;
|
|
||||||
import org.toop.eventbus.Events;
|
import org.toop.eventbus.Events;
|
||||||
import org.toop.eventbus.GlobalEventBus;
|
import org.toop.eventbus.GlobalEventBus;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class UIGameBoard extends JFrame {
|
public class UIGameBoard {
|
||||||
private static final Logger logger = LogManager.getLogger(UIGameBoard.class);
|
private static final int TICTACTOE_SIZE = 3;
|
||||||
|
|
||||||
private final int TICTACTOE_SIZE = 3;
|
private JPanel tttPanel; // Root panel for this game
|
||||||
private final int REVERSI_SIZE = 8;
|
|
||||||
private JLabel name;
|
|
||||||
private JLabel ip;
|
|
||||||
private JLabel gameName;
|
|
||||||
private JPanel tttPanel;
|
|
||||||
private JPanel cellPanel;
|
|
||||||
private JButton backToMainMenuButton;
|
private JButton backToMainMenuButton;
|
||||||
private JButton[] buttons = new JButton[9];
|
|
||||||
private JButton[] cells;
|
private JButton[] cells;
|
||||||
public UIGameBoard(String game,GameSelectorWindow gameSelectorWindow) {
|
private String currentPlayer = "X";
|
||||||
|
|
||||||
//cellPanel = new JPanel();
|
private String gameConnectionId;
|
||||||
JPanel gamePanel = new JPanel();
|
private String serverId;
|
||||||
if(game.toLowerCase().equals("tic tac toe")) {
|
private String gameId;
|
||||||
gamePanel = createGridPanel(TICTACTOE_SIZE, TICTACTOE_SIZE);
|
|
||||||
}
|
private LocalGameSelector parentSelector;
|
||||||
if(game.toLowerCase().equals("reversi")) {
|
|
||||||
gamePanel = createGridPanel(REVERSI_SIZE, REVERSI_SIZE);
|
public UIGameBoard(String gameType, String gameConnectionId, String serverId, String gameId, LocalGameSelector parent) {
|
||||||
|
this.parentSelector = parent;
|
||||||
|
|
||||||
|
this .gameConnectionId = gameConnectionId;
|
||||||
|
this.serverId = serverId;
|
||||||
|
this.gameId = gameId;
|
||||||
|
|
||||||
|
// Root panel
|
||||||
|
tttPanel = new JPanel(new BorderLayout());
|
||||||
|
|
||||||
|
// Back button
|
||||||
|
backToMainMenuButton = new JButton("Back to Main Menu");
|
||||||
|
tttPanel.add(backToMainMenuButton, BorderLayout.SOUTH);
|
||||||
|
backToMainMenuButton.addActionListener(e ->
|
||||||
|
// TODO reset game and connections
|
||||||
|
parent.showMainMenu()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Game grid
|
||||||
|
JPanel gameGrid = createGridPanel(TICTACTOE_SIZE, TICTACTOE_SIZE);
|
||||||
|
tttPanel.add(gameGrid, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private JPanel createGridPanel(int sizeX, int sizeY) {
|
||||||
|
JPanel panel = new JPanel(new GridLayout(sizeX, sizeY));
|
||||||
|
cells = new JButton[sizeX * sizeY];
|
||||||
|
|
||||||
|
for (int i = 0; i < sizeX * sizeY; i++) {
|
||||||
|
cells[i] = new JButton(" ");
|
||||||
|
cells[i].setFont(new Font("Arial", Font.BOLD, 100 / sizeX));
|
||||||
|
panel.add(cells[i]);
|
||||||
|
|
||||||
|
final int index = i;
|
||||||
|
cells[i].addActionListener((ActionEvent e) -> {
|
||||||
|
cells[index].setText(currentPlayer);
|
||||||
|
if (Objects.equals(currentPlayer, "X")) { currentPlayer = "O"; }
|
||||||
|
else { currentPlayer = "X"; }
|
||||||
|
GlobalEventBus.post(new Events.ServerEvents.SendCommand(this.gameConnectionId,
|
||||||
|
"gameid ", this.gameId,
|
||||||
|
"player ", this.currentPlayer, // TODO: Actual player names
|
||||||
|
"MOVE", "" + index));
|
||||||
|
System.out.println("Cell clicked: " + index);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
cellPanel.removeAll();
|
return panel;
|
||||||
cellPanel.add(gamePanel);
|
|
||||||
cellPanel.revalidate();
|
|
||||||
cellPanel.repaint();
|
|
||||||
//tttPanel.add(cellPanel);
|
|
||||||
backToMainMenuButton.addActionListener((
|
|
||||||
ActionEvent e) -> {
|
|
||||||
gameSelectorWindow.returnToMainMenu();
|
|
||||||
System.out.println("gothere");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//Set the IP, game name and name
|
|
||||||
public void setIGN(String ip, String gameName, String name) {
|
|
||||||
this.ip.setText(ip);
|
|
||||||
this.gameName.setText(gameName);
|
|
||||||
this.name.setText(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public JPanel getTTTPanel() {
|
public JPanel getTTTPanel() {
|
||||||
return tttPanel;
|
return tttPanel;
|
||||||
}
|
}
|
||||||
//Creates a grid of buttons and adds a global event bus event on click with the index of the button.
|
}
|
||||||
private JPanel createGridPanel(int sizeX, int sizeY) {
|
|
||||||
JPanel cellPanel = new JPanel(new GridLayout(sizeX,sizeY));
|
|
||||||
cells = new JButton[sizeX*sizeY];
|
|
||||||
for(int i =0; i < sizeX*sizeY; i++){
|
|
||||||
cells[i] = new JButton(" ");
|
|
||||||
cells[i].setPreferredSize(new Dimension(1000/sizeX,1000/sizeY));
|
|
||||||
cells[i].setFont(new Font("Arial", Font.BOLD, 480/sizeX));
|
|
||||||
cells[i].setFocusPainted(false);
|
|
||||||
cellPanel.add(cells[i]);
|
|
||||||
final int index = i;
|
|
||||||
cells[i].addActionListener((ActionEvent e) -> {
|
|
||||||
setCell(index,"X");//■ todo get current player
|
|
||||||
GlobalEventBus.post(new Events.ServerEvents.CellClicked(index));
|
|
||||||
logger.info("Grid button {} was clicked.", index);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return cellPanel;
|
|
||||||
}
|
|
||||||
public void setCell(int cell, String newValue){
|
|
||||||
cells[cell].setText(newValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -112,8 +112,9 @@ public class TicTacToeServer extends TcpServer {
|
|||||||
String gameId = this.newGame((String) command.arguments.getFirst(), (String) command.arguments.get(1));
|
String gameId = this.newGame((String) command.arguments.getFirst(), (String) command.arguments.get(1));
|
||||||
this.sendQueue.offer("game created successfully|gameid " + gameId);
|
this.sendQueue.offer("game created successfully|gameid " + gameId);
|
||||||
} else if (command.command == TicTacToeServerCommand.START_GAME) {
|
} else if (command.command == TicTacToeServerCommand.START_GAME) {
|
||||||
this.runGame((String) command.arguments.getFirst());
|
boolean success = this.runGame((String) command.arguments.getFirst());
|
||||||
this.sendQueue.offer("svr game is running successfully");
|
if (success) {this.sendQueue.offer("svr game is running successfully");}
|
||||||
|
else {this.sendQueue.offer("svr running game failed");}
|
||||||
} else if (command.command == TicTacToeServerCommand.END_GAME) {
|
} else if (command.command == TicTacToeServerCommand.END_GAME) {
|
||||||
this.endGame((String) command.arguments.getFirst());
|
this.endGame((String) command.arguments.getFirst());
|
||||||
this.sendQueue.offer("svr game ended successfully");
|
this.sendQueue.offer("svr game ended successfully");
|
||||||
@@ -150,13 +151,15 @@ public class TicTacToeServer extends TcpServer {
|
|||||||
return gameId;
|
return gameId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void runGame(String gameId) {
|
public boolean runGame(String gameId) {
|
||||||
TicTacToe game = this.games.get(gameId);
|
TicTacToe game = this.games.get(gameId);
|
||||||
if (game != null) {
|
if (game != null) {
|
||||||
game.run();
|
game.run();
|
||||||
logger.info("Running game: {}, players: {}", gameId, game.getPlayers());
|
logger.info("Running game: {}, players: {}", gameId, game.getPlayers());
|
||||||
|
return true;
|
||||||
} else {
|
} else {
|
||||||
logger.warn("Tried to run unknown game {}", gameId);
|
logger.warn("Tried to run unknown game {}", gameId);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,10 +52,10 @@ public final class ServerConnection implements Runnable {
|
|||||||
// return;
|
// return;
|
||||||
// } // TODO: DO I CARE?
|
// } // TODO: DO I CARE?
|
||||||
|
|
||||||
if (!this.running) {
|
// if (!this.running) {
|
||||||
logger.warn("Server has been stopped");
|
// logger.warn("Server has been stopped");
|
||||||
return;
|
// return;
|
||||||
}
|
// } // TODO: Server not running
|
||||||
|
|
||||||
String command = String.join(" ", args);
|
String command = String.join(" ", args);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user