-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStagCheck.java
More file actions
77 lines (68 loc) · 2.41 KB
/
StagCheck.java
File metadata and controls
77 lines (68 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.io.*;
import java.net.*;
import java.lang.Process;
public class StagCheck
{
public static void main(String args[])
{
String playerName = "Steve";
String response;
Process server = startNewServer();
System.out.print("look...");
response = executeCommand(playerName + ": look");
if(response.contains("cabin")) System.out.println("SUCCESS");
else System.out.println("FAIL");
System.out.print("get....");
executeCommand(playerName + ": get axe");
response = executeCommand(playerName + ": inv");
if(response.contains("axe")) System.out.println("SUCCESS");
else System.out.println("FAIL");
System.out.print("goto...");
response = executeCommand(playerName + ": goto forest");
if(response.contains("tree")) System.out.println("SUCCESS");
else System.out.println("FAIL");
killOldServer(server);
}
private static Process startNewServer()
{
try {
String[] command = {"java", "-classpath", "dot-parser.jar:json-parser.jar:.", "StagServer", "entities.dot", "actions.json"};
Process process = Runtime.getRuntime().exec(command);
// Sleep for a bit to give the server some time to warm up
Thread.sleep(2000);
return process;
} catch(InterruptedException ie) {
return null;
} catch(IOException ioe) {
return null;
}
}
public static String executeCommand(String command)
{
try {
String response = "";
String incoming;
Socket socket = new Socket("127.0.0.1", 8888);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write(command + "\n");
out.flush();
while((incoming = in.readLine()) != null) response = response + incoming + "\n";
in.close();
out.close();
socket.close();
return response;
} catch(IOException ioe) {
System.out.println(ioe);
return "";
}
}
private static void killOldServer(Process server)
{
try {
server.destroy();
server.waitFor();
} catch(InterruptedException ie) {
}
}
}