Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. '''
  2. autosave.py
  3.  
  4. automatically save all existing PCs, every 5 mins. Extract those that are
  5. linkdead.
  6. '''
  7. import char, event, mudsys
  8.  
  9. # Get the autosave delay from settings, or, you know... not.
  10. try:
  11.     AUTOSAVE_DELAY = int(mudsys.sys_getval('AUTOSAVE_DELAY'))
  12. except:
  13.     AUTOSAVE_DELAY = 300
  14.  
  15. ## State
  16. scheduled = False
  17.    
  18. ## The Event
  19. def autosave(owner, data, arg):
  20.     ''' Iterate through every character in the game, saving any player
  21.        characters and forcing link-dead player characters to quit. '''
  22.     global scheduled
  23.     if not scheduled:
  24.         return
  25.    
  26.     for ch in char.char_list():
  27.         if ch.is_npc:
  28.             continue
  29.        
  30.         try:
  31.             # Save characters.
  32.             mudsys.do_save(ch)
  33.            
  34.             # Quit link-dead characters.
  35.             if ch.sock is None:
  36.                 mudsys.do_quit(ch)
  37.         except:
  38.             pass
  39.    
  40.     if AUTOSAVE_DELAY > 0 and owner is not cmd_autosave:
  41.         event.start_event(None, AUTOSAVE_DELAY, autosave)
  42.     else:
  43.         scheduled = False
  44.  
  45. ## State Stuff
  46. def start():
  47.     global scheduled
  48.  
  49.     if scheduled or AUTOSAVE_DELAY == 0:
  50.         return
  51.    
  52.     scheduled = True
  53.     event.start_event(None, AUTOSAVE_DELAY, autosave)
  54.  
  55. def stop():
  56.     global scheduled
  57.     scheduled = False
  58.  
  59. ## The Command
  60. def cmd_autosave(ch, cmd, arg):
  61.     ''' Usage: autosave <value|now>
  62.        
  63.        Change the autosave delay, which defaults to 300. If you specify a
  64.        value, that value will be used in the future. Specify 0 to disable
  65.        autosaving. If you supply the word 'now' instead of a numeric value,
  66.        an autosave will be carried out immediately. '''
  67.     global AUTOSAVE_DELAY
  68.    
  69.     if not arg:
  70.         if not AUTOSAVE_DELAY:
  71.             ch.send("Autosave is currently disabled.")
  72.         else:
  73.             ch.send("Characters will automatically be saved every %d seconds."%\
  74.                 AUTOSAVE_DELAY)
  75.         return
  76.    
  77.     if arg.lower() == 'now':
  78.         autosave(cmd_autosave, None, None)
  79.         return
  80.    
  81.     try:
  82.         arg = int(arg)
  83.     except:
  84.         ch.send("Invalid value! The value must be an integer or 'now'.")
  85.         return
  86.    
  87.     mudsys.sys_setval('AUTOSAVE_DELAY', str(arg))
  88.     AUTOSAVE_DELAY = arg
  89.    
  90.     if AUTOSAVE_DELAY and not scheduled:
  91.         start()
  92.    
  93.    
  94. ## Initialization
  95. mudsys.add_cmd("autosave", None, cmd_autosave, "admin", False)
  96. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement