queue approach

This commit is contained in:
Wiktor Mazur
2020-03-15 17:54:24 +01:00
parent fee41b24bc
commit ffebb7af4c
3 changed files with 33 additions and 7 deletions
+1
View File
@@ -2,5 +2,6 @@ pyside2~=5.13
pyinstaller pyinstaller
toml~=0.10 toml~=0.10
requests~=2.22 requests~=2.22
locked-dict
Pillow~=6.2 Pillow~=6.2
./backend ./backend
+28 -5
View File
@@ -1,5 +1,6 @@
from typing import Iterable from typing import Iterable
from dataclasses import dataclass from dataclasses import dataclass
from queue import Queue
from .utils.config import config from .utils.config import config
from .utils.number import Number from .utils.number import Number
@@ -69,11 +70,13 @@ class GameStats():
if stat_type == VisibilityType.Zone: if stat_type == VisibilityType.Zone:
for t in (StatType.Damage, StatType.Time, StatType.Fame): for t in (StatType.Damage, StatType.Time, StatType.Fame):
self.history[t].update(self.zone[t]) self.history[t].update(self.zone[t])
self._construct_new_stats((VisibilityType.Zone, VisibilityType.LastFight)) self._construct_new_stats(
(VisibilityType.Zone, VisibilityType.LastFight))
elif stat_type == VisibilityType.LastFight: elif stat_type == VisibilityType.LastFight:
self._construct_new_stats([VisibilityType.LastFight]) self._construct_new_stats([VisibilityType.LastFight])
elif stat_type == VisibilityType.Overall: elif stat_type == VisibilityType.Overall:
self._construct_new_stats((VisibilityType.Zone, VisibilityType.Overall, VisibilityType.LastFight)) self._construct_new_stats(
(VisibilityType.Zone, VisibilityType.Overall, VisibilityType.LastFight))
def damage_stats(self, stat_type): def damage_stats(self, stat_type):
if stat_type == VisibilityType.Zone: if stat_type == VisibilityType.Zone:
@@ -106,7 +109,8 @@ class GameStats():
if t == StatType.Time: if t == StatType.Time:
getattr(self, s)[t] = time_stats.TimeStats() getattr(self, s)[t] = time_stats.TimeStats()
elif t == StatType.Damage: elif t == StatType.Damage:
getattr(self, s)[t] = damage_stats.DamageStats.from_other(getattr(self, s)[t]) getattr(self, s)[t] = damage_stats.DamageStats.from_other(
getattr(self, s)[t])
elif t == StatType.Fame: elif t == StatType.Fame:
getattr(self, s)[t] = fame_stats.FameStats() getattr(self, s)[t] = fame_stats.FameStats()
@@ -123,6 +127,7 @@ class GameStats():
game_stats = GameStats() game_stats = GameStats()
event_queue = Queue()
class FameStat: class FameStat:
@@ -143,14 +148,32 @@ def last_fight_stats():
return get_stats(VisibilityType.LastFight) return get_stats(VisibilityType.LastFight)
cached_players = () # do not compute values if there is nothing in queue
def get_stats(stat_type: VisibilityType): def get_stats(stat_type: VisibilityType):
global cached_players
new_events = False
while not event_queue.empty():
new_events = True
game_stats.register_event(event_queue.get_nowait())
fame = game_stats.fame_stats(stat_type) fame = game_stats.fame_stats(stat_type)
time = game_stats.time_stats(stat_type)['seconds_in_game'] time = game_stats.time_stats(stat_type)['seconds_in_game']
fame = FameStat(Number(fame['fame']), Number( fame = FameStat(Number(fame['fame']), Number(
(fame['fame'] / time) * 60 * 60 if time > 0.0 else 0.0)) (fame['fame'] / time) * 60 * 60 if time > 0.0 else 0.0))
if not new_events and cached_players:
return (cached_players, fame, time)
players = game_stats.damage_stats(stat_type) players = game_stats.damage_stats(stat_type)
return players, fame, time return (players, fame, time)
def queue_an_event(event):
event_queue.put(event)
def reset_zone_stats(): def reset_zone_stats():
@@ -171,5 +194,5 @@ def is_ready():
def initialize(): def initialize():
initialization_result = backend_proxy.initialize() initialization_result = backend_proxy.initialize()
backend_proxy.subscribe(game_stats.register_event) backend_proxy.subscribe(queue_an_event)
return initialization_result return initialization_result
+2
View File
@@ -11,6 +11,8 @@ from dataclasses import field
from dataclasses import dataclass from dataclasses import dataclass
from typing import Optional, List from typing import Optional, List
from locked_dict.locked_dict import LockedDict
from . import time_utils from . import time_utils
from ..event_receiver import CombatEventReceiver, VisibilityEventReceiver from ..event_receiver import CombatEventReceiver, VisibilityEventReceiver
from .statistics import Stats from .statistics import Stats