commit 823d3440847cd78dbf1c494175ad81a4597d5e7e Author: sigonasr2 Date: Mon May 29 18:30:23 2023 -0500 Add client server dungeon helper app diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/sig/.jvmopts b/sig/.jvmopts new file mode 100644 index 0000000..526234d --- /dev/null +++ b/sig/.jvmopts @@ -0,0 +1,2 @@ +--add-opens=java.base/java.util=ALL-UNNAMED +--add-opens=java.base/java.lang=ALL-UNNAMED \ No newline at end of file diff --git a/sig/App.java b/sig/App.java new file mode 100644 index 0000000..6d8d3cf --- /dev/null +++ b/sig/App.java @@ -0,0 +1,229 @@ +import java.io.BufferedInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Map.Entry; +import java.awt.AWTException; +import java.awt.Robot; +import java.awt.event.KeyEvent; + +import lc.kra.system.keyboard.GlobalKeyboardHook; +import lc.kra.system.keyboard.event.GlobalKeyAdapter; +import lc.kra.system.keyboard.event.GlobalKeyEvent; + +class Client { + // initialize socket and input output streams + private Socket socket = null; + private DataInputStream input = null; + private DataOutputStream out = null; + + private static boolean run = true; + // constructor to put ip address and port + public Client(String address, int port) + { + GlobalKeyboardHook keyboardHook = new GlobalKeyboardHook(true); // Use false here to switch to hook instead of raw input + + System.out.println("Global keyboard hook successfully started, press [escape] key to shutdown. Connected keyboards:"); + for (Entry keyboard : GlobalKeyboardHook.listKeyboards().entrySet()) { + System.out.format("%d: %s\n", keyboard.getKey(), keyboard.getValue()); + } + + keyboardHook.addKeyListener(new GlobalKeyAdapter() { + + @Override + public void keyPressed(GlobalKeyEvent event) { + try { + switch (event.getVirtualKeyCode()){ + case GlobalKeyEvent.VK_RETURN:{ + out.writeUTF("FOLLOW"); + }break; + case GlobalKeyEvent.VK_NUMPAD8: + case GlobalKeyEvent.VK_UP:{ + out.writeUTF("FORWARD"); + }break; + case GlobalKeyEvent.VK_NUMPAD5: + case GlobalKeyEvent.VK_DOWN:{ + out.writeUTF("BACKWARD"); + }break; + case GlobalKeyEvent.VK_NUMPAD4: + case GlobalKeyEvent.VK_LEFT:{ + out.writeUTF("LEFT"); + }break; + case GlobalKeyEvent.VK_NUMPAD6: + case GlobalKeyEvent.VK_RIGHT:{ + out.writeUTF("RIGHT"); + }break; + case GlobalKeyEvent.VK_NUMPAD0: + case GlobalKeyEvent.VK_NUMPAD1: + case GlobalKeyEvent.VK_1: + case GlobalKeyEvent.VK_2: + case GlobalKeyEvent.VK_3: + case GlobalKeyEvent.VK_Z:{ + out.writeUTF("FIGHT"); + }break; + case GlobalKeyEvent.VK_NUMPAD3: + case 110: + case GlobalKeyEvent.VK_DELETE:{ + out.writeUTF("CHILL"); + }break; + } + } catch (IOException e) { + e.printStackTrace(); + } + if (event.getVirtualKeyCode() == GlobalKeyEvent.VK_ESCAPE) { + run = false; + } + } + + @Override + public void keyReleased(GlobalKeyEvent event) { + System.out.println(event); + } + }); + // establish a connection + try { + socket = new Socket(address, port); + System.out.println("Connected"); + + // takes input from terminal + input = new DataInputStream(System.in); + + // sends output to the socket + out = new DataOutputStream( + socket.getOutputStream()); + } + catch (IOException u) { + System.out.println(u); + return; + } + + // string to read message from input + String line = ""; + + // keep reading until "Over" is input + while (!line.equals("Over")) { + try { + line = input.readLine(); + out.writeUTF(line); + } + catch (IOException i) { + System.out.println(i); + } + } + + // close the connection + try { + input.close(); + out.close(); + socket.close(); + keyboardHook.shutdownHook(); + } + catch (IOException i) { + System.out.println(i); + } + } + + public static void main(String args[]) + { + Client client = new Client("127.0.0.1", 5000); + } +} + +class Server +{ + //initialize socket and input stream + private Socket socket = null; + private ServerSocket server = null; + private DataInputStream in = null; + Robot r; + + private void PressKeyWithModifier(int modifier,int keycode) { + r.keyPress(modifier); + r.keyPress(keycode); + r.keyRelease(keycode);r.delay(100); + r.keyRelease(modifier); + } + + // constructor with port + public Server(int port) + { + try { + r = new Robot(); + } catch (AWTException e) { + e.printStackTrace(); + } + // starts server and waits for a connection + try + { + server = new ServerSocket(port); + System.out.println("Server started"); + + System.out.println("Waiting for a client ..."); + + socket = server.accept(); + System.out.println("Client accepted"); + + // takes input from the client socket + in = new DataInputStream( + new BufferedInputStream(socket.getInputStream())); + + String line = ""; + + // reads message from client until "Over" is sent + while (!line.equals("Over")) + { + try + { + line = in.readUTF(); + switch (line){ + case "FOLLOW":{ + PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7); + }break; + case "FORWARD":{ + + }break; + case "BACKWARD":{ + + }break; + case "LEFT":{ + + }break; + case "RIGHT":{ + + }break; + case "FIGHT":{ + + }break; + case "CHILL":{ + + }break; + default:{ + System.out.println("Unknown command: "+line); + } + } + } + catch(IOException i) + { + System.out.println(i); + break; + } + } + System.out.println("Closing connection"); + + // close connection + socket.close(); + in.close(); + } + catch(IOException i) + { + System.out.println(i); + } + } + + public static void main(String args[]) + { + Server server = new Server(5000); + } +} \ No newline at end of file diff --git a/sig/Client$1.class b/sig/Client$1.class new file mode 100644 index 0000000..ce494fe Binary files /dev/null and b/sig/Client$1.class differ diff --git a/sig/Client.class b/sig/Client.class new file mode 100644 index 0000000..79e4001 Binary files /dev/null and b/sig/Client.class differ diff --git a/sig/Server.class b/sig/Server.class new file mode 100644 index 0000000..94a80ac Binary files /dev/null and b/sig/Server.class differ diff --git a/sig/system-hook-3.8.jar b/sig/system-hook-3.8.jar new file mode 100644 index 0000000..c5d3f88 Binary files /dev/null and b/sig/system-hook-3.8.jar differ