Esempio n. 1
0
class Ranking(orm.Document):
    collection_name = 'rankings'
    fields = [('id',
               orm.ObjectIDField(required=True,
                                 load_from=MONGO_ID_SELECTOR,
                                 dump_to=MONGO_ID_SELECTOR)),
              ('region', orm.StringField(required=True)),
              ('tournaments', orm.ListField(orm.ObjectIDField())),
              ('time', orm.DateTimeField()),
              ('ranking', orm.ListField(orm.DocumentField(RankingEntry)))]
Esempio n. 2
0
class Merge(orm.Document):
    collection_name = 'merges'
    fields = [('id',
               orm.ObjectIDField(required=True,
                                 load_from=MONGO_ID_SELECTOR,
                                 dump_to=MONGO_ID_SELECTOR)),
              ('requester_user_id', orm.StringField()),
              ('source_player_obj_id', orm.ObjectIDField(required=True)),
              ('target_player_obj_id', orm.ObjectIDField(required=True)),
              ('time', orm.DateTimeField())]

    def validate_document(self):
        if self.source_player_obj_id == self.target_player_obj_id:
            return False, "source and target must be different"

        return True, None
Esempio n. 3
0
class Ranking(orm.Document):
    collection_name = 'rankings'
    fields = [('id',
               orm.ObjectIDField(required=True,
                                 load_from=MONGO_ID_SELECTOR,
                                 dump_to=MONGO_ID_SELECTOR)),
              ('region', orm.StringField(required=True)),
              ('tournaments', orm.ListField(orm.ObjectIDField())),
              ('time', orm.DateTimeField()),
              ('ranking', orm.ListField(orm.DocumentField(RankingEntry)))]

    def get_ranking_for_player_id(self, player_id):
        for entry in self.ranking:
            if entry.player == player_id:
                return entry.rank
        return None
Esempio n. 4
0
class PendingTournament(orm.Document):
    collection_name = 'pending_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)),
              ('date', orm.DateTimeField()),
              ('regions', orm.ListField(orm.StringField())),
              ('url', orm.StringField()), ('raw_id', orm.ObjectIDField()),
              ('matches', orm.ListField(orm.DocumentField(AliasMatch))),
              ('players', orm.ListField(orm.StringField())),
              ('alias_to_id_map',
               orm.ListField(orm.DocumentField(AliasMapping)))]

    def validate_document(self):
        # check: set of aliases = set of aliases in matches
        players_aliases = set(self.players)
        matches_aliases = {match.winner for match in self.matches} | \
                          {match.loser for match in self.matches}
        mapping_aliases = {
            mapping.player_alias
            for mapping in self.alias_to_id_map
        }

        if players_aliases != matches_aliases:
            return False, "set of players in players differs from set of players in matches"

        # check: set of aliases in mapping is subset of player aliases
        if not mapping_aliases.issubset(players_aliases):
            return False, "alias mappings contain mapping for alias not in tournament"

        return True, None

    def set_alias_id_mapping(self, alias, id):
        if self.alias_to_id_map is None:
            self.alias_to_id_map = []

        for mapping in self.alias_to_id_map:
            if mapping.player_alias == alias:
                mapping.player_alias = alias
                mapping.player_id = id
                return

        # if we've gotten out here, we couldn't find an existing match, so add
        # a new element
        self.alias_to_id_map.append(
            AliasMapping(player_alias=alias, player_id=id))

    def delete_alias_id_mapping(self, alias):
        if self.alias_to_id_map is None:
            self.alias_to_id_map = []

        for mapping in self.alias_to_id_map:
            if mapping.player_alias == alias:
                self.alias_to_id_map.remove(mapping)
                return mapping

    @classmethod
    def from_scraper(cls, type, scraper, region_id):
        raw_file = RawFile(id=ObjectId(), data=str(scraper.get_raw()))
        pending_tournament = cls(id=ObjectId(),
                                 name=scraper.get_name(),
                                 type=type,
                                 date=scraper.get_date(),
                                 regions=[region_id],
                                 url=scraper.get_url(),
                                 raw_id=raw_file.id,
                                 players=scraper.get_players(),
                                 matches=scraper.get_matches())

        # check if scraper returned valid pending tournament:
        valid, errors = pending_tournament.validate()
        if not valid:
            print "ERROR:", errors
            print pending_tournament.url

            # for scrapers that may return some extra players not in matches
            # remove these players:
            pending_tournament.players = list(
                {match.winner for match in pending_tournament.matches} | \
                {match.loser for match in pending_tournament.matches})

        return pending_tournament, raw_file
Esempio n. 5
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)