-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
131 lines (103 loc) · 3.35 KB
/
utils.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'''
Street Fighter V tools
--
should be pretty obvious what to do. check the bottom for an example.
NOTE: there are some brittle spots, but I'm erring on the side of savvy user so
I don't have to burn cycles being defensive.
'''
import glob
import itertools
import os
# import sys
from pprint import pprint as pp
def get(only=None):
'''
returns dictionary of character data
only optionally specifies the charcode for one character to load
'''
data = {}
if only:
charname, chardata = load_char_from_path('./OKI/{}.txt'.format(only))
data[charname] = chardata
else:
for fn in glob.glob('./OKI/???.txt'):
charname, chardata = load_char_from_path(fn)
data[charname] = chardata
return data
def parse_data(k, v):
'''
accepts a key and a value from a move and returns a reformatted value if
appropriate.
'''
# XXX may not be complete.
# int?
try:
return int(v)
except ValueError:
# XXX brittle
_k = k.lower().strip()
if _k == 'active':
if '(' in v:
return _split_ints(v)
if _k == 'cancel ability':
return v.split(',')
if _k == 'attack level':
return v.split('*')
if '*' in v: # NOTE: this can clobber attack level
return _split_ints(v)
return v
def load_char_from_path(fn):
'''
returns charname(str), chardata(dict)
'''
with open(fn) as f: # XXX brittle
charname = os.path.basename(fn).split('.')[0] # XXX brittle
keys = []
cdata = {}
idx = 0
for line in f:
line = line.strip()
els = line.split('\t')
if els[0] == 'Move':
keys = [e.strip().lower() for e in els]
continue
row = dict(zip(keys, els))
row['index'] = idx
for prop, val in row.items():
row[prop] = parse_data(prop, val)
# sometimes total isn't present even though startup, active, and recovery are known
if row.get('total') in ('', '-', '?', '??'):
try:
row['total'] = row['startup'] + row['active'] + row['recovery'] - 1
except TypeError:
pass
mvname = row['move']
# add tags to make queries possible
tags = set()
if mvname not in ('stun', 'taunt', 'health', 'v-trigger'):
if mvname.startswith('jump'):
tags.add('jump')
elif mvname.startswith('dash'):
tags.add('dash')
else:
tags.add('oki')
else:
tags.add('stats')
if row.get('hit advantage') == 'KD':
tags.add('kd')
cdata[mvname] = row
cdata[mvname]['tags'] = tags
idx += 1
return charname, cdata
def _split_ints(v):
r = [int(''.join(x)) for isnum, x in itertools.groupby(v, key=str.isdigit) if isnum]
if not r:
return v
return r
if __name__ == '__main__':
d = get('GUL')
pp(d)
# for charname, moves in d.items():
# for move, props in moves.items():
# for prop, val in props.items():
# d[charname][move][prop] = parse_data(prop, val)