minecraft-monitor/parse.py

74 lines
1.7 KiB
Python

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)