PtokaX forum

Archive => Archived 4.0 boards => Request for Lua 4 scripts => Topic started by: Madman on 17 July, 2004, 16:18:16

Title: chatstats send text when enter
Post by: Madman on 17 July, 2004, 16:18:16
-- chatstats v2
-- by tezlo
-- modified by tezlo and Optimus
-- modified by Madman for his "needs" :p
-- Thanks nErBoS For the help //Madman

Bot = "-=Stats=-"

--everybody can..
--?mystats
--?userstats
--?stats [category] (there are 4 categories.. posts chars --words smilies)

--OPs also can..
--!savestats and !loadstats (obsolete.. stats get saved --automatically)
--!clearstats

eyes = ":;8B="
nose = "-o'"
mouth = "%[%)%(%]DpP"
-- modify the above to fit your needs!

stats = {}
names = { posts = 1, chars = 2, words = 3, smilies = 4 }
-- can you think of something else to count? post up


function Main()
loadStats()
end

function OnExit()
saveStats()
end

function DataArrival(user, data)
if strsub(data, 1, 1) == "<" then
local s, e, str = strfind(data, "^%b<> (.*)%|$")
local s, e, cmd, args = strfind(str, "^([%!%?]%a+)%s*(.*)$")
if s then
cmd = strlower(cmd)
if cmd == "?mystats" then cmdStats(user, user.sName)
elseif cmd == "?userstats" then cmdStats(user, args)
elseif cmd == "?stats" then cmdTopStats(user, args)
elseif cmd == "!savestats" and (user.iProfile == 0) then saveStats(user)
elseif cmd == "!loadstats" and (user.iProfile == 0)  then loadStats(user)
elseif cmd == "!clearstats" and (user.iProfile == 0)  then clearStats(user)
else return end return 1
elseif (strsub(data, 1, 7) ~= "<-=Fake" and strsub(data, 1, 7) ~= " updStats(user.sName, str)
end
end
end

function cmdStats(user, target)
local tmp = stats[target]
if tmp then user:SendData(">>> chatstats for "..target..": "..tmp[1].." posts "..tmp[2].." characters "..tmp[3].." words "..tmp[4].." smilies")
else user:SendData(">>> no record for "..target) end
end

function cmdTopStats(user, args)
local id = names[args] or 1
local index = sortStats(id)
local chat = ""
chat = chat.."<-=Stats=-> "..user.sName.." Checked the stats\r\n\r\n\t============== -=Current Top 20 Chatstats=- ==============\r\n"
chat = chat.."\tNr:\tPosts:\tChartrs:\tWords:\tSmilies:\tNick:\r\n"
local n = getn(index)
if n > 20 then n = 20 end
for i = 1, n do
local nick = index[i]
local tmp = stats[nick]
chat = chat.."\t"..i..".\t "..tmp[id].."\t "..tmp[2].."\t "..tmp[3].."\t "..tmp[4].."\t"..nick.."\r\n"
end
SendToAll(chat)
end


function loadStats(user)
stats = dofile("chatstats.dat") or {}
if user then user:SendData(">>> Stas Loaded") end
end

function saveStats(user)
local f = openfile("chatstats.dat", "w+")
assert(f, "chatstats.dat")
write(f, "return {\n")
for nick, table in stats do
write(f, "\t"..format("[%q]", nick).." = { "..table[1], ", "..table[2]..", "..table[3]..", "..table[4].." },\n")
end write(f, "}") closefile(f)
if user then user:SendData(">>> Stats Saved") end
end

function clearStats(user)
stats = {}
if user then user:SendData(">>> Stats cleared") end
end

function sortStats(id)
local index = {n=0}
foreach(stats, function(nick, tmp) tinsert(%index, nick) end)
local f = function(x, y) return stats[x][%id] > stats[y][%id] end
sort(index, f) return index
end

function updStats(nick, str)
local tmp = stats[nick] or {0,0,0,0}
tmp[1], tmp[2], tmp[3], tmp[4] = tmp[1]+1, tmp[2]+strlen(str), tmp[3]+cntargs(str, "(%a+)"), tmp[4]+cntsmilies(str)
stats[nick] = tmp
end

function cntargs(str, rule)
local s, n = gsub(str, rule, "")
return n
end

function cntsmilies(str)
return cntargs(str, "(["..eyes.."]["..nose.."]?["..mouth.."])") + cntargs(str, "(["..mouth.."]["..nose.."]?["..eyes.."])")
end

Ok, This is the Chatstats i use....
Now, i dont know if it is possibole or not....
But i want when a user connects to the hub and if he has 100 to 199 posts then it sends a text like
The chatoholic has entred
and if the user have 200 to 299 posts it says
Non stop talking is in the building....

Hope it can be done

//A chat addicted Madman ;p
Title:
Post by: nErBoS on 17 July, 2004, 23:59:27
Hi,

Let me see if i understood right, you want the script to count the number chats that a nick made ???

Best regrads, nErBoS
Title:
Post by: Madman on 18 July, 2004, 01:07:16
Yes.... something like that....
The script already keep track of nr of Posts, Chartrs, Words and Smilies....

What i want is when a user that have posted between 100 to 199 posts entres the hub it says
The chatoholic has entred
and if a user that have posted
200 to 299 posts it says
Non stop talking is in the building....

hope that clear thing up for ya =)
Title:
Post by: Madman on 18 July, 2004, 12:48:18
Thanks.. That is what i wanted =)

One thing tho...
i changed 100 and 200 to 10 and 20.. just to test....
and to get the text
Non stop talking is in the building...
I have to post 21 posts...
but no problem... i just set
postcount1 = 20
to
postcount1 = 19
same thing with postcount2
but... no problem... just wanted you to know.... =)

*edit*

Found a little error...
I just addaed postcount3
so i have not done this error....i think...
It is when a user that have no posts enters....
then it shows this....

Syntax error: attempt to index local `tmp' (a nil value)
stack traceback:
   1:  function `NewUserConnected' at line 44
Title:
Post by: Madman on 18 July, 2004, 21:21:32
Thanks =)
It seems to work just fine now =)