-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.py
124 lines (104 loc) · 4 KB
/
webserver.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# webserver.py
#
# Copyright 2015 zeglor <zeglor@zeglor-desktop>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Player message looks something like that: {"action": action_requested}
# What messages is player able to send us:
# * Report that he's still online (should not be called that often) {"action": "heartbeat"}
# * Request a new game to join {"action": "joinGame" }
# * Report his move {"action": "move", "cell": [x, y]}
from gevent import sleep, monkey, spawn
monkey.patch_all()
from gevent.wsgi import WSGIServer
from flask import Flask, session, request, render_template, jsonify
from game import Player, cleanup
from random import uniform as randUniform
from datetime import datetime
from secret import secret_key
POLL_TIMEOUT = 5
class RemotePlayer:
def __init__(self, session, request):
playerKey = session.get("id")
knownGameState = request.get_json().get("knownGameState")
self.player = Player(playerKey, None, knownGameState)
session["id"] = self.player.key
self.player.dbSave()
#if self.player.game is not None:
# self.player.game.dbSave()
def __enter__(self):
return self.player
def __exit__(self, type, value, traceback):
self.player.dbSave()
app = Flask(__name__)
#app.debug = True
app.secret_key = secret_key
@app.route("/pub/", methods=["POST"])
def pub():
with RemotePlayer(session, request) as player:
data = request.get_json()
action = data.get("action")
# process requested action
if action == "heartbeat":
# player gets automatically updated after request is finished
# we should store only game state
if player.game is not None:
player.game.dbSave()
elif action == "joinGame":
# pdb.set_trace()
player.startOrJoinGame()
elif action == "move":
cellIndex = [int(indx) for indx in data.get("cell")]
player.game.makeMove(player, cellIndex)
return jsonify({'status': 'ok'})
@app.route("/sub/", methods=["POST"])
def sub():
def timeoutPassed(pollTime):
return (datetime.now() - pollTime).total_seconds() >= POLL_TIMEOUT
with RemotePlayer(session, request) as player:
game = player.game
if game is None:
return jsonify({"type": "error", "reason": "not_connected"})
try:
isUrgentMessage = request.get_json()["urgent"]
except KeyError:
isUrgentMessage = False
pollTime = datetime.now()
while (not game.hasUpdatesForPlayer(player)) and (not timeoutPassed(pollTime)) and not isUrgentMessage:
sleep(randUniform(0.03, 0.3))
# now we either have updates for player, or timeout passed, or player requested immediate update
if timeoutPassed(pollTime):
return jsonify({"type": "timeout"})
else:
# make message
msg = game.getStateDict(player)
msg["type"] = "event"
return jsonify(msg)
@app.route("/")
def index():
return render_template('tictactoe.html')
def cleanup_forever():
while (True):
sleep(60)
cleanup()
if __name__ == '__main__':
spawn(cleanup_forever)
http_server = WSGIServer(('', 8080), app)
http_server.serve_forever()