Search code examples
lualua-5.1ptokax

add variable value in table in lua for ptokax script


Recently i updated my ptokax to 0.5.3 and since then my votekick script has stopped working as in my script takes input from other online users as 1 or 2 as accept or deny the user to be kicked or not but now whenever the user enters 1 or 2 the script has stopped taking input and inserting it in the table i suspects its maybe due to some syntax change . please have a look at my script and suggest .

   data = " <Nick> 2" -- this is the way script takes input frm dc chat
                s,e,vote= string.find(data,"%b<>%s(.+)")

                if vote == "1" then
                    table.insert(votesPlus,user.sNick)
                    Core.SendToAll("*--"..user.sNick.." accepts--")
                    if #votesPlus == votes or # votesMinus == votes then
                        stop(nTimerId)
                    end
                return true
                elseif vote == "2" then
                    table.insert(votesMinus,user.sNick)
                    Core.SendToAll("*--"..user.sNick.." denies--")
                    if #votesPlus == votes or # votesMinus == votes then
                        stop(nTimerId)
                    end
                    return true
                else
                    -- the user is not voting even when poll active
                end

Solution

    1. Please mention whether you're using the PtokaX released to be used with Lua 5.3.0 or 5.1.5.
    2. The NMDC hub protocols define that the chat messages are sent in the following format:

      <Nick> the message|
      

      where | acts as the delimiter.

    Apart from that, I don't see any issues with your script. You can, although, optimise performance:

    local vote = data:match "%b<>%s(%d)"
    -- since you only want a single digit to be matched
    -- also, use local variable whenever possible
    
    if vote == "1" then
        table.insert(votesPlus, user.sNick)
        Core.SendToAll( "*--"..user.sNick.." accepts--" )
        if #votesPlus == votes or #votesMinus == votes then
            stop( nTimerId )
        end
        return true
    elseif vote == "2" then
        table.insert(votesMinus, user.sNick)
        Core.SendToAll( "*--"..user.sNick.." denies--" )
        if #votesPlus == votes or #votesMinus == votes then
            stop( nTimerId )
        end
        return true
    else
        -- the user is not voting even when poll active
        -- return false so that further scripts might be able to process it
        return false
    end
    

    PS: I think you should also check if the same user is voting twice! Also, you can put the following code:

    if #votesPlus == votes or #votesMinus == votes then
        stop( nTimerId )
    end
    

    in the call to OnTimer function.