Moved login and disconnect from session-level to sessionhandler level to make the process cleaner with hooks rather than direct calls.

This commit is contained in:
Griatch 2013-02-17 18:48:48 +01:00
parent 261363bae7
commit 25505d69a6
7 changed files with 109 additions and 70 deletions

View file

@ -118,14 +118,12 @@ class Command(object):
# used by the help system to group commands in lists.
help_category = "general"
# this normally does not need to be changed. It allows to turn off
# auto-help entry creation for individual commands.
# This allows to turn off auto-help entry creation for individual commands.
auto_help = True
# There is also the property 'obj'. This gets set by the system
# on the fly to tie this particular command to a certain in-game entity.
# self.obj should NOT be defined here since it will not be overwritten
# if it already exists.
# auto-set (by Evennia on command instantiation) are:
# obj - which object this command is defined on
# sessid - which session-id (if any) is responsible for triggering this command
#
def __init__(self):
"the lockhandler works the same as for objects."
@ -190,6 +188,29 @@ class Command(object):
"""
return self.lockhandler.check(srcobj, access_type, default=default)
def msg(self, msg="", data=None, from_obj=None, to_obj=None, all_sessions=False):
"""
This is a shortcut instad of calling msg() directly on an object - it will
determine
detect if caller is an Object or a Player and also appends self.sessid
automatically.
msg - text string of message to send
data - optional dictionary of data
from_obj - source of message. Defaults to self.caller.
to_obj - target object of message. Defaults to self.caller
all_sessions (bool) - default is to send only to the session connected to
the target object
"""
from_obj = from_obj or self.caller
to_obj = to_obj or from_obj
if hasattr(to_obj, "sessid"):
sessid = all_sessions and None or to_obj.sessid
else:
sessid = None
to_obj.msg(msg, from_obj=from_obj, data=data, sessid=sessid)
# Common Command hooks
def at_pre_cmd(self):

View file

@ -388,7 +388,11 @@ class CmdQuit(MuxCommand):
Usage:
@quit
Gracefully disconnect from the game.
Switch:
all - disconnect all connected sessions
Gracefully disconnect your current session from the
game. Use the /all switch to disconnect from all sessions.
"""
key = "@quit"
locks = "cmd:all()"
@ -400,10 +404,20 @@ class CmdQuit(MuxCommand):
else:
player = self.caller
player.msg("{RQuitting{n. Hope to see you soon again.", sessid=self.sessid)
player.disconnect_session_from_player(self.sessid)
#for session in self.caller.sessions:
# session.session_disconnect()
if 'all' in self.switches:
player.msg("{RQuitting{n all sessions. Hope to see you soon again.", sessid=self.sessid)
for session in player.get_all_sessions():
player.disconnect_session_from_player(session.sessid)
else:
nsess = len(player.get_all_sessions())
if nsess == 2:
player.msg("{RQuitting{n. One session is still connected.", sessid=self.sessid)
elif nsess > 2:
player.msg("{RQuitting{n. %i session are still connected." % (nsess-1), sessid=self.sessid)
else:
# we are quitting the last available session
player.msg("{RQuitting{n. Hope to see you soon again.", sessid=self.sessid)
player.disconnect_session_from_player(self.sessid)
class CmdWho(MuxCommand):
"""

View file

@ -97,7 +97,7 @@ class CmdUnconnectedConnect(MuxCommand):
# at_pre_login()
# player.at_post_login() - calls look if no character is set
# character.at_post_login() - this calls look command by default
session.session_login(player)
session.sessionhandler.login(session, player)
class CmdUnconnectedCreate(MuxCommand):
"""
@ -224,8 +224,8 @@ class CmdUnconnectedQuit(MuxCommand):
def func(self):
"Simply close the connection."
session = self.caller
session.msg("Good bye! Disconnecting ...")
session.session_disconnect()
#session.msg("Good bye! Disconnecting ...")
session.sessionhandler.disconnect(session, "Good bye! Disconnecting ...")
class CmdUnconnectedLook(MuxCommand):
"""