Skip to content

Commit dda41fb

Browse files
committed
Fix up event names and hierarchy.
* PacketEvent is now ProgressEvent. * SetToHotkeyEvent is now SetControlGroupEvent. * AddToHotkeyEvent is now AddToControlGroupEvent. * GetFromHotkeyEvent is now GetControlGroupEvent. * PlayerAbilityEvent is no longer part of the event hierarchy. * event.name is no longer a class property; it can only be accessed from an event instance.
1 parent 2d4bd6b commit dda41fb

File tree

8 files changed

+117
-139
lines changed

8 files changed

+117
-139
lines changed

CHANGELOG.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
CHANGELOG
22
============
33

4+
0.7.0 -
5+
---------------------------
6+
7+
* PacketEvent is now ProgressEvent.
8+
* SetToHotkeyEvent is now SetControlGroupEvent.
9+
* AddToHotkeyEvent is now AddToControlGroupEvent.
10+
* GetFromHotkeyEvent is now GetControlGroupEvent.
11+
* PlayerAbilityEvent is no longer part of the event hierarchy.
12+
* event.name is no longer a class property; it can only be accessed from an event instance.
13+
14+
15+
416
0.6.4 - September 22nd 2013
517
---------------------------
618

719
* Fix bug in code for logging errors.
8-
* Fix siege tank supply count
20+
* Fix siege tank supply count.
921
* Small improvements to message.events parsing.
1022

1123
0.6.3 - September 15th 2013

sc2reader/engine/engine.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from sc2reader.events import *
66
from sc2reader.engine.events import InitGameEvent, EndGameEvent, PluginExit
77

8+
89
class GameEngine(object):
910
""" GameEngine Specification
1011
--------------------------
@@ -56,9 +57,8 @@ def handleEventName(self, event, replay)
5657
* handleMessageEvent - called for events in replay.message.events
5758
* handleGameEvent - called for events in replay.game.events
5859
* handleTrackerEvent - called for events in replay.tracker.events
59-
* handlePlayerActionEvent - called for all game events indicating player actions
6060
* handleAbilityEvent - called for all types of ability events
61-
* handleHotkeyEvent - called for all player hotkey events
61+
* handleControlGroupEvent - called for all player control group events
6262
6363
Plugins may also handle optional ``InitGame`` and ``EndGame`` events generated
6464
by the GameEngine before and after processing all the events:
@@ -196,26 +196,18 @@ def _get_event_handlers(self, event, plugins):
196196

197197
def _get_plugin_event_handlers(self, plugin, event):
198198
handlers = list()
199-
if isinstance(event, Event) and self._has_event_handler(plugin, Event):
200-
handlers.append(self._get_event_handler(plugin, Event))
201-
if isinstance(event, MessageEvent) and self._has_event_handler(plugin, MessageEvent):
202-
handlers.append(self._get_event_handler(plugin, MessageEvent))
203-
if isinstance(event, GameEvent) and self._has_event_handler(plugin, GameEvent):
204-
handlers.append(self._get_event_handler(plugin, GameEvent))
205-
if isinstance(event, TrackerEvent) and self._has_event_handler(plugin, TrackerEvent):
206-
handlers.append(self._get_event_handler(plugin, TrackerEvent))
207-
if isinstance(event, PlayerActionEvent) and self._has_event_handler(plugin, PlayerActionEvent):
208-
handlers.append(self._get_event_handler(plugin, PlayerActionEvent))
209-
if isinstance(event, AbilityEvent) and self._has_event_handler(plugin, AbilityEvent):
210-
handlers.append(self._get_event_handler(plugin, AbilityEvent))
211-
if isinstance(event, HotkeyEvent) and self._has_event_handler(plugin, HotkeyEvent):
212-
handlers.append(self._get_event_handler(plugin, HotkeyEvent))
213-
if self._has_event_handler(plugin, event):
214-
handlers.append(self._get_event_handler(plugin, event))
199+
if isinstance(event, Event) and hasattr(plugin, 'handleEvent'):
200+
handlers.append(getattr(plugin, 'handleEvent', None))
201+
if isinstance(event, MessageEvent) and hasattr(plugin, 'handleMessageEvent'):
202+
handlers.append(getattr(plugin, 'handleMessageEvent', None))
203+
if isinstance(event, GameEvent) and hasattr(plugin, 'handleGameEvent'):
204+
handlers.append(getattr(plugin, 'handleGameEvent', None))
205+
if isinstance(event, TrackerEvent) and hasattr(plugin, 'handleTrackerEvent'):
206+
handlers.append(getattr(plugin, 'handleTrackerEvent', None))
207+
if isinstance(event, AbilityEvent) and hasattr(plugin, 'handleAbilityEvent'):
208+
handlers.append(getattr(plugin, 'handleAbilityEvent', None))
209+
if isinstance(event, ControlGroupEvent) and hasattr(plugin, 'handleControlGroupEvent'):
210+
handlers.append(getattr(plugin, 'handleControlGroupEvent', None))
211+
if hasattr(plugin, 'handle'+event.name):
212+
handlers.append(getattr(plugin, 'handle'+event.name, None))
215213
return handlers
216-
217-
def _has_event_handler(self, plugin, event):
218-
return hasattr(plugin, 'handle'+event.name)
219-
220-
def _get_event_handler(self, plugin, event):
221-
return getattr(plugin, 'handle'+event.name, None)

sc2reader/engine/plugins/apm.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class APMTracker(object):
88
"""
99
Builds ``player.aps`` and ``player.apm`` dictionaries where an action is
10-
any Selection, Hotkey, or Ability event.
10+
any Selection, ControlGroup, or Ability event.
1111
1212
Also provides ``player.avg_apm`` which is defined as the sum of all the
1313
above actions divided by the number of seconds played by the player (not
@@ -23,7 +23,15 @@ def handleInitGame(self, event, replay):
2323
human.aps = defaultdict(int)
2424
human.seconds_played = replay.length.seconds
2525

26-
def handlePlayerActionEvent(self, event, replay):
26+
def handleControlGroupEvent(self, event, replay):
27+
event.player.aps[event.second] += 1
28+
event.player.apm[int(event.second/60)] += 1
29+
30+
def handleSelectionEvent(self, event, replay):
31+
event.player.aps[event.second] += 1
32+
event.player.apm[int(event.second/60)] += 1
33+
34+
def handleAbilityEvent(self, event, replay):
2735
event.player.aps[event.second] += 1
2836
event.player.apm[int(event.second/60)] += 1
2937

sc2reader/events/game.py

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class GameEvent(Event):
1313
"""
1414
This is the base class for all game events. The attributes below are universally available.
1515
"""
16-
name = 'GameEvent'
17-
1816
def __init__(self, frame, pid):
1917
#: The id of the player generating the event. This is 16 for global non-player events.
2018
#: Prior to Heart of the Swarm this was the player id. Since HotS it is
@@ -34,6 +32,9 @@ def __init__(self, frame, pid):
3432
#: A flag indicating if it is a local or global event.
3533
self.is_local = (pid != 16)
3634

35+
#: Short cut string for event class name
36+
self.name = self.__class__.__name__
37+
3738
def _str_prefix(self):
3839
player_name = self.player.name if getattr(self, 'pid', 16) != 16 else "Global"
3940
return "%s\t%-15s " % (Length(seconds=int(self.frame/16)), player_name)
@@ -47,9 +48,6 @@ class GameStartEvent(GameEvent):
4748
Recorded when the game starts and the frames start to roll. This is a global non-player
4849
event.
4950
"""
50-
51-
name = 'GameStartEvent'
52-
5351
def __init__(self, frame, pid, data):
5452
super(GameStartEvent, self).__init__(frame, pid)
5553

@@ -58,9 +56,6 @@ class PlayerLeaveEvent(GameEvent):
5856
"""
5957
Recorded when a player leaves the game.
6058
"""
61-
62-
name = 'PlayerLeaveEvent'
63-
6459
def __init__(self, frame, pid, data):
6560
super(PlayerLeaveEvent, self).__init__(frame, pid)
6661

@@ -70,9 +65,6 @@ class UserOptionsEvent(GameEvent):
7065
This event is recorded for each player at the very beginning of the game before the
7166
:class:`GameStartEvent`.
7267
"""
73-
74-
name = 'UserOptionsEvent'
75-
7668
def __init__(self, frame, pid, data):
7769
super(UserOptionsEvent, self).__init__(frame, pid)
7870
#:
@@ -118,12 +110,8 @@ def create_command_event(frame, pid, data):
118110
return SelfAbilityEvent(frame, pid, data)
119111

120112

121-
class PlayerActionEvent(GameEvent):
122-
name = 'PlayerActionEvent'
123-
124-
125113
@loggable
126-
class AbilityEvent(PlayerActionEvent):
114+
class AbilityEvent(GameEvent):
127115
"""
128116
Ability events are generated when ever a player in the game issues a command
129117
to a unit or group of units. They are split into three subclasses of ability,
@@ -133,11 +121,6 @@ class AbilityEvent(PlayerActionEvent):
133121
See :class:`LocationAbilityEvent`, :class:`TargetAbilityEvent`, and :class:`SelfAbilityEvent`
134122
for individual details.
135123
"""
136-
137-
name = 'AbilityEvent'
138-
139-
is_player_action = True
140-
141124
def __init__(self, frame, pid, data):
142125
super(AbilityEvent, self).__init__(frame, pid)
143126

@@ -236,9 +219,6 @@ class LocationAbilityEvent(AbilityEvent):
236219
Note that like all AbilityEvents, the event will be recorded regardless
237220
of whether or not the command was successful.
238221
"""
239-
240-
name = 'LocationAbilityEvent'
241-
242222
def __init__(self, frame, pid, data):
243223
super(LocationAbilityEvent, self).__init__(frame, pid, data)
244224

@@ -265,9 +245,6 @@ class TargetAbilityEvent(AbilityEvent):
265245
266246
Note that all AbilityEvents are recorded regardless of whether or not the command was successful.
267247
"""
268-
269-
name = 'TargetAbilityEvent'
270-
271248
def __init__(self, frame, pid, data):
272249
super(TargetAbilityEvent, self).__init__(frame, pid, data)
273250

@@ -318,9 +295,6 @@ class SelfAbilityEvent(AbilityEvent):
318295
319296
Note that all AbilityEvents are recorded regardless of whether or not the command was successful.
320297
"""
321-
322-
name = 'SelfAbilityEvent'
323-
324298
def __init__(self, frame, pid, data):
325299
super(SelfAbilityEvent, self).__init__(frame, pid, data)
326300

@@ -329,7 +303,7 @@ def __init__(self, frame, pid, data):
329303

330304

331305
@loggable
332-
class SelectionEvent(PlayerActionEvent):
306+
class SelectionEvent(GameEvent):
333307
"""
334308
Selection events are generated when ever the active selection of the
335309
player is updated. Unlike other game events, these events can also be
@@ -340,10 +314,6 @@ class SelectionEvent(PlayerActionEvent):
340314
by non-player actions. When a player action updates a control group
341315
a :class:`HotkeyEvent` is generated.
342316
"""
343-
344-
name = 'SelectionEvent'
345-
is_player_action = True
346-
347317
def __init__(self, frame, pid, data):
348318
super(SelectionEvent, self).__init__(frame, pid)
349319

@@ -392,39 +362,32 @@ def __str__(self):
392362
def create_control_group_event(frame, pid, data):
393363
update_type = data['control_group_update']
394364
if update_type == 0:
395-
return SetToHotkeyEvent(frame, pid, data)
365+
return SetControlGroupEvent(frame, pid, data)
396366
elif update_type == 1:
397-
return AddToHotkeyEvent(frame, pid, data)
367+
return AddToControlGroupEvent(frame, pid, data)
398368
elif update_type == 2:
399-
return GetFromHotkeyEvent(frame, pid, data)
369+
return GetControlGroupEvent(frame, pid, data)
400370
elif update_type == 3:
401371
# TODO: What could this be?!?
402-
return HotkeyEvent(frame, pid, data)
372+
return ControlGroupEvent(frame, pid, data)
403373

404374

405375
@loggable
406-
class HotkeyEvent(PlayerActionEvent):
376+
class ControlGroupEvent(GameEvent):
407377
"""
408-
Hotkey events are recorded when ever a player action modifies a control
409-
group. I know that calling control group events hotkey events doesn't make
410-
sense but for backwards compatibility I haven't changed it yet. Sorry.
411-
412-
There are three kinds of hotkey events, generated by each of the possible
378+
ControlGroup events are recorded when ever a player action modifies or accesses a control
379+
group. There are three kinds of events, generated by each of the possible
413380
player actions:
414381
415-
* :class:`SetToHotkeyEvent` - Recorded when a user sets a control group (ctrl+#).
416-
* :class:`GetFromHotkeyEvent` - Recorded when a user retrieves a control group (#).
417-
* :class:`AddToHotkeyEvent` - Recorded when a user adds to a control group (shift+ctrl+#)
382+
* :class:`SetControlGroup` - Recorded when a user sets a control group (ctrl+#).
383+
* :class:`GetControlGroup` - Recorded when a user retrieves a control group (#).
384+
* :class:`AddToControlGroup` - Recorded when a user adds to a control group (shift+ctrl+#)
418385
419386
All three events have the same set of data (shown below) but are interpretted differently.
420387
See the class entry for details.
421388
"""
422-
423-
name = 'HotkeyEvent'
424-
is_player_action = True
425-
426389
def __init__(self, frame, pid, data):
427-
super(HotkeyEvent, self).__init__(frame, pid)
390+
super(ControlGroupEvent, self).__init__(frame, pid)
428391

429392
#: Index to the control group being modified
430393
self.control_group = data['control_group_index']
@@ -445,38 +408,33 @@ def __init__(self, frame, pid, data):
445408
self.mask_data = data['remove_mask'][1]
446409

447410

448-
class SetToHotkeyEvent(HotkeyEvent):
411+
class SetControlGroupEvent(ControlGroupEvent):
449412
"""
450-
Extends :class:`HotkeyEvent`
413+
Extends :class:`ControlGroupEvent`
451414
452415
This event does a straight forward replace of the current control group contents
453416
with the player's current selection. This event doesn't have masks set.
454417
"""
455418

456-
name = 'SetToHotkeyEvent'
457419

458-
459-
class AddToHotkeyEvent(HotkeyEvent):
420+
class AddToControlGroupEvent(SetControlGroupEvent):
460421
"""
461-
Extends :class:`HotkeyEvent`
422+
Extends :class:`ControlGroupEvent`
462423
463424
This event adds the current selection to the control group.
464425
"""
465426

466-
name = 'AddToHotkeyEvent'
467-
468427

469-
class GetFromHotkeyEvent(HotkeyEvent):
428+
class GetControlGroupEvent(ControlGroupEvent):
470429
"""
471-
Extends :class:`HotkeyEvent`
430+
Extends :class:`ControlGroupEvent`
431+
472432
This event replaces the current selection with the contents of the control group.
473433
The mask data is used to limit that selection to units that are currently selectable.
474434
You might have 1 medivac and 8 marines on the control group but if the 8 marines are
475435
inside the medivac they cannot be part of your selection.
476436
"""
477437

478-
name = 'GetFromHotkeyEvent'
479-
480438

481439
@loggable
482440
class CameraEvent(GameEvent):
@@ -485,9 +443,6 @@ class CameraEvent(GameEvent):
485443
It does not matter why the camera changed, this event simply records the current
486444
state of the camera after changing.
487445
"""
488-
489-
name = 'CameraEvent'
490-
491446
def __init__(self, frame, pid, data):
492447
super(CameraEvent, self).__init__(frame, pid)
493448

@@ -515,8 +470,10 @@ def __str__(self):
515470

516471
@loggable
517472
class ResourceTradeEvent(GameEvent):
518-
name = 'ResourceTradeEvent'
519-
473+
"""
474+
Generated when a player trades resources with another player. But not when fullfulling
475+
resource requests.
476+
"""
520477
def __init__(self, frame, pid, data):
521478
super(ResourceTradeEvent, self).__init__(frame, pid)
522479

@@ -552,8 +509,9 @@ def __str__(self):
552509

553510

554511
class ResourceRequestEvent(GameEvent):
555-
name = 'ResourceRequestEvent'
556-
512+
"""
513+
Generated when a player creates a resource request.
514+
"""
557515
def __init__(self, frame, pid, data):
558516
super(ResourceRequestEvent, self).__init__(frame, pid)
559517

@@ -577,8 +535,9 @@ def __str__(self):
577535

578536

579537
class ResourceRequestFulfillEvent(GameEvent):
580-
name = 'ResourceRequestFulfillEvent'
581-
538+
"""
539+
Generated when a player accepts a resource request.
540+
"""
582541
def __init__(self, frame, pid, data):
583542
super(ResourceRequestFulfillEvent, self).__init__(frame, pid)
584543

@@ -587,8 +546,9 @@ def __init__(self, frame, pid, data):
587546

588547

589548
class ResourceRequestCancelEvent(GameEvent):
590-
name = 'ResourceRequestCancelEvent'
591-
549+
"""
550+
Generated when a player cancels their resource request.
551+
"""
592552
def __init__(self, frame, pid, data):
593553
super(ResourceRequestCancelEvent, self).__init__(frame, pid)
594554

0 commit comments

Comments
 (0)