From 5631718969f16fb2e1f42068af79d384f6ccc19e Mon Sep 17 00:00:00 2001 From: luxick Date: Tue, 27 Feb 2018 20:54:10 +0100 Subject: [PATCH] Stat info for current episode. --- dsst/dsst_gtk3/gtk_ui.py | 42 +- dsst/dsst_gtk3/handlers/death_handlers.py | 6 +- dsst/dsst_gtk3/handlers/dialog_handlers.py | 2 +- dsst/dsst_gtk3/handlers/handlers.py | 2 +- dsst/dsst_gtk3/handlers/season_handlers.py | 2 +- dsst/dsst_gtk3/resources/glade/window.glade | 466 +++++++++++++++----- dsst/dsst_sql/sql.py | 6 +- 7 files changed, 397 insertions(+), 129 deletions(-) diff --git a/dsst/dsst_gtk3/gtk_ui.py b/dsst/dsst_gtk3/gtk_ui.py index 051a4ce..d505647 100644 --- a/dsst/dsst_gtk3/gtk_ui.py +++ b/dsst/dsst_gtk3/gtk_ui.py @@ -1,3 +1,5 @@ +from collections import Counter + import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk @@ -53,13 +55,15 @@ class GtkUi: if season_id is None or season_id == -1: return # Rebuild episodes store - ep_id = self.get_selected_episode_id() selection = self.ui.get_object('episodes_tree_view').get_selection() - # selection.handler_block_by_func(self.handlers.on_selected_episode_changed) - store = self.ui.get_object('episodes_store') - store.clear() + selection.handler_block_by_func(self.handlers.on_selected_episode_changed) + model, selected_paths = selection.get_selected_rows() + model.clear() for episode in sql_func.get_episodes_for_season(season_id): - store.append([episode.id, episode.number, str(episode.date)]) + model.append([episode.id, episode.number, str(episode.date)]) + if selected_paths: + selection.select_path(selected_paths[0]) + selection.handler_unblock_by_func(self.handlers.on_selected_episode_changed) # Load player stats for season player_stats = {} @@ -72,7 +76,10 @@ class GtkUi: for name, stats in player_stats.items(): store.append([name, stats[0], stats[1]]) # Load enemy stats for season - enemy_stats = {enemy.name: [0, 0, enemy.id] for enemy in sql.Season.get(sql.Season.id == season_id).enemies} + season = sql.Season.get(sql.Season.id == season_id) + enemy_stats = { + enemy.name: [False, len(sql.Death.select().where(sql.Death.enemy == enemy)), enemy.id] + for enemy in season.enemies} store = self.ui.get_object('enemy_season_store') store.clear() for name, stats in enemy_stats.items(): @@ -84,10 +91,31 @@ class GtkUi: episode_id = self.get_selected_episode_id() if not episode_id: return + episode = sql.Episode.get(sql.Episode.id == episode_id) store = self.ui.get_object('episode_players_store') store.clear() - for player in sql.Episode.get(sql.Episode.id == self.get_selected_episode_id()).players: + for player in episode.players: store.append([player.id, player.name, player.hex_id]) + # Reload death store for notebook view + store = self.ui.get_object('episode_deaths_store') + store.clear() + for death in episode.deaths: + penalties = [x.drink.name for x in death.penalties] + penalties = [f'{number}x {drink}' for drink, number in Counter(penalties).items()] + penalty_string = ', '.join(penalties) + store.append([death.id, death.player.name, death.enemy.name, penalty_string]) + + # Stat grid + self.ui.get_object('ep_death_count_label').set_text(str(len(episode.deaths))) + drink_count = sum(len(death.penalties) for death in episode.deaths) + self.ui.get_object('ep_drinks_label').set_text(str(drink_count)) + cl_booze = sum(len(death.penalties) * death.penalties[0].size for death in episode.deaths) + self.ui.get_object('ep_booze_label').set_text(str(cl_booze) + "cl") + enemy_list = [death.enemy.name for death in episode.deaths] + sorted_list = Counter(enemy_list).most_common(1) + if sorted_list: + enemy_name, deaths = sorted_list[0] + self.ui.get_object('ep_enemy_name_label').set_text(f'{enemy_name} ({deaths} Deaths)') def get_selected_season_id(self) -> int: """Read ID of the selected season from the UI diff --git a/dsst/dsst_gtk3/handlers/death_handlers.py b/dsst/dsst_gtk3/handlers/death_handlers.py index dfcf981..b4c4da4 100644 --- a/dsst/dsst_gtk3/handlers/death_handlers.py +++ b/dsst/dsst_gtk3/handlers/death_handlers.py @@ -1,3 +1,4 @@ +from gi.repository import Gtk from dsst_gtk3 import dialogs, gtk_ui @@ -9,8 +10,9 @@ class DeathHandlers: def do_add_death(self, *_): ep_id = self.app.get_selected_episode_id() result = dialogs.show_edit_death_dialog(self.app.ui, ep_id) - if result: + if result == Gtk.ResponseType.OK: self.app.reload_for_season() + self.app.reload_for_episode() def on_penalty_drink_changed(self, widget, path, text): - self.app.ui.get_object('player_penalties_store')[path][2] = text \ No newline at end of file + self.app.ui.get_object('player_penalties_store')[path][2] = text diff --git a/dsst/dsst_gtk3/handlers/dialog_handlers.py b/dsst/dsst_gtk3/handlers/dialog_handlers.py index 6237e66..952fbe5 100644 --- a/dsst/dsst_gtk3/handlers/dialog_handlers.py +++ b/dsst/dsst_gtk3/handlers/dialog_handlers.py @@ -23,7 +23,7 @@ class DialogHandlers: if entry.get_text(): store = self.app.ui.get_object('enemy_season_store') enemy = sql.Enemy.create(name=entry.get_text(), season=self.app.get_selected_season_id()) - store.append([enemy.name, False, 0]) + store.append([enemy.name, False, 0, enemy.id]) entry.set_text('') def do_manage_drinks(self, *_): diff --git a/dsst/dsst_gtk3/handlers/handlers.py b/dsst/dsst_gtk3/handlers/handlers.py index 789bcc2..ef57755 100644 --- a/dsst/dsst_gtk3/handlers/handlers.py +++ b/dsst/dsst_gtk3/handlers/handlers.py @@ -1,11 +1,11 @@ import gi -import sql_func gi.require_version('Gtk', '3.0') from gi.repository import Gtk from dsst_gtk3.handlers.season_handlers import SeasonHandlers from dsst_gtk3.handlers.base_data_handlers import BaseDataHandlers from dsst_gtk3.handlers.dialog_handlers import DialogHandlers from dsst_gtk3.handlers.death_handlers import DeathHandlers +from dsst_sql import sql_func class Handlers(SeasonHandlers, BaseDataHandlers, DialogHandlers, DeathHandlers): diff --git a/dsst/dsst_gtk3/handlers/season_handlers.py b/dsst/dsst_gtk3/handlers/season_handlers.py index 77228fc..a9a71a8 100644 --- a/dsst/dsst_gtk3/handlers/season_handlers.py +++ b/dsst/dsst_gtk3/handlers/season_handlers.py @@ -11,7 +11,7 @@ class SeasonHandlers: name = dialogs.enter_string_dialog(self.app.ui, 'Name for the new Season') if name: sql.Season.create(game_name=name, number=1) - self.app.reload_seasons() + self.app.reload_base_data() def do_season_selected(self, *_): self.app.reload_for_season() diff --git a/dsst/dsst_gtk3/resources/glade/window.glade b/dsst/dsst_gtk3/resources/glade/window.glade index 979ce8a..d205f74 100644 --- a/dsst/dsst_gtk3/resources/glade/window.glade +++ b/dsst/dsst_gtk3/resources/glade/window.glade @@ -810,6 +810,7 @@ + @@ -1514,53 +1515,310 @@ True False - vertical + 5 - + True - True - episode_deaths_store - - + False + vertical + + + True + False + [Episode Stats] + center + + + + + + False + True + 0 + - - Player + + True + False + 5 + 2 + True - - - 1 - + + True + False + Total Deaths: + 1 + + + 0 + 0 + + + + + True + False + Total Drinks: + 1 + + + 0 + 1 + + + + + True + False + Hardest Enemy: + 1 + + + 0 + 3 + + + + + True + False + Most Deaths: + 1 + + + 0 + 4 + + + + + True + False + Total Booze: + 1 + + + 0 + 2 + + + + + True + False + [DeathCount] + + + 1 + 0 + + + + + True + False + [DrinkCount] + + + 1 + 1 + + + + + True + False + [ToatalBooze] + + + 1 + 2 + + + + + True + False + + + 1 + 3 + + + + + True + False + + + 1 + 4 + + + False + True + 1 + - - Enemy - - - - 2 - - + + + + + False + True + 0 + + + + + True + False + vertical + + + False + True + 1 + + + + + True + False + vertical + 2 + + + True + False + 2 + Deaths this episode + 0 + + False + True + 0 + - - Penalty + + True + True + in - - - 3 - + + True + True + episode_deaths_store + 0 + + + + + + Player + + + + 1 + + + + + + + Enemy + + + + 2 + + + + + + + Penalty + + + + 3 + + + + + + + True + True + 1 + + + + + True + False + 2 + Victories this episode + 0 + + + False + True + 2 + + + + + True + True + in + + + True + True + episode_victories_store + + + + + + Player + + + + + + + + Enemy + + + + + + + + + + True + True + 3 + True True - 0 + 2 @@ -1572,7 +1830,7 @@ True False - Episode Deaths + Episode Stats 1 @@ -1580,28 +1838,10 @@ - - True - False - vertical - - - - - - 2 - + - - True - False - Episode Victories - - - 2 - False - + @@ -1703,71 +1943,6 @@ True False - 5 - 5 - 5 - 5 - All Drinks - 0 - - - False - True - 2 - - - - - True - True - in - - - True - True - True - drink_store - 0 - - - - - - Name - - - - 1 - - - - - - - Vol. - - - - 2 - - - - - - - - - True - True - 3 - - - - - True - False - 5 - 5 5 5 Enemies This Season @@ -1776,7 +1951,7 @@ False True - 4 + 2 @@ -1828,6 +2003,69 @@ + + True + True + 3 + + + + + True + False + 5 + 5 + 5 + 5 + All Drinks + 0 + + + False + True + 4 + + + + + True + True + in + + + True + True + True + drink_store + 0 + + + + + + Name + + + + 1 + + + + + + + Vol. + + + + 2 + + + + + + + True True diff --git a/dsst/dsst_sql/sql.py b/dsst/dsst_sql/sql.py index 5ea2d01..4ec9a69 100644 --- a/dsst/dsst_sql/sql.py +++ b/dsst/dsst_sql/sql.py @@ -75,9 +75,9 @@ class Death(Model): class Penalty(Model): id = AutoField() size = DecimalField() - ForeignKeyField(Drink) - ForeignKeyField(Player, backref='penalties') - ForeignKeyField(Death, backref='penalties') + drink = ForeignKeyField(Drink) + player = ForeignKeyField(Player, backref='penalties') + death = ForeignKeyField(Death, backref='penalties') class Meta: database = db