Example #1
0
    def get(self, uid):
        """Return score information for this user"""
        player = Player.load(uid)
        total_lst = Score.get_global_score()
        total_map = {total_lst[0]['color']: total_lst[0],
                     total_lst[1]['color']: total_lst[1]}

        return flask.make_response(
            json.dumps({'score': player.score,
                        'turns': player.moves_made,
                        'games': player.games_participated_in,
                        'totalTurns': total_map['Red']['turns'] +
                                      total_map['Blue']['turns'],
                        'redWins': total_map['Red']['wins'],
                        'blueWins': total_map['Blue']['wins']}))
Example #2
0
    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)))
Example #3
0
    def get(self, uid):
        """Start the login process for the given uid"""
        logging.info('API_Auth.get %s', uid)

        try:
            player = Player.load(uid, None)
        except KeyError as err:
            logging.error(err)
            return flask.make_response(oh_no())

        if player.twitter_creds:
            return flask.make_response(
                json.dumps({'popupType': 'logout',
                            'name': player.name}))

        # If the current user does not have twitter_creds, then bring up the
        # do you want to login popup
        return flask.make_response(
            json.dumps(
                {'popupType': 'startLogin',
                 'url': flask.url_for('login_redirect')}))
Example #4
0
    def get(self, uid):
        """Load and return user specified by uid"""
        # If twitter name is set, then we came from the twitter login callback
        twitter_name = flask.session.pop('twitter_name', None)
        popup_type = twitter_name and 'newTwitter' or 'returningPlayer'

        logging.info('API_Player.get() uid:%s twitter_name:%s args:%s',
                     uid, twitter_name, flask.request.args)

        try:
            player = Player.load(uid, twitter_name)
        except KeyError as err:
            # Invalid (non-existing) user id, just create a new user
            logging.error(err)
            return self.post()

        if twitter_name and not player.twitter_creds:
            # User has just gotten their twitter creds
            player.name = twitter_name
            player.twitter_creds = True
            player.save()
            logging.info('Player %s got twitter_creds, now know as %s',
                         player.pid, twitter_name)

        # Apply any score change that results from other peoples moves
        score_change = Score.check_for_score_update(player.pid)
        if score_change:
            player.score += score_change
            player.save()

        return flask.make_response(json.dumps({'token': player.pid,
                                               'name': player.name,
                                               'color': player.color,
                                               'score': player.score,
                                               'creds' : player.twitter_creds,
                                               'popupType': popup_type}))
Example #5
0
    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}))