From cffca90288862eafdb1457b1906b3d26f4781217 Mon Sep 17 00:00:00 2001 From: Wiktor Mazur Date: Mon, 2 Mar 2020 21:53:07 +0100 Subject: [PATCH] Time + fame backward compatible with previous 0.8.1 --- application/src/engine.py | 59 +++++++++++++++++++++++---- application/src/stats/damage_stats.py | 32 ++++++--------- application/src/stats/fame_stats.py | 20 +++++++-- application/src/stats/statistics.py | 26 ++++++++++++ application/src/stats/time_stats.py | 25 ++++++++++-- application/src/stats/time_utils.py | 5 ++- 6 files changed, 131 insertions(+), 36 deletions(-) create mode 100644 application/src/stats/statistics.py diff --git a/application/src/engine.py b/application/src/engine.py index a8fa3d2..58121e5 100644 --- a/application/src/engine.py +++ b/application/src/engine.py @@ -4,7 +4,7 @@ from .environment import TEST_ENV_ENABLED from . import backend_proxy from .backend_proxy import InitializationResult, INITIALIZATION_RESULT -from .stats import damage_stats, fame_stats +from .stats import damage_stats, fame_stats, time_stats from .consts import events as ev_consts @@ -20,14 +20,17 @@ class GameStats(): self.zone = { 'damage': damage_stats.DamageStats(), 'fame': fame_stats.FameStats(), + 'time': time_stats.TimeStats(), } self.last_fight = { 'damage': damage_stats.DamageStats(), 'fame': fame_stats.FameStats(), + 'time': time_stats.TimeStats(), } self.history = { 'damage': damage_stats.DamageStats(), - 'fame': damage_stats.DamageStats() + 'fame': fame_stats.FameStats(), + 'time': time_stats.TimeStats(), } def register_event(self, event): @@ -37,11 +40,13 @@ class GameStats(): self.zone['damage']) elif event[ev_consts.EvKeyName] == ev_consts.EvNameZoneChange: self.history['damage'].update(self.zone['damage']) - # self.history['fame'].update(self.zone['fame']) + self.history['fame'].update(self.zone['fame']) + self.history['time'].update(self.zone['time']) self.zone['damage'] = damage_stats.DamageStats() self.zone['fame'] = fame_stats.FameStats() - + self.zone['time'] = time_stats.TimeStats() + self.zone['damage'].receive(event) self.last_fight['damage'].receive(event) self.zone['fame'].receive(event) @@ -65,6 +70,22 @@ class GameStats(): elif stat_type == StatType.Overall: return self.history['damage'].combined(self.zone['damage']).stats() + def fame_stats(self, stat_type): + if stat_type == StatType.Zone: + return self.zone['fame'].stats() + elif stat_type == StatType.LastFight: + return self.last_fight['fame'].stats() + elif stat_type == StatType.Overall: + return self.history['fame'].combined(self.zone['fame']).stats() + + def time_stats(self, stat_type): + if stat_type == StatType.Zone: + return self.zone['time'].stats() + elif stat_type == StatType.LastFight: + return self.last_fight['time'].stats() + elif stat_type == StatType.Overall: + return self.history['time'].combined(self.zone['time']).stats() + def _are_everyone_in_session_out_of_combat(self): return all(player.combat_state == damage_stats.CombatState.OutOfCombat for player in self.zone['damage'].players.values()) @@ -73,9 +94,10 @@ class GameStats(): for stat in stats: result.update(stat.stats()) - + return result + game_stats = GameStats() @@ -148,23 +170,44 @@ def with_percentage(session): def zone_stats(with_damage=False): + fame = game_stats.fame_stats(StatType.Zone) + time = game_stats.time_stats(StatType.Zone) + fame['fame_per_hour'] = fame['fame'] / \ + time['seconds_in_game'] if time['seconds_in_game'] > 0 else 0.0 return stats({ 'players': game_stats.damage_stats(StatType.Zone), - 'main': {}}, + 'main': { + **fame, + **time, + }}, with_damage) def overall_stats(with_damage=False): + fame = game_stats.fame_stats(StatType.Overall) + time = game_stats.time_stats(StatType.Overall) + fame['fame_per_hour'] = fame['fame'] / \ + time['seconds_in_game'] if time['seconds_in_game'] > 0 else 0.0 return stats({ 'players': game_stats.damage_stats(StatType.Overall), - 'main': {}}, + 'main': { + **fame, + **time, + }}, with_damage) def last_fight_stats(with_damage=False): + fame = game_stats.fame_stats(StatType.LastFight) + time = game_stats.time_stats(StatType.LastFight) + fame['fame_per_hour'] = fame['fame'] / \ + time['seconds_in_game'] if time['seconds_in_game'] > 0 else 0.0 return stats({ 'players': game_stats.damage_stats(StatType.LastFight), - 'main': {}}, + 'main': { + **fame, + **time, + }}, with_damage) diff --git a/application/src/stats/damage_stats.py b/application/src/stats/damage_stats.py index 513c06d..46a4056 100644 --- a/application/src/stats/damage_stats.py +++ b/application/src/stats/damage_stats.py @@ -13,6 +13,7 @@ from typing import Optional from . import time_utils from ..event_receiver import CombatEventReceiver +from .statistics import Stats @dataclass @@ -27,13 +28,17 @@ class CombatState: @dataclass -class Player: +class Player(Stats): name: str items: dict = field(default_factory=lambda: {'weapon': None}) damage_done: float = 0.0 combat_time: CombatTime = CombatTime() combat_state: CombatState = CombatState.OutOfCombat + @staticmethod + def new(self): + return Player() + @staticmethod def from_other(other): return Player(other.name, other.items) @@ -76,7 +81,7 @@ class Player: if self.time_in_combat == 0.0: return 0.0 - return time_utils.as_seconds(self.damage_done / self.time_in_combat) + return time_utils.as_milliseconds(self.damage_done / self.time_in_combat) def stats(self): return { @@ -87,12 +92,16 @@ class Player: 'items': self.items} -class DamageStats(CombatEventReceiver): +class DamageStats(CombatEventReceiver, Stats): def __init__(self, players=None): if not players: players = {} self.players = players + @staticmethod + def new(self): + return DamageStats() + @staticmethod def from_other(other): return DamageStats({k: Player.from_other(v) for (k, v) in other.players.items()}) @@ -133,20 +142,3 @@ class DamageStats(CombatEventReceiver): def on_items_update(self, id: int, items: dict): self.players[id].register_items(items) - - -def combined_stats(stats_list): - combined = {} - - for stats in stats_list: - print(stats) - if stats['player'] in combined: - current = combined[stats['player']] - current['damage'] += stats['damage'] - current['time_in_combat'] += stats['time_in_combat'] - current['dps'] += stats['dps'] - current['items'] = stats['items'] - else: - combined[stats['player']] = stats - - return [s for s in combined.values()] diff --git a/application/src/stats/fame_stats.py b/application/src/stats/fame_stats.py index 873d723..96b533d 100644 --- a/application/src/stats/fame_stats.py +++ b/application/src/stats/fame_stats.py @@ -1,15 +1,29 @@ from dataclasses import dataclass from ..event_receiver import FameEventReceiver +from .statistics import Stats @dataclass -class FameStats(FameEventReceiver): +class FameStats(FameEventReceiver, Stats): fame: float = 0.0 - def __init__(self): - self.fame = 0.0 + @classmethod + def new(self): + return FameStats() + + @classmethod + def from_other(other: Stats): + return FameStats(fame=other.fame) + + def update(self, other: Stats): + self.fame += other.fame def on_fame_update(self, value: float): self.fame += value + def stats(self): + return { + 'fame': self.fame, + 'fame_per_hour': 0.0 # TODO: remove, added because backward compatibility + } \ No newline at end of file diff --git a/application/src/stats/statistics.py b/application/src/stats/statistics.py new file mode 100644 index 0000000..fdc171f --- /dev/null +++ b/application/src/stats/statistics.py @@ -0,0 +1,26 @@ +import abc + + +class Stats(abc.ABC): + @abc.abstractmethod + def new(self): + pass + + @abc.abstractclassmethod + def from_other(other): + pass + + @abc.abstractmethod + def update(self, other): + pass + + @abc.abstractmethod + def stats(self): + pass + + def combined(self, other): + stats = self.new() + stats.update(self) + stats.update(other) + + return stats diff --git a/application/src/stats/time_stats.py b/application/src/stats/time_stats.py index f061673..f80f4a4 100644 --- a/application/src/stats/time_stats.py +++ b/application/src/stats/time_stats.py @@ -1,9 +1,26 @@ +from dataclasses import field +from dataclasses import dataclass + from . import time_utils +from .statistics import Stats -class TimeStats: - def __init__(self): - self.time = time_utils.now() +@dataclass +class TimeStats(Stats): + time : float = field(default_factory=lambda: time_utils.now()) + + @classmethod + def new(self): + return TimeStats() def stats(self): - return time_utils.delta(self.time) \ No newline at end of file + return { + 'seconds_in_game': time_utils.as_seconds(time_utils.delta(self.time)) + } + + @classmethod + def from_other(other): + return TimeStats(time=other.time) + + def update(self, other): + self.time = other.time diff --git a/application/src/stats/time_utils.py b/application/src/stats/time_utils.py index 9736525..a235724 100644 --- a/application/src/stats/time_utils.py +++ b/application/src/stats/time_utils.py @@ -10,5 +10,8 @@ def delta(time): return (delta.seconds * 1000.0 + delta.microseconds / 1000.0) +def as_milliseconds(time): + return time * 1000.0 + def as_seconds(time): - return time * 1000.0 \ No newline at end of file + return int(time / 1000.0) \ No newline at end of file