11# -*- coding: utf-8 -*-
22import json
3+ from sc2reader .utils import Length
34
45class TrackerEvent (object ):
56 def __init__ (self , frames ):
67 #: The frame of the game this event was applied
78 self .frame = frames
89
9- def __str__ (self ):
10- """ Dumps the event data to a json string. """
11- return json .dumps (self .__dict__ )
12-
1310 def load_context (self , replay ):
1411 pass
1512
13+ def _str_prefix (self ):
14+ return "{0}\t " .format (Length (seconds = int (self .frame / 16 )))
15+
16+ def __str__ (self ):
17+ return self ._str_prefix () + self .name
1618
1719class PlayerStatsEvent (TrackerEvent ):
1820 name = 'PlayerStatsEvent'
@@ -131,6 +133,8 @@ def __init__(self, frames, data):
131133 def load_context (self , replay ):
132134 self .player = replay .player [self .pid ]
133135
136+ def __str__ (self ):
137+ return self ._str_prefix ()+ "{0: >15} - Stats Update" .format (self .player )
134138
135139class UnitBornEvent (TrackerEvent ):
136140 name = 'UnitBornEvent'
@@ -195,9 +199,15 @@ def load_context(self, replay):
195199
196200 replay .active_units [self .unit_id_index ] = self .unit
197201 self .unit .location = self .location
198- self .unit .birth = self .frame
199- self .unit .owner = self .unit_upkeeper
202+ self .unit .started_at = self .frame
203+ self .unit .finished_at = self .frame
204+
205+ if self .unit_upkeeper :
206+ self .unit .owner = self .unit_upkeeper
207+ self .unit .owner .units .append (self .unit )
200208
209+ def __str__ (self ):
210+ return self ._str_prefix ()+ "{0: >15} - Unit born {1}" .format (self .unit_upkeeper ,self .unit )
201211
202212class UnitDiedEvent (TrackerEvent ):
203213 name = 'UnitDiedEvent'
@@ -235,7 +245,7 @@ def __init__(self, frames, data):
235245 def load_context (self , replay ):
236246 if self .unit_id in replay .objects :
237247 self .unit = replay .objects [self .unit_id ]
238- self .unit .death = self .frame
248+ self .unit .died_at = self .frame
239249 self .unit .location = self .location
240250 if self .unit_id_index in replay .active_units :
241251 del replay .active_units [self .unit_id_index ]
@@ -249,6 +259,9 @@ def load_context(self, replay):
249259 elif self .killer_pid :
250260 pass #print "Unknown killer pid", self.killer_pid
251261
262+ def __str__ (self ):
263+ return self ._str_prefix ()+ "{0: >15} - Unit died {1}." .format (self .unit .owner , self .unit )
264+
252265
253266class UnitOwnerChangeEvent (TrackerEvent ):
254267 name = 'UnitOwnerChangeEvent'
@@ -293,10 +306,19 @@ def load_context(self, replay):
293306
294307 if self .unit_id in replay .objects :
295308 self .unit = replay .objects [self .unit_id ]
296- self .unit .owner = self .unit_upkeeper
297309 else :
298310 print "Unit owner changed before it was born!"
299311
312+ if self .unit_upkeeper :
313+ if unit .owner :
314+ print "stduff"
315+ unit .owner .units .remove (unit )
316+ unit .owner = self .unit_upkeeper
317+ self .unit_upkeeper .units .append (unit )
318+
319+ def __str__ (self ):
320+ return self ._str_prefix ()+ "{0: >15} took {1}" .format (self .unit_upkeeper , self .unit )
321+
300322
301323class UnitTypeChangeEvent (TrackerEvent ):
302324 name = 'UnitTypeChangeEvent'
@@ -326,6 +348,9 @@ def load_context(self, replay):
326348 else :
327349 print "Unit type changed before it was born!"
328350
351+ def __str__ (self ):
352+ return self ._str_prefix ()+ "{0: >15} - Unit {0} type changed to {1}" .format (self .unit .owner , self .unit , self .unit_type_name )
353+
329354
330355class UpgradeCompleteEvent (TrackerEvent ):
331356 name = 'UpgradeCompleteEvent'
@@ -354,6 +379,8 @@ def load_context(self, replay):
354379 # TODO: We don't have upgrade -> ability maps
355380 # TODO: we can probably do the same thing we did for units
356381
382+ def __str__ (self ):
383+ return self ._str_prefix ()+ "{0: >15} - {1}upgrade completed" .format (self .player , self .upgrade_type_name )
357384
358385class UnitInitEvent (TrackerEvent ):
359386 name = 'UnitInitEvent'
@@ -419,9 +446,16 @@ def load_context(self, replay):
419446 replay .objects [self .unit_id ] = self .unit
420447
421448 replay .active_units [self .unit_id_index ] = self .unit
422- self .unit .owner = self .unit_upkeeper
423449 self .unit .location = self .location
424- self .unit .birth = self .frame
450+ self .unit .started_at = self .frame
451+ # self.unit.finished_at = self.frame
452+
453+ if self .unit_upkeeper :
454+ self .unit .owner = self .unit_upkeeper
455+ self .unit .owner .units .append (self .unit )
456+
457+ def __str__ (self ):
458+ return self ._str_prefix ()+ "{0: >15} - Unit inititated {1}" .format (self .unit_upkeeper , self .unit )
425459
426460
427461class UnitDoneEvent (TrackerEvent ):
@@ -445,9 +479,12 @@ def __init__(self, frames, data):
445479 def load_context (self , replay ):
446480 if self .unit_id in replay .objects :
447481 self .unit = replay .objects [self .unit_id ]
482+ self .unit .finished_at = self .frame
448483 else :
449484 print "Unit done before it was started!"
450485
486+ def __str__ (self ):
487+ return self ._str_prefix ()+ "{0: >15} - Unit {1} done" .format (self .unit .owner , self .unit )
451488
452489class UnitPositionsEvent (TrackerEvent ):
453490 name = 'UnitPositionsEvent'
@@ -482,3 +519,6 @@ def load_context(self, replay):
482519 self .units .append (unit )
483520 else :
484521 print "Unit moved that doesn't exist!"
522+
523+ def __str__ (self ):
524+ return self ._str_prefix ()+ "Unit positions update"
0 commit comments