From cf18029be1f1fea7beca1eaf91945650d9dc78f6 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Sun, 15 Jun 2008 21:29:27 +0000 Subject: [PATCH] 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. --- apps/objects/models.py | 15 +++++++++++- src/commands/info.py | 2 +- src/comsys.py | 9 +++++++ src/scripts/basicobject.py | 2 +- src/scripts/player/__init__.py | 0 src/scripts/player/basicplayer.py | 39 +++++++++++++++++++++++++++++++ src/session.py | 25 ++++---------------- 7 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 src/scripts/player/__init__.py create mode 100644 src/scripts/player/basicplayer.py diff --git a/apps/objects/models.py b/apps/objects/models.py index 2b3bc3a411..efa69aa4a3 100755 --- a/apps/objects/models.py +++ b/apps/objects/models.py @@ -135,6 +135,15 @@ class Object(models.Model): """ 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): """ Emits something to any sessions attached to the object. @@ -636,7 +645,11 @@ class Object(models.Model): Returns an object's script parent. """ 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 the scriptlink variable can't be populated, this will fail diff --git a/src/commands/info.py b/src/commands/info.py index df66762d1a..5e7389a6b7 100644 --- a/src/commands/info.py +++ b/src/commands/info.py @@ -33,7 +33,7 @@ def cmd_time(cdat): Server local time. """ 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): """ diff --git a/src/comsys.py b/src/comsys.py index 757adcbd8d..b00f6cd428 100644 --- a/src/comsys.py +++ b/src/comsys.py @@ -198,6 +198,15 @@ def get_cwho_list(channel_name, return_muted=False): """ sess_list = session_mgr.get_session_list() 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): """ diff --git a/src/scripts/basicobject.py b/src/scripts/basicobject.py index e78673be03..8d6b15b023 100644 --- a/src/scripts/basicobject.py +++ b/src/scripts/basicobject.py @@ -4,7 +4,7 @@ default. It will have the necessary outline for developers to sub-class and over """ from src import ansi -class BasicObject: +class BasicObject(object): def __init__(self, source_obj): """ Get our ducks in a row. diff --git a/src/scripts/player/__init__.py b/src/scripts/player/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/scripts/player/basicplayer.py b/src/scripts/player/basicplayer.py new file mode 100644 index 0000000000..84fafebe23 --- /dev/null +++ b/src/scripts/player/basicplayer.py @@ -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) diff --git a/src/session.py b/src/session.py index a36e785824..682a2acada 100755 --- a/src/session.py +++ b/src/session.py @@ -8,7 +8,6 @@ from datetime import datetime from twisted.conch.telnet import StatefulTelnetProtocol -from django.utils import simplejson from django.contrib.auth.models import User from apps.objects.models import Object @@ -69,14 +68,6 @@ class SessionProtocol(StatefulTelnetProtocol): """ logger.log_infomsg('Disconnect: %s' % (self,)) 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): """ @@ -121,7 +112,7 @@ class SessionProtocol(StatefulTelnetProtocol): result = Object.objects.get(id=self.uid) return result except: - return False + return None def game_connect_screen(self): """ @@ -150,21 +141,15 @@ class SessionProtocol(StatefulTelnetProtocol): self.conn_time = time.time() pobject = self.get_pobject() session_mgr.disconnect_duplicate_session(self) - pobject.set_flag("CONNECTED", True) - - self.msg("You are now logged in as %s." % (self.name,)) - pobject.get_location().emit_to_contents("%s has connected." % (pobject.get_name(show_dbref=False),), exclude=pobject) - self.execute_cmd("look") + + pobject.get_scriptlink().at_pre_login() + pobject.get_scriptlink().at_post_login() + logger.log_infomsg("Login: %s" % (self,)) # Update their account's last login time. user.last_login = datetime.now() 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): """