forked from IIEleven11/sc2notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchHistory.py
59 lines (51 loc) · 1.87 KB
/
MatchHistory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import json
import datetime
from Observer import Observer
class MatchHistory(Observer):
matches = {}
recents = []
def notify(self, data):
if data['event'] == 'exit' and len(data['players']) == 2:
found = False
opFound = False
result = ""
op = None
for p in data['players']:
if p['isme']:
# if we already found us, we're probably same name?
if found:
opFound = True
op = p
result = "Unknown"
else:
found = True
result = p['result']
elif p['isme'] == False and opFound == False:
opFound = True
op = p
if found and opFound:
m = {}
m['opponent'] = op['name']
m['race'] = op['race']
m['gametime'] = data['displayTime']
m['result'] = result
m['date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
if op['name'] in self.matches:
self.matches[op['name']].append(m)
else:
self.matches[op['name']] = [m]
with open('matches.json', 'w+') as outfile:
json.dump(self.matches, outfile)
self.recents.append(m)
print(self.recents)
def __init__(self):
Observer.__init__(self)
try:
with open('matches.json', 'r') as infile:
m = json.load(infile)
self.matches = m
except:
# exception if file doesnt exist, we dont need to
# create it now - matches dict is already created
# and file will get created on first write
pass