Okay. Here is the complete code and a break down of how it works. I've modified how "oldwholist" gets its content to make sure that both tables are working how they should.
I know a lot of this should be in separate functions, but I'm very new to Lua so getting it working first before I neaten it all up and put it in a plugin.
Firstly, I grab the data from my MUD's website to get it ready to break down into the 'wholist' table.
if not http then http = require("socket.http") end
local mysocket = require("socket.http")
mysocket.TIMEOUT = 2
local page = http.request("http://www.t2tmud.org/who.php")
Next I use gmatch to strip out the names of the online Players from all the API data, these names are put into the "wholist" table.
local player = {}
i = 1
for player in string.gmatch(page, "title...(%w+)")
do
wholist [ i ] = "" .. player
i = i + 1
end
I then run the ipairs/foreach/if functions to see if "wholist" is different to "oldwholist", to see if someone has logged in or out, changes are sent to Output. I'm aware this won't work the first time the script is run, as it needs to run twice to populate both "wholist" and "oldwholist".
for k,v in ipairs (wholist) do
a_inverse[v] = k
end
for k,v in ipairs (oldwholist) do
if a_inverse[v] == nil then
print(v .. " logged out.")
end
b_inverse[v] = k
end
for k,v in ipairs (wholist) do
if b_inverse[v] == nil then
print(v .. " logged in.")
end
end
Next I use the same function as earlier to populate "oldwholist" from the websites data, this means that on the next cycle, "wholist" will be updated before "oldwholist", allowing the above comparing function to work.
i = 1
for player in string.gmatch(page, "title...(%w+)")
do
oldwholist [ i ] = "" .. player
i = i + 1
end
Finally I turn the "wholist" table into a string and send it to a miniwindow.
wholistfinal = table.concat (wholist, ",")
win = "win_" .. GetPluginID ()
WindowCreate (win, 0, 0, 2000, 33, miniwin.pos_top_left, 0, ColourNameToRGB("navy"))
WindowFont (win, "f", "Courier New", 10, true, false, false, false)
WindowText (win, "f", wholistfinal,
2, 0, 0, 0,
ColourNameToRGB ("white"),
false)
WindowShow (win, true)
About 5% of the time, I get the correct log on/off messages, despite the miniwindow changing correctly each time someone logs in or out. I'm currently running this in a timer, it will end up in a loop in a plugin. Also the [ i ] aren't typos, they where turning everything italic.
Thanks for any help or tips to help me neaten the whole code up. |