This is a rather big update, encompassing quite a few areas that will lead to what I hope will be a very easy to modify yet flexible codebase. New stuff includes:

* A simple command parser that supports MUX/MUSH-style switches.
* Global command alias support. Aliases can be edited via web interface or SQL.
* A new SQL-based configuration system that can be edited directly or via a Django web interface.
* The listen port is now configurable via aforementioned config system instead of hard-wired as it was previously. Yick :)
This commit is contained in:
Greg Taylor 2006-11-26 23:11:11 +00:00
parent fa21a35fe4
commit aabce6e7e3
4 changed files with 82 additions and 45 deletions

View file

@ -3,7 +3,7 @@ from asynchat import async_chat
import socket, asyncore, time, sys
from sessions import PlayerSession
from django.db import models
from apps.config.models import Config
from apps.config.models import ConfigValue, CommandAlias
from apps.objects.models import Object
#
@ -39,26 +39,45 @@ class Server(dispatcher):
"""
The main server class from which everything branches.
"""
def __init__(self, port):
def __init__(self):
self.session_list = []
self.object_list = {}
self.cmd_alias_list = {}
self.configvalue = {}
self.game_running = True
print '-'*50
self.load_configvalues()
self.load_objects()
self.load_cmd_aliases()
dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(('', port))
self.bind(('', int(self.configvalue['site_port'])))
self.listen(100)
self.session_list = []
self.object_list = {}
self.game_running = True
print '-'*50
self.load_config()
self.load_objects()
print ' Server started on port %i.' % (port,)
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,))
def load_config(self):
def load_configvalues(self):
"""
Loads our site's configuration up for easy access.
"""
self.config = Config.objects.all()[0]
configs = ConfigValue.objects.all()
for conf in configs:
self.configvalue[conf.conf_key] = conf.conf_value
print ' Configuration Loaded.'
def load_objects(self):
@ -71,6 +90,15 @@ class Server(dispatcher):
self.object_list[dbnum] = object
print ' Objects Loaded: %i' % (len(self.object_list),)
def load_cmd_aliases(self):
"""
Load up our command aliases.
"""
alias_list = CommandAlias.objects.all()
for alias in alias_list:
self.cmd_alias_list[alias.user_input] = alias.equiv_command
print ' Aliases Loaded: %i' % (len(self.cmd_alias_list),)
def handle_accept(self):
"""
What to do when we get a connection.
@ -83,7 +111,7 @@ class Server(dispatcher):
print 'Sessions active:', len(self.session_list)
if __name__ == '__main__':
server = Server(4000)
server = Server()
try:
while server.game_running:
@ -91,4 +119,5 @@ if __name__ == '__main__':
Timer(time.time())
except KeyboardInterrupt:
print 'Interrupted'
server.announce_all('The server has been shutdown. Please check back soon.')
print '--> Server killed by keystroke.'