events.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <html>
  2. <head>
  3. <link href="../tutorial.css" rel="stylesheet" type="text/css">
  4. </head>
  5. <body>
  6. <div class="header">
  7. The NakedMud Tutorial :: Events
  8. </div>
  9. <!-- content starts here -->
  10. <div class="content-wrap"><div class="content-body-wrap"><div class="content">
  11. <div class="head">Events</div>
  12. <div class="info">
  13. NakedMud provides support for delayed events. These are simply functions that
  14. are called after a pre-specified amount of time. They are not to be confused
  15. with Events in, say, Java, which refer to specific things that take place
  16. (such as someone clicking on a mouse) which have a set of 'listeners' that
  17. execute some function once the event occurs. NakedMud supports this sort of
  18. behavior as well, in the form of hooks.
  19. <p></p>
  20. Events have three parameters. An owner, data, and a string argument, each of
  21. which might possibly be None. The owner is the person, place, or thing the event
  22. belongs to. If the owner ever leaves the game, the event is canceled. The
  23. owner can be None. For instance, you may want to write a combat update event
  24. that iterates across all characters and make them perform an attack every
  25. second. To do this, you could start the event up without an owner when your
  26. module is initialized, and have the event do its work, and re-start itself
  27. whenever it is called.
  28. </div>
  29. <div class="head">Delayed Commands</div>
  30. <div class="info">
  31. The most basic thing that could be done with events is creating a command that
  32. delays another command for a specified amount of time. Here is how something
  33. like that would be written:
  34. <pre class="code">
  35. import mudsys, event, mud
  36. def delayed_cmd_event(owner, data, arg):
  37. ch.act(arg)
  38. def cmd_delay(ch, cmd, arg):
  39. '''attempt to delay a command by a specified amount of time.'''
  40. try:
  41. # take two arguments: the delay time, and the command to execute
  42. time, todo = mud.parse_args(ch, True, cmd, arg, "int(time) string(cmd)")
  43. except: return
  44. # notify the character that their command has been delayed,
  45. # and start the event handler
  46. ch.send("In %d seconds you will perform '%s'." % (time, todo))
  47. event.start_event(ch, time, delayed_cmd_event, None, todo)
  48. mudsys.add_cmd("delay", None, cmd_delay, "player", False)
  49. </pre>
  50. <p></p>
  51. Data can be anything you want it to be. Suppose you want to implement a delayed
  52. fireball spell that hurts everyone in the room except for the person who set it
  53. up. The owner would be the room, and the data might be the character. You could
  54. possibly do it as the owner being the character and the data being None, but
  55. think what might happen if the character decided to move before the event went
  56. off! Here is one way to do a delayed fireball event
  57. <pre class="code">
  58. import mudsys, event
  59. def delayed_fireball_event(room, caster, arg):
  60. '''do the delayed fireball event. Hurt everyone in the room except the
  61. caster. arg is None.'''
  62. room.send("With violent force, a delayed fireball explodes in the room!")
  63. for ch in room:
  64. if ch != caster:
  65. # do something mean here
  66. ############
  67. # FINISH ME
  68. ############
  69. def cmd_delayed_fireball(ch, cmd, arg):
  70. '''cast a delayed fireball.'''
  71. ch.send("You set up a delayed fireball to go off in 5 seconds!")
  72. event.start_event(ch.room, 5, delayed_fireball_event, ch, None)
  73. mudsys.add_cmd("df", None, cmd_delayed_fireball, "player", False)
  74. </pre>
  75. </div>
  76. <div class="head">Writing Handlers</div>
  77. <div class="info">
  78. Suppose you want some sort of handler to run on a periodic basis. Maybe you want
  79. everyone in combat to attack each second, or maybe you want to extract linkdead
  80. characters every five minutes. Periodic handlers can be created with events that
  81. requeue theirselves. Here is an example of an event that saves characters every
  82. 5 minutes, and extracts those that are linkdead:
  83. <pre class="code">
  84. '''
  85. autosave.py
  86. automatically save all existing PCs, every 5 mins. Extract those that are
  87. linkdead.
  88. '''
  89. import mudsys, event, mud
  90. import char as mudchar
  91. __autosave_delay__ = 5 * 60
  92. def autosave_event(owner, data, arg):
  93. '''go through all our characters and save them. Then, force-quit them if
  94. they are linkdead.'''
  95. try:
  96. for ch in mudchar.char_list():
  97. if ch.is_pc:
  98. mudsys.do_save(ch)
  99. if ch.socket == None:
  100. mudsys.do_quit(ch)
  101. except: pass
  102. # make sure we requeue the event outside the body of the try block.
  103. # this is just so that if something goes wrong with the event, the event
  104. # is still requeued.
  105. event.start_event(None, __autosave_delay__, autosave_event)
  106. event.start_event(None, __autosave_delay__, autosave_event)
  107. </pre>
  108. </div>
  109. <div class="head">Starting and Stopping</div>
  110. <div class="info">
  111. Once an event is started, it cannot be canceled unless its owner is removed
  112. from the game, so be certain to design your events with this in mind.
  113. <p></p>
  114. Events are started with a call to start_event. This function takes three
  115. mandatory arguments, and two optional ones. The first argument is the owner,
  116. the second is the time to delay the event, and the third is the event itself.
  117. The fourth and fifth are the data and the string argument, respectively.
  118. </div>
  119. <!-- content ends here-->
  120. </div></div></div>
  121. <!-- navigation starts here -->
  122. <div class="nav-wrap"><div class="nav">
  123. <iframe src="nav.html" height="100%" width="100%" scrolling=no frameborder=0>
  124. </iframe>
  125. <!-- navigation ends here -->
  126. </div></div>
  127. <!--div class="footer">Edit Date: Nov 15, 2008. By Geoff Hollis</div-->
  128. </body>
  129. </html>