Back-end is in for the flag system.

This commit is contained in:
Greg Taylor 2006-12-17 02:31:50 +00:00
parent 4f85361f6b
commit 22edad226f
7 changed files with 138 additions and 34 deletions

View file

@ -43,12 +43,13 @@ class Object(models.Model):
"""
name = models.CharField(maxlength=255)
owner = models.ForeignKey('self', related_name="obj_owner")
owner = models.ForeignKey('self', related_name="obj_owner", blank=True, null=True)
zone = models.ForeignKey('self', related_name="obj_zone", blank=True, null=True)
home = models.ForeignKey('self', related_name="obj_home", blank=True, null=True)
type = models.SmallIntegerField(choices=global_defines.OBJECT_TYPES)
description = models.TextField(blank=True)
location = models.ForeignKey('self', related_name="obj_location", blank=True, null=True)
flags = models.TextField(blank=True)
date_created = models.DateField(editable=False, auto_now_add=True)
# Rather than keeping another relation for this, we're just going to use
@ -57,7 +58,9 @@ class Object(models.Model):
# cost of a little bit more memory usage. No biggy.
# A list of objects located inside the object.
contents_list = []
# TODO: Re-activate this once we get the contents loader working.
# contents_list = []
# A dictionary of attributes assocated with the object. The keys are the
# attribute's names.
attrib_list = {}
@ -78,7 +81,71 @@ class Object(models.Model):
"""
BEGIN COMMON METHODS
"""
"""
def get_flags(self):
"""
Returns an object's flag list.
"""
return self.flags
def has_flag(self, flag):
"""
Does our object have a certain flag?
"""
return flag in self.flags.split()
def set_flag(self, flag, value):
"""
Add a flag to our object's flag list.
"""
has_flag = self.has_flag(flag)
if value == False and has_flag:
# The flag is there and we want to un-set it.
flags_list = self.flags.split()
flags_list.remove(flag)
self.flags = ' '.join(flags_list)
# Not all flags are saved, such as CONNECTED.
# Don't waste queries on these things.
if flag not in global_defines.NOSAVE_FLAGS:
self.save()
elif value == False and not has_flag:
# Object doesn't have the flag to begin with.
pass
elif value == True and has_flag:
# We've already go it.
pass
else:
# Add the flag.
flags_list = self.flags.split()
flags_list.append(flag.upper())
self.flags = ' '.join(flags_list)
if flag not in global_defines.NOSAVE_FLAGS:
self.save()
def get_owner(self):
"""
Returns an object's owner.
"""
# Players always own themselves.
if self.is_player():
return self
else:
return self.owner
def get_home(self):
"""
Returns an object's home.
"""
return self.home
def get_location(self):
"""
Returns an object's location.
"""
return self.location
def get_attribute(self, attrib):
"""
Returns the value of an attribute on an object.
@ -100,6 +167,12 @@ class Object(models.Model):
something horribly long with the load routine right now.
"""
return list(Object.objects.filter(location__id=self.id))
def get_zone(self):
"""
Returns the object that is marked as this object's zone.
"""
return self.zone
def move_to(self, server, target):
"""

View file

@ -102,8 +102,9 @@ def cmd_examine(cdat):
target_obj.flag_string(),
ansi["normal"],
))
session.msg("Type: %s Flags: <TBI>" % (target_obj.get_type(),))
session.msg("Zone: <TBI>")
session.msg("Type: %s Flags: %s" % (target_obj.get_type(), target_obj.get_flags()))
session.msg("Owner: %s " % (target_obj.get_owner(),))
session.msg("Zone: %s" % (target_obj.get_zone(),))
for attribute in target_obj.attrib_list:
session.msg("%s%s%s: %s" % (ansi["hilite"], attribute, ansi["normal"], target_obj.get_attribute(attribute)))
@ -133,8 +134,8 @@ def cmd_examine(cdat):
session.msg('%s' %(exit,))
if not target_obj.is_room():
session.msg("Home: <TBI>")
session.msg("Location: %s" % (target_obj.location,))
session.msg("Home: %s" % (target_obj.get_home(),))
session.msg("Location: %s" % (target_obj.get_location(),))
def cmd_quit(cdat):
"""

View file

@ -1,5 +1,6 @@
from apps.objects.models import Object
import functions_db
import functions_general
import commands_general
import cmdhandler
@ -141,19 +142,31 @@ def cmd_find(cdat):
session.msg("No search pattern given.")
return
memory_based = True
if memory_based:
results = functions_db.list_search_object_namestr(server.object_list.values(), searchstring)
results = functions_db.list_search_object_namestr(server.object_list.values(), searchstring)
if len(results) > 0:
session.msg("Name matches for: %s" % (searchstring,))
for result in results:
session.msg(" %s" % (result,))
session.msg("%d matches returned." % (len(results),))
else:
session.msg("No name matches found for: %s" % (searchstring,))
if len(results) > 0:
session.msg("Name matches for: %s" % (searchstring,))
for result in results:
session.msg(" %s" % (result,))
session.msg("%d matches returned." % (len(results),))
else:
session.msg("No name matches found for: %s" % (searchstring,))
def cmd_wall(cdat):
"""
Announces a message to all connected players.
"""
session = cdat['session']
server = cdat['server']
wallstring = ' '.join(cdat['uinput']['splitted'][1:])
if wallstring == '':
session.msg("Announce what?")
return
message = "%s shouts \"%s\"" % (session.pobject.name, wallstring)
functions_general.announce_all(server, message)
def cmd_shutdown(cdat):
"""
Shut the server down gracefully.

View file

@ -15,14 +15,20 @@ def cmd_connect(cdat):
password = cdat['uinput']['splitted'][2]
account = User.objects.filter(username=uname)
user = account[0]
autherror = "Invalid username or password!"
# No username match
if account.count() == 0:
session.msg(autherror)
return
# We have at least one result, so we can check hte password.
user = account[0]
if not user.check_password(password):
session.msg(autherror)
else:
user = account[0]
uname = user.username
session.login(user)
@ -31,6 +37,7 @@ def cmd_create(cdat):
Handle the creation of new accounts.
"""
session = cdat['session']
server = session.server
uname = cdat['uinput']['splitted'][1]
email = cdat['uinput']['splitted'][2]
password = cdat['uinput']['splitted'][3]

View file

@ -111,21 +111,23 @@ def create_object(server, odat):
new_object.name = odat["name"]
new_object.type = odat["type"]
#if odat["home"]:
# new_object.home = odat["home"]
#else:
# new_object.home = odat["location"]
#if odat["owner"].zone:
# new_object.zone = odat["owner"].zone
#else:
# new_object.zone = None
#if odat["type"] is 1:
# new_object.owner = new_object
#else:
# new_object.owner = odat["owner"]
# If this is a player, set him to own himself.
if odat["type"] == 1:
new_object.owner = None
new_object.zone = None
else:
new_object.owner = odat["owner"]
if new_object.owner.zone:
new_object.zone = new_object.owner.zone
# If we have a 'home' key, use that for our home value. Otherwise use
# the location key.
if odat.get("home",False):
new_object.home = odat["home"]
else:
new_object.home = odat["location"]
new_object.save()
# Add the object to our server's dictionary of objects.

View file

@ -7,3 +7,9 @@ OBJECT_TYPES = (
(4, 'EXIT'),
(5, 'GARBAGE'),
)
# This is a list of flags that the server actually uses. Anything not in this
# list is a custom flag.
SERVER_FLAGS = ["CONNECTED"]
# These flags are not saved.
NOSAVE_FLAGS = ["CONNECTED"]

View file

@ -57,6 +57,7 @@ class PlayerSession(async_chat):
"""
Break the connection and do some accounting.
"""
self.pobject.set_flag("CONNECTED", False)
async_chat.handle_close(self)
self.logged_in = False
self.server.remove_session(self)
@ -83,7 +84,8 @@ class PlayerSession(async_chat):
self.name = user.username
self.logged_in = True
self.conn_time = time.time()
self.pobject.set_flag("CONNECTED", True)
self.msg("You are now logged in as %s." % (self.name,))
cdat = {"session": self, "uinput":'look', "server": self.server}
cmdhandler.handle(cdat)