Switch internal counters to new-style Element
[mudpy.git] / mudpy / misc.py
index 32c0d68..202b6b2 100644 (file)
@@ -57,7 +57,11 @@ class Element:
 
             # parse out appropriate category and subkey names, add to list
             if self.key.find(":") > 0:
+                # TODO(fungi) this can be removed once old_style Elements
+                # are no longer needed
                 self.category, self.subkey = self.key.split(":", 1)
+            elif self.key.find(".") > 0:
+                self.category, self.subkey = self.key.split(".", 1)[-2:]
             else:
                 self.category = "other"
                 self.subkey = self.key
@@ -73,10 +77,6 @@ class Element:
         # record or reset a pointer to the origin file
         self.origin = self.universe.files[origin.source]
 
-        # add a data section to the origin if necessary
-        if self.key not in self.origin.data:
-            self.origin.data[self.key] = {}
-
         # add or replace this element in the universe
         self.universe.contents[self.key] = self
         self.universe.categories[self.category][self.subkey] = self
@@ -88,7 +88,11 @@ class Element:
 
     def destroy(self):
         """Remove an element from the universe and destroy it."""
-        del(self.origin.data[self.key])
+        if self.old_style:
+            del self.origin.data[self.key]
+        else:
+            for facet in dict(self.facethash):
+                self.remove_facet(facet)
         del self.universe.categories[self.category][self.subkey]
         del self.universe.contents[self.key]
         del self
@@ -109,9 +113,13 @@ class Element:
 
     def remove_facet(self, facet):
         """Remove a facet from the element."""
-        if self.has_facet(facet):
-            del(self.origin.data[self.key][facet])
-            self.origin.modified = True
+        if self.old_style and self.has_facet(facet):
+            del self.origin.data[self.key][facet]
+        elif ".".join((self.key, facet)) in self.origin.data:
+            del self.origin.data[".".join((self.key, facet))]
+        if facet in self.facethash:
+            del self.facethash[facet]
+        self.origin.modified = True
 
     def ancestry(self):
         """Return a list of the element's inheritance lineage."""
@@ -156,6 +164,8 @@ class Element:
                                   "disallowed")
         if facet in ["loglevel"]:
             value = int(value)
+        elif facet in ["administrator"]:
+            value = bool(value)
         if not self.has_facet(facet) or not self.get(facet) == value:
             if self.old_style:
                 if self.key not in self.origin.data:
@@ -679,11 +689,15 @@ class User:
     def authenticate(self):
         """Flag the user as authenticated and disconnect duplicates."""
         if self.state is not "authenticated":
-            log("User " + self.account.get("name") + " logged in.", 2)
             self.authenticated = True
             if ("mudpy.limit" in universe.contents and self.account.subkey in
                     universe.contents["mudpy.limit"].get("admins")):
-                self.account.set("administrator", "True")
+                self.account.set("administrator", True)
+                log("Administrator %s authenticated." %
+                    self.account.get("name"), 2)
+            else:
+                # log("User %s authenticated." % self.account.get("name"), 2)
+                log("User %s authenticated." % self.account.subkey, 2)
 
     def show_menu(self):
         """Send the user their current menu."""
@@ -930,16 +944,12 @@ class User:
     def new_avatar(self):
         """Instantiate a new, unconfigured avatar for this user."""
         counter = 0
-        while "avatar:" + self.account.get("name") + ":" + str(
-            counter
-        ) in universe.categories.get("actor", {}).keys():
+        while ("avatar_%s_%s" % (self.account.get("name"), counter)
+                in universe.categories.get("actor", {}).keys()):
             counter += 1
         self.avatar = Element(
-            "actor:avatar:" + self.account.get("name") + ":" + str(
-                counter
-            ),
-            universe, old_style=True
-        )
+            "actor.avatar_%s_%s" % (self.account.get("name"), counter),
+            universe)
         self.avatar.append("inherit", "archetype:avatar")
         self.account.append("avatars", self.avatar.key)
 
@@ -1391,7 +1401,7 @@ def on_pulse():
 
     # add an element for counters if it doesn't exist
     if "counters" not in universe.categories.get("internal", {}):
-        Element("internal:counters", universe, old_style=True)
+        Element("internal.counters", universe)
 
     # update the log every now and then
     if not universe.categories["internal"]["counters"].get("mark"):
@@ -1723,7 +1733,7 @@ def handler_entering_account_name(user):
 
         # otherwise, this could be a brand new user
         else:
-            user.account = Element("account:" + name, universe, old_style=True)
+            user.account = Element("account.%s" % name, universe)
             user.account.set("name", name)
             log("New user: " + name, 2)
             user.state = "checking_new_account_name"
@@ -2164,14 +2174,13 @@ def command_show(actor, parameters):
         if len(arguments) != 2:
             message = "You must specify one file."
         elif arguments[1] in universe.files:
-            message = ('These are the elements in the "' + arguments[1]
+            message = ('These are the nodes in the "' + arguments[1]
                        + '" file:$(eol)')
-            elements = universe.files[arguments[1]].data.keys()
-            elements.sort()
+            elements = sorted(universe.files[arguments[1]].data)
             for element in elements:
                 message += "$(eol)   $(grn)" + element + "$(nrm)"
         else:
-            message = 'Category "' + arguments[1] + '" does not exist.'
+            message = 'File "%s" does not exist.' % arguments[1]
     elif arguments[0] == "element":
         if len(arguments) != 2:
             message = "You must specify one element."