metamorph.py
上传用户:ghyvgy
上传日期:2009-05-26
资源大小:547k
文件大小:2k
源码类别:

其他游戏

开发平台:

Python

  1. from twisted.spread.pb import Perspective, Service
  2. class Bug(Perspective):
  3.     angst = 0
  4.     def attached(self, remoteBugWatcher, identity):
  5.         self.remoteBugWatcher = remoteBugWatcher
  6.         self.psychologist = None
  7.         self.angst += 5 # It's hard to get up in the morning.
  8.         return Perspective.attached(self, remoteBugWatcher, identity)
  9.     def detached(self, remoteBugWatcher, identity):
  10.         self.remoteBugWatcher = None
  11.         if self.psychologist:
  12.             self.psychologist.leaveTherapy(self)
  13.             self.psychologist = None
  14.         return Perspective.detached(self, remoteBugWatcher, identity)
  15.     # Remote methods.
  16.     def perspective_goToPsychologist(self, name):
  17.         psychologist = self.service.psychologists.get(name)
  18.         if psychologist:
  19.             psychologist.joinTherapy(self)
  20.             self.psychologist = psychologist
  21.             return "You made it to group therapy."
  22.         else:
  23.             return "There's no such psychologist."
  24.     def perspective_psychoanalyze(self):
  25.         if self.psychologist:
  26.             self.psychologist.requestTherapy(self)
  27.             return "Therapy requested!"
  28.         else:
  29.             return "You're not near a therapist."
  30.     # Local methods.
  31.     def heal(self, points):
  32.         self.angst -= points
  33.         if points > 0:
  34.             feeling = "better"
  35.         else:
  36.             feeling = "worse"
  37.         self.remoteBugWatcher.callRemote(
  38.             "healed",
  39.             "You feel %s points %s.  Your angst is now: %s." %
  40.             (abs(points), feeling, self.angst))
  41. class Psychologist:
  42.     def __init__(self, name, skill, world):
  43.         self.name = name
  44.         self.skill = skill
  45.         self.group = []
  46.         world.psychologists[name] = self
  47.     def joinTherapy(self, bug):
  48.         self.group.append(bug)
  49.     def leaveTherapy(self, bug):
  50.         self.group.remove(bug)
  51.     def requestTherapy(self, bug):
  52.         for bug in self.group:
  53.             bug.heal(self.skill + len(self.group))
  54. class BuggyWorld(Service):
  55.     perspectiveClass = Bug
  56.     def __init__(self, *args, **kw):
  57.         Service.__init__(self, *args, **kw)
  58.         self.psychologists = {}
  59.         Psychologist("Freud", -5, self)
  60.         Psychologist("Pavlov", 1, self)
  61.         Psychologist("The Tick", 10, self)