-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpybw_event_handler.py
121 lines (112 loc) · 4.89 KB
/
pybw_event_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import pybw_swig
import traceback
import hotshot
class VerboseEventHandler(object):
def _announce(self, text):
pybw_swig.sendText(text[:80])
#print '>',text
def onConnect(self):
self.game = pybw_swig.getGame()
print "Connected to broodwar!"
def onDisconnect(self):
print "Disconnected from broodwar!"
def onMatchStart(self):
self._announce("The map is %s, a %d player map"%(self.game.mapName, len(list(self.game.startLocations))) )
if self.game.isReplay:
self._announce("List of players in this replay:")
for p in self.game.players:
if not p.isNeutral and not len(list(p.units)) == 0:
self._announce("* %s, playing %s"%(p.name, p.race.name))
else:
self._announce("*Match started, %s vs %s"%(self.game.self, self.game.enemy))
def onMatchEnd(self, is_winner):
print "Match ended%s" % ( ['.', ', I won!'][is_winner] )
def onSendText(self, text):
print "I said: '%s'" % text
def onReceiveText(self, player, text):
print "%s said: '%s'" % (player.name, text)
def onPlayerLeft(self, player):
print "OnPlayerLeft: %r" % player
def onUnitCreate(self, unit):
if self.game.getFrameCount() > 1:
# if we are in a replay, then we will print out the build order
if self.game.isReplay:
if (unit.type.isBuilding and unit.player.isNeutral == False):
seconds = self.game.frameCount / 24
self._announce("%.2d:%.2d: %s builds a %s"%(seconds/60, seconds%60, unit.player.name, unit.type.name))
else:
self._announce("onUnitCreate: %r" % unit)
def onUnitDestroy(self, unit):
self._announce("onUnitDestroy: %r" % unit)
def onSaveGame(self, gameName):
print "onSaveGame to", gameName
def onUnitMorph(self, unit):
# if we are in a replay, then we will print out the build order
if self.game.isReplay:
if (unit.type.isBuilding and unit.player.isNeutral == False):
seconds = self.game.frameCount / 24
self._announce("%.2d:%.2d: %s builds a %s"%(seconds/60, seconds%60, unit.player.name, unit.type.name))
else:
self._announce("onUnitMorph: %r" % unit)
class PyBW_EventHandler(object):
def __init__(self, broodwar, profile=False):
self.broodwar = broodwar
self.listeners = []
self.profile = profile
def _dispatchEvent(self, event, *args):
#~ print "_dispatchEvent:", event
for listener in self.listeners:
if hasattr(listener, event):
try:
f = getattr(listener, event)#(*args)
if self.profile and event=='onMatchFrame':
self.prof.runcall(f, *args)
else:
f(*args)
except Exception, e:
print "Error dispatching event %s to %s:"%( event, listener )
traceback.print_exc()
def onConnect(self):
if self.profile:
self.prof = hotshot.Profile("pybw.prof")
self._dispatchEvent("onConnect")
def onDisconnect(self):
self._dispatchEvent("onDisconnect")
if self.profile:
self.prof.close()
def onMatchStart(self):
self._dispatchEvent("onMatchStart")
def onMatchEnd(self, is_winner):
self._dispatchEvent("onMatchEnd", is_winner)
def onMatchFrame(self):
self._dispatchEvent("onMatchFrame")
def onMenuFrame(self):
self._dispatchEvent("onMenuFrame")
def onSendText(self, text):
self._dispatchEvent("onSendText", text)
def onReceiveText(self, player, text):
self._dispatchEvent("onReceiveText", text)
def onPlayerLeft(self, player):
self._dispatchEvent("onPlayerLeft", player)
def onNukeDetect(self, target):
self._dispatchEvent("onNukeDetect", target)
def onUnitDiscover(self, unit):
self._dispatchEvent("onUnitDiscover", unit)
def onUnitEvade(self, unit):
self._dispatchEvent("onUnitEvade", unit)
def onUnitShow(self, unit):
self._dispatchEvent("onUnitShow", unit)
def onUnitHide(self, unit):
self._dispatchEvent("onUnitHide", unit)
def onUnitCreate(self, unit):
self._dispatchEvent("onUnitCreate", unit)
def onUnitDestroy(self, unit):
self._dispatchEvent("onUnitDestroy", unit)
def onUnitMorph(self, unit):
self._dispatchEvent("onUnitMorph", unit)
def onUnitRenegade(self, unit):
self._dispatchEvent("onUnitRenegade", unit)
def onUnitComplete(self, unit):
self._dispatchEvent("onUnitComplete", unit)
def onSaveGame(self, gameName):
self._dispatchEvent("onSaveGame", gameName)