My first plugin in python, how can I make it prettier?

Posted by Somegirl on Sat 30 Oct 2004 11:10 PM — 3 posts, 15,764 views.

#0
I just started with python a couple weeks ago and managed to rewrite my OnPluginPartialLine in it. But this is the meat of the first complete plugin I made that is all-inclusive. It works the way it is, but I wanted to ask everyone's expert opinions on what I could have done better. Any input is appreciated. I am working on more critical tasks and am sometimes at a loss of how to script something in python. How could I make this plugin more efficient? How much useless or extra code do I have? What would you do to improve this? Thanks in advance for everyone's more experienced input.
<trigger
   enabled="y"
   group="fishing"
   match="^([0-9]+h, [0-9]+m(, [0-9]+(w|e)(, [0-9]+(w|e)|)|) [cexkdb]+\-|)You reel in the last bit of line.*$"
   name="bait_hook_1"
   regexp="y"
   script="CreateAction"
   sequence="100"
   ></trigger>
<trigger
   enabled="y"
   group="fishing"
   match="^([0-9]+h, [0-9]+m(, [0-9]+(w|e)(, [0-9]+(w|e)|)|) [cexkdb]+\-|)With a final tug\, you finish reeling in the line and land a.*$"
   name="bait_hook_2"
   regexp="y"
   script="CreateAction"
   sequence="100"
></trigger>
<trigger
   enabled="y"
   group="fishing"
   match="^You feel a fish nibbling on your hook\.$"
   name="tease_line_1"
   regexp="y"
   script="CreateAction"
   sequence="100"
></trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="^([0-9]+h, [0-9]+m(, [0-9]+(w|e)(, [0-9]+(w|e)|)|) [cexkdb]+\-|)You quickly jerk back your fishing pole and feel the line go taut\. You\'ve hooked$"
   name="reel_line_3"
   regexp="y"
   script="CreateAction"
   sequence="100"
  ></trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="Relaxing the tension on your line you are able to reel again."
   name="reel_line_2"
   sequence="100"
  >
  <send>reel line</send>
  </trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="^You see the water ripple as a fish makes a medium strike at your bait\.$"
   name="jerk_line_1"
   regexp="y"
   script="CreateAction"
   sequence="100"
  >
  </trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="^You stagger as a fish makes a large strike at your bait\.$"
   name="jerk_line_2"
   regexp="y"
   script="CreateAction"
   sequence="100"
  ></trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="^You feel a fish make a small strike at your bait\.$"
   name="jerk_line_3"
   regexp="y"
   script="CreateAction"
   sequence="100"
  ></trigger>
  <trigger
   enabled="y"
   group="fishing"
   match="^You feel a fish make a medium strike at your bait\.$"
   name="jerk_line_4"
   regexp="y"
   script="CreateAction"
   sequence="100"
  ></trigger>
   <trigger
   enabled="y"
   group="fishing"
   match="^You quickly jerk back your fishing pole\, to no avail\.$"
   regexp="y"
   script="CreateAction"
   sequence="100"
  >
  </trigger>
 <trigger
   enabled="y"
   group="fishing"
   match="^a (large|small|good\-sized) one\!$"
   name="reel_line_1"
   regexp="y"
   script="CreateAction"
   sequence="100"
  >
  </trigger>
<trigger
   enabled="y"
   group="fishing"
   match="^You have recovered balance on all limbs\.$"
   regexp="y"
   script="OnBalance"
   sequence="100"
   >
  </trigger>
<aliases>
  <alias
   name="switch_fishing"
   script="Switch"
   match="^fish(on|off)(n|ne|e|se|s|sw|w|nw|)$"
   enabled="y"
   expand_variables="y"
   group="fishing"
   regexp="y"
   sequence="100"
  >
  </alias>
  <alias
   name="bait_hook_1"
   match="^bh$"
   enabled="y"
   group="fishing"
   regexp="y"
   sequence="100"
  >
  <send>bait hook with bait</send>
  </alias>
</aliases>
balance=[1]
ActionCount=0
Direction=[]
Action=[]
def Switch(name, output, wildcards):
    global Direction
    if wildcards[0]=='on':
        #wield pole, bait and cast? turn on triggs etc
        #set direction with wildcards[1]: syntax fishon east
        world.EnableTriggerGroup("fishing", 1)
        world.Execute ("uwa")#unwield all
        world.Send ("wield pole")
        Direction=wildcards[1]#need to send cast and Direction to Action?
        CreateAction("bait_hook","blah","blah")
    elif wildcards[0]=='off':
        #turn off triggs, reel in, remove bait, unwield pole
        world.EnableTriggerGroup("fishing", 0)
        world.send("reel line")
        world.send("get bait from pole")
        world.send("unwield pole")

def OnBalance(name, output, wildcards):#balance taken at jerk, tease, bait, cast
    global balance
    balance=[1]
    DoAction()#got balance, now do whatever is needed

    
def DoAction():#here I need to bait, cast, tease, jerk and reel
    #need counter to do jerk and tease 3 times, 
    global ActionCount, Action, balance
    if Action:
        if cmp('bait hook',Action)==0:
            world.execute("bh")
            Action=('cast line '+Direction)
        elif Action==('cast line '+Direction):
            world.send(Action)
            Action=[]
        elif Action=='reel line':
            world.send(Action)
        else:
            if ActionCount >= 3:
                ActionCount = 0
                Action=[]
            elif ActionCount < 3:
                world.Send(Action)
                ActionCount = ActionCount+1
                balance=[]
                
def CreateAction(name, output, wildcards):# is this needed?
    global Action, ActionCount, balance#delimit the action names and send to appropriate
    if name:
        Action=" ".join(name.split('_')[:2])
        ActionCount=0
        if balance:
            DoAction()        
    else:
        return True

    
Amended on Sat 30 Oct 2004 11:51 PM by Nick Gammon
#1
Crap I really screwed that post up, but I hope you get the idea. I can't figure out how to edit it.
Australia Forum Administrator #2
If you have loggged into the forum, you should be able to click the "Edit" button to correct a post.

I have done it for you, the word [code] is case-sensitive, and then you should "quote forum codes" which is an option in the MUSHclient notepad, to put backslashes in front of square brackets and backslashes themselves.