Problem with GMCP Affs in a Table

Posted by Gabil on Fri 13 Sep 2024 10:40 PM — 3 posts, 6,157 views.

#0
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>

<plugin
   name="GMCP_Affliction_Tracker"
   author="My Name"
   id="b1a9d25f4c8e7a92d3b4f6e0"
   language="Lua"
   purpose="Track GMCP afflictions"
   save_state="y"
   version="1.2"
>
<description trim="y">
   This plugin tracks the afflictions added and removed via GMCP and displays the active afflictions in a table.
</description>

</plugin>

<script>
<![CDATA[
-- Initialize afflictions table if it doesn't exist
if not afflictions then
    afflictions = {}
end

-- handle affliction being added
function gmcp_affliction_add()
    if gmcp and gmcp.Char and gmcp.Char.Afflictions and gmcp.Char.Afflictions.Add then
        local affliction_name = gmcp.Char.Afflictions.Add.name
        if affliction_name then
            -- Add affliction to the list

            table.insert(afflictions, affliction_name)
            ColourNote("green", "", "Affliction added: " .. affliction_name)
            -- Display updated afflictions
            display_afflictions_table()
        else
            ColourNote("red", "", "No valid affliction add name received in GMCP.")
        end
    else
        ColourNote("red", "", "No valid affliction add data received in GMCP.")
    end
end

-- handle affliction being removed
function gmcp_affliction_remove()
    if gmcp and gmcp.Char and gmcp.Char.Afflictions and gmcp.Char.Afflictions.Remove then
        local affliction_name = gmcp.Char.Afflictions.Remove[1]
        if affliction_name then
            -- Remove affliction by name
            for i, name in ipairs(afflictions) do
                if name == affliction_name then
                    table.remove(afflictions, i)
                    ColourNote("green", "", "Affliction removed: " .. affliction_name)
                    display_afflictions_table()
                    break
                end
            end
        else
            ColourNote("red", "", "No valid affliction remove name received in GMCP.")
        end
    else
        ColourNote("red", "", "No valid affliction remove data received in GMCP.")
    end
end

-- display all active afflictions in a table format
function display_afflictions_table()
    ColourNote("white", "blue", "=== Active Afflictions ===")
    if #afflictions == 0 then
        ColourNote("yellow", "", "No active afflictions.")
    else
        local affliction_list = table.concat(afflictions, ", ")
        ColourNote("yellow", "", "Current Afflictions: " .. affliction_list)
    end
end

-- gmcp event handling for affliction add-remove
function OnPluginTelnetSubnegotiation(msg_type, data)
    if msg_type == 201 then  -- GMCP Telnet subnegotiation type is 201
        local message = string.match(data, "([%a.]+)%s+(.*)")
        if message == "Char.Afflictions.Add" then
            gmcp_affliction_add()
        elseif message == "Char.Afflictions.Remove" then
            gmcp_affliction_remove()
        end
    end
end

]]>
</script>

</muclient>


I wrote this plugin to display my own afflictions in a table in an IRE game. However, I keep getting the result 'No valid affliction add data received in GMCP.' every time an affliction is added or removed. Thank you in advance for your help.

Edit: GMCP_handler_NJG plugin installed.
Amended on Fri 13 Sep 2024 10:46 PM by Gabil
Australia Forum Administrator #1
Please add some debugging. In the function gmcp_affliction_add add the lines:


require "tprint"
tprint (gmcp)


Then post what you see when an affliction is added or removed.

Where is the variable "gmcp" set up anyway? I think the GMCP data is probably in "data" so that when you call:


            gmcp_affliction_add()


You should probably have instead:


            gmcp_affliction_add(data)


And then in the declaration for the function gmcp_affliction_add take that as an argument:


function gmcp_affliction_add (gmcp)


If you don't do that then gmcp will be nil because you never set it to anything.
#2
Thank you, Nick. Your guidance and this debug management were very helpful. It is working good now.

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>

<plugin
   name="GMCP_Affliction_Tracker"
   author="My Name"
   id="b1a9d25f4c8e7a92d3b4f6e0"
   language="Lua"
   purpose="Track GMCP afflictions"
   save_state="y"
   version="1.2"
>
<description trim="y">
   This plugin tracks the afflictions added and removed via GMCP and displays the active afflictions in a table.
</description>

</plugin>

<script>
<![CDATA[
require "json"

-- Initialize afflictions table if it doesn't exist
if not afflictions then
    afflictions = {}
end

-- display all active afflictions in a table format
function display_afflictions_table()
    ColourNote("white", "blue", "=== Active Afflictions ===")
    if #afflictions == 0 then
        ColourNote("yellow", "", "No active afflictions.")
    else
        local affliction_list = table.concat(afflictions, ", ")
        ColourNote("yellow", "", "Current Afflictions: " .. affliction_list)
    end
end

-- handle affliction being added
function gmcp_affliction_add(gmcp_data)
    -- JSON to table
    local gmcp = json.decode(gmcp_data)

    if gmcp and gmcp.name then
        local affliction_name = gmcp.name
        if affliction_name then
            -- Add affliction to the list
            table.insert(afflictions, affliction_name)
            ColourNote("green", "", "Affliction added: " .. affliction_name)
            -- Display updated afflictions
            display_afflictions_table()
        else
            ColourNote("red", "", "No valid affliction add name received in GMCP.")
        end
    else
        ColourNote("red", "", "No valid affliction add data received in GMCP.")
    end
end

-- handle affliction being removed
function gmcp_affliction_remove(gmcp_data)
    -- JSON to table
    local gmcp = json.decode(gmcp_data)

    if gmcp and type(gmcp) == "table" and gmcp[1] then
        local affliction_name = gmcp[1]
        if affliction_name then
            -- Remove affliction by name
            for i, name in ipairs(afflictions) do
                if name == affliction_name then
                    table.remove(afflictions, i)
                    ColourNote("green", "", "Affliction removed: " .. affliction_name)
                    display_afflictions_table()
                    break
                end
            end
        else
            ColourNote("red", "", "No valid affliction remove name received in GMCP.")
        end
    else
        ColourNote("red", "", "No valid affliction remove data received in GMCP.")
    end
end


-- gmcp event handling for affliction add-remove
function OnPluginTelnetSubnegotiation(msg_type, data)
    if msg_type == 201 then  -- GMCP Telnet subnegotiation type is 201
        local message, gmcp_data = string.match(data, "([%a.]+)%s+(.*)")
        if message == "Char.Afflictions.Add" then
            gmcp_affliction_add(gmcp_data)
        elseif message == "Char.Afflictions.Remove" then
            gmcp_affliction_remove(gmcp_data)
        end
    end
end

]]>
</script>

</muclient>