diff --git a/application/src/backend_proxy.py b/application/src/backend_proxy.py index 13f92f4..ce5f4e1 100644 --- a/application/src/backend_proxy.py +++ b/application/src/backend_proxy.py @@ -95,6 +95,10 @@ def _testing_call_sequence(callback): *deal_damage(12, 'Sword', 80.0), *deal_damage(13, 'Bow', 221.0), {'name': 'UpdateFame', 'value': {'value': 2000}}, + {'name': 'ZoneChange'}, + *add_player(1, 'Cursed', 'T5_MAIN_CURSEDSTAFF@2'), + *deal_damage(1, 'Cursed', 80.0), + {'name': 'ZoneChange'}, ] sequence[environment.TestType.ZoneChange] = [ diff --git a/application/src/engine.py b/application/src/engine.py index f2e33db..f3bf6b5 100644 --- a/application/src/engine.py +++ b/application/src/engine.py @@ -60,7 +60,7 @@ class GameStats(): self._construct_new_stats([VisibilityType.LastFight]) elif event[ev_consts.EvKeyName] == ev_consts.EvNameZoneChange: - self.history[StatType.Combat].update(self.zone[StatType.Combat]) + self.history[StatType.Combat].update_non_idle(self.zone[StatType.Combat]) self.history[StatType.Fame].update(self.zone[StatType.Fame]) self.history[StatType.Time].update(self.zone[StatType.Time]) @@ -128,14 +128,6 @@ class GameStats(): elif t == StatType.Fame: getattr(self, s)[t] = fame_stats.FameStats() - def _merged_stats(self, stats: dict): - result = {} - - for stat in stats: - result.update(stat.stats()) - - return result - game_stats = GameStats() event_queue: Queue = Queue() @@ -160,10 +152,11 @@ def last_fight_stats(combat_stat_type: str): cached_players = () # do not compute values if there is nothing in queue -cached_players_type = None +cached_stat_type = None +cached_combat_stat_type = None def get_stats(stat_type: str, combat_stat_type: str): - global cached_players, cached_players_type + global cached_players, cached_stat_type, cached_combat_stat_type new_events = False while not event_queue.empty(): @@ -175,7 +168,7 @@ def get_stats(stat_type: str, combat_stat_type: str): fame = FameStat(Number(fame['fame']), Number( (fame['fame'] / time) * 60 * 60 if time > 0.0 else 0.0)) - if not new_events and cached_players and cached_players_type != stat_type: + if not new_events and cached_players and cached_stat_type == stat_type and cached_combat_stat_type == combat_stat_type: return (cached_players, fame, time) cached_players = { @@ -183,7 +176,8 @@ def get_stats(stat_type: str, combat_stat_type: str): CombatStatType.Healing: lambda stat_type : game_stats.get_healing_stats(stat_type) }[combat_stat_type](stat_type) - cached_players_type = stat_type + cached_stat_type = stat_type + cached_combat_stat_type = combat_stat_type return (cached_players, fame, time) diff --git a/application/src/stats/combat_stats.py b/application/src/stats/combat_stats.py index bfbd2c2..ee60322 100644 --- a/application/src/stats/combat_stats.py +++ b/application/src/stats/combat_stats.py @@ -52,6 +52,16 @@ class Player(Stats): healing_done: float = 0.0 combat_time: CombatTime = field(default_factory=lambda: CombatTime()) combat_state: int = CombatState.OutOfCombat + active: bool = False + + def activate(self): + self.active = True + + def is_active(self) -> bool: + return self.active + + def has_stats(self) -> bool: + return bool(self.damage_done or self.healing_done) @staticmethod def new(self): @@ -69,6 +79,7 @@ class Player(Stats): self.combat_time.entered_combat = other.combat_time.entered_combat self.combat_time.time_in_combat += other.combat_time.time_in_combat self.items = other.items + self.active = other.active or self.active def register_items(self, value): self.items = value @@ -78,12 +89,14 @@ class Player(Stats): return self.damage_done += value + self.activate() def register_healing_done(self, value): 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() @@ -152,6 +165,13 @@ class CombatStats(CombatEventReceiver, Stats): self.players[id] = Player.from_other(player) self.players[id].update(player) + def update_non_idle(self, other): + self.update(other) + self.players = dict(filter(lambda elem: elem[1].has_stats(), self.players.items())) + + for player in self.players.values(): + player.activate() + def combined(self, other): stats = CombatStats(self.visibility) stats.update(self) @@ -173,12 +193,14 @@ class CombatStats(CombatEventReceiver, Stats): return self.party.combat_state() def visible_players(self) -> Iterable[Player]: - return (player for player in self.players.values() if self.visibility.test(player.name)) + return (player for player in self.players.values() if player.is_active() and self.visibility.test(player.name)) def on_player_appeared(self, id: int, name: str): if id not in self.players: self.players[id] = Player(name, self.party) + self.players[id].activate() + def on_damage_done(self, id: int, damage: float): self.players[id].register_damage_done(damage)