Imported from archive.
[mudpy.git] / lib / muff / muffmisc.py
index ba90981..d0765ef 100644 (file)
@@ -3,15 +3,32 @@
 # Copyright (c) 2005 mudpy, Jeremy Stanley <fungi@yuggoth.org>, all rights reserved.
 # Licensed per terms in the LICENSE file distributed with this software.
 
+# used by several functions for random calls
+import random
+
+# random_name uses string.strip
+import string
+
+# the log function uses time.asctime for creating timestamps
+import time
+
 # hack to load all modules in the muff package
 import muff
 for module in muff.__all__:
        exec("import " + module)
 
-def broadcast(output):
+def broadcast(message):
        """Send a message to all connected users."""
-       for each_user in muffvars.userlist:
-               each_user.send(output)
+       for each_user in muffvars.userlist: each_user.send(message)
+
+def log(message):
+       """Log a message."""
+
+       # the time in posix log timestamp format
+       timestamp = time.asctime()[4:19]
+
+       # send the timestamp and message to standard output
+       print(timestamp + " " + message)
 
 def wrap_ansi_text(text, width):
        """Wrap text with arbitrary width while ignoring ANSI colors."""
@@ -82,3 +99,42 @@ def wrap_ansi_text(text, width):
        # return the newly-wrapped text
        return text
 
+def weighted_choice(data):
+       """Takes a dict weighted by value and returns a random key."""
+
+       # this will hold our expanded list of keys from the data
+       expanded = []
+
+       # create thee expanded list of keys
+       for key in data.keys():
+               for count in range(data[key]):
+                       expanded.append(key)
+
+       # return one at random
+       return random.choice(expanded)
+
+def random_name():
+       """Returns a random character name."""
+
+       # the vowels and consonants needed to create romaji syllables
+       vowels = [ "a", "i", "u", "e", "o" ]
+       consonants = ["'", "k", "z", "s", "sh", "z", "j", "t", "ch", "ts", "d", "n", "h", "f", "m", "y", "r", "w" ]
+
+       # this dict will hold our weighted list of syllables
+       syllables = {}
+
+       # generate the list with an even weighting
+       for consonant in consonants:
+               for vowel in vowels:
+                       syllables[consonant + vowel] = 1
+
+       # we'll build the name into this string
+       name = ""
+
+       # create a name of random length from the syllables
+       for syllable in range(random.randrange(2, 6)):
+               name += weighted_choice(syllables)
+
+       # strip any leading quotemark, capitalize and return the name
+       return string.strip(name, "'").capitalize()
+