We now have a fully functional @set command, complete with flags and attributes.

This commit is contained in:
Greg Taylor 2006-12-17 07:31:01 +00:00
parent 833d7b3b45
commit 1b03c8cf31
6 changed files with 105 additions and 8 deletions

View file

@ -13,7 +13,7 @@ class Attribute(models.Model):
""" """
name = models.CharField(maxlength=255) name = models.CharField(maxlength=255)
value = models.CharField(maxlength=255) value = models.CharField(maxlength=255)
is_hidden = models.BooleanField() is_hidden = models.BooleanField(default=0)
object = models.ForeignKey("Object") object = models.ForeignKey("Object")
def __str__(self): def __str__(self):
@ -97,6 +97,50 @@ class Object(models.Model):
""" """
return ' '.join(self.flags_active) return ' '.join(self.flags_active)
def clear_attribute(self, attribute):
"""
Removes an attribute entirely.
attribute: (str) The attribute's name.
"""
if self.has_attribute(attribute):
attrib_obj = self.get_attribute_obj(attribute)
attrib_obj.delete()
del self.attrib_list[attribute]
return True
else:
return False
def set_attribute(self, attribute, new_value):
"""
Sets an attribute on an object. Creates the attribute if need
be.
attribute: (str) The attribute's name.
new_value: (str) The value to set the attribute to.
"""
if self.has_attribute(attribute):
# Attribute already exists, update it.
attrib_obj = self.attrib_list[attribute]
attrib_obj.value = new_value
attrib_obj.save()
else:
# Attribute object doesn't exist, create it.
new_attrib = Attribute()
new_attrib.name = attribute
new_attrib.value = new_value
new_attrib.object = self
new_attrib.save()
self.attrib_list[attribute] = new_attrib
def has_attribute(self, attribute):
"""
See if we have an attribute set on the object.
attribute: (str) The attribute's name.
"""
return self.attrib_list.has_key(attribute)
def has_flag(self, flag): def has_flag(self, flag):
""" """
Does our object have a certain flag? Does our object have a certain flag?
@ -165,11 +209,29 @@ class Object(models.Model):
""" """
return self.location return self.location
def get_attribute(self, attrib): def get_attribute_value(self, attrib):
""" """
Returns the value of an attribute on an object. Returns the value of an attribute on an object.
attrib: (str) The attribute's name.
""" """
return self.attrib_list.get(attrib, False) if self.has_attribute(attrib):
attrib_value = self.attrib_list[attrib]
return attrib_value.value
else:
return False
def get_attribute_obj(self, attrib):
"""
Returns the attribute object matching the specified name.
attrib: (str) The attribute's name.
"""
if self.has_attribute(attrib):
attrib_obj = self.attrib_list[attrib]
return attrib_obj
else:
return False
def load_to_location(self): def load_to_location(self):
""" """
@ -246,6 +308,8 @@ class Object(models.Model):
""" """
Search an object's contents for name and dbref matches. Don't put any Search an object's contents for name and dbref matches. Don't put any
logic in here, we'll do that from the end of the command or function. logic in here, we'll do that from the end of the command or function.
oname: (str) The string to filter from.
""" """
return [prospect for prospect in self.contents_list if prospect.name_match(oname)] return [prospect for prospect in self.contents_list if prospect.name_match(oname)]
@ -264,6 +328,8 @@ class Object(models.Model):
def get_type(self, return_number=False): def get_type(self, return_number=False):
""" """
Returns the numerical or string representation of an object's type. Returns the numerical or string representation of an object's type.
return_number: (bool) True returns numeric type, False returns string.
""" """
if return_number: if return_number:
return self.type return self.type
@ -273,6 +339,8 @@ class Object(models.Model):
def is_type(self, otype): def is_type(self, otype):
""" """
See if an object is a certain type. See if an object is a certain type.
otype: (str) A string representation of the object's type (ROOM, THING)
""" """
otype = otype[0] otype = otype[0]

View file

@ -107,7 +107,7 @@ def cmd_examine(cdat):
session.msg("Zone: %s" % (target_obj.get_zone(),)) session.msg("Zone: %s" % (target_obj.get_zone(),))
for attribute in target_obj.attrib_list: for attribute in target_obj.attrib_list:
session.msg("%s%s%s: %s" % (ansi["hilite"], attribute, ansi["normal"], target_obj.get_attribute(attribute))) session.msg("%s%s%s: %s" % (ansi["hilite"], attribute, ansi["normal"], target_obj.get_attribute_value(attribute)))
con_players = [] con_players = []
con_things = [] con_things = []

View file

@ -168,8 +168,23 @@ def cmd_set(cdat):
if len(attrib_args) > 1: if len(attrib_args) > 1:
# We're dealing with an attribute/value pair. # We're dealing with an attribute/value pair.
attrib_name = attrib_args[0].upper() attrib_name = attrib_args[0].upper()
attrib_value = ' '.join(attrib_args[1:]) splicenum = eq_args[1].find(':') + 1
session.msg("%s - %s set." % (victim.get_name(), attrib_name)) attrib_value = eq_args[1][splicenum:]
# In global_defines.py, see NOSET_ATTRIBS for protected attribute names.
if not functions_db.modifiable_attrib(attrib_name):
session.msg("You can't modify that attribute.")
return
if attrib_value:
# An attribute value was specified, create or set the attribute.
verb = 'set'
victim.set_attribute(attrib_name, attrib_value)
else:
# No value was given, this means we delete the attribute.
verb = 'cleared'
victim.clear_attribute(attrib_name)
session.msg("%s - %s %s." % (victim.get_name(), attrib_name, verb))
else: else:
# Flag manipulation form. # Flag manipulation form.
flag_list = eq_args[1].split() flag_list = eq_args[1].split()

View file

@ -11,7 +11,16 @@ def modifiable_flag(flagname):
return True return True
else: else:
return False return False
def modifiable_attrib(attribname):
"""
Check to see if a particular attribute is modifiable.
"""
if attribname not in global_defines.NOSET_ATTRIBS:
return True
else:
return False
def get_nextfree_dbnum(): def get_nextfree_dbnum():
""" """
Figure out what our next free database reference number is. Figure out what our next free database reference number is.

View file

@ -11,7 +11,12 @@ OBJECT_TYPES = (
# This is a list of flags that the server actually uses. Anything not in this # This is a list of flags that the server actually uses. Anything not in this
# list is a custom flag. # list is a custom flag.
SERVER_FLAGS = ["CONNECTED"] SERVER_FLAGS = ["CONNECTED"]
# These flags are not saved. # These flags are not saved.
NOSAVE_FLAGS = ["CONNECTED"] NOSAVE_FLAGS = ["CONNECTED"]
# These flags can't be modified by players. # These flags can't be modified by players.
NOSET_FLAGS = ["CONNECTED"] NOSET_FLAGS = ["CONNECTED"]
# These attribute names can't be modified by players.
NOSET_ATTRIBS = ["TEST"]

View file

@ -89,7 +89,7 @@ class Server(dispatcher):
""" """
attribute_list = Attribute.objects.all() attribute_list = Attribute.objects.all()
for attrib in attribute_list: for attrib in attribute_list:
attrib.object.attrib_list[attrib.name] = attrib.value attrib.object.attrib_list[attrib.name] = attrib
print ' Attributes Loaded: %d' % (len(attribute_list),) print ' Attributes Loaded: %d' % (len(attribute_list),)
def load_cmd_aliases(self): def load_cmd_aliases(self):