创建具有复杂规则的记分板

时间:2010-12-06 10:10:14

标签: python django

我需要通过这样的规则列表创建记分板:

锦标赛第一阶段的球队位置由最多分数决定。对于一场比赛的胜利,球队收取3分,平局为1分,亏损为0分。

如果两支或更多球队的成绩相同,他们的位置将以这种方式确定:

  1. 根据比赛的结果:

    • 点数更多;

    • 得分和失误目标之间的最佳区别;

    • 在这些游戏中获得更多目标;

    • 在这些比赛中,外国球场的进球次数更多;

  2. 所有游戏中获得更多胜利;

  3. 所有比赛中得分与失球之间的最佳差异;

  4. 所有比赛中得分最多的目标;

  5. 所有比赛中其他球场得分最多的目标;

  6. 现在我有这样的模特:

    from django.db import models
    
    class Team(models.Model):
        name = models.CharField(max_length=30)
    
        def __unicode__(self):
            return self.name
    
    class Game(models.Model):
        beginning = models.DateTimeField()
        place = models.CharField(max_length=50)
        spectators = models.IntegerField()
    
        def __unicode__(self):
            return self.beginning.strftime("%A, %d. %B %Y %I:%M%p")
    
    class TeamGame(models.Model):
    
        RESULT_WIN = 'w'
        RESULT_DRAW = 'd'
        RESULT_LOSE = 'l'
    
        RESULT_CHOICES = (
            (RESULT_WIN, 'Win'),
            (RESULT_LOSE, 'Lose'),
            (RESULT_DRAW, 'Draw'),
        )
    
        HOME_FIELD = 'h'
        OUT_FIELD = 'o'
    
        FIELD_CHOICES = (
            (HOME_FIELD, 'home'),
            (OUT_FIELD, 'out')
        )
    
        game = models.ForeignKey(Game)
        team_name = models.ForeignKey(Team)
        goals_hit = models.IntegerField()
        goals_get = models.IntegerField()
        result = models.CharField(max_length=1, choices=RESULT_CHOICES, blank=True)
        field = models.CharField(max_length=1, choices=FIELD_CHOICES, blank=True)
    

    我怎么能创造这样一个可怕的东西?:) Plz,帮助!)

2 个答案:

答案 0 :(得分:1)

你想要做的就是使用sort()。 python的排序按字典顺序对元组进行排序,即如果第一项是相同的,它会检查第二项(等等......)

def rank_teams():
    rank = []
    # iterate through teams and create a tuple with most important to least important 
    # sort params. team bust be the last object
    for team in Team.objects.all():
        points = 0
        best_goal_gap = 0
        best_goals = 0
        best_away_goals = 0
        all_victories = 0
        for game in TeamGame.objects.filter(team_name=team):
            points += {'w':3, 'd':1, 'l':0}[game.result]
            best_goal_gap = max(best_goal_gap, game.goals_hit-game.goals_get)
            best_goals = max(best_goals, game.goals_get)
            if game.field == 'o':
                best_away_goals = max(best_away_goals, game.goals_get)
            if game.result == 'w':
                all_victories +=1
        rank.append((points, best_goal_gap, best_goals, best_away_goals, all_victories, team))
    #sort the teams
    rank.sort()
    team_rank = [team for p,g,b,a,v,team in rank]
    return team_rank

此函数将返回按所需顺序排序的团队对象列表(根据第一个项目符号点。

答案 1 :(得分:0)

我想你一次要做三件事;不。

  1. 将每个团队的信息插入表格(模型)
  2. 根据该信息(模型)计算每个团队的分数
  3. 根据得分(视图)
  4. 按顺序显示团队