def _Attack(self, targetPlayerId): """ Protected. Attack the target player with your weapon. """ if self.currentRoom: room = objmgr.GetObject(self.currentRoom) weapon = self._GetWeapon() if not weapon: request.ClientMessage(self.id, 'You have no weapon equipped.') return targetPlayer = objmgr.GetObject(targetPlayerId) if targetPlayer.currentRoom != self.currentRoom: request.ClientMessage(self.id, 'You are not in the same room as %s' % (targetPlayer.name)) return print 'here' # compute success import random if random.random() > weapon.tohit: # compute damage damage = random.randint(weapon.mindamage, weapon.maxdamage) # notify whoever cares; note that damage is an arg room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.ACTION_ATTACK_SUCCESS, self.id, targetPlayerId), 0.0, damage) request.ClientMessage(self.id, 'You attacked %s for %s damage.' % (targetPlayer.name, damage)) else: room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.ACTION_ATTACK_FAILURE, self.id, targetPlayerId), 0.0) request.ClientMessage(self.id, 'You missed.')
def _OnAttackMe(self, key, damage): """ Protected. Event handler activated when someone attacks me. """ srcname = objmgr.GetObject(key.src).name self.health -= damage if self.health <= 0: request.ClientMessage(self.id, 'You were killed by %s.' % (srcname,)) room = objmgr.GetObject(self.currentRoom) room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.PLAYER_DIES, self.id), 0.0) room.ExitRoom(self) else: request.ClientMessage(self.id, '%s just attacked you for %s.' % (srcname, damage))
def _OnPlaceItem(self, key): """ Private; always called in the base class; derived class can implement OnPlaceItem() (i.e. not virtual) to extend. """ print '_OnPlaceItem( %s )' % (key, ) player = objmgr.GetObject(key.src) item = objmgr.GetObject(key.subj) player._RemoveFromInventory(item) item._AssignOwner(None) self.__worldRegion.itemids.append(key.subj) if hasattr(self, 'OnPlaceItem'): self.OnPlaceItem(key)
def GetGameObject(self, obId): """ Return a protected game object from the world region, based on its object id. """ gameobject = objmgr.GetObject(obId) return _GetProtectedObject(gameobject)
def _OnWaveAtMe(self, key): """ Protected. Event handler activated when someone waves at me. """ srcname = objmgr.GetObject(key.src).name request.ClientMessage(self.id, '%s just waved at you.' % (srcname,))
def _Wave(self, targetPlayerId): """ Protected. Wave at the targetPlayer. """ if self.currentRoom: room = objmgr.GetObject(self.currentRoom) room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.ACTION_WAVE, self.id, targetPlayerId), 0.0)
def ClientMessage(playerid, msg): """ Sends a message to the player associates with the clientid using a deferred call. """ player = objmgr.GetObject(playerid) response = Response(player.clientid, msg) deferred.Call(gameserver.ID_SERVER, 'respond', (response, ))
def ShowInventory(clientid): """ Display your inventory. """ player = objmgr.GetPlayerByClientId(clientid) itemstr = 'You have:' + os.linesep for itemid in player.inventory: item = objmgr.GetObject(itemid) itemstr += '%s%s' % (item.name, os.linesep) return Response(clientid, itemstr)
def ExitRoom(clientid): """ Leave the current room. """ player = objmgr.GetPlayerByClientId(clientid) room = objmgr.GetObject(player.currentRoom) if room: room.ExitRoom(player) return Response(clientid, 'You have left %s' % (room.name, )) else: return Response(clientid, 'You are not in a room.')
def ListRooms(clientid): """ Return a list of available rooms. """ roomids = objmgr.GetRoomIds() roomstr = '' for roomid in roomids: room = objmgr.GetObject(roomid) roomstr += '%-10s- %s%s' % (room.name, room.sandbox.__doc__, os.linesep) return Response(clientid, 'Avaliable Rooms:' + os.linesep + roomstr)
def _OnPlayerDies(self, key): """ Notify the world when a player dies. """ print '_OnPlaceItem( %s )' % (key, ) player = objmgr.GetObject(key.src) for playerid in self.playerids: if playerid != key.subj: # don't notify player who died - he already knows self.ClientMessage(playerid, '%s has died.' % (player.GetName(), ))
def _ReleaseItem(self, item): """ Protected. Release ownership of an item. """ if self.currentRoom: room = objmgr.GetObject(self.currentRoom) if item.id in self.inventory: room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.ACTION_PLACE_OBJECT, self.id, item.id), 0.0) return 1 else: return 0
def _AcquireItem(self, item): """ Protected. Take ownership of an item. """ if self.currentRoom: room = objmgr.GetObject(self.currentRoom) if item.id in room.itemids: room.eventManager.PostGameEvent(gameeventkeys.GameEventKey(gameevents.ACTION_TAKE_OBJECT, self.id, item.id), 0.0) return 1 else: return 0
def __call__(self): """ Make this class a callable object (a.k.a. a functor), which acts just like a fuction or method. """ target = objmgr.GetObject(self.targetId) if target is not None: try: method = getattr(target, self.call) except AttributeError: print "No %s on %s" % (self.call, target) return apply(method, self.args) # make the call if self.callback is not None: # notify that call is complete apply(self.callback, self.cbArgs) else: print 'No valid target for %s' % (self, )
def _GetWeapon(self): """ Protected. Access our weapon. """ return objmgr.GetObject(self.weaponid)