From 854f4d49407e45d67dd5754afd21a7e59970ca5b Mon Sep 17 00:00:00 2001 From: Joseph Burton Date: Sun, 3 May 2020 21:06:38 +0100 Subject: Multiplayer support (#221) * First pass on multiplayer * Apply review suggestions * Dedicated Enigma server * Don't jump to references when other users do stuff * Better UI + translations * french translation * Apply review suggestions * Document the protocol * Fix most issues with scrolling. * Apply review suggestions * Fix zip hash issues + add a bit more logging * Optimize zip hash * Fix a couple of login bugs * Add message log and user list * Make Message an abstract class * Make status bar work, add chat box * Hide message log/users list when not connected * Fix status bar not resetting entirely * Run stop server task on server thread to prevent multithreading race conditions * Add c2s message to packet id list * Fix message scroll bar not scrolling to the end * Formatting * User list size -> ushort * Combine contains and remove check * Check removal before sending packet * Add password to login packet * Fix the GUI closing the rename text field when someone else renames something * Update fr_fr.json * oups * Make connection/server create dialogs not useless if it fails once * Refactor UI state updating * Fix imports * Fix Collab menu * Fix NPE when rename not allowed * Make the log file a configurable option * Don't use modified UTF * Update fr_fr.json * Bump version to 0.15.4 * Apparently I can't spell neither words nor semantic versions Co-authored-by: Yanis48 Co-authored-by: 2xsaiko --- .../enigma/gui/dialog/ConnectToServerDialog.java | 82 ++++++++++++++++++++++ .../enigma/gui/dialog/CreateServerDialog.java | 65 +++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java create mode 100644 src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java (limited to 'src/main/java/cuchaz/enigma/gui/dialog') diff --git a/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java new file mode 100644 index 0000000..c5f505c --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/dialog/ConnectToServerDialog.java @@ -0,0 +1,82 @@ +package cuchaz.enigma.gui.dialog; + +import cuchaz.enigma.network.EnigmaServer; +import cuchaz.enigma.utils.I18n; + +import javax.swing.*; +import java.awt.Frame; + +public class ConnectToServerDialog { + + public static Result show(Frame parentComponent) { + JTextField usernameField = new JTextField(System.getProperty("user.name"), 20); + JPanel usernameRow = new JPanel(); + usernameRow.add(new JLabel(I18n.translate("prompt.connect.username"))); + usernameRow.add(usernameField); + JTextField ipField = new JTextField(20); + JPanel ipRow = new JPanel(); + ipRow.add(new JLabel(I18n.translate("prompt.connect.ip"))); + ipRow.add(ipField); + JTextField portField = new JTextField(String.valueOf(EnigmaServer.DEFAULT_PORT), 10); + JPanel portRow = new JPanel(); + portRow.add(new JLabel(I18n.translate("prompt.port"))); + portRow.add(portField); + JPasswordField passwordField = new JPasswordField(20); + JPanel passwordRow = new JPanel(); + passwordRow.add(new JLabel(I18n.translate("prompt.password"))); + passwordRow.add(passwordField); + + int response = JOptionPane.showConfirmDialog(parentComponent, new Object[]{usernameRow, ipRow, portRow, passwordRow}, I18n.translate("prompt.connect.title"), JOptionPane.OK_CANCEL_OPTION); + if (response != JOptionPane.OK_OPTION) { + return null; + } + + String username = usernameField.getText(); + String ip = ipField.getText(); + int port; + try { + port = Integer.parseInt(portField.getText()); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.nan"), I18n.translate("prompt.connect.title"), JOptionPane.ERROR_MESSAGE); + return null; + } + if (port < 0 || port >= 65536) { + JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.invalid"), I18n.translate("prompt.connect.title"), JOptionPane.ERROR_MESSAGE); + return null; + } + char[] password = passwordField.getPassword(); + + return new Result(username, ip, port, password); + } + + public static class Result { + private final String username; + private final String ip; + private final int port; + private final char[] password; + + public Result(String username, String ip, int port, char[] password) { + this.username = username; + this.ip = ip; + this.port = port; + this.password = password; + } + + public String getUsername() { + return username; + } + + public String getIp() { + return ip; + } + + public int getPort() { + return port; + } + + public char[] getPassword() { + return password; + } + } + +} diff --git a/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java b/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java new file mode 100644 index 0000000..eea1dff --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/dialog/CreateServerDialog.java @@ -0,0 +1,65 @@ +package cuchaz.enigma.gui.dialog; + +import cuchaz.enigma.network.EnigmaServer; +import cuchaz.enigma.utils.I18n; + +import javax.swing.*; +import java.awt.*; + +public class CreateServerDialog { + + public static Result show(Frame parentComponent) { + JTextField portField = new JTextField(String.valueOf(EnigmaServer.DEFAULT_PORT), 10); + JPanel portRow = new JPanel(); + portRow.add(new JLabel(I18n.translate("prompt.port"))); + portRow.add(portField); + JPasswordField passwordField = new JPasswordField(20); + JPanel passwordRow = new JPanel(); + passwordRow.add(new JLabel(I18n.translate("prompt.password"))); + passwordRow.add(passwordField); + + int response = JOptionPane.showConfirmDialog(parentComponent, new Object[]{portRow, passwordRow}, I18n.translate("prompt.create_server.title"), JOptionPane.OK_CANCEL_OPTION); + if (response != JOptionPane.OK_OPTION) { + return null; + } + + int port; + try { + port = Integer.parseInt(portField.getText()); + } catch (NumberFormatException e) { + JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.nan"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE); + return null; + } + if (port < 0 || port >= 65536) { + JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.port.invalid"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE); + return null; + } + + char[] password = passwordField.getPassword(); + if (password.length > EnigmaServer.MAX_PASSWORD_LENGTH) { + JOptionPane.showMessageDialog(parentComponent, I18n.translate("prompt.password.too_long"), I18n.translate("prompt.create_server.title"), JOptionPane.ERROR_MESSAGE); + return null; + } + + return new Result(port, password); + } + + public static class Result { + private final int port; + private final char[] password; + + public Result(int port, char[] password) { + this.port = port; + this.password = password; + } + + public int getPort() { + return port; + } + + public char[] getPassword() { + return password; + } + } + +} -- cgit v1.2.3