Created
October 15, 2019 05:01
-
-
Save lelandbatey/44dfdf525c07df62187d252108b353c9 to your computer and use it in GitHub Desktop.
Read and process MTGA log events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
def arena_card_db(): | |
carddb = dict() | |
with open("AllCards.json", 'r') as mtgjsonf: | |
carddb = json.load(mtgjsonf) | |
arena_cards = list() | |
for card_name, card in carddb.items(): | |
if 'mtgArenaId' in card: | |
arena_cards.append(card) | |
return carddb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from load_arena_cards import arena_card_db | |
def get_json_event(logfile): | |
lines = logfile.split('\n') | |
current_block = "" | |
for idx, line in enumerate(lines): | |
if line and (line.startswith("[UnityCrossThreadLogger]") or line.startswith("[Client GRE]")): | |
# this is the start of a new block (with title), end the last one | |
# print(current_block) | |
if "{" in current_block: # try to speed up debug runs by freeing up json watcher task | |
# which is likely the slowest | |
last_idx = current_block.rindex("}") | |
# ^ After gamestate logs, there is garbage (non-json) in the same block; strip it out | |
# TODO: this is hella hacky and should be fixed | |
current_block = current_block[:last_idx + 1] | |
new_logfile = "\n".join(lines[idx + 1:]) | |
return current_block, new_logfile | |
current_block = line.strip() + "\n" | |
elif line and line.startswith("]") or line.startswith("}"): | |
current_block += line.strip() + "\n" | |
# this is the END of a block, end it and start a new one | |
if "{" in current_block: # try to speed up debug runs by freeing up json watcher task | |
# which is likely the slowest | |
new_logfile = "\n".join(lines[idx + 1:]) | |
return current_block, new_logfile | |
current_block = "" | |
else: | |
# we're in the middle of a block somewhere | |
stripped = line.strip() | |
if stripped: | |
current_block += stripped + "\n" | |
return current_block, "" | |
def main(): | |
carddb = arena_card_db() | |
logdata = "" | |
# with open('output_log.txt') as lf: | |
# logdata = lf.read() | |
logdata = ''' | |
[UnityCrossThreadLogger] asdfasdfasdfasdfasdf | |
{ | |
asdasdfasdf | |
} | |
[UnityCrossThreadLogger] shorter this time | |
{ | |
12364576768 | |
} | |
''' | |
events = list() | |
while True: | |
event, logdata = get_json_event(logdata) | |
print('EVENT: "{}"\n\nLOGDATA: "{}"'.format(event, logdata)) | |
if logdata.strip() == "": | |
break | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment