Imported from archive.
[mudpy.git] / lib / muff / muffmisc.py
index d0765ef..d2848b2 100644 (file)
@@ -6,6 +6,9 @@
 # used by several functions for random calls
 import random
 
+# used to match the 'L' at the end of a long int in repr_long
+import re
+
 # random_name uses string.strip
 import string
 
@@ -19,7 +22,7 @@ for module in muff.__all__:
 
 def broadcast(message):
        """Send a message to all connected users."""
-       for each_user in muffvars.userlist: each_user.send(message)
+       for each_user in muffvars.userlist: each_user.send("$(eol)" + message)
 
 def log(message):
        """Log a message."""
@@ -138,3 +141,82 @@ def random_name():
        # strip any leading quotemark, capitalize and return the name
        return string.strip(name, "'").capitalize()
 
+def repr_long(value):
+       string_value = repr(value)
+       if re.match('\d*L$', string_value): return string_value.strip("L")
+       else: return string_value
+
+def getlong(config, section, option):
+       return int(config.get(section, option).strip("L"))
+
+def setlong(config, section, option, value):
+       return config.set(section, option, repr_long(value))
+
+def replace_macros(user, text, is_input=False):
+       while True:
+               macro_start = string.find(text, "$(")
+               if macro_start == -1: break
+               macro_end = string.find(text, ")", macro_start) + 1
+               macro = text[macro_start:macro_end]
+               if macro in muffvars.macros.keys():
+                       text = string.replace(text, macro, muffvars.macros[macro])
+
+               # the user's account name
+               elif macro == "$(account)":
+                       text = string.replace(text, macro, user.name)
+
+               # if we get here, log and replace it with null
+               else:
+                       text = string.replace(text, macro, "")
+                       if not is_input:
+                               log("Unexpected replacement macro " + macro + " encountered.")
+
+       # replace the look-like-a-macro sequence
+       text = string.replace(text, "$_(", "$(")
+
+       return text
+
+def check_time(frequency):
+       """Check for a factor of the current increment count."""
+       if type(frequency) is str:
+               frequency = muffconf.getint("time", frequency)
+       return not getlong(muffvars.variable_data, "time", "elapsed") % frequency
+
+def on_pulse():
+       """The things which should happen on each pulse, aside from reloads."""
+
+       # open the listening socket if it hasn't been already
+       if not muffvars.newsocket: muffsock.initialize_server_socket()
+
+       # assign a user if a new connection is waiting
+       user = muffsock.check_for_connection(muffvars.newsocket)
+       if user: muffvars.userlist.append(user)
+
+       # iterate over the connected users
+       for user in muffvars.userlist: user.pulse()
+
+       # update the log every now and then
+       if check_time("frequency_log"):
+               log(repr(len(muffvars.userlist)) + " connection(s)")
+
+       # periodically save everything
+       if check_time("frequency_save"):
+               muffvars.save()
+               for user in muffvars.userlist: user.save()
+
+       # pause for a configurable amount of time (decimal seconds)
+       time.sleep(muffconf.getfloat("time", "increment"))
+
+       # increment the elapsed increment counter
+       setlong(muffvars.variable_data, "time", "elapsed",
+               getlong(muffvars.variable_data, "time", "elapsed") + 1)
+
+def reload_data():
+       """Reload data into new persistent objects."""
+
+       # reload the users
+       temporary_userlist = []
+       for user in muffvars.userlist: temporary_userlist.append(user)
+       for user in temporary_userlist: user.reload()
+       del(temporary_userlist)
+