forked from quadra-game/quadra-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qserv.py
226 lines (180 loc) · 5.76 KB
/
qserv.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
import datetime
import logging
import pickle
import time
from google.appengine.ext import webapp
import models
REQUIRED_SCORE_KEYS = ['score', 'name', 'rec', 'level', 'lines']
def set_param(params, key, value):
path = key.split('/')
dir = params
file = path.pop()
while len(path):
item = path.pop(0)
if item not in dir:
dir[item] = {}
dir = dir[item]
dir[file] = value
def format_params(root, params):
output = ''
for (key, value) in params.iteritems():
if isinstance(value, dict):
output += format_params(root + '/' + key, value)
else:
output += root + '/' + key + ' ' + str(value) + '\n'
return output
def put_score(params):
missing_keys = filter(lambda key: key not in params, REQUIRED_SCORE_KEYS)
if missing_keys:
logging.error('score missing required key(s): %s'
% ', '.join(missing_keys))
return
score = int(params['score'])
item = models.Score.get_by_key_name('score:' + str(score))
if item:
try:
data = pickle.loads(item.data)
if data['rec'] != params['rec']:
logging.warning('new score %d (by "%s") equalled old score (by "%s"),'
' ignoring new score' % (score, data['name'], params['name']))
item = None
except:
logging.error('error unpickling score %d, will overwrite' % score)
item = None
if not item:
# We scrub a few bits of informations before storing it.
score_data = params.copy()
score_data.pop('num', None)
score_data.pop('port', None)
if 'info' in score_data:
score_data['info'] = params['info'].copy()
score_data['info'].pop('remoteaddr', None)
item = models.Score(key_name='score:' + str(score),
score=score, data=pickle.dumps(score_data))
item.put()
class QServHandler(webapp.RequestHandler):
# Default number of scores/demos to send on gethighscores
default_demos = 5
# Number of scores kept in our store
scores_to_keep = 100
# Timeout before expiring games
timeout = datetime.timedelta(0, 180)
def get(self):
return self.process()
def post(self):
return self.process()
def postdemo(self):
put_score(self.params)
self.gethighscores()
def get_scores(self):
query = models.Score.all()
scores_entities = {}
for score in query:
scores_entities[score.score] = score
scores_list = scores_entities.keys()
scores_list.sort(None, None, True)
if len(scores_list) > self.scores_to_keep:
old_scores = scores_list[self.scores_to_keep:]
del scores_list[self.scores_to_keep:]
for score in old_scores:
logging.info('deleting score ' + str(score))
scores_entities[score].delete()
scores = {}
for score in scores_list:
scores[score] = pickle.loads(scores_entities[score].data)
return scores
def gethighscores(self):
scores = self.get_scores()
num = int(self.params.get('num', self.default_demos))
scores_list = scores.keys()
scores_list.sort(None, None, True)
if len(scores_list) > num:
del scores_list[num:]
self.response.headers['Content-Type'] = 'text/plain; charset=latin1'
self.response.out.write('Ok\n')
high = 0
for score in scores_list:
data = format_params('high' + str(high).zfill(3), scores[score])
if isinstance(data, unicode):
data = data.encode('latin1')
self.response.out.write(data)
high += 1
def postgame(self):
key = self.params['info']['remoteaddr'] + ':' + str(self.params['port'])
game = models.Game(key_name='game:' + key,
addr=self.params['info']['remoteaddr'],
port=int(self.params['port']),
data=pickle.dumps(self.params))
game.put()
self.response.out.write('Game updated\n')
def getgames(self):
games = {}
expiration = datetime.datetime.utcnow() - self.timeout
for game in models.Game.all():
if game.timestamp < expiration:
game.delete()
continue
games[(game.addr, game.port)] = pickle.loads(game.data)
set_param(games[(game.addr, game.port)], 'info/lastupdate',
int(time.mktime(game.timestamp.timetuple())))
strip_players = False
strip_port = False
if 'info' in self.params:
if 'quadra_version' in self.params['info']:
if self.params['info']['quadra_version'] == '1.1.2':
strip_players = True
else:
if 'qsnoop_version' not in self.params['info']:
strip_port = True
self.response.out.write('Current games\n')
seen = {}
for addr, info in games.iteritems():
key = addr[0]
if not strip_port:
key += ':' + str(addr[1])
if key in seen:
continue
seen[key] = True
if strip_players:
info.pop('players', None)
self.response.out.write(format_params(key, info))
def deletegame(self):
game = models.Game.get_by_key_name('game:' +
self.params['info']['remoteaddr'] + ':' +
str(self.params['port']))
if game is not None:
game.delete()
self.response.out.write('Game deleted\n')
else:
self.response.out.write('Game not found\n')
def process(self):
self.params = {}
lines = self.request.get('data').splitlines()
if not len(lines):
lines = ['nothing']
cmd = lines.pop(0)
for line in lines:
(key, value) = line.split(None, 1)
set_param(self.params, key, value)
try:
if int(self.params['port']) > 65535:
raise ValueError
except:
self.params['port'] = 3456
set_param(self.params, 'info/remoteaddr', self.request.remote_addr)
logging.info('command: ' + cmd)
self.response.headers['Content-Type'] = 'text/plain'
if cmd == 'postdemo':
self.postdemo()
elif cmd == 'gethighscores':
self.gethighscores()
elif cmd == 'postgame':
self.postgame()
elif cmd == 'getgames':
self.getgames()
elif cmd == 'deletegame':
self.deletegame()
else:
self.response.set_status(500)
self.response.out.write('Hi, I\'m the Python Quadra game server.\n'
'You should use Quadra to talk to me :).\n')