Moving some of the login tasks out of sessions.py and into the BasicPlayer script parent. Also, added seconds to the 'time' command, which I apparently forgot.

This commit is contained in:
Greg Taylor 2008-06-15 21:29:27 +00:00
parent b12ba45cc7
commit cf18029be1
7 changed files with 69 additions and 23 deletions

View file

@ -135,6 +135,15 @@ class Object(models.Model):
""" """
BEGIN COMMON METHODS BEGIN COMMON METHODS
""" """
def get_session(self):
"""
Returns the session object for a player, or None if none exists.
"""
if self.is_player():
return session_mgr.session_from_object(self)
else:
return None
def emit_to(self, message): def emit_to(self, message):
""" """
Emits something to any sessions attached to the object. Emits something to any sessions attached to the object.
@ -636,7 +645,11 @@ class Object(models.Model):
Returns an object's script parent. Returns an object's script parent.
""" """
if not self.scriptlink: if not self.scriptlink:
self.scriptlink = scripthandler.scriptlink(self, self.get_attribute_value('__parent', 'basicobject')) if self.is_player():
script_to_load = 'player/basicplayer'
else:
script_to_load = 'basicobject'
self.scriptlink = scripthandler.scriptlink(self, self.get_attribute_value('__parent', script_to_load))
if self.scriptlink: if self.scriptlink:
# If the scriptlink variable can't be populated, this will fail # If the scriptlink variable can't be populated, this will fail

View file

@ -33,7 +33,7 @@ def cmd_time(cdat):
Server local time. Server local time.
""" """
session = cdat['session'] session = cdat['session']
session.msg('Current server time : %s' % (time.strftime('%a %b %d %H:%M %Y (%Z)', time.localtime(),))) session.msg('Current server time : %s' % (time.strftime('%a %b %d %H:%M:%S %Y (%Z)', time.localtime(),)))
def cmd_uptime(cdat): def cmd_uptime(cdat):
""" """

View file

@ -198,6 +198,15 @@ def get_cwho_list(channel_name, return_muted=False):
""" """
sess_list = session_mgr.get_session_list() sess_list = session_mgr.get_session_list()
return [sess for sess in sess_list if plr_has_channel(sess, channel_name, return_muted)] return [sess for sess in sess_list if plr_has_channel(sess, channel_name, return_muted)]
def load_object_channels(pobject):
"""
Parse JSON dict of a user's channel list from their CHANLIST attribute.
"""
chan_list = pobject.get_attribute_value("__CHANLIST")
if chan_list:
session = session_mgr.session_from_object(pobject)
session.channels_subscribed = simplejson.loads(chan_list)
def send_cmessage(channel_name, message): def send_cmessage(channel_name, message):
""" """

View file

@ -4,7 +4,7 @@ default. It will have the necessary outline for developers to sub-class and over
""" """
from src import ansi from src import ansi
class BasicObject: class BasicObject(object):
def __init__(self, source_obj): def __init__(self, source_obj):
""" """
Get our ducks in a row. Get our ducks in a row.

View file

View file

@ -0,0 +1,39 @@
"""
The basic Player object script parent.
"""
import time
from src import comsys
from src.scripts.basicobject import BasicObject
class BasicPlayer(BasicObject):
def at_pre_login(self):
"""
Everything done here takes place before the player is actually
'logged in', in a sense that they're not ready to send logged in
commands or receive communication.
"""
pobject = self.source_obj
session = pobject.get_session()
# Load the player's channels from their JSON __CHANLIST attribute.
comsys.load_object_channels(pobject)
pobject.set_attribute("Last", "%s" % (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()),))
pobject.set_attribute("Lastsite", "%s" % (session.address[0],))
pobject.set_flag("CONNECTED", True)
def at_post_login(self):
"""
The user is now logged in. This is what happens right after the moment
they are 'connected'.
"""
pobject = self.source_obj
session = pobject.get_session()
session.msg("You are now logged in as %s." % (pobject.name,))
pobject.get_location().emit_to_contents("%s has connected." %
(pobject.get_name(show_dbref=False),), exclude=pobject)
session.execute_cmd("look")
def class_factory(source_obj):
return BasicPlayer(source_obj)

View file

@ -8,7 +8,6 @@ from datetime import datetime
from twisted.conch.telnet import StatefulTelnetProtocol from twisted.conch.telnet import StatefulTelnetProtocol
from django.utils import simplejson
from django.contrib.auth.models import User from django.contrib.auth.models import User
from apps.objects.models import Object from apps.objects.models import Object
@ -69,14 +68,6 @@ class SessionProtocol(StatefulTelnetProtocol):
""" """
logger.log_infomsg('Disconnect: %s' % (self,)) logger.log_infomsg('Disconnect: %s' % (self,))
self.handle_close() self.handle_close()
def load_user_channels(self):
"""
Parse JSON dict of a user's channel list from their CHANLIST attribute.
"""
chan_list = self.get_pobject().get_attribute_value("__CHANLIST")
if chan_list:
self.channels_subscribed = simplejson.loads(chan_list)
def lineReceived(self, data): def lineReceived(self, data):
""" """
@ -121,7 +112,7 @@ class SessionProtocol(StatefulTelnetProtocol):
result = Object.objects.get(id=self.uid) result = Object.objects.get(id=self.uid)
return result return result
except: except:
return False return None
def game_connect_screen(self): def game_connect_screen(self):
""" """
@ -150,21 +141,15 @@ class SessionProtocol(StatefulTelnetProtocol):
self.conn_time = time.time() self.conn_time = time.time()
pobject = self.get_pobject() pobject = self.get_pobject()
session_mgr.disconnect_duplicate_session(self) session_mgr.disconnect_duplicate_session(self)
pobject.set_flag("CONNECTED", True)
pobject.get_scriptlink().at_pre_login()
self.msg("You are now logged in as %s." % (self.name,)) pobject.get_scriptlink().at_post_login()
pobject.get_location().emit_to_contents("%s has connected." % (pobject.get_name(show_dbref=False),), exclude=pobject)
self.execute_cmd("look")
logger.log_infomsg("Login: %s" % (self,)) logger.log_infomsg("Login: %s" % (self,))
# Update their account's last login time. # Update their account's last login time.
user.last_login = datetime.now() user.last_login = datetime.now()
user.save() user.save()
pobject.set_attribute("Last", "%s" % (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()),))
pobject.set_attribute("Lastsite", "%s" % (self.address[0],))
# Load their channel selection from a JSON-encoded string attribute.
self.load_user_channels()
def msg(self, message): def msg(self, message):
""" """