Skip to content

Commit 1e1c64e

Browse files
committed
Add event processing and amp calcuation.
Also shifts a couple more attribute definitions back to the replay.__init__ function for easy reference and consistency in code.
1 parent a167e1e commit 1e1c64e

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

sc2reader/objects.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Replay(object):
4444

4545
def __init__(self, replay_file, **options):
4646
#Useful references
47-
self.opt = options
47+
self.opt = AttributeDict(**options)
4848
self.filename = replay_file.name
4949

5050
#header information
@@ -65,14 +65,14 @@ def __init__(self, replay_file, **options):
6565
self.map = ""
6666
self.realm = ""
6767
self.events = list()
68+
self.events_by_type = defaultdict(list)
6869
self.results = dict()
6970
self.teams = defaultdict(list)
7071
self.observers = list() #Unordered list of Observer
7172
self.players = list() #Unordered list of Player
7273
self.people = list() #Unordered list of Players+Observers
7374
self.humans = list() #Unordered list of Human People
7475
self.person = PersonDict() #Maps pid to Player/Observer
75-
self.events_by_type = dict()
7676
self.attributes = list()
7777
self.messages = list()
7878
self.recorder = None # Player object
@@ -224,6 +224,9 @@ class Player(Person):
224224
def __init__(self, pid, name, replay):
225225
super(Player,self).__init__(pid, name, replay)
226226
self.is_observer = False
227+
self.aps = defaultdict(int)
228+
self.apm = defaultdict(int)
229+
self.avg_apm = 0
227230
# TODO: set default external interface variables?
228231

229232
@property

sc2reader/processors.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def FullProcessor(replay):
77
# Populate replay with details
8-
replay.realm = replay.raw.init_data.realm
8+
replay.realm = replay.raw.initData.realm
99
replay.map = replay.raw.details.map
1010
replay.file_time = replay.raw.details.file_time
1111
replay.unix_timestamp = windows_to_unix(replay.file_time)
@@ -14,7 +14,7 @@ def FullProcessor(replay):
1414

1515
# Organize the attribute data to be useful
1616
attribute_data = defaultdict(dict)
17-
for attr in replay.raw.attribute_events:
17+
for attr in replay.raw.attributes_events:
1818
attribute_data[attr.player][attr.name] = attr.value
1919

2020
# Populate replay with attributes
@@ -68,7 +68,7 @@ def FullProcessor(replay):
6868
player.chosen_race = attributes['Race']
6969
player.difficulty = attributes['Difficulty']
7070
player.type = attributes['Player Type']
71-
player.realm = replay.raw.init_data.realm
71+
player.realm = replay.raw.initData.realm
7272
player.uid = pdata.bnet.uid
7373
player.subregion = pdata.bnet.subregion
7474
player.handicap = pdata.handicap
@@ -88,7 +88,7 @@ def FullProcessor(replay):
8888
replay.person[pid] = player
8989

9090
# Create observers out of the leftover names
91-
all_players = replay.raw.init_data.player_names
91+
all_players = replay.raw.initData.player_names
9292
for i in range(observer_index,len(all_players)):
9393
observer = Observer(i+1,all_players[i],replay)
9494
replay.observers.append(observer)
@@ -121,6 +121,38 @@ def FullProcessor(replay):
121121
for player in replay.players:
122122
replay.teams[player.team].append(player)
123123

124+
# Copy the events over
125+
# TODO: the events need to be fixed both on the reader and processor side
126+
replay.events = replay.raw.game_events
127+
for event in replay.events:
128+
if event.is_local:
129+
# Set up the object cross references
130+
event.player = replay.person[event.pid]
131+
event.player.events.append(event)
132+
133+
# event.apply() #Skip this for now
134+
l = replay.events_by_type[event.name]
135+
l.append(event)
136+
137+
# Gather data for APM measurements
138+
for event in replay.events:
139+
if event.is_local and event.is_player_action:
140+
player = event.player
141+
if not player.is_observer:
142+
# Count up the APS, APM
143+
second, minute = event.second, event.second/60
144+
player.aps[event.second] += 1
145+
player.apm[minute] += 1
146+
player.avg_apm += 1
147+
148+
# Average the APM for actual players
149+
for player in replay.players:
150+
if player.events:
151+
event_minutes = player.events[-1].second/60.0
152+
if event_minutes:
153+
player.avg_apm /= event_minutes
154+
155+
# TODO: only need to integrate results processing now!
124156
return replay
125157

126158

0 commit comments

Comments
 (0)