Example #1
0
class User(orm.Document):
    collection_name = 'users'
    fields = [('id',
               orm.StringField(required=True,
                               load_from=MONGO_ID_SELECTOR,
                               dump_to=MONGO_ID_SELECTOR)),
              ('username', orm.StringField(required=True)),
              ('salt', orm.StringField(required=True)),
              ('hashed_password', orm.StringField(required=True)),
              ('admin_regions', orm.ListField(orm.StringField())),
              ('admin_level',
               orm.StringField(
                   required=True,
                   default='REGION',
                   validators=[orm.validate_choices(ADMIN_LEVEL_CHOICES)]))]
Example #2
0
class Tournament(orm.Document):
    collection_name = 'tournaments'
    fields = [('id',
               orm.ObjectIDField(required=True,
                                 load_from=MONGO_ID_SELECTOR,
                                 dump_to=MONGO_ID_SELECTOR)),
              ('name', orm.StringField(required=True)),
              ('type',
               orm.StringField(
                   required=True,
                   validators=[orm.validate_choices(SOURCE_TYPE_CHOICES)])),
              ('date', orm.DateTimeField()),
              ('regions', orm.ListField(orm.StringField())),
              ('url', orm.StringField()), ('raw_id', orm.ObjectIDField()),
              ('matches', orm.ListField(orm.DocumentField(Match))),
              ('players', orm.ListField(orm.ObjectIDField())),
              ('orig_ids', orm.ListField(orm.ObjectIDField()))]

    def validate_document(self):
        # check: set of players in players = set of players in matches
        players_ids = {player for player in self.players}
        matches_ids = {match.winner for match in self.matches} | \
                      {match.loser for match in self.matches}

        # Check for duplicate players
        if len(players_ids) != len(self.players):
            return False, "Tournament contains duplicate players"

        if players_ids != matches_ids:
            return False, "set of players in players differs from set of players in matches"

        # check: no one plays themselves
        for match in self.matches:
            if match.winner == match.loser:
                return False, "tournament contains match where player plays themself"

        # check: len of orig_ids should equal len of players
        if len(self.orig_ids) != len(self.players):
            return False, "different number of orig_ids and players"

        return True, None

    def post_init(self):
        # if orig_ids empty, set to players
        if not self.orig_ids:
            self.orig_ids = [player for player in self.players]

    def replace_player(self, player_to_remove=None, player_to_add=None):
        if player_to_remove is None or player_to_add is None:
            raise TypeError(
                "player_to_remove and player_to_add cannot be None!")

        player_to_remove_id = player_to_remove.id
        player_to_add_id = player_to_add.id

        if player_to_remove_id not in self.players:
            print "Player with id %s is not in this tournament. Ignoring." % player_to_remove.id
            return

        self.players.remove(player_to_remove_id)
        self.players.append(player_to_add_id)

        for match in self.matches:
            if match.winner == player_to_remove_id:
                match.winner = player_to_add_id

            if match.loser == player_to_remove_id:
                match.loser = player_to_add_id

    def contains_player(self, player):
        return player.id in self.players

    @classmethod
    def from_pending_tournament(cls, pending_tournament):
        # takes a real alias to id map instead of a list of objects
        def _get_player_id_from_map_or_throw(alias_to_id_map, alias):
            if alias in alias_to_id_map:
                return alias_to_id_map[alias]
            else:
                raise ValueError('Alias %s has no ID in map\n: %s' %
                                 (alias, alias_to_id_map))

        alias_to_id_map = dict([(entry.player_alias, entry.player_id)
                                for entry in pending_tournament.alias_to_id_map
                                if entry.player_id is not None])

        # we need to convert pending tournament players/matches to player IDs
        print pending_tournament.players, pending_tournament.matches
        players = [
            _get_player_id_from_map_or_throw(alias_to_id_map, p)
            for p in pending_tournament.players
        ]

        matches = []
        counter = 0
        for am in pending_tournament.matches:
            m = Match(match_id=counter,
                      winner=_get_player_id_from_map_or_throw(
                          alias_to_id_map, am.winner),
                      loser=_get_player_id_from_map_or_throw(
                          alias_to_id_map, am.loser),
                      excluded=False)
            matches.append(m)
            counter += 1

        return cls(id=pending_tournament.id,
                   name=pending_tournament.name,
                   type=pending_tournament.type,
                   date=pending_tournament.date,
                   regions=pending_tournament.regions,
                   url=pending_tournament.url,
                   raw_id=pending_tournament.raw_id,
                   matches=matches,
                   players=players,
                   orig_ids=players)