Skip to content

Commit f1e2f80

Browse files
committed
Refactor the Attribute class to be more managable.
1 parent 3bd1d44 commit f1e2f80

File tree

1 file changed

+24
-48
lines changed

1 file changed

+24
-48
lines changed

sc2reader/objects.py

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ def __iter__(self):
3636

3737
class Attribute(object):
3838

39+
id_map = {
40+
0x01F4: ("Player Type", PLAYER_TYPE_CODES),
41+
0x07D1: ("Game Type", GAME_FORMAT_CODES),
42+
0x0BB8: ("Game Speed", GAME_SPEED_CODES),
43+
0x0BB9: ("Race", RACE_CODES),
44+
0x0BBA: ("Color", TEAM_COLOR_CODES),
45+
0x0BBB: ("Handicap", None),
46+
0x0BBC: ("Difficulty", DIFFICULTY_CODES),
47+
0x0BC1: ("Category", GAME_TYPE_CODES),
48+
0x07D2: ("Teams1v1", lambda v: int(self.value[0])),
49+
0x07D3: ("Teams2v2", lambda v: int(self.value[0])),
50+
0x07D4: ("Teams3v3", lambda v: int(self.value[0])),
51+
0x07D5: ("Teams4v4", lambda v: int(self.value[0])),
52+
0x07D6: ("TeamsFFA", lambda v: int(self.value[0])),
53+
0x07D7: ("Teams5v5", lambda v: int(self.value[0]))
54+
}
55+
3956
def __init__(self, data):
4057
#Unpack the data values and add a default name of unknown to be
4158
#overridden by known attributes; acts as a flag for exclusion
@@ -44,54 +61,13 @@ def __init__(self, data):
4461
#Strip off the null bytes
4562
while self.value[-1] == '\x00': self.value = self.value[:-1]
4663

47-
if self.id == 0x01F4:
48-
self.name, self.value = "Player Type", PLAYER_TYPE_CODES[self.value]
49-
50-
elif self.id == 0x07D1:
51-
self.name,self.value = "Game Type", GAME_FORMAT_CODES[self.value]
52-
53-
elif self.id == 0x0BB8:
54-
self.name, self.value = "Game Speed", GAME_SPEED_CODES[self.value]
55-
56-
elif self.id == 0x0BB9:
57-
self.name, self.value = "Race", RACE_CODES[self.value]
58-
59-
elif self.id == 0x0BBA:
60-
self.name, self.value = "Color", TEAM_COLOR_CODES[self.value]
61-
62-
elif self.id == 0x0BBB:
63-
self.name = "Handicap"
64-
65-
elif self.id == 0x0BBC:
66-
self.name, self.value = "Difficulty", DIFFICULTY_CODES[self.value]
67-
68-
elif self.id == 0x0BC1:
69-
self.name, self.value = "Category", GAME_TYPE_CODES[self.value]
70-
71-
elif self.id == 0x07D2:
72-
self.name = "Teams1v1"
73-
self.value = int(self.value[0])
74-
75-
elif self.id == 0x07D3:
76-
self.name = "Teams2v2"
77-
self.value = int(self.value[0])
78-
79-
elif self.id == 0x07D4:
80-
self.name = "Teams3v3"
81-
self.value = int(self.value[0])
82-
83-
elif self.id == 0x07D5:
84-
self.name = "Teams4v4"
85-
self.value = int(self.value[0])
86-
87-
elif self.id == 0x07D6:
88-
self.name = "TeamsFFA"
89-
self.value = int(self.value[0])
90-
91-
# Complete guesses here, there are several ids that might be correct
92-
elif self.id == 0x07D7:
93-
self.name = "Teams5v5"
94-
self.value = int(self.value[0])
64+
if self.id in self.id_map:
65+
self.name, lookup = self.id_map[self.id]
66+
if lookup:
67+
if callable(lookup):
68+
self.value = lookup(self.value)
69+
else:
70+
self.value = lookup[self.value]
9571

9672
def __repr__(self):
9773
return str(self)

0 commit comments

Comments
 (0)