Exemplo n.º 1
0
    def post(self):
        try:
            card_set_indices = list(
                map(int, self.request.arguments['card_sets']))
        except ValueError as exc:
            raise APIError(Codes.NOT_AN_INTEGER, exc)
        if min(card_set_indices) < 0 or \
           max(card_set_indices) >= len(self.application.card_sets):
            raise APIError(Codes.ILLEGAL_RANGE, card_set_indices)
        card_sets = [self.application.card_sets[i] for i in card_set_indices]

        password = self.get_argument('password', '')  # not yet implemented
        if password:
            password = hash_obj(password)
        name = self.get_argument('name', '')
        if not name:
            name = 'Game %d' % (len(self.application.games) + 1)

        max_score = self.get_argument('max_score')
        if not max_score:
            max_score = INFINITY
        try:
            max_score = int(max_score)
            max_players = int(self.get_argument('max_players'))
            max_clue_length = int(self.get_argument('max_clue_length'))
        except ValueError as exc:
            raise APIError(Codes.NOT_AN_INTEGER, exc)

        if (not Limits.MIN_NAME <= len(name) <= Limits.MAX_NAME) or \
           (not Limits.MIN_PLAYERS <= max_players <= Limits.MAX_PLAYERS) or \
           (not Limits.MIN_SCORE <= max_score <= Limits.MAX_SCORE) or \
           (not Limits.MIN_CLUE_LENGTH <= max_clue_length <= Limits.MAX_CLUE_LENGTH):
            raise APIError(Codes.ILLEGAL_RANGE)

        game = Game(self.user, card_sets, password, name, max_players,
                    max_score, max_clue_length)
        self.application.games.append(game)
        self.write(str(len(self.application.games) - 1))
Exemplo n.º 2
0
 def cast_vote(self, user, card):
     """Make the given user vote for a card, or throws APIError."""
     if self.state != States.VOTE:
         raise APIError(Codes.VOTE_BAD_STATE)
     if user == self.clue_maker():
         raise APIError(Codes.VOTE_NOT_TURN)
     if not user in self.players:
         raise APIError(Codes.VOTE_UNKNOWN_USER)
     if not self.round.has_card(card):
         raise APIError(Codes.VOTE_INVALID)
     if self.round.has_voted(user):
         raise APIError(Codes.VOTE_ALREADY)
     self.round.cast_vote(user, card)
     if self.round.has_everyone_voted():
         # Transition from VOTE to CLUE or END.
         self._do_scoring()
         self.turn = (self.turn + 1) % len(self.players)
         self.state = States.CLUE
         if self.deck.is_empty():
             self.state = States.END
         for p in self.players.values():
             if p.score >= self.max_score:
                 self.state = States.END
     self.ping()