-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.pde
145 lines (118 loc) · 4.07 KB
/
Host.pde
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import processing.net.Client;
import processing.net.Server;
import java.util.Random;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLConnection;
PApplet myApplet = this;
class Host {
Server myServer;
Client myClient;
URL gameURL;
URLConnection myConn;
String baseURL = "https://ripgod.herokuapp.com";
// String baseURL = "http://localhost:20793";
String gameRoom;
boolean pinged = false;
//Button used to allocate roles to players
ActionButton roleAlloc;
Host() {
genGameRoom();
roleAlloc = new ActionButton(Action.START_GAME, new PVector(width / 2, height * 0.75));
try {
gameURL = new URL(String.format("%s/admin", baseURL));
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
void draw() {
roleAlloc.draw();
}
void mouseClick() {
roleAlloc.mouseClicked();
}
/*
* Creates the room to play the game
*/
void ping() {
if (!pinged) {
try {
myConn = gameURL.openConnection();
myConn.connect();
System.out.println("What, no errors?");
BufferedReader in = new BufferedReader(new InputStreamReader(myConn.getInputStream()));
System.out.println("Okay, please give me an error...");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
pinged = true;
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* Generates the code which will be used for the game room.
*/
void genGameRoom() {
StringBuilder code = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 5; i++) code.append((char) (random.nextInt(26) + 65));
gameRoom = code.toString();
baseURL += "/" + gameRoom;
}
/*
* Performs a GET request to the specified endpoint
*/
ArrayList<String> sendRequest(String endpoint) {
ArrayList<String> response = new ArrayList<String>();
try {
URL reqURL = new URL(String.format("%s/%s", baseURL, endpoint));
URLConnection reqConn = reqURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(reqConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
response.add(inputLine);
in.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
return null;
}
return response;
}
/*
* Allows for sending a POST request to an endpoint on the server.
*/
ArrayList<String> postData(String endpoint, JSONObject data) {
ArrayList<String> response = new ArrayList<String>();
try {
URL postURL = new URL(String.format("%s/%s", baseURL, endpoint));
HttpURLConnection post = (HttpURLConnection) postURL.openConnection();
post.setRequestMethod("POST");
post.setRequestProperty("Content-Type", "application/json");
post.setDoOutput(true);
PrintWriter postWriter = new PrintWriter(post.getOutputStream());
postWriter.write(data.toString());
postWriter.close();
String inputLine;
BufferedReader in = new BufferedReader(new InputStreamReader(post.getInputStream()));
while ((inputLine = in.readLine()) != null)
response.add(inputLine);
in.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
return response;
}
String getGameURL() {
return baseURL;
}
}