You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DungeonHelper/sig/App.java

279 lines
9.2 KiB

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.Rectangle;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
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<Long, String> 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_NUMPAD7:{
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_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:
case GlobalKeyEvent.VK_RETURN:{
out.writeUTF("CHILL");
}break;
case GlobalKeyEvent.VK_NUMPAD9:{
out.writeUTF("PASS");
}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("192.168.1.89", 5000);
}
}
class Server
{
//initialize socket and input stream
private Socket socket = null;
private ServerSocket server = null;
private DataInputStream in = null;
Robot r;
boolean fighting=false;
private void PressKeyWithModifier(int modifier,int keycode) {
r.keyPress(modifier);
r.keyPress(keycode);
r.keyRelease(keycode);r.delay(100);
r.keyRelease(modifier);
}
float GetMythraHealth(){
BufferedImage img=r.createScreenCapture(new Rectangle(105, 193, 107, 1));
for (int i=img.getWidth()-1;i>=0;i--){
if ((img.getRGB(i,0)&255)>100){
return i/(img.getWidth()-1)*100;
}
}
return 0;
}
float GetAyaHealth(){
BufferedImage img=r.createScreenCapture(new Rectangle(105, 243, 107, 1));
for (int i=img.getWidth()-1;i>=0;i--){
if ((img.getRGB(i,0)&255)>100){
return i/(img.getWidth()-1)*100;
}
}
return 0;
}
float GetTargetHealth(){
BufferedImage img=r.createScreenCapture(new Rectangle(704, 149, 539, 1));
for (int i=img.getWidth()-1;i>=0;i--){
if ((img.getRGB(i,0)&255)>100){
return i/(img.getWidth()-1)*100;
}
}
return 0;
}
// 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();
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;
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;
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);
}
}