X-Git-Url: https://mudpy.org/gitweb?p=mudpy.git;a=blobdiff_plain;f=lib%2Fmuff%2Fmuffmisc.py;h=d0765ef5a788708790a0bc780ce11e8b93c9b96f;hp=ba90981b75f1e2b537f2b61840627a7d8cf98559;hb=994d6e52ce3d5c719991e8f798642cdb8a24b7d1;hpb=0f39af78818acbbee0b99145ff5ff303553027c6 diff --git a/lib/muff/muffmisc.py b/lib/muff/muffmisc.py index ba90981..d0765ef 100644 --- a/lib/muff/muffmisc.py +++ b/lib/muff/muffmisc.py @@ -3,15 +3,32 @@ # Copyright (c) 2005 mudpy, Jeremy Stanley , 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() +