Example #1
0
def make_player(player):
    if player is None:
        pl = Player('BYE', 'T', -10000, 0, 0, 0)
        pl.dbpl = None
        return pl

    try:
        rating = player.current_rating
        pl = Player(
            player.tag,
            player.race,
            rating.rating,
            rating.rating_vp,
            rating.rating_vt,
            rating.rating_vz,
            rating.dev,
            rating.dev_vp,
            rating.dev_vt,
            rating.dev_vz,
        )
    except:
        pl = Player(
            player.tag,
            player.race,
            start_rating(player.country,
                         etn(lambda: get_latest_period().id) or 1),
            0.0,
            0.0,
            0.0,
            INIT_DEV,
            INIT_DEV,
            INIT_DEV,
            INIT_DEV,
        )

    pl.dbpl = player

    return pl
Example #2
0
def find_player(query=None, lst=None, make=False, soft=False, strict=False):
    queryset = Player.objects.all()

    if not lst:
        try:
            lst = [s.strip() for s in shlex.split(query) if s.strip() != '']
        except:
            return []

    tag, country, race = None, None, None

    # {{{ Build filter
    for s in lst:
        # If numeric, assume a restriction on ID
        if type(s) is int or s.isdigit():
            queryset = queryset.filter(id=int(s))
            continue

        # If only one character, assume a restriction on race
        if len(s) == 1 and s.upper() in 'PTZSR':
            race = s.upper()
            queryset = queryset.filter(race=s.upper())
            continue

        # Otherwise, always search by player tag, team and aliases
        filter_type = "iexact"
        if soft:
            filter_type = "icontains"

        # Helper function that formats the filter to use `filter_type`
        def format_filter(**kwargs):
            ret = dict()
            for k in kwargs:
                if k.endswith("MATCHES"):
                    ret[k.replace("MATCHES", filter_type)] = kwargs[k]
                else:
                    ret[k] = kwargs[k]
            return ret

        tag_filter = format_filter(tag__MATCHES=s)
        alias_filter = format_filter(alias__name__MATCHES=s)
        full_name_filter = format_filter(name__MATCHES=s)
        romanized_name_filter = format_filter(romanized_name__MATCHES=s)

        q = (
            Q(**tag_filter) |
            Q(**alias_filter) |
            Q(**full_name_filter) |
            Q(**romanized_name_filter)
        )
        if not strict or len(lst) > 1:
            group_name_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__name__MATCHES=s,
                groupmembership__group__is_team=True)

            group_alias_filter = format_filter(
                groupmembership__current=True,
                groupmembership__group__alias__name__MATCHES=s,
                groupmembership__group__is_team=True)

            q |= Q(**group_name_filter) | Q(**group_alias_filter)

            # ...and perhaps country codes
            found_country = False
            if len(s) == 2 and s.upper() in data.cca2_to_ccn:
                country = s.upper()
                found_country = True
                q |= Q(country=s.upper())

            if len(s) == 3 and s.upper() in data.cca3_to_ccn:
                country = ccn_to_cca2(cca3_to_ccn(s.upper()))
                found_country = True
                q |= Q(country=ccn_to_cca2(cca3_to_ccn(s.upper())))

            renorm = s[0].upper() + s[1:].lower()
            if renorm in data.cn_to_ccn:
                country = ccn_to_cca2(cn_to_ccn(renorm))
                found_country = True
                q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

            if not found_country:
                tag = s

        queryset = queryset.filter(q)
    # }}}

    # {{{ If no results, make player if allowed
    if not queryset.exists() and make:
        # {{{ Raise exceptions if missing crucial data
        if tag == None:
            msg = _("Player '%s' was not found and cound not be made (missing player tag)") % ' '.join(lst)
            raise Exception(msg)

        if race == None:
            msg = _("Player '%s' was not found and cound not be made (missing race)") % ' '.join(lst)
            raise Exception(msg)
        # }}}

        p = Player(tag=tag, country=country, race=race)
        p.save()

        return Player.objects.filter(id=p.id)
    # }}}

    return queryset.distinct()
Example #3
0
 def copy(self):
     return Player(copy=self)