Re-organization of the command handler for the sake of efficiency and cleanliness.

This commit is contained in:
Greg Taylor 2006-12-03 00:40:19 +00:00
parent 703fddcb7f
commit 8352c939ff
5 changed files with 256 additions and 265 deletions

View file

@ -1,23 +1,19 @@
from commands_staff import StaffCommands import commands_staff
from commands_general import GenCommands import commands_general
from commands_unloggedin import UnLoggedInCommands import commands_unloggedin
""" """
This is the command processing module. It is instanced once in the main This is the command processing module. It is instanced once in the main
server module and the handle() function is hit every time a player sends server module and the handle() function is hit every time a player sends
something. something.
""" """
# We'll use this for our getattr() in the Handler class.
gencommands = GenCommands()
staffcommands = StaffCommands()
unloggedincommands = UnLoggedInCommands()
class UnknownCommand(Exception): class UnknownCommand(Exception):
""" """
Throw this when a user enters an an invalid command. Throw this when a user enters an an invalid command.
""" """
class Handler: def handle(cdat):
def handle(self, cdat):
""" """
Use the spliced (list) uinput variable to retrieve the correct Use the spliced (list) uinput variable to retrieve the correct
command, or return an invalid command error. command, or return an invalid command error.
@ -54,16 +50,12 @@ class Handler:
if session.logged_in: if session.logged_in:
# If it's prefixed by an '@', it's a staff command. # If it's prefixed by an '@', it's a staff command.
if parsed_input['root_cmd'][0] != '@': if parsed_input['root_cmd'][0] != '@':
cmdtable = gencommands cmd = getattr(commands_general, 'do_%s' % (parsed_input['root_cmd'],), None )
else: else:
parsed_input['root_cmd'] = parsed_input['root_cmd'][1:] parsed_input['root_cmd'] = parsed_input['root_cmd'][1:]
cmdtable = staffcommands cmd = getattr(commands_staff, 'do_%s' % (parsed_input['root_cmd'],), None )
else: else:
cmdtable = unloggedincommands cmd = getattr(commands_unloggedin, 'do_%s' % (parsed_input['root_cmd'],), None )
# cmdtable now equals the command table we need to use. Do a command
# lookup for a particular function based on the user's input.
cmd = getattr(cmdtable, 'do_%s' % (parsed_input['root_cmd'],), None )
if callable(cmd): if callable(cmd):
cdat['uinput'] = parsed_input cdat['uinput'] = parsed_input

View file

@ -1,12 +1,12 @@
import settings import settings
from ansi import * from ansi import *
class GenCommands: """
""" Generic command module. Pretty much every command should go here for
Generic command class. Pretty much every command should go here for now.
now. """
"""
def do_look(self, cdat): def do_look(cdat):
""" """
Handle looking at objects. Handle looking at objects.
""" """
@ -24,7 +24,7 @@ class GenCommands:
) )
session.msg(retval) session.msg(retval)
def do_quit(self, cdat): def do_quit(cdat):
""" """
Gracefully disconnect the user as per his own request. Gracefully disconnect the user as per his own request.
""" """
@ -32,7 +32,7 @@ class GenCommands:
session.msg("Quitting!") session.msg("Quitting!")
session.handle_close() session.handle_close()
def do_who(self, cdat): def do_who(cdat):
""" """
Generic WHO command. Generic WHO command.
""" """
@ -46,7 +46,7 @@ class GenCommands:
session.msg(retval) session.msg(retval)
def do_say(self, cdat): def do_say(cdat):
""" """
Room-based speech command. Room-based speech command.
""" """
@ -61,7 +61,7 @@ class GenCommands:
session.msg(retval) session.msg(retval)
def do_version(self, cdat): def do_version(cdat):
""" """
Version info command. Version info command.
""" """

View file

@ -1,11 +1,12 @@
from apps.objects.models import Object from apps.objects.models import Object
import functions_db import functions_db
import commands_general
class StaffCommands: """
""" Restricted staff commands.
Restricted staff commands. """
"""
def do_dig(self, cdat): def do_dig(cdat):
""" """
Digs a new room out. Digs a new room out.
""" """
@ -20,7 +21,7 @@ class StaffCommands:
newroom.name = roomname newroom.name = roomname
newroom.type = "Room" newroom.type = "Room"
def do_nextfree(self, cdat): def do_nextfree(cdat):
""" """
Returns the next free object number. Returns the next free object number.
""" """
@ -32,7 +33,7 @@ class StaffCommands:
session.msg(retval) session.msg(retval)
def do_teleport(self, cdat): def do_teleport(cdat):
""" """
Teleports an object somewhere. Teleports an object somewhere.
""" """
@ -65,11 +66,11 @@ class StaffCommands:
else: else:
session.msg("Teleported.") session.msg("Teleported.")
pobject.move_to(results[0]) pobject.move_to(results[0])
#GenCommands.do_look(cdat) commands_general.do_look(cdat)
#session.msg("Args: %s\n\rEqargs: %s" % (args, eq_args,)) #session.msg("Args: %s\n\rEqargs: %s" % (args, eq_args,))
def do_find(self, cdat): def do_find(cdat):
""" """
Searches for an object of a particular name. Searches for an object of a particular name.
""" """

View file

@ -1,10 +1,10 @@
from django.contrib.auth.models import User from django.contrib.auth.models import User
class UnLoggedInCommands: """
""" Commands that are available from the connect screen.
Commands that are available from the connect screen. """
"""
def do_connect(self, cdat): def do_connect(cdat):
""" """
This is the connect command at the connection screen. Fairly simple, This is the connect command at the connection screen. Fairly simple,
uses the Django database API and User model to make it extremely simple. uses the Django database API and User model to make it extremely simple.
@ -25,7 +25,7 @@ class UnLoggedInCommands:
uname = user.username uname = user.username
session.login(user) session.login(user)
def do_create(self, cdat): def do_create(cdat):
""" """
Handle the creation of new accounts. Handle the creation of new accounts.
""" """
@ -43,7 +43,7 @@ class UnLoggedInCommands:
else: else:
server.create_user(session, uname, email, password) server.create_user(session, uname, email, password)
def do_quit(self, cdat): def do_quit(cdat):
""" """
We're going to maintain a different version of the quit command We're going to maintain a different version of the quit command
here for unconnected users for the sake of simplicity. The logged in here for unconnected users for the sake of simplicity. The logged in

View file

@ -1,12 +1,10 @@
from asyncore import dispatcher from asyncore import dispatcher
from asynchat import async_chat from asynchat import async_chat
import socket, asyncore, time, sys import socket, asyncore, time, sys
from cmdhandler import * import cmdhandler
from apps.objects.models import Object from apps.objects.models import Object
from django.contrib.auth.models import User from django.contrib.auth.models import User
chandler = Handler()
class PlayerSession(async_chat): class PlayerSession(async_chat):
""" """
This class represents a player's sesssion. From here we branch down into This class represents a player's sesssion. From here we branch down into
@ -53,7 +51,7 @@ class PlayerSession(async_chat):
self.cmd_last = time.time() self.cmd_last = time.time()
# Stuff anything we need to pass in this dictionary. # Stuff anything we need to pass in this dictionary.
cdat = {"server": self.server, "uinput": uinput, "session": self} cdat = {"server": self.server, "uinput": uinput, "session": self}
chandler.handle(cdat) cmdhandler.handle(cdat)
def handle_close(self): def handle_close(self):
""" """