Pretty sizable update.

* Finished the major work on the command interpreter. Still needs to do some stripping of funky characters for the sake of security, probably.
* Account creation is now syncd up with the regular object list.
* We can now determine our next free database reference number for the creation of new objects, very similar to MUX/MUSH.
* Added some of the stuff we're going to need for efficient recycling of garbage objects.
* Misc. tweaks and improvements across the board.
This commit is contained in:
Greg Taylor 2006-11-28 21:51:36 +00:00
parent 0ac644aef2
commit 2cd8c50f3d
6 changed files with 161 additions and 80 deletions

View file

@ -4,7 +4,8 @@ import socket, asyncore, time, sys
from sessions import PlayerSession
from django.db import models
from apps.config.models import ConfigValue, CommandAlias
from apps.objects.models import Object
from apps.objects.models import Object, Attribute
from django.contrib.auth.models import User
#
## Begin: Time Functions
@ -34,7 +35,7 @@ def Timer(timer):
#
## End: Time Functions
#
class Server(dispatcher):
"""
The main server class from which everything branches.
@ -45,10 +46,15 @@ class Server(dispatcher):
self.cmd_alias_list = {}
self.configvalue = {}
self.game_running = True
print '-'*50
# Load stuff up into memory for easy/quick access.
self.load_configvalues()
self.load_objects()
self.load_attributes()
self.load_cmd_aliases()
# Start accepting connections.
dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
@ -56,18 +62,10 @@ class Server(dispatcher):
self.listen(100)
print ' %s started on port %s.' % (self.configvalue['site_name'], self.configvalue['site_port'],)
print '-'*50
def announce_all(self, message, with_ann_prefix=True):
"""
Announces something to all connected players.
"""
if with_ann_prefix:
prefix = 'Announcement:'
else:
prefix = ''
for session in self.session_list:
session.push('%s %s' % (prefix, message,))
"""
BEGIN SERVER STARTUP METHODS
"""
def load_configvalues(self):
"""
@ -88,7 +86,16 @@ class Server(dispatcher):
for object in object_list:
dbnum = object.id
self.object_list[dbnum] = object
print ' Objects Loaded: %i' % (len(self.object_list),)
print ' Objects Loaded: %d' % (len(self.object_list),)
def load_attributes(self):
"""
Load all of our attributes into memory.
"""
attribute_list = Attribute.objects.all()
for attrib in attribute_list:
attrib.object.attrib_list[attrib.name] = attrib.value
print ' Attributes Loaded: %d' % (len(attribute_list),)
def load_cmd_aliases(self):
"""
@ -109,7 +116,76 @@ class Server(dispatcher):
print 'Connection:', str(session)
self.session_list.append(session)
print 'Sessions active:', len(self.session_list)
"""
BEGIN GENERAL METHODS
"""
def create_user(self, session, uname, email, password):
"""
Handles the creation of new users.
"""
start_room = int(self.get_configvalue('player_dbnum_start'))
start_room_obj = self.object_list[start_room]
# The user's entry in the User table must match up to an object
# on the object table. The id's are the same, we need to figure out
# the next free unique ID to use and make sure the two entries are
# the same number.
uid = self.get_nextfree_dbnum()
user = User.objects.create_user(uname, email, password)
# It stinks to have to do this but it's the only trivial way now.
user.id = uid
user.save
# Create a player object of the same ID in the Objects table.
user_object = Object(id=uid, type=1, name=uname, location=start_room_obj)
user_object.save()
# Activate the player's session and set them loose.
session.login(user)
print 'Registration: %s' % (session,)
session.push("Welcome to %s, %s.\n\r" % (self.get_configvalue('site_name'), session.name,))
def announce_all(self, message, with_ann_prefix=True):
"""
Announces something to all connected players.
"""
if with_ann_prefix:
prefix = 'Announcement:'
else:
prefix = ''
for session in self.session_list:
session.push('%s %s' % (prefix, message,))
def get_configvalue(self, configname):
"""
Retrieve a configuration value.
"""
return self.configvalue[configname]
def get_nextfree_dbnum(self):
"""
Figure out what our next free database reference number is.
"""
# First we'll see if there's an object of type 5 (GARBAGE) that we
# can recycle.
nextfree = Object.objects.filter(type__exact=5)
if nextfree:
# We've got at least one garbage object to recycle.
#return nextfree.id
return nextfree[0].id
else:
# No garbage to recycle, find the highest dbnum and increment it
# for our next free.
return Object.objects.order_by('-id')[0].id + 1
"""
END Server CLASS
"""
"""
BEGIN MAIN APPLICATION LOGIC
"""
if __name__ == '__main__':
server = Server()
@ -119,5 +195,5 @@ if __name__ == '__main__':
Timer(time.time())
except KeyboardInterrupt:
server.announce_all('The server has been shutdown. Please check back soon.')
server.announce_all('The server has been shutdown. Please check back soon.\n\r')
print '--> Server killed by keystroke.'