Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ General ➜ Problem with GMCP Affs in a Table

Problem with GMCP Affs in a Table

You need to log onto the forum to reply or create new threads.

  Refresh page


Posted by Gabil   (5 posts)  Bio
Date Fri 13 Sep 2024 10:40 PM (UTC)

Amended on Fri 13 Sep 2024 10:46 PM (UTC) by Gabil

Message
<?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.
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 14 Sep 2024 01:47 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Gabil   (5 posts)  Bio
Date Reply #2 on Sun 15 Sep 2024 01:33 AM (UTC)
Message
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>
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


276 views.

You need to log onto the forum to reply or create new threads.

  Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.