debriefing polling & parsing WIP

This commit is contained in:
Vasyl Horbachenko
2018-06-13 00:34:28 +03:00
parent dc303997a6
commit 51c32747b4
4 changed files with 57 additions and 24 deletions

52
userdata/debriefing.py Normal file
View File

@@ -0,0 +1,52 @@
import typing
import json
import threading
import time
import os
from datetime import datetime
DEBRIEFING_LOG_EXTENSION = "log"
class Debriefing:
def __init__(self):
self.destroyed_units = {} # type: typing.Dict[str, typing.Dict[str, int]]
@classmethod
def parse(cls, path: str):
with open(path, "r") as f:
events = json.load(f)
return Debriefing()
def debriefing_directory_location() -> str:
return "build/debriefing"
def _logfiles_snapshot() -> typing.Dict[str, float]:
result = {}
for file in os.listdir(debriefing_directory_location()):
fullpath = os.path.join(debriefing_directory_location(), file)
result[file] = os.path.getmtime(fullpath)
return result
def _poll_new_debriefing_log(snapshot: typing.Dict[str, float], callback: typing.Callable):
should_run = True
while should_run:
for file, timestamp in _logfiles_snapshot().items():
if file not in snapshot or timestamp != snapshot[file]:
callback(Debriefing.parse(os.path.join(debriefing_directory_location(), file)))
should_run = False
break
time.sleep(1)
def wait_for_debriefing(callback: typing.Callable):
threading.Thread(target=_poll_new_debriefing_log, args=(_logfiles_snapshot(), callback)).start()

View File

@@ -1,19 +0,0 @@
import typing
import json
class Debriefing:
def __init__(self):
self.destroyed_units = {} # type: typing.Dict[str, typing.Dict[str, int]]
def parse(self, path: str):
with open(path, "r") as f:
events = json.load(f)
def debriefing_directory_location() -> str:
return "build/debrfiefing"
def wait_for_debriefing(callback: typing.Callable):
pass