Esempio n. 1
0
 def post(self):
     """Create a new user"""
     player = RESTPlayer.new(random.choice(raottt.COLORS))
     player.save()
     return flask.make_response(json.dumps({'token': player.pid,
                                            'name': player.name,
                                            'color': player.color,
                                            'score': player.score,
                                            'popupType': 'newPlayer'}))
Esempio n. 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)))
Esempio n. 3
0
 def post(self):
     """Create a new user"""
     player = RESTPlayer('Blue', opponent)
     bench.register(player)
     print("***** NEW USER CREATED *****")
     print("* I shall call you {}".format(player.name))
     print("* I will use id {}".format(player.upid))
     return res_wrap(json.dumps({'token': player.upid,
                                 'name': player.name,
                                 'color': player.color}))
Esempio n. 4
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}))