|
| 1 | +import logging |
| 2 | +from datetime import datetime |
| 3 | +from cddagl.functions import strfdelta |
| 4 | +from cddagl.constants import DURATION_FORMAT |
| 5 | + |
| 6 | +from PyQt5.QtCore import ( |
| 7 | + Qt, QTimer |
| 8 | + ) |
| 9 | +from PyQt5.QtWidgets import ( |
| 10 | + QTabWidget, QGroupBox, QGridLayout, QLabel, QVBoxLayout, QPushButton |
| 11 | + ) |
| 12 | +from cddagl.sql.functions import get_config_value, set_config_value |
| 13 | + |
| 14 | +logger = logging.getLogger('cddagl') |
| 15 | + |
| 16 | +class StatisticsTab(QTabWidget): |
| 17 | + def __init__(self): |
| 18 | + super(StatisticsTab, self).__init__() |
| 19 | + |
| 20 | + self.game_start_time = None |
| 21 | + self.last_game_duration = get_config_value('last_played', 0) |
| 22 | + |
| 23 | + current_played_group_box = CurrentPlayedGroupBox() |
| 24 | + self.current_played_group_box = current_played_group_box |
| 25 | + self.reset_current_button = current_played_group_box.reset_current_button |
| 26 | + |
| 27 | + total_game_duration_group_box = TotalPlayedGroupBox() |
| 28 | + self.total_game_duration_group_box = total_game_duration_group_box |
| 29 | + self.reset_total_button = total_game_duration_group_box.reset_total_button |
| 30 | + |
| 31 | + layout = QVBoxLayout() |
| 32 | + layout.addWidget(current_played_group_box) |
| 33 | + layout.addWidget(total_game_duration_group_box) |
| 34 | + self.setLayout(layout) |
| 35 | + |
| 36 | + def set_text(self): |
| 37 | + self.current_played_group_box.set_text() |
| 38 | + self.current_played_group_box.set_label_text() |
| 39 | + self.total_game_duration_group_box.set_text() |
| 40 | + |
| 41 | + def get_main_window(self): |
| 42 | + return self.parentWidget().parentWidget().parentWidget() |
| 43 | + |
| 44 | + def get_main_tab(self): |
| 45 | + return self.parentWidget().parentWidget().main_tab |
| 46 | + |
| 47 | + def game_started(self): |
| 48 | + self.game_start_time = datetime.now() |
| 49 | + game_timer = QTimer() |
| 50 | + game_timer.setInterval(1000) |
| 51 | + game_timer.timeout.connect(self.game_tick) |
| 52 | + self.game_timer = game_timer |
| 53 | + game_timer.start() |
| 54 | + self.reset_current_button.setEnabled(False) |
| 55 | + self.reset_total_button.setEnabled(False) |
| 56 | + |
| 57 | + def game_ended(self): |
| 58 | + total_game_duration = int(get_config_value('total_played',0)) |
| 59 | + total_game_duration += self.last_game_duration |
| 60 | + set_config_value('last_played', self.last_game_duration) |
| 61 | + set_config_value('total_played', total_game_duration) |
| 62 | + self.game_start_time = None |
| 63 | + self.game_timer.stop() |
| 64 | + self.reset_current_button.setEnabled(True) |
| 65 | + self.reset_total_button.setEnabled(True) |
| 66 | + |
| 67 | + def game_tick(self): |
| 68 | + elapsed = int(datetime.now().timestamp() - self.game_start_time.timestamp()) |
| 69 | + self.last_game_duration = elapsed |
| 70 | + self.current_played_group_box.set_label_text() |
| 71 | + self.total_game_duration_group_box.set_label_text() |
| 72 | + |
| 73 | +class CurrentPlayedGroupBox(QGroupBox): |
| 74 | + def __init__(self): |
| 75 | + super(CurrentPlayedGroupBox, self).__init__() |
| 76 | + |
| 77 | + layout = QGridLayout() |
| 78 | + |
| 79 | + current_played_label = QLabel() |
| 80 | + current_played_label.setStyleSheet("font-size: 32px;") |
| 81 | + layout.addWidget(current_played_label, 0, 0, Qt.AlignHCenter) |
| 82 | + self.current_played_label = current_played_label |
| 83 | + |
| 84 | + reset_current_button = QPushButton() |
| 85 | + reset_current_button.setStyleSheet("font-size: 20px;") |
| 86 | + reset_current_button.clicked.connect(self.reset_current) |
| 87 | + layout.addWidget(reset_current_button, 1, 0, Qt.AlignHCenter) |
| 88 | + self.reset_current_button = reset_current_button |
| 89 | + |
| 90 | + self.setLayout(layout) |
| 91 | + self.set_text() |
| 92 | + |
| 93 | + def reset_current(self): |
| 94 | + self.parentWidget().last_game_duration = 0 |
| 95 | + set_config_value('last_played', 0) |
| 96 | + self.set_label_text() |
| 97 | + |
| 98 | + def get_main_tab(self): |
| 99 | + return self.parentWidget().get_main_tab() |
| 100 | + |
| 101 | + def set_text(self): |
| 102 | + self.reset_current_button.setText(_('RESET')) |
| 103 | + last_game_duration = int(get_config_value('last_played', 0)) |
| 104 | + fmt_last_game_duration = strfdelta(last_game_duration, _(DURATION_FORMAT), inputtype='s') |
| 105 | + self.current_played_label.setText(fmt_last_game_duration) |
| 106 | + self.setTitle(_('Last game duration:')) |
| 107 | + |
| 108 | + def set_label_text(self): |
| 109 | + last_game_duration = self.parentWidget().last_game_duration |
| 110 | + fmt_last_game_duration = strfdelta(last_game_duration, _(DURATION_FORMAT), inputtype='s') |
| 111 | + self.current_played_label.setText(fmt_last_game_duration) |
| 112 | + |
| 113 | + |
| 114 | +class TotalPlayedGroupBox(QGroupBox): |
| 115 | + def __init__(self): |
| 116 | + super(TotalPlayedGroupBox, self).__init__() |
| 117 | + |
| 118 | + layout = QGridLayout() |
| 119 | + |
| 120 | + total_game_duration_label = QLabel() |
| 121 | + total_game_duration_label.setStyleSheet("font-size: 32px;") |
| 122 | + layout.addWidget(total_game_duration_label, 0, 0, Qt.AlignHCenter) |
| 123 | + self.total_game_duration_label = total_game_duration_label |
| 124 | + |
| 125 | + reset_total_button = QPushButton() |
| 126 | + reset_total_button.setStyleSheet("font-size: 20px;") |
| 127 | + reset_total_button.clicked.connect(self.reset_total) |
| 128 | + layout.addWidget(reset_total_button, 1, 0, Qt.AlignHCenter) |
| 129 | + self.reset_total_button = reset_total_button |
| 130 | + |
| 131 | + self.setLayout(layout) |
| 132 | + self.set_text() |
| 133 | + |
| 134 | + def reset_total(self): |
| 135 | + set_config_value('total_played', 0) |
| 136 | + self.set_label_text() |
| 137 | + |
| 138 | + def get_main_tab(self): |
| 139 | + return self.parentWidget().get_main_tab() |
| 140 | + |
| 141 | + def set_text(self): |
| 142 | + self.reset_total_button.setText(_('RESET')) |
| 143 | + total_game_duration = int(get_config_value('total_played', 0)) |
| 144 | + fmt_total_game_duration = strfdelta(total_game_duration, _(DURATION_FORMAT), inputtype='s') |
| 145 | + self.total_game_duration_label.setText(fmt_total_game_duration) |
| 146 | + self.setTitle(_('Total time in game:')) |
| 147 | + |
| 148 | + def set_label_text(self): |
| 149 | + total_game_duration = int(get_config_value('total_played', 0)) + int(self.parentWidget().last_game_duration) |
| 150 | + fmt_total_game_duration = strfdelta(total_game_duration, _(DURATION_FORMAT), inputtype='s') |
| 151 | + self.total_game_duration_label.setText(fmt_total_game_duration) |
0 commit comments