def cleanup(ttl): """Remove the inplay flag for abandoned games""" cutoff = datetime.datetime.utcnow() - datetime.timedelta(seconds=ttl) game_lst = [g for g in MongoDb.game.find({'checkout': {'$lt': cutoff}})] for game in game_lst: game = Game.load(game['gid']) logging.info('Spok force returning game %s checked out at %s', game.gid, game.checkout) game.inplay = False game.player = None game.checkout = None game.save()
def get(self, uid): """Return debug information about this game""" logging.error('Debug gid %s', uid) try: game = Game.load(uid) game.show() except KeyError: logging.error( 'API_Debug.get for unknown gid %s', uid) return flask.make_response(oh_no()) return flask.make_response( json.dumps( {'gid': game.gid, 'value': game.score.value, 'nextColor': game.next_color, 'checkout': str(game.checkout), 'player': game.player, 'score': game.score.dumpd()}))
def put(self, uid): """Apply a move to the specified game""" logging.debug('API_Game.put %s', flask.request.form) try: pid = flask.request.form['token'] source = int(flask.request.form['source']) target = int(flask.request.form['target']) except (KeyError, ValueError): return flask.make_response(oh_no()) player = RESTPlayer(Player.load(pid).dumpd()) if not player: logging.error('API_Game.put from unknown pid %s', pid) return flask.make_response(oh_no()) # Apply any score change that results from other people moves score_change = Score.check_for_score_update(player.pid) if score_change: player.score += score_change player.save() game = None try: game = Game.load(uid, validate=True, player=player) except ValueError: logging.error( 'API_Game.put pid %s returned to abandoned gid %s', player.pid, uid) except KeyError: logging.error( 'API_Game.put for unknown gid %s from pid %s', uid, pid) if not game: # Instead of giving the player an error, we just pretend to have # processed the move and they will get a new game. They probably # wont event notice ... return flask.make_response(json.dumps({'displayMsg': False, 'message': '', 'score': player.score})) player.queue_move((source, target)) score_change = game.make_move(player) player.score += score_change player.moves_made += 1 player.save() game.inplay = False game.save() if game.game_over(): game.cleanup(player.color) # There will be more points because the user participated in a # winning game score_additional = Score.check_for_score_update(player.pid) player.score += score_additional player.save() return flask.make_response(json.dumps( {'displayMsg': True, 'message': ('<p>Nice Move!</P><p>You just added {} points to ' 'your own score, and you helped out all the other ' '{} players who participated in this game.</p>' ''.format(score_change + score_additional, player.color)), 'score': player.score})) return flask.make_response(json.dumps({'displayMsg': False, 'message': '', 'score': player.score}))