Skip to content

Commit

Permalink
Merge pull request #108 from hndrewaall/bugfix/75-current-ep-season
Browse files Browse the repository at this point in the history
Fix latest episode/season logic
  • Loading branch information
hndrewaall authored Feb 26, 2017
2 parents 036b90b + 56c771f commit 8a42944
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
50 changes: 24 additions & 26 deletions app/league/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def latest_season(self):
def season_stats(self, season=None):
"""Get player statistics for a season."""
if season is None:
season = self.latest_season()
season = Game.latest_season_episode()[0]

wins, losses = 0, 0
for game in ([game for game in self.games if game.season == season]):
if ((game.winner == Color.white and game.white == self) or
Expand All @@ -79,20 +80,25 @@ def season_stats(self, season=None):

return {'wins': wins, 'losses': losses}

def latest_episode(self):
"""Get latest episode player has played in."""
return sorted([game.episode for game in self.games])[-1]
def latest_season_episode(self):
"""Get latest season and episode player has played in."""
games = self.games
if len(games) > 0:
return sorted([(game.season, game.episode)
for game in games])[-1]
else:
return (0, 0)

def episode_stats(self, episode=None, season=None):
def episode_stats(self, season_episode=None):
"""Get player statistics for an episode."""
if episode is None:
episode = self.latest_episode()
if season is None:
season = self.latest_season()
latest_season_episode = Game.latest_season_episode()
if season_episode is None:
season_episode = latest_season_episode
wins, losses = 0, 0

for game in ([game for game in self.games
if game.season == season and
game.episode == episode]):
if game.season == season_episode[0] and
game.episode == season_episode[1]]):
if ((game.winner == Color.white and game.white == self) or
(game.winner == Color.black and game.black == self)):
wins += 1
Expand Down Expand Up @@ -207,30 +213,22 @@ def players(self):
return frozenset((self.white, self.black))

@classmethod
def latest_episode(cls):
"""Get latest episode."""
games = cls.query.all()
if len(games) > 0:
return sorted([game.episode for game in games])[-1]
else:
return 1

@classmethod
def latest_season(cls):
"""Get latest season."""
def latest_season_episode(cls):
"""Get latest episode and season."""
games = cls.query.all()
if len(games) > 0:
return sorted([game.season for game in games])[-1]
return sorted([(game.season, game.episode) for game in games])[-1]
else:
return 1
return (0, 0)

@classmethod
def episode_stats(cls, episode=None, season=None, num_players=5):
"""Get statistics for an episode."""
latest_season_episode = cls.latest_season_episode()
if episode is None:
episode = cls.latest_episode()
episode = latest_season_episode[1]
if season is None:
season = cls.latest_season()
season = latest_season_episode[0]
wins, games_played = {}, {}
games = [game for game in cls.query.all()
if game.season == season and
Expand Down
11 changes: 9 additions & 2 deletions app/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def test_set_played_at(self, db):
def test_get_max_season_ep(self, db):
"""Test calculation of maximum season and episode."""
games = [GameFactory(season=s, episode=e)
for (s, e) in [(1, 1), (1, 2)]]
for (s, e) in [(1, 1), (1, 2), (2, 1)]]
map(methodcaller('save'), games)
assert Game.get_max_season_ep() == (1, 2)
assert Game.get_max_season_ep() == (2, 2)

def test_latest_season_episode(self, db):
"""Test calculation of latest (season, episode) tuple."""
games = [GameFactory(season=s, episode=e)
for (s, e) in [(1, 1), (1, 2), (2, 1), (3, 1)]]
map(methodcaller('save'), games)
assert Game.latest_season_episode() == (3, 1)

0 comments on commit 8a42944

Please sign in to comment.