Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- autosave.py
- automatically save all existing PCs, every 5 mins. Extract those that are
- linkdead.
- '''
- import char, event, mudsys
- # Get the autosave delay from settings, or, you know... not.
- try:
- AUTOSAVE_DELAY = int(mudsys.sys_getval('AUTOSAVE_DELAY'))
- except:
- AUTOSAVE_DELAY = 300
- ## State
- scheduled = False
- ## The Event
- def autosave(owner, data, arg):
- ''' Iterate through every character in the game, saving any player
- characters and forcing link-dead player characters to quit. '''
- global scheduled
- if not scheduled:
- return
- for ch in char.char_list():
- if ch.is_npc:
- continue
- try:
- # Save characters.
- mudsys.do_save(ch)
- # Quit link-dead characters.
- if ch.sock is None:
- mudsys.do_quit(ch)
- except:
- pass
- if AUTOSAVE_DELAY > 0 and owner is not cmd_autosave:
- event.start_event(None, AUTOSAVE_DELAY, autosave)
- else:
- scheduled = False
- ## State Stuff
- def start():
- global scheduled
- if scheduled or AUTOSAVE_DELAY == 0:
- return
- scheduled = True
- event.start_event(None, AUTOSAVE_DELAY, autosave)
- def stop():
- global scheduled
- scheduled = False
- ## The Command
- def cmd_autosave(ch, cmd, arg):
- ''' Usage: autosave <value|now>
- Change the autosave delay, which defaults to 300. If you specify a
- value, that value will be used in the future. Specify 0 to disable
- autosaving. If you supply the word 'now' instead of a numeric value,
- an autosave will be carried out immediately. '''
- global AUTOSAVE_DELAY
- if not arg:
- if not AUTOSAVE_DELAY:
- ch.send("Autosave is currently disabled.")
- else:
- ch.send("Characters will automatically be saved every %d seconds."%\
- AUTOSAVE_DELAY)
- return
- if arg.lower() == 'now':
- autosave(cmd_autosave, None, None)
- return
- try:
- arg = int(arg)
- except:
- ch.send("Invalid value! The value must be an integer or 'now'.")
- return
- mudsys.sys_setval('AUTOSAVE_DELAY', str(arg))
- AUTOSAVE_DELAY = arg
- if AUTOSAVE_DELAY and not scheduled:
- start()
- ## Initialization
- mudsys.add_cmd("autosave", None, cmd_autosave, "admin", False)
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement