-
Notifications
You must be signed in to change notification settings - Fork 1
/
battleship-api-dev.txt
378 lines (330 loc) · 7.94 KB
/
battleship-api-dev.txt
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
create game
post username, message
POST /api/games
{
"username": {username},
"message": {message},
"rules": {rulesetId}
}
response new game id and session or error
201
{
"game": {gameid},
"session": {session}
}
400, 500
{message}
get list of opened
sends nothing
GET /api/games/
response list of {game id, messsge}
200
[
{
"game": {gameid},
"owner": {name},
"message": {game message},
"rules": {rulesetId}
},
...
]
500
{message}
invite stranger
post game id and session (only owner can invite strangers) and message
POST /api/games/{gameid}/{session}/setpublic
{
"message": {message}
}
response success if added in list or error
200
"ok"
404, 500
{
"error": {error}
}
connect
post game id, username, role (guest|player), short message
POST /api/games/{gameid}/connect/{guest|player}
{
"name": {name},
"message": {message}
}
response session, or error.
202
{
"session": {session}
}
404, 403, 400, 500
{message}
write message
post game id, session, message
POST /api/games/{gameid}/{session}/chat/
{message}
response success or error
201
"ok"
404, 403, 400, 500
{
"error": {message}
}
get rules list
sends nothing
GET /api/games/rules
response rules list or error
200
[
{
"id": {id}
"name": {name},
"description": {text},
"rules": {ship set}
}
]
404, 403, 400, 500
{
"error": {message}
}
read messages
send game id, session, last check date(or nothing)
GET /api/games/{gameid}/{session}/chat?lastcheck={date}
response list of [{name, message, date}], last check date or error
200
[
{
"name": {name},
"message": {message},
"time": {date}
},
...
]
404, 500
{message}
send map
post session id (owner and player can send map), json with map. Only empty or ship on map
POST /api/games/{gameid}/{session}/setmap
[[0,0,0,1,1,0,0...],[...],[...],...]
response ok or error (wrong map or other)
202
"ok"
406, 500
{message}
shoot
post game id, session (only owner and player can shoot and only in ther turn) and coords
POST /api/games/{gameid}/{session}/shoot
{
"x": {x},
"y": {y},
}
response result (hit|miss|sink|win) or error
202
{hit|miss|sink|win}
404, 403, 400, 500
{message}
get game status
send game id and session
GET /api/games/{gameid}/{session}/
response status (map contain only unknown or hit if game is not finished and everything if finished) or error
200
{
"game": {gameid},
"message": {message},
"you": {owner|player|guest},
"turn": {owner|player|notready},
"owner": {
"name": {name},
"message": {message},
"map": [[0,0,0,1,1,0,0...],[...],[...],...]
},
"player": {
"name": {name},
"message": {message},
"map": [[0,0,0,1,1,0,0...],[...],[...],...]
},
"guests": [
{
"name": {name},
"message": {message}
}
]
}
404, 500
{message}
get short status
send game id and session
GET /api/games/{gameid}
response status (map contain only unknown or hit if game is not finished and everything if finished) or error
200
{
"game": {gameid},
"message": {message},
"owner": {name},
"rules": {rules}
}
404, 500
{message}
invite a bot (when bot will be ready...)
post game id and session (only owner can invite a bot)
POST /api/games/{gameid}/{session}/invitebot
response success or error
200
"ok"
404, 501
"error": {message}
Rules:
[
{
"id": {id},
"name": {friendlyName},
"description": {description},
"ships": [[1,4],..]
},
...
]
Game:
{
"game": {gameid},
"date": {datetime},
"message": {message},
"rules": {free|starwars|classic|...}
"owner": {
"name": {name},
"message": {message},
"session": {session},
"map": [[0,0,0,1,1,0,0...],[...],[...],...]
},
"player": {
"name": {name},
"message": {message},
"session": {session},
"map": [[0,0,0,1,1,0,0...],[...],[...],...]
},
"turn": {owner|player|notready|finished},
"winner": {owner|player|""},
"public": {true|false},
"guests": [
{
"name": {name},
"message": {message},
"session": {session}
},
...
],
}
Messages:
{
"game": {game}
"name": {name},
"message": {message},
"time": {date}
}
Initial Game structure:
{
"game": {gameid},
"date": {datetime},
"rules": {free|starwars|classic|...},
"owner": {
"name": {name},
"message": {message},
"session": {session},
},
"turn": "notready",
"public": false,
"guests": [],
"chat: []
}
Map:
0 - empty
1 - ship
2 - miss
3 - hit
Notes:
Cheatfree games with predefined rules.
Settings are in config file.
For each rule:
rules: text with description
ships description:
[(1,4), (2,3), (3,2), (4,1)]
in send map I need to check rules. To do it:
get list of counts for separated non empty cells: [0,0,0,1,1,0,1,1,1,0] -> [2,3]
get the same from transposed map
get amount of twos, threes and so on. It should be as it is in the rules set.
for ones amount should be sum of length of not-ones from another list plus amount of ones multiple 2
1 0 1 0 1 1 1 1 0 1
1 0 1 0 0 0 0 0 0 0
1 0 0 0 0 1 1 0 1 0
0 0 1 1 0 0 0 0 1 0
1 0 0 0 0 1 0 0 0 0
isProjectionByRule [[1,2],[2,2],[3,1]] [1,1,1,1,1,1,2,1] [3,1,0,2,1,1,1]
1 - 7 4 = 11 - 7 = 4 / 2 = 2
2 - 1 1 = 2 -> 2*2 = 4
3 - 0 1 = 1 -> 3*1 = 3
[[1,0,1,0,1],[1,0,1,0,0],[1,0,0,0,0],[0,0,1,1,0],[1,0,0,0,0]]
[[1,1,1,0,1],[0,0,0,0,0],[1,1,0,1,0],[0,0,0,1,0],[1,0,0,0,0]]
1 0 1 0 1 1 1 1 0 1
1 0 1 0 0 0 0 0 1 0
1 0 0 0 0 1 1 0 1 0
0 1 1 0 0 0 0 0 0 0
1 0 0 0 0 1 0 0 0 0
[[1,0,1,0,1],[1,0,1,0,0],[1,0,0,0,0],[0,1,1,0,0],[1,0,0,0,0]]
[[1,1,1,0,1],[0,0,0,1,0],[1,1,0,1,0],[0,0,0,0,0],[1,0,0,0,0]]
1 0 1 0 1 1 1 1 0 1
1 0 1 0 0 0 0 0 0 0
1 0 0 0 0 1 1 0 3 0
0 0 3 3 0 0 0 0 3 0
1 0 0 0 0 1 0 0 0 0
2 3
y from map (head . (drop y) $ map)
l <- 3 0 3 3 0
and $ map (3==) $ getBefore (\x -> x==2 || x==0) $ drop x l
3 3
and [x' == 1 | x` < drop (x + 1)]
x from transpose map (head . (drop y) . transpose $ map)
0 3 3 0 3
getBefore :: Ord a => a -> [a] -> [a]
getBefore t (x:[]) = [x | x/=t]
getBefore t (x:xs) | x/=t = [x] ++ getBefore t xs
| otherwise = []
NOTREADY|NOTREADY_WITH_MAP|CONFIG|CONFIG_WAIT_OWNER|CONFIG_WAIT_PLAYER|OWNER|PLAYER|FINISHED
new
player_join
owner_map
player_map
owner_shoot
player_shoot
finished
new(1) - player_join(2) - owner_map(4) - player_map(6) - owner(6) - player(7) - finished(8)
- player_map(5) - owner_map(6) - owner(6) - player(7) - finished(8)
- owner_map(3) - player_join(4) - player_map(6) - owner(6) - player(7) - finished(8)
1 - NOTREADY
2 - CONFIG
3 - NOTREADY_WITH_MAP
4 - CONFIG_WAIT_PLAYER
5 - CONFIG_WAIT_OWNER
6 - OWNER
7 - PLAYER
8 - FINISHED
6-7-8 - I changed to handle winner
Create game:
curl -L http://localhost:9000/api/games/ -XPOST -H "Content-Type: application/json" -d'{"name":"dima 1","message":"Ha-ha-ha!!!!!","rules":"starwars"}' -v
Connect as player
curl -L http://localhost:9000/api/games/3c0baf16-88e1-428c-8430-5b5dcc84898e/connect/player -XPOST -H "Content-Type: application/json" -d'{"name":"dima","message":"Ha-ha-ha!!!"}' -v
Connect as guest:
curl -L http://localhost:9000/api/games/3c0baf16-88e1-428c-8430-5b5dcc84898e/connect/guest -XPOST -H "Content-Type: application/json" -d'{"name":"g1","message":"flkdjfldjflsdk fsdlfjslkjf"}' -v
Show status:
curl -L http://localhost:9000/api/games/3c0baf16-88e1-428c-8430-5b5dcc84898e/7e906a03-be82-431c-bbbe-1e602c78aaab -v
curl -L http://localhost:9000/api/games/3c0baf16-88e1-428c-8430-5b5dcc84898e/1f865540-3672-46d3-95da-f27377622a15 -v
curl -L http://localhost:9000/api/games/3c0baf16-88e1-428c-8430-5b5dcc84898e/ef3f8bc4-be8b-41fb-8971-7e3030126f50 -v
isShotSane
getCell
0 - MISS
1 -
isSink
True
isWin
True - SANK
False - WON
False - HIT
2 - ERR
3 - ERR
ERR