-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestApp.py
37 lines (27 loc) · 1 KB
/
RestApp.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
from SnakeGame import SnakeGame
from flask import Flask, request
from flask_restful import Resource, Api
import json
app = Flask(__name__)
api = Api(app)
class SnakeSimple(Resource):
def get(self):
game = SnakeGame()
game.newGame()
return {'turn':game.turn,'snakeLength':game.snakeLength,'validGame':game.validGame,
'gameBoard':json.dumps(game.gameBoard)}
def put(self):
game = SnakeGame()
gameString = request.form['game']
gameMap = json.loads(gameString)
game.turn = gameMap['turn']
game.snakeLength = gameMap['snakeLength']
game.validGame = gameMap['validGame']
game.gameBoard = gameMap['gameBoard']
game.checkSnakeSize()
game.checkEating()
return {'turn':game.turn,'snakeLength':game.snakeLength,'validGame':game.validGame,
'gameBoard':json.dumps(game.gameBoard)}
api.add_resource(SnakeSimple, "/")
if __name__ == '__main__':
app.run(host="0.0.0.0",port=80)