-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUtil.java
More file actions
201 lines (200 loc) · 5.48 KB
/
ClientUtil.java
File metadata and controls
201 lines (200 loc) · 5.48 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ClientUtil
{
public static String host = "http://localhost:8880";
public static class Action
{
public int agentid;
public String type;
public int x;
public int y;
public Action(int agentid, String type, int x, int y)
{
this.agentid = agentid;
this.type = type;
this.x = x;
this.y = y;
}
}
public static void sleep(long msec)
{
try {
Thread.sleep(msec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class User
{
public String error;
public String screenName;
public String name;
public String id;
public String password;
}
public static User userRegist(String screenName, String name, String password) throws IOException
{
Map<String, Object> body = new HashMap<>();
body.put("screenName", screenName);
body.put("name", name);
body.put("password", password);
String resp = fetch("POST", host + "/match",
new String[]{
"Content-Type", "application/json"
},
new Gson().toJson(body)
);
return new Gson().fromJson(resp, User.class);
}
public static User userShow(String identifier) throws IOException
{
String resp = fetch("GET", host + "/users/show/" + identifier,
new String[]{
"Content-Type", "application/json"
},
null
);
return new Gson().fromJson(resp, User.class);
}
public static void userDelete(String name, String id, String password) throws IOException
{
Map<String, Object> body = new HashMap<>();
body.put("name", name);
body.put("id", id);
body.put("password", password);
String resp = fetch("POST", host + "/users/delete",
new String[]{
"Content-Type", "application/json"
},
new Gson().toJson(body)
);
}
public static class Match
{
public String userId;
public String spec;
public String accessToken;
public String gameId;
}
public static Match match(String name, String id, String password, String spec) throws IOException
{
Map<String, Object> body = new HashMap<>();
body.put("name", name);
body.put("id", id);
body.put("password", password);
body.put("spec", spec);
String resp = fetch("POST", host + "/match",
new String[]{
"Content-Type", "application/json"
},
new Gson().toJson(body)
);
return new Gson().fromJson(resp, Match.class);
}
public static GameInfo getGameInfo(String roomid) throws IOException
{
String resp = fetch("GET", host + "/match/" + roomid,
new String[]{
"Content-Type", "application/json"
},
null
);
return new Gson().fromJson(resp, GameInfo.class);
}
public static void setAction(String roomid, String playerid, Action[] actions) throws IOException
{
printJson(actions);
Map<String, Object> body = new HashMap<>();
body.put("time", new Date().getTime() / 1000);
body.put("actions", actions);
fetch("POST", host + "/match/" + roomid + "/action",
new String[]{
"Content-Type", "application/json",
"Authorization", playerid
},
new Gson().toJson(body)
);
}
public static int diffTime(int unixTime)
{
return unixTime - (int) (new Date().getTime() / 1000);
}
public static class GameInfo
{
public String error;
public String roomID;
public Boolean gaming;
public Boolean ending;
public Integer width;
public Integer height;
public Integer[] points;
public Integer startedAtUnixTime;
public Integer nextTurnUnixTime;
public Integer turn;
public Integer totalTurn;
public Integer[][] tiled;
public static class Player
{
public String id;
public static class Agent
{
public String agentID;
public Integer x;
public Integer y;
}
public Agent[] agents;
public static class Point
{
public Integer basepoint;
public Integer wallpoint;
}
public Point point;
public Integer tilePoint;
public Integer areaPoint;
}
public Player[] players;
}
public static void printJson(Object obj) {
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
}
private static String fetch(String method, String url, String[] headers, String body) throws IOException
{
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod(method);
if (headers != null) {
for (int i = 0; i < headers.length; i += 2) {
conn.setRequestProperty(headers[i], headers[i + 1]);
}
}
if (body != null) {
conn.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(body);
out.flush();
}
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
sb.append(line);
}
return sb.toString();
}
return null;
}
}