def PostObj(self, cmd): if cmd.verb == pub.verbs.give: if cmd.dirobj.name == "gold coin": cmd.Tell("As you place the coin in the gargoyle's palm, \ its face comes to life. It cocks one bulging eye at the coin for a moment, \ then nods approvingly. With the help of a clawed finger, it begins to \ count -- but, finding only one coin in its palm, it stops abruptly and \ throws the coin down in disgust.") cmd.dirobj.MoveTo(self.container) pub.scheduler.AddEvent(3, pub.Event(self, 'object.Freeze()')) if cmd.dirobj.synonyms[0] == "tin coin": if self.CanSee(cmd.dirobj): cmd.Tell( "As you place the false coinage in the gargoyle's \ hand, its face springs to life. It cocks a bulging eye at your offering. \ Noting the dull grey color and featureless surfaces, its eyes grow wide. \ With an angry flick of its arm, it hurls " + cmd.dirobj(the) + " over your head.") pub.scheduler.AddEvent(3,pub.Event(self, \ 'object.Throw("'+cmd.dirobj.name+'")')) pub.scheduler.AddEvent(4, pub.Event(self, 'object.Freeze()')) else: #---- tin coin in the dark ----# if cmd.dirobj.quantity > 1: shapes = "shapes" their = "their" coins = "coins" else: shapes = "shape" their = "its" coins = "coin" cmd.Tell("As you place " + cmd.dirobj(the) + " into the \ gargoyle's glowing hand, its face comes to life once again. It cocks an \ eye at the dark round " + shapes + " in its hand. Frowning, it turns them over \ and appears to be judging " + their + " weight. With a shrug, it lifts a \ clawed finger and begins counting.") if cmd.dirobj.quantity < 3: cmd.Tell("Finding only "+str(cmd.dirobj.quantity) \ + ' ' + coins + ", it stops abruptly and throws the "+ coins + " down in \ disgust." ) cmd.dirobj.MoveTo(self.container) pub.scheduler.AddEvent( 3, pub.Event(self, 'object.Freeze()')) else: cmd.Tell("It stabs its finger at the coins three \ times, and compares this to the three clawed toes on its foot. Apparently \ satisfied, the gargoyle opens its mouth and tosses the coins down.") pub.scheduler.AddEvent( 3, pub.Event(self, 'object.Open()')) return CANCEL return OK
def Open(self): self.Announce("Grinning, the gargoyle splits in half, right down \ the middle. His two halves separate and slide apart, taking the two halves \ of the great stone door with them. A dark passageway extends beyond the \ door to the north.") if self.door: self.door.open = TRUE self.closedDesc = self.desc self.desc = self.openDesc pub.scheduler.AddEvent(10, pub.Event(self, 'object.Close()'))
def do(self, cmd): """ schedule command for execution, if actor isn't busy initiate immediatly """ if cmd.actor.busytill > pub.scheduler.minutes: pub.scheduler.AddAbsEvent(cmd.actor.busytill, \ pub.Event(self, 'object.begin(cmd)',cmd) ) return # if actor is not busy, then begin command immediatly if self.begin(cmd): cmd.tell()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #---------------------------------------------------------------------- # Create some objects # #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #---------------------------------------------------------------------- # Run the game # #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - print print " YOUR GAME TITLE" print " by You\n" print "\nHere's the intro blurb (i.e. prologue) to your game. Make it \ as long as you like, wrapping the words with a backslash if you want." raw_input("\n\n[Press Return.]") # wait for Return print "\n\n\n" # tell the player the current room description player.Tell(player.container.GetDesc(player)) # run the game until it's no longer RUNNING while pub.gameStatus == RUNNING: if not pub.scheduler.events: pub.scheduler.AddEvent( 0, pub.Event(player, 'object.Act()') ) pub.scheduler.Update()
def handle_request(savedir, title='PUB', header='', midder='', footer='', updates=3): """ Handle the CGI request. """ global id, sid, storagedir, restored, max_saved_game_age, max_tmp_game_age # buffer stdout to catch print/debug/warnings w/in pub b4 CGI header stdout = sys.stdout sys.stdout = StringIO() # find script name to be used in the html form action attribute scriptname = os.environ.get('SCRIPT_NAME', '.') # check savedir if not isdir(savedir): raise "Invalid savedir passed to pubcgi.handle_request" if not savedir[-1] in ['/', '\\']: savedir = savedir + os.path.sep storagedir = savedir # get session id from cookie if available cookie = Cookie.SimpleCookie() cookie.load(os.environ.get('HTTP_COOKIE','')) if cookie.has_key(id): sid = cookie[id].value if sid and isfile(storagedir + str(sid)): restoregame(storagedir + sid, TRUE) else: sid = md5(str(time.time() * random.randint(0, 1000000))).hexdigest() pub.player.Tell(pub.player.container.GetDesc(pub.player)) # get command form = cgi.FieldStorage() command = form.getvalue('command','') if command: pub.player.HandleMsg(command) pub.scheduler.AddEvent( 0, pub.Event(pub.player, 'object.Act()') ) # there is probably a better way to handle this # once I learn more about the scheduler? for i in xrange(updates): pub.scheduler.Update() # there is probably a better way to do this, too if restored: restoregame(storagedir + sid, TRUE) pub.player.Tell('Game restored....') pub.player.Tell(pub.player.container.GetDesc(pub.player)) sid = sid[1:] # get text text = string.join(pub.player.outbuf) pub.player.outbuf = [] if pub.gameStatus != RUNNING: if isfile(storagedir + sid): remove(storagedir + sid) else: savegame(storagedir + sid, TRUE) #os.chmod(storagedir + sid, 0664) # reassign stdout printed = sys.stdout.getvalue() if printed: # possible log, but for now we'll just ... pass sys.stdout = stdout # return output #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # return header # set cookie cookie[id]=sid print cookie print 'Content-type: text/html\n\n' if not header: header = """ <html> <head> <title>%s</title> </head> <body onLoad="document.forms[0].command.focus()"> <center> <h3>%s</h3> <table border="0" width="75%%"><tr><td> <hr> """ % (title, title) if not midder: midder = "<br><center>" if not footer: footer = """ <div class="commands"> <a href="gredgar.cgi?command=look">Look</a> • <a href="gredgar.cgi?command=inventory">Inventory</a> • <a href="gredgar.cgi?command=help">Help</a> • <a href="gredgar.cgi?command=save">Save</a> • <a href="gredgar.cgi?command=quit">Quit</a> </div> </center> <br> <hr> </td></tr></table> </center> </body> </html> """ # return page print header print """ <form action="%s" method="post"> %s %s <input type="text" id="command" name="command" size="40" maxlength="80"> <input id="submit" type="submit" value="Submit"> </form> """ % (scriptname, text, midder) print footer # periodically, delete old games if (time.localtime()[5] + 1) % 60 == 0: import glob cleanupsaves(glob.glob(storagedir + '[0-9a-f]*'), max_tmp_game_age) cleanupsaves(glob.glob(storagedir + 'g[0-9a-f]*'), max_saved_game_age)
def Close(self): self.Announce("The gargoyle door slides shut again, and the two \ halves of the gargoyle fuse with an evil grin.") if self.door: self.door.open = FALSE self.desc = self.closedDesc pub.scheduler.AddEvent(2, pub.Event(self, 'object.Freeze()'))
def setUp(self): """Create some objects used by several methods of TestScheduler.""" self.scheduler = pub.Scheduler() self.obj = pub.BaseThing('obj') self.event = pub.Event(self.obj, "")
def setUp(self): """Create some objects""" self.obj = pub.BaseThing('shrubbery') self.event = pub.Event(self.obj, 'self.x = 2')