开发者

Creating scoreboard with complicated rules

开发者 https://www.devze.com 2023-01-28 20:59 出处:网络
I need to make a scoreboard creating by such list of rules: Teams\' places at the first stage of the Championship are determined by the most amount of points. For the victory in a match the team char

I need to make a scoreboard creating by such list of rules:

Teams' places at the first stage of the Championship are determined by the most amount of points. For the victory in a match the team charges 3 points, for draw - 1 point and for loss - 0 points.

In case of equal results of two or more teams, their places are determined in such way:

  1. According to the results of game(s) with each other:

    • greater number of points;

    • The best difference between scored and conceded goals;

    • Greater number of goals scored in these games;

    • Greater number of goals scored on a foreign field in these games;

  2. A greater number of victories in all games;

  3. The best difference between goals scored and goals conceded in all matches;

  4. The largest number of goals scored in all matches;

  5. The largest number of goals scored in others' fields in all matches;

Now I have such kind of models:

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_FIE开发者_如何学CLD = '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)

How can I create such a horrible thing?:) Plz, help!)


seems like what you want to do is use sort(). python's sort sorts tuples lexicographically, i.e. if the first item is the same it checks the second (etc...)

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

this function will return a list of team objects sorted in the order you desire (according to the first bullet point.


I think you're trying to do three things at once; don't.

  1. Insert each team's information into the table (model)
  2. Calculate each team's score based on that information (model)
  3. Display the teams in order based on score (view)
0

精彩评论

暂无评论...
验证码 换一张
取 消