Skip to content

Commit afa6e6c

Browse files
committed
Clean up the details reader. Use the Color object.
Mostly cosmetic changes with two primary exceptions: * The race is now purely determined by a lookup in the LOCALIZED_RACES dict and not by a quick check on the first character. This lowers the quality of recognition in the short run, but should set us up better for localization in the future after the LOCALIZED_RACES dict has been more fully populated. * A player's color related fields are now encapsulated by the Color object. Old code can be converted with no side effects by applying the following changes: - the .color_rgba is now the .color object it self: player.color_rgba -> player.color - the .color string is the str() conversion of the .color object: player.color -> str(player.color) - the .color_hex string is now a property of the .color object: player.color_hex -> player.color.hex
1 parent da6f1d3 commit afa6e6c

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

sc2reader/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
LOCALIZED_RACES = {
88

99
# deDE
10+
'Terraner': 'Terran',
1011
# enUS
12+
'Terran': 'Terran',
13+
'Protoss': 'Protoss',
14+
'Zerg': 'Zerg',
1115
# esES
1216
# esMX
1317
# frFR

sc2reader/processors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ def process_results(replay):
139139
# Check if replay file has recorded the winner
140140
remaining = set()
141141
for player in replay.players:
142-
if player.outcome == 1:
142+
if player.result == 1:
143143
replay.results[player.team] = "Won"
144-
elif player.outcome == 2:
144+
elif player.result == 2:
145145
replay.results[player.team] = "Lost"
146146
else:
147147
remaining.add(player.team)

sc2reader/readers.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from sc2reader.parsers import *
44
from sc2reader.objects import *
55
from sc2reader.utils import LITTLE_ENDIAN, BIG_ENDIAN
6-
from sc2reader.utils import timestamp_from_windows_time
6+
from sc2reader.utils import timestamp_from_windows_time, AttributeDict
77

88
#################################################
99

@@ -55,51 +55,43 @@ def load_header(self, replay, buffer):
5555
##################################################
5656

5757
class DetailsReader(object):
58+
color_fields = ('a','r','g','b',)
59+
player_fields = ('name','bnet','race','color','?','?','handicap','?','result')
60+
5861
def read(self, buffer, replay):
5962
data = buffer.read_data_struct()
6063

6164
for pid, pdata in enumerate(data[0]):
62-
fields = ('name','battlenet','race','color','??','??','handicap','??','outcome',)
63-
pdata = dict(zip(fields, [pdata[i] for i in sorted(pdata.keys())]))
64-
65-
# TODO?: get a map of realm,subregion => region in here
66-
player = Player(pid+1, pdata['name'], replay)
67-
player.uid = pdata['battlenet'][4]
68-
player.subregion = pdata['battlenet'][2]
69-
player.handicap = pdata['handicap']
65+
values = [v for k,v in sorted(pdata.iteritems())]
66+
pdata = AttributeDict(zip(self.player_fields, values))
67+
68+
#Handle the basic player attributes
69+
player = Player(pid+1, pdata.name, replay)
70+
player.uid = pdata.bnet[4]
71+
player.subregion = pdata.bnet[2]
72+
player.handicap = pdata.handicap
7073
player.realm = replay.realm
74+
player.result = pdata.result
75+
# TODO?: get a map of realm,subregion => region in here
7176

72-
# Some European language, like DE will have races written slightly differently (ie. Terraner).
73-
# To avoid these differences, only examine the first letter, which seem to be consistent across languages.
74-
race = pdata['race']
75-
if race[0] == 'T':
76-
race = "Terran"
77-
if race[0] == 'P':
78-
race = "Protoss"
79-
if race[0] == 'Z':
80-
race = "Zerg"
81-
# Check against non-western localised races
82-
player.actual_race = LOCALIZED_RACES.get(race, race)
83-
84-
color = [pdata['color'][i] for i in sorted(pdata['color'].keys())]
85-
color = dict(zip(('a','r','g','b',), color))
86-
player.color_hex = "%(r)02X%(g)02X%(b)02X" % color
87-
player.color = COLOR_CODES.get(player.color_hex, player.color_hex)
88-
player.color_rgba = color
89-
90-
player.outcome = pdata['outcome']
77+
# Now convert all different localizations to western if we can
78+
# TODO: recognize current locale and use that instead of western
79+
# TODO: fill in the LOCALIZED_RACES table
80+
player.actual_race = LOCALIZED_RACES.get(pdata.race, pdata.race)
81+
82+
''' Conversion to the new color object:
83+
color_rgba is the color object itself
84+
color_hex is color.hex
85+
color is str(color)'''
86+
values = [v for k,v in sorted(pdata.color.iteritems())]
87+
player.color = Color(zip(self.color_fields, values))
9188

9289
# Add player to replay
9390
replay.players.append(player)
94-
91+
92+
#Non-player details
9593
replay.map = data[1]
9694
replay.file_time = data[5]
97-
98-
# TODO: This doesn't seem to produce exactly correct results, ie. often off by one
99-
# second compared to file timestamps reported by Windows.
100-
# This might be due to wrong value of the magic constant 116444735995904000
101-
# or rounding errors. Ceiling or Rounding the result didn't produce consistent
102-
# results either.
10395
unix_timestamp = timestamp_from_windows_time(replay.file_time)
10496
replay.date = datetime.fromtimestamp(unix_timestamp)
10597
replay.utc_date = datetime.utcfromtimestamp(unix_timestamp)

0 commit comments

Comments
 (0)