Migrating from CMud and looking for design patterns

Post Reply
aleron
Posts: 1
Joined: Thu Jun 29, 2017 6:25 pm

Migrating from CMud and looking for design patterns

Post by aleron »

Hi,

I'm about a week into using Mudlet and loving it so far. I'm in the process of trying to move my scripts and bots from CMud over so that's driving much of my learning.

One thing I'm trying to figure out is what are my options for porting CMud code like this contrived example:

Code: Select all

get all sack
#WAIT 2000
drop all.sword
#WAIT 2000
#ECHO "done"
I've read Time Engine wiki page so I am aware that there is no equivalent to #WAIT available in Mudlet. I've already ported some code over and used tempTrigger() to accomplish some goals. However, in a lot of my CMud bots/scripts it's not at all rare that I have a piece of code like the above, but much longer and more complex. Porting all of these and nesting tempTrigger()'s seems to make maintenance of said code more painful.

One thing I have started doing is breaking these code blocks up into multiple functions and chaining them together with tempTrigger() to accomplish something similar to the original code without nesting quoted tempTimer() blocks:

Code: Select all

TEST = TEST or {}

function TEST.test()
  fg("cyan")
  echo("\nTEST.test()\n")
  TEST.test1()
end

function TEST.test1()
  echo("TEST.test1()\n")
  tempTimer(2, [[TEST.test2()]])
end

function TEST.test2()
  echo("TEST.test2()\n")
  tempTimer(2, [[TEST.test3()]])
end
function TEST.test3()
  echo("TEST.test3()\n")
end
This appears to be the best (i.e., readable and easy to modify and maintain) solution I can come up with at the moment.

My question to the more experienced Mudlet users: are there some common patterns for tackling this sort of code that will make it easier to read, develop, and maintain?

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Migrating from CMud and looking for design patterns

Post by Nyyrazzilyss »

Code: Select all

send("get all sack")
tempTimer(2, [[send("drop all.sword")]])
tempTimer(4, [[echo("done")]])

Jor'Mox
Posts: 1146
Joined: Wed Apr 03, 2013 2:19 am

Re: Migrating from CMud and looking for design patterns

Post by Jor'Mox »

This is something I use to let me send a sequence of commands with waits in between. It works like the sendAll function, only when you want a wait between two commands, put a number in between the two, corresponding to how long you want to wait. In general, I find the recursion strategy to be effective for helping to manage these things more easily, without having to keep elaborate track of how long to wait before each command is executed.
Code: [show] | [select all] lua
local runQueue
function runQueue(tbl)
    local info = table.remove(tbl,1)
    if info then
        local run = function()
                send(info[2])
                runQueue(tbl)
            end
        if info[1] ~= 0 then
            tempTimer(info[1], run)
        else
            run()
        end
    end
end

function sendQueue(...)
    local tbl = {}
    local args = arg
    for k,v in ipairs(args) do
        if k % 2 == 1 and type(v) ~= "number" then
            table.insert(args,k,0)
        end
    end
    for k = 1,#args,2 do
        tbl[(k + 1) / 2] = {args[k],args[k+1]}
    end
    runQueue(tbl)
end

Nyyrazzilyss
Posts: 334
Joined: Thu Mar 05, 2015 2:53 am

Re: Migrating from CMud and looking for design patterns

Post by Nyyrazzilyss »

Just to followup -

I don't personally find placing a wait for execution of a command to be the best idea. Especially when it starts getting complex, any time of internet slow downs / mud lag/ etc usually break this.

I find it usually better to look for whether there's a unique response from the mud that you can trigger on to recognize when the wanted delay has expired.

Post Reply