コード例 #1
0
ファイル: spok.py プロジェクト: christianreimer/raottt
def create_games(num_games, lower, upper):
    """Create ``num_games`` games"""
    blue = ComputerPlayer.new('Blue')
    red = ComputerPlayer.new('Red')

    for _ in range(num_games):
        if random.choice(raottt.COLORS) == 'Red':
            game = Game.new('Red')
            player_toggle = toggle(red, blue)
        else:
            game = Game.new('Blue')
            player_toggle = toggle(blue, red)

        turns = 0  # Make pylint happy
        for turns in range(random.randint(lower, upper)):
            if game.game_over():
                break

            player = player_toggle.__next__()
            game.make_move(player)
            game.validate()

        logging.info('Spok created game with %s turns, next player %s',
                     turns+1, game.next_color)
        game.save()
コード例 #2
0
ファイル: spok.py プロジェクト: christianreimer/raottt
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()
コード例 #3
0
ファイル: server.py プロジェクト: christianreimer/raottt
    def get(self, uid):
        """Return a game that can be played by the player with pid"""
        logging.debug('API_Game.get %s', uid)

        try:
            player = RESTPlayer(Player.load(uid).dumpd())
        except KeyError:
            logging.error('API_Game.get for unknown pid %s', uid)
            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 = Game.pick(player)
        return flask.make_response(json.dumps(adapter.enrich_message(game)))
コード例 #4
0
ファイル: server.py プロジェクト: christianreimer/raottt
    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()}))
コード例 #5
0
ファイル: server.py プロジェクト: christianreimer/raottt
    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}))