Skip to content

Commit

Permalink
add option to set points for teams in free round and fix bug that pre…
Browse files Browse the repository at this point in the history
…vents starting a tournament
  • Loading branch information
fabianWrede committed Jun 8, 2018
1 parent a617fbe commit a7839ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def create_tournament(args):
'tournament name.')
return

processes.create_tournament(args.tournament[0])
processes.create_tournament(args.tournament[0], args.free_round[0],
args.free_round[1])


def add_team(args):
Expand Down Expand Up @@ -375,6 +376,12 @@ def ask_yes_no(msg):
'the name given in '
'"tournament" '
'argument.')
parser_create_tournament.add_argument(
'-f', '--free-round', metavar='Points free round', nargs=2,
default=[13, 0],
help='Points for free rounds. Free rounds count as a win, this '
'can be used to set how many points the team and the "free" '
'team "scored".')
parser_create_tournament.set_defaults(func=create_tournament)

# add_team
Expand Down
5 changes: 3 additions & 2 deletions src/controller/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
_open_tournament = None


def create_tournament(name):
def create_tournament(name, points_fr_win=13, points_fr_loss=0):
global _open_tournament
_open_tournament = Tournament(name)
_open_tournament = Tournament(name=name, points_fr_win=points_fr_win,
points_fr_loss=points_fr_loss)
save(_open_tournament)


Expand Down
12 changes: 8 additions & 4 deletions src/controller/swiss_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def calculate_next_round(tournament):
for t in reversed(teams):
if (t.fl == min_free_rounds):
t.fl = t.fl + 1
game = Game(game_id, t, None, 13, 0)
game = Game(game_id, t, None, tournament.points_fr_win,
tournament.points_fr_loss)
game_id += 1
rnd.games.append(game)
teams.remove(t)
Expand Down Expand Up @@ -152,14 +153,18 @@ def calculate_next_round(tournament):
try:
teams.remove(team)
except ValueError:
pass
pass

rnd.games += games

else: # special case first round
game_id = 1
# free round for team in the middle
if len(teams) % 2 == 1:
t = teams[len(teams) // 2]
t.fl = t.fl + 1
game = Game(game_id, t, None, 13, 0)
game = Game(game_id, t, None, tournament.points_fr_win,
tournament.points_fr_loss)
game_id += 1
rnd.games.append(game)
teams.remove(t)
Expand All @@ -173,7 +178,6 @@ def calculate_next_round(tournament):
'No match possible in first round.')
raise NoMatchError

rnd.games += games
tournament.rounds.append(rnd)


Expand Down
20 changes: 15 additions & 5 deletions src/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ def decode_json(cls, dct):


class Tournament(BaseData):
def __init__(self, name, teams=[], rounds=[], playoffs=[]):
def __init__(self, name, teams=[], rounds=[], playoffs=[],
points_fr_win=13, points_fr_loss=0):
self.id = name
self.name = name
self.teams = teams
self.rounds = rounds
self.playoffs = playoffs
self.points_fr_win = points_fr_win
self.points_fr_loss = points_fr_loss


def is_started(self):
return len(self.rounds) > 0
Expand Down Expand Up @@ -43,16 +47,22 @@ def get_team_by_name(self, id):
return None

def encode_json(self):
dct = {'_type': self.__class__.__name__, 'name': self.name,
'teams': self.teams, 'rounds': self.rounds,
'playoffs': self.playoffs}
dct = {'_type': self.__class__.__name__,
'name': self.name,
'teams': self.teams,
'rounds': self.rounds,
'playoffs': self.playoffs,
'points_fr_win': self.points_fr_win,
'points_fr_loss': self.points_fr_loss}
return dct

@classmethod
def decode_json(cls, dct):
return Tournament(name=dct.get('name'), teams=dct.get('teams'),
rounds=dct.get('rounds'),
playoffs=dct.get('playoffs'))
playoffs=dct.get('playoffs'),
points_fr_win=dct.get('points_fr_win'),
points_fr_loss=dct.get('points_fr_loss'))


class Round(BaseData):
Expand Down

0 comments on commit a7839ee

Please sign in to comment.