From 32f057dc883056271833b106b59aaafd8bda7057 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sun, 4 Jun 2023 23:53:10 -0500 Subject: [PATCH] Do not bog down the socket with updates. Instead run actions in separate thread. --- sig/App.java | 329 ++++++++++++++++++++++++++------------------------- 1 file changed, 171 insertions(+), 158 deletions(-) diff --git a/sig/App.java b/sig/App.java index 329f483..8b40243 100644 --- a/sig/App.java +++ b/sig/App.java @@ -5,6 +5,11 @@ import java.io.File; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Queue; import java.util.Timer; import java.util.TimerTask; import java.util.Map.Entry; @@ -124,18 +129,6 @@ class Client { // takes input from terminal input = new DataInputStream(System.in); - - Timer updateTimer=new Timer(); - updateTimer.schedule(new TimerTask(){ - @Override - public void run() { - try { - out.writeUTF("UPDATE"); - } catch (IOException e) { - e.printStackTrace(); - } - } - },100,100); // sends output to the socket out = new DataOutputStream( socket.getOutputStream()); @@ -299,74 +292,7 @@ class Server } } - void run(Rotation rot){ - switch (rot){ - case SCHOLAR:{ - if (nextActionTimer==0){ - if (GetMythraMana()<80&&lucidDreamingTimer==0){ - lucidDreamingTimer=60000; - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_6); - nextActionTimer=1000; - return; - } - if (allowedToAetherflow&&aetherflowTimer==0){ - PressKeyWithModifier(KeyEvent.VK_ALT, KeyEvent.VK_2); - nextActionTimer=1000; - aetherflowStacks=3; - aetherflowTimer=60000; - return; - } - float ayaHealth=GetAyaHealth(); - float mythraHealth=GetMythraHealth(); - if (ayaHealth<40){ - PressKey(KeyEvent.VK_F2); - r.delay(100); - Heal(Target.AYA); - return; - } - if (ayaHealth<60){ - if (whisperingDawnTimer==0&&feyIlluminationTimer<60000){ - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_C); - whisperingDawnTimer=60000; - nextActionTimer=1000; - return; - } else - if (feyIlluminationTimer==0&&whisperingDawnTimer<30000){ - PressKey(KeyEvent.VK_X); - feyIlluminationTimer=120000; - nextActionTimer=1000; - return; - } - } - if (mythraHealth<40){ - PressKey(KeyEvent.VK_F1); - r.delay(100); - Heal(Target.MYTHRA); - return; - } - //Looks like we're attacking. - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_8); - float targetHealth=GetTargetHealth(); - if (!MultipleTargets()){ - if (targetHealth>50&&bioTimer==0){ - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_2); - bioTimer=30000; - nextActionTimer=2500; - return; - } else { - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_1); - nextActionTimer=2500; - return; - } - } else { - PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_Z); - nextActionTimer=2500; - return; - } - }break; - } - } - } + CollectioncommandQueue=Collections.synchronizedCollection(new ArrayList<>()); // constructor with port public Server(int port) @@ -392,6 +318,169 @@ class Server new BufferedInputStream(socket.getInputStream())); String line = ""; + + Thread t = new Thread(new Runnable(){ + + + void run(Rotation rot){ + switch (rot){ + case SCHOLAR:{ + if (nextActionTimer==0){ + if (GetMythraMana()<80&&lucidDreamingTimer==0){ + lucidDreamingTimer=60000; + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_6); + nextActionTimer=1000; + return; + } + if (allowedToAetherflow&&aetherflowTimer==0){ + PressKeyWithModifier(KeyEvent.VK_ALT, KeyEvent.VK_2); + nextActionTimer=1000; + aetherflowStacks=3; + aetherflowTimer=60000; + return; + } + float ayaHealth=GetAyaHealth(); + float mythraHealth=GetMythraHealth(); + if (ayaHealth<40){ + PressKey(KeyEvent.VK_F2); + r.delay(100); + Heal(Target.AYA); + return; + } + if (ayaHealth<60){ + if (whisperingDawnTimer==0&&feyIlluminationTimer<60000){ + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_C); + whisperingDawnTimer=60000; + nextActionTimer=1000; + return; + } else + if (feyIlluminationTimer==0&&whisperingDawnTimer<30000){ + PressKey(KeyEvent.VK_X); + feyIlluminationTimer=120000; + nextActionTimer=1000; + return; + } + } + if (mythraHealth<40){ + PressKey(KeyEvent.VK_F1); + r.delay(100); + Heal(Target.MYTHRA); + return; + } + //Looks like we're attacking. + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_8); + float targetHealth=GetTargetHealth(); + if (!MultipleTargets()){ + if (targetHealth>50&&bioTimer==0){ + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_2); + bioTimer=30000; + nextActionTimer=2500; + return; + } else { + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_1); + nextActionTimer=2500; + return; + } + } else { + PressKeyWithModifier(KeyEvent.VK_CONTROL, KeyEvent.VK_Z); + nextActionTimer=2500; + return; + } + }break; + } + } + } + + @Override + public void run() { + while (true){ + for (String command:commandQueue){ + switch (command){ + case "FOLLOW":{ + PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7); + }break; + case "FORWARD":{ + r.keyPress(KeyEvent.VK_W); + r.delay(1000); + r.keyRelease(KeyEvent.VK_W); + }break; + case "BACKWARD":{ + r.keyPress(KeyEvent.VK_S); + r.delay(1000); + r.keyRelease(KeyEvent.VK_S); + }break; + case "LEFT":{ + r.keyPress(KeyEvent.VK_Q); + r.delay(1000); + r.keyRelease(KeyEvent.VK_Q); + }break; + case "RIGHT":{ + r.keyPress(KeyEvent.VK_E); + r.delay(1000); + r.keyRelease(KeyEvent.VK_E); + }break; + case "FIGHT":{ + fighting=true; + fightingaoe=false; + System.out.println("Fight mode: "+fighting); + }break; + case "FIGHTAOE":{ + fighting=fightingaoe=true; + System.out.println("Fight mode: "+fighting); + }break; + case "CHILL":{ + fighting=false; + System.out.println("Fight mode: "+fighting); + }break; + case "PASS":{ + System.out.println("Mythra's Health: "+GetMythraHealth()+"%"); + System.out.println("Aya's Health: "+GetAyaHealth()+"%"); + System.out.println("Target's Health: "+GetTargetHealth()+"%"); + }break; + case "SPRINT":{ + if (nextActionTimer==0){ + PressKey(KeyEvent.VK_N); + r.delay(100); + PressKey(KeyEvent.VK_N); + } else { + queueUpSprint=true; + } + }break; + case "UPDATE":{ + + }break; + default:{ + System.out.println("Unknown command: "+command); + } + } + throw new UnsupportedOperationException("Unimplemented method 'run'"); + } + + if (queueUpSprint&&nextActionTimer==0){ + PressKey(KeyEvent.VK_N); + r.delay(100); + PressKey(KeyEvent.VK_N); + nextActionTimer=100; + queueUpSprint=false; + } else { + if (fighting){ + run(Rotation.SCHOLAR); + } + } + long difference=System.currentTimeMillis()-lastFrame; + bioTimer=Math.max(0,bioTimer-difference); + lucidDreamingTimer=Math.max(0,lucidDreamingTimer-difference); + nextActionTimer=Math.max(0,nextActionTimer-difference); + aetherflowTimer=Math.max(0,aetherflowTimer-difference); + whisperingDawnTimer=Math.max(0,whisperingDawnTimer-difference); + feyIlluminationTimer=Math.max(0,feyIlluminationTimer-difference); + lastFrame=System.currentTimeMillis(); + r.delay(100); + } + } + }); + + t.start(); // reads message from client until "Over" is sent while (!line.equals("Over")) @@ -400,90 +489,14 @@ class Server { line = in.readUTF(); System.out.println("Received: "+line); - switch (line){ - case "FOLLOW":{ - PressKeyWithModifier(KeyEvent.VK_CONTROL,KeyEvent.VK_7); - }break; - case "FORWARD":{ - r.keyPress(KeyEvent.VK_W); - r.delay(1000); - r.keyRelease(KeyEvent.VK_W); - }break; - case "BACKWARD":{ - r.keyPress(KeyEvent.VK_S); - r.delay(1000); - r.keyRelease(KeyEvent.VK_S); - }break; - case "LEFT":{ - r.keyPress(KeyEvent.VK_Q); - r.delay(1000); - r.keyRelease(KeyEvent.VK_Q); - }break; - case "RIGHT":{ - r.keyPress(KeyEvent.VK_E); - r.delay(1000); - r.keyRelease(KeyEvent.VK_E); - }break; - case "FIGHT":{ - fighting=true; - fightingaoe=false; - System.out.println("Fight mode: "+fighting); - }break; - case "FIGHTAOE":{ - fighting=fightingaoe=true; - System.out.println("Fight mode: "+fighting); - }break; - case "CHILL":{ - fighting=false; - System.out.println("Fight mode: "+fighting); - }break; - case "PASS":{ - System.out.println("Mythra's Health: "+GetMythraHealth()+"%"); - System.out.println("Aya's Health: "+GetAyaHealth()+"%"); - System.out.println("Target's Health: "+GetTargetHealth()+"%"); - }break; - case "SPRINT":{ - if (nextActionTimer==0){ - PressKey(KeyEvent.VK_N); - r.delay(100); - PressKey(KeyEvent.VK_N); - } else { - queueUpSprint=true; - } - }break; - case "UPDATE":{ - - }break; - default:{ - System.out.println("Unknown command: "+line); - } - } + commandQueue.add(line); + } catch(IOException i) { System.out.println(i); break; } - - if (queueUpSprint&&nextActionTimer==0){ - PressKey(KeyEvent.VK_N); - r.delay(100); - PressKey(KeyEvent.VK_N); - nextActionTimer=100; - queueUpSprint=false; - } else { - if (fighting){ - run(Rotation.SCHOLAR); - } - } - long difference=System.currentTimeMillis()-lastFrame; - bioTimer=Math.max(0,bioTimer-difference); - lucidDreamingTimer=Math.max(0,lucidDreamingTimer-difference); - nextActionTimer=Math.max(0,nextActionTimer-difference); - aetherflowTimer=Math.max(0,aetherflowTimer-difference); - whisperingDawnTimer=Math.max(0,whisperingDawnTimer-difference); - feyIlluminationTimer=Math.max(0,feyIlluminationTimer-difference); - lastFrame=System.currentTimeMillis(); } System.out.println("Closing connection");