From 63f3c9d3ae46c84e82ea0e2e61649d22930f3c38 Mon Sep 17 00:00:00 2001 From: User <> Date: Wed, 13 Mar 2019 19:40:11 +0100 Subject: [PATCH] old state parser --- parse.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 parse.py diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..34b67a0 --- /dev/null +++ b/parse.py @@ -0,0 +1,73 @@ +from json import loads, dumps + +events = [] + +def doOffline(state, cur_t, cur_ps): + (last_t, last_ev, last_evt, last_ps) = state + working = (cur_t - last_t < 70) + + if not working and last_ev == 'online': + events.append({'period': [last_evt, last_t], 'type': 'players', 'players': last_ps}) + return (cur_t, 'error', last_t, cur_ps) + else: + return state + + +def doBackOnline(state, cur_t, cur_ps): + (last_t, last_ev, last_evt, last_ps) = state + working = (cur_t - last_t < 70) + + if working and last_ev != 'online': + events.append({'period': [last_evt, cur_t], 'type': 'error'}) + return (cur_t, 'online', last_t, last_ps) + else: + return state + + +def doPlayerChange(state, cur_t, cur_ps): + (last_t, last_ev, last_evt, last_ps) = state + working = (cur_t - last_t < 70) + + if working and last_ps != cur_ps: + events.append({'period': [last_evt, cur_t], 'type': 'players', 'players': last_ps}) + return (cur_t, 'online', cur_t, cur_ps) + else: + return state + + +def doUpdateTime(state, cur_t, cur_ps): + (last_t, last_ev, last_evt, last_ps) = state + return (cur_t, last_ev, last_evt, last_ps) + + +def doCurrent(state): + (last_t, last_ev, last_evt, last_ps) = state + events.append({'period': [last_evt, last_t], 'type': 'current', 'players': last_ps}) + + +with open('all-time.txt') as f: + state = () + cur_t = 0 + cur_ps = [] + + for line in f: + current = loads(line) + cur_t = current['time'] + cur_ps = current['players'] + + if state == (): + state = (cur_t, 'online', cur_t, cur_ps) + else: + state = doOffline(state, cur_t, cur_ps) + state = doBackOnline(state, cur_t, cur_ps) + state = doPlayerChange(state, cur_t, cur_ps) + state = doUpdateTime(state, cur_t, cur_ps) + + doCurrent(state) + + +def dumpJSON(events): + for event in events: + print(dumps(event)) + +dumpJSON(events)