Time + fame backward compatible with previous 0.8.1

This commit is contained in:
Wiktor Mazur
2020-03-02 21:53:07 +01:00
parent 6720adc78b
commit cffca90288
6 changed files with 131 additions and 36 deletions
+12 -20
View File
@@ -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()]
+17 -3
View File
@@ -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
}
+26
View File
@@ -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
+21 -4
View File
@@ -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)
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
+4 -1
View File
@@ -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
return int(time / 1000.0)