-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·479 lines (392 loc) · 12.7 KB
/
app.py
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/usr/bin/python3
'''[This is a RPC game engine for Advance wars]'''
import os
import logging
import datetime
import secrets
from flask import redirect, render_template, abort, request
from flask_socketio import Namespace, join_room, leave_room
import jsons
from manager import GameManager
from gameboard import GameBoard
from config import Config
from app_core import app, jsonrpc, db, socketio
from models import Game
from mapping import Map, MAP1
logger = logging.getLogger(__name__)
logging.basicConfig(filename='app.log', level=logging.INFO)
config_game = Config()
#
# Helper functions
#
def setup_logging(level):
'''Setup logging.'''
logger.setLevel(level)
def game_load(token):
'''Loads the game token specified'''
game = Game.from_token(db.session, token)
if game:
mngr = GameManager(config_game, jsons.loads(game.board, GameBoard))
return mngr
board = GameBoard.create(Map.parse(MAP1))
mngr = GameManager(config_game, board)
return mngr
def game_save(mngr, token):
'''Saves the current game state'''
game = Game.from_token(db.session, token)
if not game:
game = Game(mngr.board, token)
else:
game.update = datetime.datetime.now()
game.board = jsons.dumps(mngr.board)
db.session.add(game)
db.session.commit()
def game_delete(token):
'''Deletes the game token specified.'''
game = Game.from_token(db.session, token)
if game:
db.session.delete(game)
db.session.commit()
def game_create(token):
'''Creates a new game with token specified'''
mngr = game_load(token)
game = Game(mngr.board, token)
db.session.add(game)
db.session.commit()
#
# REST
#
@app.route('/')
def index():
return redirect('/game/' + secrets.token_urlsafe(4))
@app.route('/game/<token>')
def game(token: str):
return render_template('render.html', token=token)
#
# Websocket
#
ws_games = {}
def ws_board_update(token):
socketio.emit('update', 'room', room=token)
def ws_msg(token, msg):
socketio.emit('message', msg, room=token)
class SocketIoNamespace(Namespace):
def on_error(self, e):
logger.error(e)
def on_connect(self):
logger.info('socketio - connect sid: %s' % request.sid)
def on_game(self, token):
logger.info('socketio - game sid: %s, token: %s' % (request.sid, token))
join_room(token)
ws_games[request.sid] = token
def on_disconnect(self):
logger.info('socketio - disconnect sid: %s' % request.sid)
if request.sid in ws_games:
leave_room(ws_games[request.sid])
del ws_games[request.sid]
socketio.on_namespace(SocketIoNamespace('/'))
#
# JSONRPC
#
@jsonrpc.method('troop_info')
def troop_info() -> dict:
'''Returns the unit config info
:return: [Unit configs]'''
logger.info('troop_info')
return jsons.dump(config_game.units)
@jsonrpc.method('message')
def message(token: str, msg: str) -> str:
'''rpc chat.
:return: [ok]
'''
logger.info('msg')
ws_msg(token, msg)
return 'ok'
@jsonrpc.method('game_delete')
def game_delete_rpc(token: str) -> str:
'''rpc delete game.
:return: [ok]
'''
logger.info(f'game_delete token={token}')
game_delete(token)
return 'ok'
@jsonrpc.method('game_create')
def game_create_rpc(token: str) -> str:
'''rpc-create game.
:return: [ok]
'''
logger.info(f'game_create token={token}')
game_create(token)
return 'ok'
@jsonrpc.method('game_board')
def game_board_rpc(token: str) -> dict:
'''rpc return game board.
:return: [gameboard]
'''
logger.info(f'game_board token={token}')
mngr = game_load(token)
return jsons.dump(mngr.board)
@jsonrpc.method('army_end_turn')
def army_end_turn_rpc(token: str) -> str:
'''rpc end current turn.
:return: [ok]
'''
mngr = game_load(token)
try:
mngr.army_end_turn()
game_save(mngr, token)
ws_board_update(token)
turn = mngr.check_turn()
logger.info(f'army_end_turn={turn.name}')
return jsons.dump(turn)
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('end_game')
def end_game_rpc(token: str) -> str:
'''rpc end game.
:return: [ok]
'''
logger.info(f'end game token={token}')
mngr = game_load(token)
try:
mngr.end_game()
game_save(mngr, token)
ws_board_update(token)
turn = mngr.check_turn()
text = turn.name + ' is the winner!'
logger.info(f'{turn.name} is the winner')
return jsons.dump(text)
except Exception as ex:
return abort(400, ex)
# return the game tile for the coord(x, y)
@jsonrpc.method('tile')
def tile_rpc(token: str, x: int, y: int) -> dict:
'''rpc return tile at coordinates
:return: [tile at coordinates given]
'''
logger.info(f'tile token={token}, x={x}, y={y}')
mngr = game_load(token)
try:
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('capture_tile')
def capture_tile_rpc(token: str, x: int, y: int) -> dict:
'''rpc capture tile
:return: [tile at coordinates given]
'''
logger.info(f'capture_city token={token}, x={x}, y={y}')
mngr = game_load(token)
try:
mngr.capture_tile(x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_wait')
def unit_wait_rpc(token: str, x: int, y: int) -> dict:
'''rpc unit wait
:return: [tile at coordinates given]
'''
logger.info(f'unit wait token={token}, x={x}, y={y}')
mngr = game_load(token)
try:
mngr.unit_wait(x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.unit_at(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_select')
def unit_select_rpc(token: str, x: int, y: int) -> dict:
'''rpc select unit at coordinate.
:return: [gameboard]
'''
logger.info(f'unit_select token={token}, x={x}, y={y}')
mngr = game_load(token)
try:
unit = mngr.unit_select(x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.unit_at(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_move')
def unit_move_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''rpc move unit from / to coordinates
:return: [tile at destination coordinates]
'''
logger.info(f'unit_move token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.unit_move(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_move2')
def unit_move2_rpc(token: str, id: str, x: int, y: int) -> dict:
'''rpc move unit for given ID to the coordinates.
:return: [tile at coordinates]
'''
logger.info(f'unit_move2 token={token}, id={id}, x={x}, y={y}')
mngr = game_load(token)
try:
mngr.unit_move2(id, x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_create')
def unit_create_rpc(token: str, army: str, unit_type: str, x: int, y: int) -> dict:
'''rpc create a unit at the coordinates given
:return: [tile at coordinates]
'''
logger.info(f'unit_create token={token}, army={army}, x={x}, y={y}')
mngr = game_load(token)
try:
mngr.unit_create(army, unit_type, x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
# need to return both attacker and defender
@jsonrpc.method('damage_estimate')
def damage_estimate_rpc(token: str, x: int, y: int, x2: int, y2: int) -> list:
'''rpc estimates the damage for attacker and defender.
:return: [tuple (attacker hp, defender hp) ]
'''
logger.info(f'damage_estimate token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
tup = mngr.damage_estimate(x, y, x2, y2)
return jsons.dump(tup)
except Exception as ex:
return abort(400, ex)
# need to return both attacker and defender
@jsonrpc.method('unit_attack')
def unit_attack_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''rpc attacks the unit from x,y to x2,y2
:return: [tile at given coordinate]
'''
logger.info(f'unit_attack token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.unit_attack(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_delete')
def unit_delete_rpc(token: str, x: int, y: int) -> dict:
'''rpc deletes unit at given coordinate.
:return: [tile at coordinates]
'''
logger.info(f'unit_delete token={token}, x={x}, y={y}')
mngr = game_load(token)
try:
mngr.unit_delete(x, y)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('check_turn')
def check_turn_rpc(token: str) -> str:
'''rpc checks the current army turn.
:return: [The current turn]
'''
logger.info(f'check turn token={token}')
mngr = game_load(token)
try:
turn = mngr.check_turn()
return jsons.dump(turn)
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_join')
def unit_join_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''rpc joins the unit from x,y to x2,y2.
:return: [Tile at x2,y2]
'''
logger.info(f'unit_join token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.unit_join(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_load')
def unit_load_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''rpc loads the unit from x,y to x2,y2.
:return: [Tile at x2,y2]
'''
logger.info(f'unit_load token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.unit_load(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_unload')
def unit_unload_rpc(token: str, x: int, y: int, x2: int, y2: int, index: int) -> dict:
'''rpc umloads the unit from x,y to x2,y2.
:return: [Tile at x2,y2]
'''
logger.info(
f'unit_unload token={token}, x={x}, y={y}, x2={x2}, y2={y2}, index={index}')
mngr = game_load(token)
try:
mngr.unit_unload(x, y, x2, y2, index)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('launch_missile')
def launch_missile_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''launches missile from x,y to x2,y2.
:return: [tile at destination coordinate]
'''
logger.info(
f'launch missile token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.launch_missile(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x, y))
except Exception as ex:
return abort(400, ex)
@jsonrpc.method('unit_resupply')
def unit_resupply_rpc(token: str, x: int, y: int, x2: int, y2: int) -> dict:
'''rpc resupply unit from x,y to x2,y2.
:return: [tile at destination coordinates]
'''
logger.info(f'resupply token={token}, x={x}, y={y}, x2={x2}, y2={y2}')
mngr = game_load(token)
try:
mngr.unit_resupply(x, y, x2, y2)
game_save(mngr, token)
ws_board_update(token)
return jsons.dump(mngr.tile_get(x2, y2))
except Exception as ex:
return abort(400, ex)
if __name__ == '__main__':
setup_logging(logging.DEBUG)
# create tables
with app.app_context():
db.create_all()
db.session.commit()
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
logging.info(f'binding to port: {port}')
socketio.run(app, host='0.0.0.0', port=port, debug=True)