<s> """<STR_LIT>""" <EOL> from builtins import object <EOL> from future . utils import listvalues <EOL> from time import time <EOL> from django . conf import settings <EOL> from evennia . commands . cmdhandler import CMD_LOGINSTART <EOL> from evennia . utils . utils import variable_from_module , is_iter , to_str , to_unicode , strip_control_sequences , make_iter <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> _PlayerDB = None <EOL> _ServerSession = None <EOL> _ServerConfig = None <EOL> _ScriptDB = None <EOL> _OOB_HANDLER = None <EOL> class DummySession ( object ) : <EOL> sessid = <NUM_LIT:0> <EOL> DUMMYSESSION = DummySession ( ) <EOL> PCONN = chr ( <NUM_LIT:1> ) <EOL> PDISCONN = chr ( <NUM_LIT:2> ) <EOL> PSYNC = chr ( <NUM_LIT:3> ) <EOL> SLOGIN = chr ( <NUM_LIT:4> ) <EOL> SDISCONN = chr ( <NUM_LIT:5> ) <EOL> SDISCONNALL = chr ( <NUM_LIT:6> ) <EOL> SSHUTD = chr ( <NUM_LIT:7> ) <EOL> SSYNC = chr ( <NUM_LIT:8> ) <EOL> SCONN = chr ( <NUM_LIT:11> ) <EOL> PCONNSYNC = chr ( <NUM_LIT:12> ) <EOL> from django . utils . translation import ugettext as _ <EOL> _SERVERNAME = settings . SERVERNAME <EOL> _MULTISESSION_MODE = settings . MULTISESSION_MODE <EOL> _IDLE_TIMEOUT = settings . IDLE_TIMEOUT <EOL> _MAX_SERVER_COMMANDS_PER_SECOND = <NUM_LIT> <EOL> _MAX_SESSION_COMMANDS_PER_SECOND = <NUM_LIT> <EOL> def delayed_import ( ) : <EOL> """<STR_LIT>""" <EOL> global _ServerSession , _PlayerDB , _ServerConfig , _ScriptDB <EOL> if not _ServerSession : <EOL> modulename , classname = settings . SERVER_SESSION_CLASS . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> _ServerSession = variable_from_module ( modulename , classname ) <EOL> if not _PlayerDB : <EOL> from evennia . players . models import PlayerDB as _PlayerDB <EOL> if not _ServerConfig : <EOL> from evennia . server . models import ServerConfig as _ServerConfig <EOL> if not _ScriptDB : <EOL> from evennia . scripts . models import ScriptDB as _ScriptDB <EOL> _ServerSession , _PlayerDB , _ServerConfig , _ScriptDB <EOL> class SessionHandler ( dict ) : <EOL> """<STR_LIT>""" <EOL> def get_sessions ( self , include_unloggedin = False ) : <EOL> """<STR_LIT>""" <EOL> if include_unloggedin : <EOL> return listvalues ( self ) <EOL> else : <EOL> return [ session for session in self . values ( ) if session . logged_in ] <EOL> def get_all_sync_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( sessid , sess . get_sync_data ( ) ) for sessid , sess in self . items ( ) ) <EOL> class ServerSessionHandler ( SessionHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( ServerSessionHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> self . server = None <EOL> self . server_data = { "<STR_LIT>" : _SERVERNAME } <EOL> def portal_connect ( self , portalsessiondata ) : <EOL> """<STR_LIT>""" <EOL> delayed_import ( ) <EOL> global _ServerSession , _PlayerDB , _ScriptDB <EOL> sess = _ServerSession ( ) <EOL> sess . sessionhandler = self <EOL> sess . load_sync_data ( portalsessiondata ) <EOL> if sess . logged_in and sess . uid : <EOL> sess . player = _PlayerDB . objects . get_player_from_uid ( sess . uid ) <EOL> sess . at_sync ( ) <EOL> _ScriptDB . objects . validate ( ) <EOL> self [ sess . sessid ] = sess <EOL> sess . data_in ( CMD_LOGINSTART ) <EOL> def portal_session_sync ( self , portalsessiondata ) : <EOL> """<STR_LIT>""" <EOL> sessid = portalsessiondata . get ( "<STR_LIT>" ) <EOL> session = self . get ( sessid ) <EOL> if session : <EOL> session . load_sync_data ( portalsessiondata ) <EOL> def portal_sessions_sync ( self , portalsessionsdata ) : <EOL> """<STR_LIT>""" <EOL> delayed_import ( ) <EOL> global _ServerSession , _PlayerDB , _ServerConfig , _ScriptDB <EOL> for sess in self . values ( ) : <EOL> del sess <EOL> for sessid , sessdict in portalsessionsdata . items ( ) : <EOL> sess = _ServerSession ( ) <EOL> sess . sessionhandler = self <EOL> sess . load_sync_data ( sessdict ) <EOL> if sess . uid : <EOL> sess . player = _PlayerDB . objects . get_player_from_uid ( sess . uid ) <EOL> self [ sessid ] = sess <EOL> sess . at_sync ( ) <EOL> init_mode = _ServerConfig . objects . conf ( "<STR_LIT>" , default = None ) <EOL> _ScriptDB . objects . validate ( init_mode = init_mode ) <EOL> _ServerConfig . objects . conf ( "<STR_LIT>" , delete = True ) <EOL> self . announce_all ( _ ( "<STR_LIT>" ) ) <EOL> def start_bot_session ( self , protocol_path , configdict ) : <EOL> """<STR_LIT>""" <EOL> self . server . amp_protocol . send_AdminServer2Portal ( DUMMYSESSION , operation = SCONN , <EOL> protocol_path = protocol_path , config = configdict ) <EOL> def portal_shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> self . server . amp_protocol . send_AdminServer2Portal ( DUMMYSESSION , <EOL> operation = SSHUTD ) <EOL> def login ( self , session , player , testmode = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . sessions_from_player ( player ) : <EOL> player . is_connected = True <EOL> session . at_login ( player ) <EOL> player . at_init ( ) <EOL> if player . db . FIRST_LOGIN : <EOL> player . at_first_login ( ) <EOL> del player . db . FIRST_LOGIN <EOL> player . at_pre_login ( ) <EOL> if _MULTISESSION_MODE == <NUM_LIT:0> : <EOL> self . disconnect_duplicate_sessions ( session ) <EOL> nsess = len ( self . sessions_from_player ( player ) ) <EOL> string = "<STR_LIT>" <EOL> string = string . format ( player = player , address = session . address , nsessions = nsess ) <EOL> session . log ( string ) <EOL> session . logged_in = True <EOL> if not testmode : <EOL> self . server . amp_protocol . send_AdminServer2Portal ( session , <EOL> operation = SLOGIN , <EOL> sessiondata = { "<STR_LIT>" : True } ) <EOL> player . at_post_login ( session = session ) <EOL> def disconnect ( self , session , reason = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> session = self . get ( session . sessid ) <EOL> if not session : <EOL> return <EOL> if hasattr ( session , "<STR_LIT>" ) and session . player : <EOL> nsess = len ( self . sessions_from_player ( session . player ) ) - <NUM_LIT:1> <EOL> string = "<STR_LIT>" <EOL> string = string . format ( player = session . player , address = session . address , nsessions = nsess ) <EOL> session . log ( string ) <EOL> session . at_disconnect ( ) <EOL> sessid = session . sessid <EOL> del self [ sessid ] <EOL> self . server . amp_protocol . send_AdminServer2Portal ( session , <EOL> operation = SDISCONN , <EOL> reason = reason ) <EOL> def all_sessions_portal_sync ( self ) : <EOL> """<STR_LIT>""" <EOL> sessdata = self . get_all_sync_data ( ) <EOL> return self . server . amp_protocol . send_AdminServer2Portal ( DUMMYSESSION , <EOL> operation = SSYNC , <EOL> sessiondata = sessdata ) <EOL> def disconnect_all_sessions ( self , reason = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> for session in self : <EOL> del session <EOL> self . server . amp_protocol . send_AdminServer2Portal ( DUMMYSESSION , <EOL> operation = SDISCONNALL , <EOL> reason = reason ) <EOL> def disconnect_duplicate_sessions ( self , curr_session , <EOL> reason = _ ( "<STR_LIT>" ) ) : <EOL> """<STR_LIT>""" <EOL> uid = curr_session . uid <EOL> doublet_sessions = [ sess for sess in self . values ( ) <EOL> if sess . logged_in <EOL> and sess . uid == uid <EOL> and sess != curr_session ] <EOL> for session in doublet_sessions : <EOL> self . disconnect ( session , reason ) <EOL> def validate_sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> tcurr = time ( ) <EOL> reason = _ ( "<STR_LIT>" ) <EOL> for session in ( session for session in self . values ( ) <EOL> if session . logged_in and _IDLE_TIMEOUT > <NUM_LIT:0> <EOL> and ( tcurr - session . cmd_last ) > _IDLE_TIMEOUT ) : <EOL> self . disconnect ( session , reason = reason ) <EOL> def player_count ( self ) : <EOL> """<STR_LIT>""" <EOL> return len ( set ( session . uid for session in self . values ( ) if session . logged_in ) ) <EOL> def all_connected_players ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( set ( session . player for session in self . values ( ) if session . logged_in and session . player ) ) <EOL> def session_from_sessid ( self , sessid ) : <EOL> """<STR_LIT>""" <EOL> if is_iter ( sessid ) : <EOL> return [ self . get ( sid ) for sid in sessid if sid in self ] <EOL> return self . get ( sessid ) <EOL> def session_from_player ( self , player , sessid ) : <EOL> """<STR_LIT>""" <EOL> sessions = [ self [ sid ] for sid in make_iter ( sessid ) <EOL> if sid in self and self [ sid ] . logged_in and player . uid == self [ sid ] . uid ] <EOL> return sessions [ <NUM_LIT:0> ] if len ( sessions ) == <NUM_LIT:1> else sessions <EOL> def sessions_from_player ( self , player ) : <EOL> """<STR_LIT>""" <EOL> uid = player . uid <EOL> return [ session for session in self . values ( ) if session . logged_in and session . uid == uid ] <EOL> def sessions_from_puppet ( self , puppet ) : <EOL> """<STR_LIT>""" <EOL> sessions = puppet . sessid . get ( ) <EOL> return sessions [ <NUM_LIT:0> ] if len ( sessions ) == <NUM_LIT:1> else sessions <EOL> sessions_from_character = sessions_from_puppet <EOL> def announce_all ( self , message ) : <EOL> """<STR_LIT>""" <EOL> for sess in self . values ( ) : <EOL> self . data_out ( sess , message ) <EOL> def data_out ( self , session , text = "<STR_LIT>" , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> text = text and to_str ( to_unicode ( text ) , encoding = session . encoding ) <EOL> except LookupError : <EOL> session . encoding = "<STR_LIT:utf-8>" <EOL> text = to_str ( to_unicode ( text ) , encoding = session . encoding ) <EOL> self . server . amp_protocol . send_MsgServer2Portal ( session , <EOL> text = text , <EOL> ** kwargs ) <EOL> def data_in ( self , session , text = "<STR_LIT>" , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if session : <EOL> text = text and to_unicode ( strip_control_sequences ( text ) , encoding = session . encoding ) <EOL> if "<STR_LIT>" in kwargs : <EOL> global _OOB_HANDLER <EOL> if not _OOB_HANDLER : <EOL> from evennia . server . oobhandler import OOB_HANDLER as _OOB_HANDLER <EOL> funcname , args , kwargs = kwargs . pop ( "<STR_LIT>" ) <EOL> if funcname : <EOL> _OOB_HANDLER . execute_cmd ( session , funcname , * args , ** kwargs ) <EOL> session . data_in ( text = text , ** kwargs ) <EOL> SESSION_HANDLER = ServerSessionHandler ( ) <EOL> SESSIONS = SESSION_HANDLER </s>
