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 make_player(player):
    rats = Rating.objects.filter(player=player).order_by('-period__id')
    if rats.count == 0:
        pl = Player(player.tag, player.race, 0.0, 0.0, 0.0, 0.0, 0.6, 0.6, 0.6, 0.6)
        pl.dbpl = player
    else:
        rat = rats[0]
        pl = Player(player.tag, player.race, rat.rating, rat.rating_vp, rat.rating_vt, rat.rating_vz,\
                    rat.dev, rat.dev_vp, rat.dev_vt, rat.dev_vz)
        pl.dbpl = player

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

    rats = Rating.objects.filter(player=player).order_by('-period__id')
    if rats.count() == 0:
        pl = Player(player.tag, player.race, 0.0, 0.0, 0.0, 0.0, 0.6, 0.6, 0.6, 0.6)
        pl.dbpl = player
    else:
        rat = rats[0]
        pl = Player(player.tag, player.race, rat.rating, rat.rating_vp, rat.rating_vt, rat.rating_vz,\
                    rat.dev, rat.dev_vp, rat.dev_vt, rat.dev_vz)
        pl.dbpl = player

    return pl
Example #4
0
def find_player(lst, make=False, soft=False):
    qset = Player.objects.all()
    
    for s in [s.strip() for s in lst if s.strip() != '']:
        # If numeric, assume it's a restriction on ID and nothing else
        if s.isdigit():
            qset = qset.filter(id=int(s))
            continue

        # Always search by player tag, team and aliases
        if soft:
            q = Q(tag__icontains=s) | Q(alias__name__icontains=s) |\
                    Q(teammembership__current=True, teammembership__team__name__icontains=s) |\
                    Q(teammembership__current=True, teammembership__team__alias__name__icontains=s)
        else:
            q = Q(tag__iexact=s) | Q(alias__name__iexact=s) |\
                    Q(teammembership__current=True, teammembership__team__name__iexact=s) |\
                    Q(teammembership__current=True, teammembership__team__alias__name__iexact=s)

        # Race query
        if len(s) == 1 and s.upper() in 'PTZSR':
            q |= Q(race=s.upper())

        # Country codes
        if len(s) == 2 and s.upper() in data.cca2_to_ccn:
            q |= Q(country=s.upper())
        if len(s) == 3 and s.upper() in data.cca3_to_ccn:
            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:
            q |= Q(country=ccn_to_cca2(cn_to_ccn(renorm)))

        qset = qset.filter(q)

    # Make player if needed and allowed
    if not qset.exists() and make:
        tag, country, race = None, None, None

        for s in [s.strip() for s in lst if s.strip() != '']:
            if s.isdigit():
                continue

            if len(s) == 1 and s.upper() in 'PTZSR':
                race = s.upper()
                continue

            if len(s) == 2 and s.upper() in data.cca2_to_ccn:
                country = s.upper()
                continue
            if len(s) == 3 and s.upper() in data.cca3_to_ccn:
                country = ccn_to_cca2(cca3_to_ccn(s.upper()))
                continue
            renorm = s[0].upper() + s[1:].lower()
            if renorm in data.cn_to_ccn:
                country = ccn_to_cca2(cn_to_ccn(renorm))
                continue

            tag = s

        if tag == None:
            raise Exception('Player \'' + ' '.join(lst) + '\' was not found and could not be made'\
                    + ' (missing player tag)')

        if race == None:
            raise Exception('Player \'' + ' '.join(lst) + '\' was not found and could not be made'\
                    + ' (missing race)')

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

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

    return qset.distinct()
Example #5
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 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)

        q = Q(**tag_filter) | Q(**alias_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 #6
0
        return None

if __name__ == '__main__':
    rid = int(sys.argv[1])
    if Player.objects.filter(sc2c_id=rid).count() > 0:
        print 'Did not import new player, already exists.'
        sys.exit(1)

    soup = BeautifulSoup(get_url(_base_url % rid))

    tag = soup.findAll('h1')[0].contents[0].strip()
    try:
        country = soup.findAll(lambda t: (u'class', u'content_flag') in t.attrs)[0]['src'].split('/')[-1].split('.')[0].upper()
    except:
        country = ''
    race = sys.argv[2]

    print '------ From %s' % (_base_url % rid)
    k = raw_input('------ Add new player: %s (%s, %s)? ' % (tag, country, race))
    if k.isdigit():
        p = Player.objects.get(id=int(k))
        p.sc2c_id = rid
        p.save()
    elif k.upper() == 'Y':
        p = Player()
        p.tag = tag
        p.country = country
        p.race = race
        p.sc2c_id = rid
        p.save()
Example #7
0
def find_player(query=None, lst=None, make=False, soft=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 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
        if soft:
            q = (
                Q(
                    groupmembership__current=True,
                    groupmembership__group__name__icontains=s,
                    groupmembership__group__is_team=True,
                ) | Q(
                    groupmembership__current=True,
                    groupmembership__group__alias__name__icontains=s,
                    groupmembership__group__is_team=True,
                ) | Q(tag__icontains=s) | Q(alias__name__icontains=s)
            )
        else:
            q = (
                Q(
                    groupmembership__current=True,
                    groupmembership__group__name__iexact=s,
                    groupmembership__group__is_team=True,
                ) | Q(
                    groupmembership__current=True,
                    groupmembership__group__alias__name__iexact=s,
                    groupmembership__group__is_team=True,
                ) | Q(tag__iexact=s) | Q(alias__name__iexact=s)
            )

        # ...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()