This repository has been archived by the owner on Sep 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mimus_server.py
407 lines (348 loc) · 17.3 KB
/
mimus_server.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
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=line-too-long,invalid-name
"""Server API."""
from __future__ import with_statement
from pprint import pformat
from redis import StrictRedis
from gcloud import pubsub
import uuid
import logging
import random
# Set up logging.
logname = 'mimus.server'
logger = logging.getLogger(logname)
# Custom modules
import db_api.objects.card as card
import db_api.objects.player as player
import db_api.enqueue as enqueue
from mimus_cfg import cfg
# Game 'session' object. One per player.
class Session(object):
"""Object represention of this player's game session.
General strategy of each of the public methods:
- Validate the client can take the requested action (if necessary)
- Make a transaction (list of DB API queries) that should all run to
completion to update the database and get the latest results of the action
- Generate a unique transaction ID. Currently only used by the DB API to
track completion of the transaction, but in the future could also be used
by the server to look up past results or track reasons for transaction failure.
- Run the transaction through the private '__execute_db_transaction' function,
which queues the queries and parses results into the object attributes as
necessary
- Return result to the client.
Attributes:
log: log file handle.
cfg: configuration dictionary (typically read from mimus_cfg.py)
session_id: an alias for player_id.
workq: Google Cloud Pub/Sub topic to place db work into.
redis: Redis connection to read db results from.
player: Local cache copy of the player row from the db.
cards: Local cache copy of the player's cards from the db.
"""
def __init__(self, player_id):
"""Initialize session object.
Sets up DB API connections to Redis and Pub/Sub for this session, and
does initial fetch of player and cards from the database.
Args:
player_id: Hashed player name.
"""
# Logging and configuration
self.log = open('backend_issues.log', 'a+')
self.cfg = cfg
self.session_id = player_id
# Connect to DB API Cloud Pub/Sub and Redis
logger.info("Connecting to DB API...")
client = pubsub.Client(project=self.cfg['gcp']['project'])
logger.info("Connecting: DB API pubsub topic '%s'",
self.cfg['pubsub']['topic'])
self.workq = client.topic(self.cfg['pubsub']['topic'])
logger.info("Connecting: DB API Redis instance at '%s:%s'",
self.cfg['redis_con']['hostname'], self.cfg['redis_con']['port'])
self.redis = StrictRedis(host=self.cfg['redis_con']['hostname'],
port=self.cfg['redis_con']['port'],
db=self.cfg['redis_con']['db'],
password=self.cfg['redis_con']['password'])
# Initialize attributes to empty
self.player = None
self.cards = {}
# Attempt to get initial attribute values from DB
self._get_player(player_id)
self._get_cards()
def _execute_db_transaction(self, trans_id, transaction):
"""Runs a prepared transaction against the database.
Attempts to update session object attributes (self.player, self.cards,
etc) with the results.
Args:
trans_id: The transaction ID.
transaction: The list of queries that make up this transaction.
Returns:
If the transaction succeeds: number of rows affected.
If the transaction fails: boolean value False.
Doesn't explicitly return results; if successful the updated results
are available in the object's attributes.
"""
logger.debug(pformat(transaction))
# Execute against db
data = enqueue.execute_batch(trans_id=trans_id,
queries=transaction,
worker_q=self.workq,
ack_redis=self.redis,
srv_id=self.session_id,
log=self.log)
# Look through the results for updates to the session.cards or session.player
if data:
if 'cardlist' in data and data['cardlist']:
self.cards = {card['id']: card for card in data['cardlist']}
if 'player' in data and data['player']:
self.player = data['player'][0]
return data['affected']
# Explicitly return false if no data was returned from the database -
# something went wrong.
return False
def _get_player(self, player_id):
"""Build and execute DB API transaction to retrieve the player row.
If player doesn't exist, build and execute DB API transaction to
initialize the player row and then retrieve it.
Args:
player_id: Hashed player name.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest player stats; if successful the
updated results are available in the object's attributes.
Raises:
RuntimeError: There was an issue retrieving the player's row.
"""
logger.info("Getting Player!")
# Var init
self.player = None
trans_id = str(uuid.uuid4())
transaction = player.get(player_id)
# Since a query that returns no rows will return a 0, explicitly check for
# the False keyword value
if self._execute_db_transaction(trans_id, transaction) is not False:
logger.debug("Printing player! %s", self.player)
if self.player:
return True
else:
# There was nothing to fetch from the db, need to make this player
trans_id = str(uuid.uuid4()) # Generate a new transaction ID
transaction = player.create(player_id, cfg)
# Get queries to make the specified number of each kind of card,
# defined in the config file.
initial_cards = self.cfg['player']['initial_cards']
for loot_type in initial_cards:
for i in range(initial_cards[loot_type]): # pylint: disable=unused-variable
card_type = random.randint(
self.cfg['loot_tables'][loot_type]['min'],
self.cfg['loot_tables'][loot_type]['max'])
transaction.extend(card.create(player_id, card_type))
logger.info("Creating initial cards for player '%d'",
player_id)
# Create player, create n cards. _get_cards is called immediately
# after, so no need to get cards yet.
transaction.extend(player.get(player_id))
return self._execute_db_transaction(trans_id, transaction)
else:
raise RuntimeError(
"Unable to retrieve player %s from the database!" % player_id)
def _get_cards(self):
"""Build and execute DB API transaction to retrieve latest player
cardlist.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest cardlist; if successful the
updated results are available in the object's attributes.
Raises:
RuntimeError: There was an issue retrieving the cards from the db.
"""
trans_id = str(uuid.uuid4())
transaction = card.get_all(self.player['id'])
results = self._execute_db_transaction(trans_id, transaction)
# Since a query that returns no rows will return a 0, explicitly check for
# the False keyword value
if results is False:
raise RuntimeError(
"Unable to retreive cards for player %s from the database!" %
self.player['id'])
return results
def level_card(self, dest_id, cards_to_consume):
"""Build and execute DB API transaction to combine cards
and retrieve latest player cardlist.
Args:
Same as those of card.combine(), which this method calls.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest cardlist; if successful the
updated results are available in the object's attributes.
Raises:
RuntimeError: There was an issue with the database transaction
required to level the card.
"""
transaction = card.combine(self.cards[dest_id], cards_to_consume)
transaction.extend(card.get_all(self.player['id']))
trans_id = str(uuid.uuid4())
results = self._execute_db_transaction(trans_id, transaction)
# Since a database transaction that returns no rows will return a 0,
# explicitly check for the False keyword value
if results is False:
raise RuntimeError("Unable to combine cards for player %s!" %
self.player['id'])
# Otherwise, everything looks successful
logger.info("Leveled cardID %d by consuming %d cards",
dest_id, len(cards_to_consume))
return results
def evolve_card(self, dest_id, cards_to_consume):
"""Build and execute DB API transaction to combine cards
and retrieve latest player cardlist.
Args:
Same as those of card.evolve(), which this method calls.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest cardlist; if successful the
updated results are available in the object's attributes.
Raises:
RuntimeError: There was an issue with the database transaction
required to evolve the card.
"""
transaction = card.evolve(self.cards[dest_id], cards_to_consume)
transaction.extend(card.get_all(self.player['id']))
trans_id = str(uuid.uuid4())
results = self._execute_db_transaction(trans_id, transaction)
# Since a query that returns no rows will return a 0, explicitly check for
# the False keyword value
if results is False:
raise RuntimeError("Unable to evolve cards for player %s!" %
player['id'])
# Otherwise, everything looks successful
logger.info("Evolved cardID %d by consuming %d cards",
dest_id, len(cards_to_consume))
return results
def play_stage(self):
"""Build and execute DB API transaction to simulate player playing a stage.
Note: Stamina is not currently validated.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest cardlist/player stats;
if successful the updated results are available in the
object's attributes.
Raises:
RuntimeError: There was an issue with the database transaction
required to evolve the card.
"""
# Obviously this could be a call out to a key/value store to get a constantly
# updating chance of drops
loot_table = self.cfg['loot_tables']['std'] # Standard loot table
num_rounds = 5 # rounds in this level
transaction = []
# Test to see if the player failed the stage
if random.random() <= self.cfg['stage']['failure_chance']:
# Roll for card drops
for i in range(num_rounds):
if (len(self.cards) + len(transaction)) < self.player['slots']:
#logger.debug(" Playing round %d" % i)
card_type = None
# Roll d100
roll = random.random()
if roll <= loot_table['drop_chance']:
# This can be replaced with a more advanced probabilistic function, just
# random for now
card_type = random.randint(loot_table['min'],
loot_table['max'])
transaction.extend(card.create(self.player['id'],
card_type))
loot_msg = " Round %2d: Rolled %.2f/%.2f for player %d, dropped card %s"
logger.info(loot_msg, i, roll, loot_table['drop_chance'],
self.player['id'], str(card_type))
else:
full_msg = "****Player (%d) doesn't have any more slots! Discarding remaining drops..."
logger.warning(full_msg, self.player['id'])
break
logger.info(" Player completed stage - %2d loot cards acquired.",
len(transaction))
# Assume player took a friend along, give them friend points
updated_player = self.player.copy()
updated_player['points'] = self.player['points'] + self.cfg[
'stage']['points_per_run']
# Test that query generation is successful. Necessary as query generation
# will fail if, for example, the player already has max friend points
update_player_query = player.update(updated_player)
if update_player_query:
transaction.extend(update_player_query)
else:
logger.error(
"Unable to update player! (continuing without update!)")
# After updates, get the latest player/cardlist
transaction.extend(player.get(self.player['id']))
transaction.extend(card.get_all(self.player['id']))
# Run transaction
trans_id = str(uuid.uuid4())
results = self._execute_db_transaction(trans_id, transaction)
# Since a query that returns no rows will return a 0, explicitly check for
# the False keyword value
if results is False:
raise RuntimeError("Unable to Play Stage for player %s!" %
self.player['id'])
return results
else:
logger.info(" Player failed stage!")
return False
def add_slots(self, num_slots):
"""Build and execute DB API transaction to add slots to a player.
If player doesn't exist, build and execute DB API transaction to
initialize the player row and then retrieve it.
Args:
player_id: Hashed player name.
Returns:
If successful: boolean True or a positive integer indicating the number
of rows affected.
If unsuccessful: boolean False or a zero-value integer.
Note: Doesn't explicitly return latest player stats; if successful the
updated results are available in the object's attributes.
Raises:
RuntimeError: There was an issue updating or retrieving the player's
row.
"""
transaction = []
# Test that query generation is successful. Necessary as query generation
# will fail if, for example, the player already has max slots
updated_player = self.player
updated_player['slots'] = self.player['slots'] + num_slots
update_player_query = player.update(updated_player)
if update_player_query:
transaction.extend(update_player_query)
else:
logger.error(
"Unable to update player! (continuing without update!)")
# Get player after updating slots
transaction.extend(player.get(self.player['id']))
# Run transaction
trans_id = str(uuid.uuid4())
results = self._execute_db_transaction(trans_id, transaction)
# Since a query that returns no rows will return a 0, explicitly check for
# the False keyword value
if results is False:
raise RuntimeError("Unable to add slots to player %s!" % player['id'])
return results