Alias/Trigger generator

by Trakis

Back to Mechanic's Corner.

Trakis2008-06-15 02:49:33
Updated on 6.16.08

Just something I wrote to make system creation easier for people using MUSHclient with Python scripting. It lets you type up all the afflictions with their parameters in a .txt file. Put a copy of this Python script (save it as .py) into the same directory as the .txt file, and it should take something that looks like this:

CODE
affliction: anorexia
cures: smoke coltsfoot, focus mind
prevents: herb, elixir, purgative
special: no
priority: 3
diagnose: anorexic.
cured: Food is no longer repulsive to you.
wound: no


And turns it into this:

CODE
=====ANOREXIA=====









def unAffAnorexia(name, output, wildcards):
    global afflictionMaster
    afflictionMaster = 0


def addAffAnorexia(name, output, wildcards):
    global afflictionMaster
    afflictionMaster = 1


def diagnosedAnorexia(name, output, wildcards):
    world.execute("addaff anorexia")


def curedAnorexia(name, output, wildcards):
    world.execute("unaffanorexia")


def focusCureAnorexia(name, output, wildcards):
    world.send("focus mind")


def smokeCureAnorexia(name, output, wildcards):
    world.send("smoke coltsfoot")


The code is fairly extensible, so there's a lot of stuff you can write off of it, and it can be as helpful or unhelpful as you want it to be. Hope you guys get some use out of it.

CODE
## Alias/Trigger generator for MUSHclient, written by Trakis
## 6.16.08
## Version 1.1

## Accepts afflictions in this format:

## affliction: anorexia
## cures: smoke coltsfoot, focus mind
## prevents: herb, elixir, purgative
## special: no
## priority: 3
## diagnose: anorexic.
## cured: Food is no longer repulsive to you.
## wound: no

infile = open("afflictions.txt", "r")

class affliction:

    def __init__(self, name):
        self.name = name
    
    cureTypes = {}            # Dictionary containing cure types, and cure commands.
    prevents =                        # What curing trees this affliction blocks, if any.
    special = "no"            # 'yes' or 'no' only.
    priority = 1000            # Numerical priority. (1000 is default)
    diagnoseLine = ""           # What line shows up when the affliction is diagnosed.
    cureLine = ""                       # What line shows up when the affliction is cured.
    knightAffliction = "no"        # 'no' if not a knight affliction. Otherwise, "critical", "heavy", "medium", "light", "mixed" for wound level.

def writeSpecialized(lines, aff, outfile):
    for line in lines:
        line = str(line)
        line = line.replace("", aff.name)
        line = line.replace("", aff.name.capitalize())
        line = line.replace("", aff.diagnoseLine)
        line = line.replace("", aff.cureLine)
        outfile.write(line+'\\n')

afflictionList =                      # Will eventually hold a complete list of all afflictions, in class format.

for line in infile:

    marker = line.find(':')
    label = line
    content = line

    content = content.lstrip()
    content = content.strip('\\n')

    if label == "affliction":
        aff = affliction(content)
        afflictionList.append(aff)
    elif label == "cures":
        cures = content.split(', ')
        for cure in cures:
            splitCure = cure.split(' ', 1)
            aff.cureTypes] = splitCure
    elif label == "prevents":
        aff.prevents = content.split(', ')
    elif label == "special":
        aff.special = content
    elif label == "priority":
        aff.priority = content
    elif label == "diagnose":
        aff.diagnoseLine = content
    elif label == "cured":
        aff.cureLine = content
    elif label == "wound":
        aff.knightAffliction = content
    else:
        pass                            # Do nothing with anything else.

infile.close()

# Write aliases/triggers

outfile = open("afflictionAliasesAndTriggers.txt", 'w')

for aff in afflictionList:

    aliases =
    triggers =
    code =
    
    #unAffAlias
    aliases.append("""""")
    
    #unAffAlias Code
    code.append()
    code.append("""def unAff(name, output, wildcards):""")
    code.append("\\t" + """global afflictionMaster""")
    code.append("\\t" + """afflictionMaster"] = 0""")
    
    #addAffAlias
    aliases.append("""""")
    
    #addAffAlias Code
    code.append()
    code.append("""def addAff(name, output, wildcards):""")
    code.append("\\t" + """global afflictionMaster""")
    code.append("\\t" + """afflictionMaster"] = 1""")

    #diagnosedTrigger
    triggers.append("""""")
    
    #diagnosedTrigger Code
    code.append()
    code.append("""def diagnosed(name, output, wildcards):""")
    code.append("\\t" + """world.execute("addaff ")""")
    
    #curedTrigger
    triggers.append("""""")
    
    #curedTrigger Code
    code.append()
    code.append("""def cured(name, output, wildcards):""")
    code.append("\\t" + """world.execute("unaff")""")

    for x in aff.cureTypes:
        #cureAffAlias
        aliases.append("""""".replace("", x))
        
        #cureAffAlias Code
        code.append()
        code.append("""def Cure(name, output, wildcards):""".replace("", x))
        code.append(("\\t" + """world.send(" ")""".replace("", x)).replace("", aff.cureTypes))
    
    outfile.write( '='*5 + aff.name.upper() + '='*5)
    outfile.write( '\\n\\n' )

    writeSpecialized(aliases, aff, outfile)
    writeSpecialized(triggers, aff, outfile)

    outfile.write('\\n\\n')
    
    for function in code:
        writeSpecialized(function, aff, outfile)
        outfile.write('\\n\\n')

outfile.close()
Unknown2008-06-15 10:04:30
Just a suggestion: wouldn't it be better to write the aliases to an XML file that could then just be copied/pasted into a world's settings and write the Python code to a separate script file? With it mixed together like this, it seems you'd have to do a lot of cutting and pasting to make it all import properly. Neat idea, though. smile.gif
Trakis2008-06-15 11:18:30
Yeah. My personal version is going to write everything to XML for me. I just wanted to keep it very general, so that people could use it, so if they want to organize their aliases and triggers in a weird way, they can do what they like.

Maybe I'll post my code for automatic XML writing when I'm done with it.