Advertisement
KirillMysnik

Untitled

May 19th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1.  
  2. """
  3. First advantage of using stages:
  4. There're 3 methods that handle the flow of the stages.
  5. set_stage_group - erases current stage queue and starts a new stage group;
  6. insert_stage_group - inserts a stage group to the begginning of the queue;
  7. append_stage_group - appends;
  8.  
  9. You can't achieve this behaviour with methods.
  10. I mean, you can achieve insert_stage_group by calling some method directly
  11. - that way it is executed before others.
  12.  
  13. If you do
  14. """
  15.  
  16. def game_start(self):
  17.     self.spawn_players()
  18.     self.setup_damage_hooks()
  19.  
  20.  
  21. """
  22. And spawn_players suddenly decides that it ran out of spawnpoints, with stages I would just do
  23. """
  24.  
  25. set_stage_group('destroy')      # Erases the queue, so setup_damage_hooks won't get executed
  26.  
  27.  
  28. """
  29. with methods you can't cancel those that will be executed later
  30.  
  31.  
  32. Second advantage of using stages:
  33. You can undo them. There's a method .undo_stages that undoes all stages that have been executed
  34. Why would I need it? Imagine you have an 'destroy' method/stage. It should stop the game from any
  35. stage it is currently at.
  36.  
  37. """
  38.  
  39. def game_start(self):
  40.     self.spawn_players()
  41.     self.setup_damage_hooks()
  42.     self.register_event_handlers()
  43.  
  44. """ Now, with methods you would probably do this """
  45.  
  46. def destroy(self):
  47.     self.undo_spawn_players()   # Teleports players back to their positions
  48.     self.undo_setup_damage_hooks()    # Obviously removes the hooks
  49.     self.undo_register_event_handlers()     # Unregisters event handlers
  50.  
  51. """
  52. Works fine when the game is started. Players are teleported from game area,
  53. damage hooks are unset, event handlers are unregistered.
  54.  
  55. But what if .spawn_players decides to destroy the game? You only need to teleport the players back,
  56. but trying to remove the hooks and unregister event handlers will raise an exception -
  57. because those have not been registered yet.
  58.  
  59. Then you need to keep a track of methods that have been executed:
  60. """
  61.  
  62. def destroy(self):
  63.     if self._players_were_spawned:
  64.         self.undo_spawn_players()
  65.    
  66.     if self._hooks_were_set_up:
  67.         self.undo_setup_damage_hooks()
  68.    
  69.     if self._handlers_were_registered:
  70.         self.undo_register_event_handlers()
  71.  
  72. """
  73. or implement different 'destroy' methods:
  74. """
  75.  
  76. def destroy_all(self): ...
  77. def destroy_all_but_hooks_and_handlers(self): ...
  78. def destroy_all_but_handlers(self): ...
  79.  
  80. """
  81. But what if something external tries to destroy the game? It will need to know what method to call.
  82.  
  83.  
  84. With stages I just do
  85. """
  86.  
  87. @stage('survival-equip-damage-hooks')
  88. def stage_survival_equip_damage_hooks(self):
  89.     # ...
  90.  
  91. @stage('undo-survival-equip-damage-hooks')
  92. def stage_undo_survival_equip_damage_hooks(self):
  93.     # ...
  94.  
  95. """
  96. Any 'undo-X' stage will be called when the game is being destroyed and stage 'X' has already been executed.
  97.  
  98. .undo_stages also receives optional argument of names of exact stage groups that need to be cancelled
  99. - so the game is not aborted completely.
  100.  
  101. For example, I have a group of stages that defines preparation of the game (countdown, players freezing)
  102. and to end this preparation I just call .undo_stages - countdown stops, players are unfrozen.
  103. And so it will be stopped successfully even if the game is being destroyed during its preparation
  104. - countdown stops (if it has been started), players are unfrozen (if they were frozen).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement