Example #1
0
    def make_match(self, pla_query, plb_query, pla_race_or, plb_race_or, sca, scb, make_flag, dup_flag):
        pla = self.get_player(pla_query, make_flag)
        plb = self.get_player(plb_query, make_flag)

        if (pla is None or plb is None) and self.is_adm:
            return None

        if not self.is_adm:
            match = PreMatch(
                group      = self.prematchgroup,
                pla        = pla,
                plb        = plb,
                pla_string = ' '.join(pla_query),
                plb_string = ' '.join(plb_query),
                sca        = sca,
                scb        = scb,
                date       = self.cleaned_data['date'],
                rca        = pla_race_or,
                rcb        = plb_race_or,
            )
            return match
        else:
            match = Match(
                pla      = pla,
                plb      = plb,
                sca      = sca,
                scb      = scb,
                rca      = pla_race_or or (pla.race if pla.race != 'S' else 'R'),
                rcb      = plb_race_or or (plb.race if plb.race != 'S' else 'R'),
                date     = self.cleaned_data['date'],
                treated  = False,
                eventobj = self.cleaned_data['eventobj'],
                game     = self.cleaned_data['game'],
                offline  = self.cleaned_data['offline'],
            )
            if check_duplicates(match, dup_flag):
                self.messages.append(Message(
                    _("Could not make match %(pla)s vs %(plb)s: possible duplicate found.") 
                        % {'pla': pla.tag, 'plb': plb.tag},
                    type=Message.ERROR,
                ))
                self.close_after = False
                return None
            if 'R' in [match.rca, match.rcb]:
                self.messages.append(Message(
                    _("Unknown race in %(pla)s vs %(plb)s: set to random.")
                        % {'pla': pla.tag, 'plb': plb.tag},
                    type=Message.WARNING,
                ))
            match.set_period()
            match.set_ratings()
            return match
Example #2
0
    def commit(self, post, submitter):
        self.messages = []

        if not self.is_valid():
            self.messages.append(
                Message(_('Entered data was invalid, no changes made.'),
                        type=Message.ERROR))
            for field, errors in self.errors.items():
                for error in errors:
                    if field == '__all__':
                        self.messages.append(Message(error,
                                                     type=Message.ERROR))
                    else:
                        self.messages.append(
                            Message(error=error,
                                    field=self.fields[field].label))
            return

        prematches = [
            PreMatch.objects.get(id=int(key.split('-')[-1]))
            for key in sorted(post.keys())
            if key[0:6] == 'match-' and post[key] == 'y'
        ]

        matches = []
        for pm in prematches:
            if not self.approve:
                group = pm.group
                pm.delete()
                if not group.prematch_set.exists():
                    group.delete()
                continue

            if pm.pla is None:
                pm.pla_string = post['match-%i-pla' % pm.id]
            if pm.plb is None:
                pm.plb_string = post['match-%i-plb' % pm.id]
            pm.save()

            for msg in fill_players(pm):
                self.messages.append(Message(msg, type=Message.ERROR))

            if pm.is_valid():
                m = Match(
                    pla=pm.pla,
                    plb=pm.plb,
                    sca=pm.sca,
                    scb=pm.scb,
                    rca=pm.rca or (pm.pla.race if pm.pla.race != 'S' else 'R'),
                    rcb=pm.rcb or (pm.plb.race if pm.plb.race != 'S' else 'R'),
                    date=self.cleaned_data['date'] or pm.group.date,
                    eventobj=self.cleaned_data['eventobj'],
                    event=pm.group.event,
                    submitter=submitter,
                    offline=pm.group.offline,
                    game=pm.group.game,
                )

                if check_duplicates(m, self.cleaned_data['dup_flag']):
                    self.messages.append(
                        Message(
                            _("Could not make match %(pla)s vs %(plb)s: possible duplicate found."
                              ) % {
                                  'pla': m.pla.tag,
                                  'plb': m.plb.tag
                              },
                            type=Message.ERROR,
                        ))
                    continue
                if 'R' in [m.rca, m.rcb]:
                    self.messages.append(
                        Message(
                            _("Unknown race in %(pla)s vs %(plb)s: set to random."
                              ) % {
                                  'pla': pla.tag,
                                  'plb': plb.tag
                              },
                            type=Message.WARNING,
                        ))

                m.set_period()
                m.set_ratings()
                m.save()

                matches.append(m)

                group = pm.group
                pm.delete()
                if not group.prematch_set.exists():
                    group.delete()
            else:
                pm.save()

        if self.approve and len(matches) > 0:
            self.messages.append(
                Message(ungettext_lazy('Successfully approved %i match.',
                                       'Successfully approved %i matches.',
                                       len(matches)) % len(matches),
                        type=Message.SUCCESS))
        elif not self.approve and len(prematches) > 0:
            self.messages.append(
                Message(ungettext_lazy('Successfully rejected %i match.',
                                       'Successfully rejected %i matches.',
                                       len(prematches)) % len(prematches),
                        type=Message.SUCCESS))
Example #3
0
    def commit(self, post, submitter):
        self.messages = []

        if not self.is_valid():
            self.messages.append(Message(_('Entered data was invalid, no changes made.'), type=Message.ERROR))
            for field, errors in self.errors.items():
                for error in errors:
                    if field == '__all__':
                        self.messages.append(Message(error, type=Message.ERROR))
                    else:
                        self.messages.append(Message(error=error, field=self.fields[field].label))
            return

        prematches = [
            PreMatch.objects.get(id=int(key.split('-')[-1]))
            for key in sorted(post.keys())
            if key[0:6] == 'match-' and post[key] == 'y'
        ]

        matches = []
        for pm in prematches:
            if self.cleaned_data['reject']:
                group = pm.group
                pm.delete()
                if not group.prematch_set.exists():
                    group.delete()
                continue

            if pm.pla is None:
                pm.pla_string = post['match-%i-pla' % pm.id]
            if pm.plb is None:
                pm.plb_string = post['match-%i-plb' % pm.id]
            pm.save()

            for msg in fill_players(pm):
                self.messages.append(Message(msg, type=Message.ERROR))

            if pm.is_valid():
                m = Match(
                    pla       = pm.pla,
                    plb       = pm.plb,
                    sca       = pm.sca,
                    scb       = pm.scb,
                    rca       = pm.rca or (pm.pla.race if pm.pla.race != 'S' else 'R'),
                    rcb       = pm.rcb or (pm.plb.race if pm.plb.race != 'S' else 'R'),
                    date      = self.cleaned_data['date'] or pm.group.date,
                    eventobj  = self.cleaned_data['eventobj'],
                    event     = pm.group.event,
                    submitter = submitter,
                    offline   = pm.group.offline,
                    game      = pm.group.game,
                )

                if check_duplicates(m, self.cleaned_data['dup_flag']):
                    self.messages.append(Message(
                        _("Could not make match %(pla)s vs %(plb)s: possible duplicate found.")
                            % {'pla': m.pla.tag, 'plb': m.plb.tag},
                        type=Message.ERROR,
                    ))
                    continue
                if 'R' in [m.rca, m.rcb]:
                    self.messages.append(Message(
                        _("Unknown race in %(pla)s vs %(plb)s: set to random.")
                            % {'pla': pla.tag, 'plb': plb.tag},
                        type=Message.WARNING,
                    ))

                m.set_period()
                m.set_ratings()
                m.save()

                matches.append(m)

                group = pm.group
                pm.delete()
                if not group.prematch_set.exists():
                    group.delete()
            else:
                pm.save()

        if self.cleaned_data['approve'] and len(matches) > 0:
            self.messages.append(Message(
                ungettext_lazy(
                    'Successfully approved %i match.',
                    'Successfully approved %i matches.',
                    len(matches)) % len(matches),
                type=Message.SUCCESS
            ))
        elif self.cleaned_data['reject'] and len(prematches) > 0:
            self.messages.append(Message(
                ungettext_lazy(
                    'Successfully rejected %i match.',
                    'Successfully rejected %i matches.',
                    len(prematches)) % len(prematches),
                type=Message.SUCCESS
            ))
Example #4
0
def process_soup(soup):
    dates = soup.findAll(lambda t: (u'class', u'recent_date') in t.attrs)
    scores = soup.findAll(lambda t: (u'class', u'recent_score') in t.attrs)
    nm = 0

    for i in range(0,len(dates)):
        url = dates[i].parent['href']
        id = int(dates[i].parent['href'].split('/')[-2])

        if len(scores[i].contents) < 5:
            print '--- Skipped match %i (score missing on overview page)' % id
            continue

        date = '20' + '-'.join(str(dates[i].contents[0]).split('.')[-1::-1])

        ns = BeautifulSoup(get_url(url))

        q = ns.findAll(lambda t: (u'class', u'match_coverage') in t.attrs)
        setting = q[0].contents[0].strip()

        q = ns.findAll(lambda t: (u'class', u'edb_logo_img') in t.attrs)
        try:
            player_a = int(q[0].parent['href'].split('/')[-2].split('-')[0])
            player_b = int(q[1].parent['href'].split('/')[-2].split('-')[0])
        except:
            print '--- Skipped match %i (couldn\'t parse players)' % id
            continue

        q = ns.findAll(lambda t: (u'class', u'match_score match_score_finished') in t.attrs)
        player_a_score = int(q[0].contents[1].contents[0])
        player_b_score = int(q[0].contents[3].contents[0])

        q = ns.findAll(lambda t: (u'class', u'content_race') in t.attrs)[0:2]
        exp_races = [t['alt'].upper() for t in q]

        try:
            pa = Player.objects.get(sc2c_id=player_a)
        except:
            os.system('./sc2cp.py %i %s' % (player_a, exp_races[0]))

        try:
            Player.objects.get(sc2c_id=player_b)
        except:
            os.system('./sc2cp.py %i %s' % (player_b, exp_races[1]))

        try:
            pa = Player.objects.get(sc2c_id=player_a)
            pb = Player.objects.get(sc2c_id=player_b)

            n1 = Match.objects.filter(pla=pa, plb=pb, sca=player_a_score, scb=player_b_score).extra(where=['abs(datediff(date,\'%s\')) < 2' % date])
            n2 = Match.objects.filter(pla=pb, plb=pa, sca=player_b_score, scb=player_a_score).extra(where=['abs(datediff(date,\'%s\')) < 2' % date])
            n1 = n1.exists()
            n2 = n2.exists()
            if n1 or n2:
                print '--- Skipped match %i (possible duplicate in database)' % id
                continue

            m = Match(pla=pa, plb=pb, sca=player_a_score, scb=player_b_score, date=date)
            m.event = setting + ' (SC2C)'
            m.set_period()
            m.save()

            nm += 1
            print '%i: %s %i-%i %s (%s)' % (m.period.id, pa.tag, player_a_score, player_b_score, pb.tag, setting)
        except:
            print '--- Skipped match %i (players not in local database: %i, %i)' % (id, player_a, player_b)

    return (nm, False)