diff --git a/src/engine.py b/src/engine.py index 6c24e42..db95aed 100644 --- a/src/engine.py +++ b/src/engine.py @@ -105,6 +105,14 @@ class GameStats(): elif stat_type == VisibilityType.Overall: return self.history[StatType.Combat].combined(self.zone[StatType.Combat]).players_healing() + def get_total_healing_stats(self, stat_type): + if stat_type == VisibilityType.Zone: + return self.zone[StatType.Combat].players_total_healing() + elif stat_type == VisibilityType.LastFight: + return self.last_fight[StatType.Combat].players_total_healing() + elif stat_type == VisibilityType.Overall: + return self.history[StatType.Combat].combined(self.zone[StatType.Combat]).players_total_healing() + def fame_stats(self, stat_type): if stat_type == VisibilityType.Zone: return self.zone[StatType.Fame].stats() @@ -156,6 +164,18 @@ def last_fight_stats(combat_stat_type: str): return get_stats(VisibilityType.LastFight, combat_stat_type) +def zone_total_healing_stats(): + return game_stats.get_total_healing_stats(VisibilityType.Zone) + + +def overall_total_healing_stats(): + return game_stats.get_total_healing_stats(VisibilityType.Overall) + + +def last_fight_total_healing_stats(): + return game_stats.get_total_healing_stats(VisibilityType.LastFight) + + cached_players = () # do not compute values if there is nothing in queue cached_stat_type = None cached_combat_stat_type = None diff --git a/src/gui/modules/main.py b/src/gui/modules/main.py index 0c06d92..756db95 100644 --- a/src/gui/modules/main.py +++ b/src/gui/modules/main.py @@ -101,7 +101,8 @@ class MainWidget(QWidget): elapsed, fame_stat.fame, fame_stat.fame_per_hour, - player_list_items + player_list_items, + self.total_healing_stats() ) def mousePressEvent(self, event): @@ -141,3 +142,12 @@ class MainWidget(QWidget): } return stats[self.top_bar.view_type.get_view_type()][self.view.get_mode()]() + + def total_healing_stats(self): + stats = { + StatsType.ZONE: engine.zone_total_healing_stats, + StatsType.LAST_FIGHT: engine.last_fight_total_healing_stats, + StatsType.OVERALL: engine.overall_total_healing_stats, + } + + return stats[self.view.get_mode()]() diff --git a/src/gui/modules/views/list_view/icon.py b/src/gui/modules/views/list_view/icon.py index 10b2260..78fab07 100644 --- a/src/gui/modules/views/list_view/icon.py +++ b/src/gui/modules/views/list_view/icon.py @@ -11,6 +11,9 @@ icon_cache = {} def get_weapon_icon(weapon): global icon_cache + if not weapon: + return None + url = f'https://albiononline2d.ams3.cdn.digitaloceanspaces.com/thumbnails/orig/{weapon}' if weapon in icon_cache: @@ -18,6 +21,8 @@ def get_weapon_icon(weapon): try: r = async_request.get(url) + if r is None or not r.ok: + return None img = Image.open(BytesIO(r.content)) img_qt = ImageQt.ImageQt(img) diff --git a/src/gui/modules/views/mode_based_list_view.py b/src/gui/modules/views/mode_based_list_view.py index 846ef41..6f66821 100644 --- a/src/gui/modules/views/mode_based_list_view.py +++ b/src/gui/modules/views/mode_based_list_view.py @@ -6,9 +6,11 @@ from typing import Callable from PySide6.QtWidgets import QHBoxLayout # type: ignore from PySide6.QtWidgets import QVBoxLayout # type: ignore from PySide6.QtWidgets import QComboBox # type: ignore +from PySide6.QtWidgets import QLabel # type: ignore from PySide6.QtWidgets import QWidget # type: ignore from ....stats.list_item import PlayerListItem +from ..view_type import ViewType from .list_view import ListItemView from .stats_type import StatsType @@ -63,13 +65,31 @@ class ModeBasedListView(QWidget): self.list_view = ListItemView() self.layout.addWidget(self.list_view) + self.total_healing_label = QLabel('Total Healing') + self.layout.addWidget(self.total_healing_label) + + self.total_healing_view = ListItemView() + self.layout.addWidget(self.total_healing_view) + + self.total_healing_label.hide() + self.total_healing_view.hide() + self.setLayout(self.layout) - def update(self, prefix, elapsed, fame, fame_per_hour, players: PlayerListItem): + def update(self, prefix, elapsed, fame, fame_per_hour, + players: PlayerListItem, total_healing: PlayerListItem): self.session_bar.player_stats.update(elapsed, fame, fame_per_hour) self.session_bar.copy_button.update( prefix, self.list_view.get_player_list_items(), self.session_bar.mode.get_mode(), fame) self.list_view.update(players) + if prefix == ViewType.HEALING_DONE and total_healing: + self.total_healing_label.show() + self.total_healing_view.show() + self.total_healing_view.update(total_healing) + else: + self.total_healing_label.hide() + self.total_healing_view.hide() + self.total_healing_view.update([]) self.session_bar.update() def get_mode(self) -> str: diff --git a/src/stats/combat_stats.py b/src/stats/combat_stats.py index 24a6e93..1c46ca5 100644 --- a/src/stats/combat_stats.py +++ b/src/stats/combat_stats.py @@ -30,8 +30,8 @@ class Party: self.get_visible_players = get_visible_players def combat_state(self) -> int: - return CombatState.OutOfCombat if all( - player.combat_state == CombatState.InCombat for player in self.get_visible_players()) else CombatState.InCombat + return CombatState.InCombat if any( + player.combat_state == CombatState.InCombat for player in self.get_visible_players()) else CombatState.OutOfCombat def time_in_combat(self) -> float: longest = 0.0 @@ -50,6 +50,7 @@ class Player(Stats): items: dict = field(default_factory=lambda: {'weapon': None}) damage_done: float = 0.0 healing_done: float = 0.0 + total_healing_done: float = 0.0 combat_time: CombatTime = field(default_factory=lambda: CombatTime()) combat_state: int = CombatState.OutOfCombat active: bool = False @@ -61,7 +62,7 @@ class Player(Stats): return self.active def has_stats(self) -> bool: - return bool(self.damage_done or self.healing_done) + return bool(self.damage_done or self.healing_done or self.total_healing_done) @staticmethod def new(self): @@ -75,6 +76,7 @@ class Player(Stats): self.name = other.name self.damage_done += other.damage_done self.healing_done += other.healing_done + self.total_healing_done += other.total_healing_done self.combat_state = other.combat_state self.combat_time.entered_combat = other.combat_time.entered_combat self.combat_time.time_in_combat += other.combat_time.time_in_combat @@ -92,11 +94,13 @@ class Player(Stats): self.activate() def register_healing_done(self, value): + self.total_healing_done += value + self.activate() + if self.party.combat_state() == CombatState.OutOfCombat: return self.healing_done += value - self.activate() def enter_combat(self): self.combat_time.entered_combat = time_utils.now() @@ -116,6 +120,11 @@ class Player(Stats): return StandalonePlayerListItem( self.name, self.items, self.healing_done, self.hps, self.combat_state) + def into_total_healing_list_item(self) -> StandalonePlayerListItem: + return StandalonePlayerListItem( + self.name, self.items, self.total_healing_done, self.total_hps, + self.combat_state) + @property def time_in_combat(self): if self.combat_state == CombatState.InCombat: @@ -140,6 +149,16 @@ class Player(Stats): return time_utils.as_milliseconds(self.healing_done / time_in_combat) + @property + def total_hps(self): + time_in_combat = self.party.time_in_combat() + + if time_in_combat == 0.0: + return 0.0 + + return time_utils.as_milliseconds( + self.total_healing_done / time_in_combat) + class CombatStats(CombatEventReceiver, Stats): def __init__(self, visibility, players=None): @@ -187,7 +206,13 @@ class CombatStats(CombatEventReceiver, Stats): def players_healing(self) -> List[PlayerListItem]: return to_player_list_items([player.into_healing_list_item() + for player in self.visible_players() + ]) + + def players_total_healing(self) -> List[PlayerListItem]: + return to_player_list_items([player.into_total_healing_list_item() for player in self.visible_players() + if player.total_healing_done ]) def party_combat_state(self): diff --git a/src/utils/async_request.py b/src/utils/async_request.py index cad008b..947c44f 100644 --- a/src/utils/async_request.py +++ b/src/utils/async_request.py @@ -2,6 +2,10 @@ import threading import queue import requests import collections +import logging + + +logger = logging.getLogger(__name__) Item = collections.namedtuple('Item', 'key value') @@ -26,7 +30,11 @@ _update_thread.start() def _async_req(url): - req = requests.get(url) + try: + req = requests.get(url, timeout=5) + except requests.RequestException as e: + logger.debug("async request failed for %s: %s", url, e) + req = None _queue.put(Item(key=url, value=req)) diff --git a/tests/test_async_request.py b/tests/test_async_request.py new file mode 100644 index 0000000..e15a1d9 --- /dev/null +++ b/tests/test_async_request.py @@ -0,0 +1,31 @@ +import requests + +from src.gui.modules.views.list_view import icon +from src.utils import async_request + + +def test_get_weapon_icon_ignores_missing_weapon(monkeypatch): + called = [] + monkeypatch.setattr(icon.async_request, 'get', lambda url: called.append(url)) + + assert icon.get_weapon_icon(None) is None + assert called == [] + + +def test_async_request_catches_request_errors(monkeypatch): + queued = [] + + def raise_connection_error(_url, timeout=None): + raise requests.ConnectionError('dns failed') + + class FakeQueue: + def put(self, item): + queued.append(item) + + monkeypatch.setattr(async_request.requests, 'get', raise_connection_error) + monkeypatch.setattr(async_request, '_queue', FakeQueue()) + + async_request._async_req('https://example.invalid/icon') + + assert queued[0].key == 'https://example.invalid/icon' + assert queued[0].value is None diff --git a/tests/test_combat_stats.py b/tests/test_combat_stats.py new file mode 100644 index 0000000..9d135de --- /dev/null +++ b/tests/test_combat_stats.py @@ -0,0 +1,37 @@ +from src.stats.combat_stats import CombatStats + + +class VisibleAll: + def test(self, _name): + return True + + +def test_self_healing_counts_while_in_combat(): + stats = CombatStats(VisibleAll()) + stats.on_player_appeared(0, 'Healer') + stats.on_enter_combat(0) + + stats.on_health_received(0, 0, 100.0) + + combat_healing = stats.players_healing() + total_healing = stats.players_total_healing() + + assert combat_healing[0].name == 'Healer' + assert combat_healing[0].value == 100.0 + assert total_healing[0].value == 100.0 + + +def test_total_healing_includes_post_combat_healing(): + stats = CombatStats(VisibleAll()) + stats.on_player_appeared(0, 'Healer') + stats.on_enter_combat(0) + stats.on_health_received(0, 0, 100.0) + stats.on_leave_combat(0) + + stats.on_health_received(0, 0, 50.0) + + combat_healing = stats.players_healing() + total_healing = stats.players_total_healing() + + assert combat_healing[0].value == 100.0 + assert total_healing[0].value == 150.0