PtokaX forum

Archive => Archived 5.0 boards => Help with scripts => Topic started by: d4niX on 11 May, 2005, 15:20:33

Title: Convert top 15 chatstats v1 to lua 5
Post by: d4niX on 11 May, 2005, 15:20:33
Hi all,
can somebody help me with this script ?
I need this stats because a like it.

( Syntax Z:\testarea52\scripts\chatstats_v1.lua:599: unexpected symbol near `%' )

http://d4niX.wz.cz/chatstats_v1.rar (http://d4nix.wz.cz/chatstats_v1.rar)

Because  this script is huge I upload it to server.

Thx a lot
Title:
Post by: NightLitch on 11 May, 2005, 15:55:24
I have rewritten it, but I need to upload it 2.
Title: Splitted it
Post by: NightLitch on 11 May, 2005, 15:56:56
Part 1
--------------------------------------------------------------------------------------
-- Top 15 ChatStats - Created by Guibs Apr-15, 2003                  --
--------------------------------------------------------------------------------------

---------------------
-- Version History --
---------------------

-- v1 Added Bigstats.ini, OpStats.ini, UserStats.ini, by Guibs Apr-14, 2003
-- v2 Rewritten to Lua 5 by NightLitch May-09, 2005
-----------------
-- Description --
-----------------

-- Show the top 5 Ops posters & the top 10 users posters
-- Stats are saved all the 10 minutes
-- Users Commands in main chat: ?stats
-- Ops Commands in pm for the stats: ?stats, ?userstats , ?mystats, !loadstats, !savestats, !clearstats, !helps

---------------
-- Greetings --
---------------

-- Helped with the retrobot? v0.99? by tezlo
-- Thks to aMutex for his original ChatStat Script
-- Thks to A.C., [NL]Pur, Dazzler & MatrixX for their helps

------------------------------------
-- Sourcecode is below this point --
------------------------------------

Bot = "Alyx"

TimeSpanInMinutes = 10

-------------------
-- Main function --
-------------------

function Main()

ChatOpStat={}
ChatUserStat={}

frmHub:RegBot(Bot)

tabConfig = parseINI("Stats/Bigstats.ini")
tabOpStats = parseINI("Stats/OpStats.ini")
tabUserStats = parseINI("Stats/UserStats.ini")
cfgStats = getsection(tabConfig,"stats")
InitTable ("Stats/OpStats.ini",tabOpStats,ChatOpStat)
InitTable ("Stats/UserStats.ini",tabUserStats,ChatUserStat)
smEyes = getkey(cfgStats, "eyes")
smNose = getkey(cfgStats, "nose")
smMouth = getkey(cfgStats, "mouth")
SetTimer(TimeSpanInMinutes*60000)
StartTimer()

end

function ChatArrival(user,data)
local tmp = string.sub(data,1,1)
local s, e, str = string.find(data,"%b<>%s*(.*)|")
if not iscommand(str) then
if (user.bOperator) then
DoUpdOpStats(user,str)
else
DoUpdUserStats(user,str)
end
elseif (cmd=="?stats")then
pm = 0
ShowTop15(user)
return 1
elseif (cmd=="!helps")then
if user.bOperator then
ShowHelpStats(user)
return 1
end
end
end

function ToArrival(user,data)
data=strsub(data,1,strlen(data)-1)
local s,e,whoTo,from,cmd = strfind(data,"$To:%s+(%S+)%s+From:%s+(%S+)%s+$%b<>%s+(%S+)")
if (whoTo == Bot) then
if (user.bOperator) then
if (cmd=="!helps")then
ShowHelpStats(user)
elseif (cmd=="!savestats") then
SaveStats(user)
elseif (cmd=="!loadstats") then
LoadStats(user)
elseif (cmd=="!clearstats") then
Reset(user)
elseif (cmd=="?mystats") then
ShowYourStats(user,cmd,args)
elseif (cmd=="?stats")then
pm = 1
ShowTop15(user)
elseif (cmd=="?userstats") then
local s,e,cmd,userName= strfind(data,"%b<>%s+(%S+)%s+(.+)")
if not userName then
user:SendPM(Bot,"***Wrong synthax: ?userstats ")
else
ShowUserStats(user,cmd,userName)
end
end
end
end
end

--------- Update Chat Stats ---------

function DoUpdUserStats(user, str)
local table = getitem(tabUserStats, user.sName)
if not table then table = mkUserStats(user) end
local stats = getvalue(table.tItems, "stats")
local s, e, chars, words, smilies = string.find(stats, "(%d+)|(%d+)|(%d+)")

chars = tonumber(chars)+string.len(str)
words = tonumber(words)+cntargs(str, "(%a+)", 2)
smilies = tonumber(smilies)+cntsmilies(str)

ChatUserStat[user.sName]=chars

putvalue(table.tItems, "stats", chars.."|"..words.."|"..smilies)
end

function DoUpdOpStats(user, str)
local table = getitem(tabOpStats, user.sName)
if not table then table = mkOpStats(user) end
local stats = getvalue(table.tItems, "stats")
local s, e, chars, words, smilies = string.find(stats, "(%d+)|(%d+)|(%d+)")

chars = tonumber(chars)+string.len(str)
words = tonumber(words)+cntargs(str, "(%a+)", 2)
smilies = tonumber(smilies)+cntsmilies(str)

ChatOpStat[user.sName]=chars

putvalue(table.tItems, "stats", chars.."|"..words.."|"..smilies)
end

--------- Show Chat Stats Commands ---------

function ShowHelpStats(user)
user:SendPM(Bot,"\r\n\===============================================\r\nUsers Commands in main chat:\r\n????????????????????????\r\n?stats\t\t- Show the Top 15 Chaters, and the stats user's\r\n\r\nOps Commands in pm for the stats:\r\n????????????????????????????\r\n?stats\t\t- Show the Top 15 Chaters and the stats user's\r\n?userstats \t- Show stats user's\r\n?mystats\t\t- Show ur current stats\r\n!loadstats\t- Load stats\r\n!savestats\t- Saves stats\r\n!clearstats\t- All stats initialized\r\n!helps\t\t- This help again\r\n===============================================")
end

--------- Load Chat Stats ---------

function LoadStats(user)
tabConfig = parseINI("Stats/Bigstats.ini")
tabOpStats = parseINI("Stats/OpStats.ini")
tabUserStats = parseINI("Stats/UserStats.ini")
cfgStats = getsection(tabConfig,"stats")
InitTable ("Stats/OpStats.ini",tabOpStats,ChatOpStat)
InitTable ("Stats/UserStats.ini",tabUserStats,ChatUserStat)
smEyes = getkey(cfgStats, "eyes")
smNose = getkey(cfgStats, "nose")
smMouth = getkey(cfgStats, "mouth")
user:SendPM(Bot,"Chatstat has been successfully loaded ...")
end

--------- Reset Chat Stats ---------

function Reset(user)
ChatOpStat={}
ChatUserStat={}
io.remove("Stats/OpStats.ini")
local handle=io.open("Stats/OpStats.ini","a")
io.write(handle,"\n")
io.close(handle)
remove("Stats/UserStats.ini")
handle=io.open("Stats/UserStats.ini","a")
io.write(handle,"\n")
io.close(handle)
tabConfig = parseINI("Stats/Bigstats.ini")
tabOpStats = parseINI("Stats/OpStats.ini")
tabUserStats = parseINI("Stats/UserStats.ini")
user:SendPM(Bot,"Chat-Stats initialized!")
end

--------- Save Chat Stats ---------

function SaveStats(user)
writeINI("Stats/OpStats.ini",tabOpStats)
writeINI("Stats/UserStats.ini",tabUserStats)
user:SendPM(Bot,"Chatstat has been successfully saved ...")
end

--------- Your Chat Stats ---------

function ShowYourStats(user, cmd, args)
local table = getitem(tabOpStats, user.sName)
if not table then table = mkOpStats(user, "") end
local stats = getkey(table, "stats")
local s, e, chars, words, smilies = string.find(stats, "(%d+)|(%d+)|(%d+)", 1)
user:SendPM(Bot,user.sName.." "..chars.." characters "..words.." words ".. smilies.." smilies")
end

--------- User Chat Stats ---------

function ShowUserStats(user, cmd, userName)
local table = getitem(tabUserStats, userName)
UserError=0
OpError=0
if not table then
UserError=1
else
local stats = getkey(table, "stats")
local s, e, chars, words, smilies = strfind(stats, "(%d+)|(%d+)|(%d+)", 1)
user:SendPM(Bot,userName.." "..chars.." characters "..words.." words ".. smilies.." smilies")
end
local table = getitem(tabOpStats, userName)
if not table then
OpError=1
else
local stats = getkey(table, "stats")
local s, e, chars, words, smilies = strfind(stats, "(%d+)|(%d+)|(%d+)", 1)
user:SendPM(Bot,userName.." "..chars.." characters "..words.." words ".. smilies.." smilies")
end
if UserError==1 and OpError==1 then
user:SendPM(Bot,userName.." is not in my stats file...")
end
end
Title:
Post by: NightLitch on 11 May, 2005, 15:57:54
Part 2

--------- Show Top 15 Chatters ---------

function ShowTop15(user)

local user1, userkey1
local user2, userkey2
local user3, userkey3
local user4, userkey4
local user5, userkey5
local user6, userkey6
local user7, userkey7
local user8, userkey8
local user9, userkey9
local user10, userkey10

local op1, opkey1
local op2, opkey2
local op3, opkey3
local op4, opkey4
local op5, opkey5

userkey1=0
userkey2=0
userkey3=0
userkey4=0
userkey5=0
userkey6=0
userkey7=0
userkey8=0
userkey9=0
userkey10=0

opkey1=0
opkey2=0
opkey3=0
opkey4=0
opkey5=0

for a,b in ChatOpStat do
if (ChatOpStat[a]>opkey1) then
op5=op4
opkey5=opkey4
op4=op3
opkey4=opkey3
op3=op2
opkey3=opkey2
op2=op1
opkey2=opkey1
op1=a
opkey1=ChatOpStat[a]
else
if (ChatOpStat[a]>opkey2) then
op5=op4
opkey5=opkey4
op4=op3
opkey4=opkey3
op3=op2
opkey3=opkey2
op2=a
opkey2=ChatOpStat[a]
else
if (ChatOpStat[a]>opkey3) then
op5=op4
opkey5=opkey4
op4=op3
opkey4=opkey3
op3=a
opkey3=ChatOpStat[a]
else
if (ChatOpStat[a]>opkey4) then
op5=op4
opkey5=opkey4
op4=a
opkey4=ChatOpStat[a]
else
if (ChatOpStat[a]>opkey5) then
op5=a
opkey5=ChatOpStat[a]
end
end
end
end
end
end

opstats1 = ""
opstats2 = ""
opstats3 = ""
opstats4 = ""
opstats5 = ""

if (op1~=nil) then
opstats1 = ("* 1.\t"..ChatOpStat[op1]..":\t"..op1)
end

if (op2~=nil) then
opstats2 = ("\r\n* 2.\t"..ChatOpStat[op2]..":\t"..op2)
end

if (op3~=nil) then
opstats3 = ("\r\n* 3.\t"..ChatOpStat[op3]..":\t"..op3)
end

if (op4~=nil) then
opstats4 = ("\r\n* 4.\t"..ChatOpStat[op4]..":\t"..op4)
end

if (op5~=nil) then
opstats5 = ("\r\n* 5.\t"..ChatOpStat[op5]..":\t"..op5)
end

opstats = opstats1..opstats2..opstats3..opstats4..opstats5

for a,b in ChatUserStat do
if (ChatUserStat[a]>userkey1) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=user5
userkey6=userkey5
user5=user4
userkey5=userkey4
user4=user3
userkey4=userkey3
user3=user2
userkey3=userkey2
user2=user1
userkey2=userkey1
user1=a
userkey1=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey2) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=user5
userkey6=userkey5
user5=user4
userkey5=userkey4
user4=user3
userkey4=userkey3
user3=user2
userkey3=userkey2
user2=a
userkey2=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey3) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=user5
userkey6=userkey5
user5=user4
userkey5=userkey4
user4=user3
userkey4=userkey3
user3=a
userkey3=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey4) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=user5
userkey6=userkey5
user5=user4
userkey5=userkey4
user4=a
userkey4=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey5) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=user5
userkey6=userkey5
user5=a
userkey5=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey6) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=user6
userkey7=userkey6
user6=a
userkey6=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey6) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=user7
userkey8=userkey7
user7=a
userkey7=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey6) then
user10=user9
userkey10=userkey9
user9=user8
userkey9=userkey8
user8=a
userkey8=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey6) then
user10=user9
userkey10=userkey9
user9=a
userkey9=ChatUserStat[a]
else
if (ChatUserStat[a]>userkey6) then
user10=a
userkey10=ChatUserStat[a]
end
end
end
end
end
end
end
end
end
end
end

userstats1 = ""
userstats2 = ""
userstats3 = ""
userstats4 = ""
userstats5 = ""
userstats6 = ""
userstats7 = ""
userstats8 = ""
userstats9 = ""
userstats10 = ""

if (user1~=nil) then
userstats1 = ("* 1.\t"..ChatUserStat[user1]..":\t"..user1)
end

if (user2~=nil) then
userstats2 = ("\r\n* 2.\t"..ChatUserStat[user2]..":\t"..user2)
end

if (user3~=nil) then
userstats3 = ("\r\n* 3.\t"..ChatUserStat[user3]..":\t"..user3)
end

if (user4~=nil) then
userstats4 = ("\r\n* 4.\t"..ChatUserStat[user4]..":\t"..user4)
end

if (user5~=nil) then
userstats5 = ("\r\n* 5.\t"..ChatUserStat[user5]..":\t"..user5)
end

if (user6~=nil) then
userstats6 = ("\r\n* 6.\t"..ChatUserStat[user6]..":\t"..user6)
end

if (user7~=nil) then
userstats7 = ("\r\n* 7.\t"..ChatUserStat[user7]..":\t"..user7)
end

if (user8~=nil) then
userstats8 = ("\r\n* 8.\t"..ChatUserStat[user8]..":\t"..user8)
end

if (user9~=nil) then
userstats9 = ("\r\n* 9.\t"..ChatUserStat[user9]..":\t"..user9)
end

if (user10~=nil) then
userstats10 = ("\r\n*10.\t"..ChatUserStat[user10]..":\t"..user10)
end

userstats = userstats1..userstats2..userstats3..userstats4..userstats5..userstats6..userstats7..userstats8..userstats9..userstats10

chars,words,smilies=nil,nil,nil

if user.bOperator then
local table = getitem(tabOpStats, user.sName)
if not table then table = mkOpStats(user) end
local stat = getkey(table, "stats")
s, e, chars, words, smilies = string.find(stat, "(%d+)|(%d+)|(%d+)", 1)
else
local table = getitem(tabUserStats, user.sName)
if not table then table = mkUserStats(user) end
local stat = getkey(table, "stats")
s, e, chars, words, smilies = string.find(stat, "(%d+)|(%d+)|(%d+)", 1)
end

if pm == 0 then
if user.bOperator then
SendToAll( "Stats","\r\nTop 5 ops posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..opstats.."\r\nTop 10 users posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..userstats.."\r\n\r\nUr current Stats:\r\n"..user.sName.." "..chars.." characters "..words.." words "..smilies.." smilies")
else
user:SendPM(Bot,"\r\nTop 5 ops posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..opstats.."\r\nTop 10 users posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..userstats.."\r\n\r\nUr current Stats:\r\n"..user.sName.." "..chars.." characters "..words.." words "..smilies.." smilies")
SendToAll(Bot,"Ur current Stats:\r\n"..user.sName.." "..chars.." characters "..words.." words "..smilies.." smilies")
end
elseif pm == 1 then
user:SendPM(Bot,"\r\nTop 5 ops posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..opstats.."\r\nTop 10 users posters in this hub : \r\n* Nr.\tPosts:\tName\t\r\n"..userstats.."\r\n\r\nUr current Stats:\r\n"..user.sName.." "..chars.." characters "..words.." words "..smilies.." smilies")
end
end
Title:
Post by: NightLitch on 11 May, 2005, 15:58:34
Part 3

--------- Tables Functions ---------

function mkOpStats(user)
local stats = { sName = user.sName, tItems = {n=0} }
putvalue(stats.tItems, "stats", "0|0|0" )
putitem(tabOpStats, stats) return stats
end

function mkUserStats(user)
local stats = { sName = user.sName, tItems = {n=0} }
putvalue(stats.tItems, "stats", "0|0|0" )
putitem(tabUserStats, stats) return stats
end

function getvalue(table, name)
local id = getid(table, name)
if id then
return table[id].sValue
end
end

function getid(tab, name)
return table.foreachi(tab, function(id, item) if item.sName == %name then return id end end)
end

function cntargs(str, rule, len)
local tmp = getargs(str, rule, len) return tmp.n
end

function getargs(str, rule, len)
local tmp = {n=0}
string.gsub(str, rule, function(s) if string.len(s) >= len then table.insert(tmp, s) end end)
return tmp
end

function getkey(table,  key)
local id = getid(table.tItems, key)
assert(id, "getkey() ["..table.sName.."] "..key)
return table.tItems[id].sValue
end

function getsection(table, section)
local id = getid(table, section)
assert(id, "getkey() ["..section.."]")
return table[id]
end

function getitem(table, name)
local id = getid(table, name)
if id then return table[id] end
end

function parsekeys(keys)
local table = {n=0}
string.gsub(keys, "%s*(%C-)=(%C*)%c*", function(key, value) putvalue(table, key, value) end)
return table
end

function putitem(tab, item)
if not item then return end
local id = getid(tab, item.sName)
if id then
tab[id] = item
else
table.insert(tab, item)
id = tab.n
end return id
end

function putvalue(tab, name, value)
if not value then return end
local id = getid(tab, name)
if id then
tab[id].sValue = value
else
table.insert(tab, { sName = name, sValue = value })
id = table.n
end return id
end

function writeINI(fName, struc)
if not struc then return end
local fHandle = io.open(fName, "w")
if fHandle then for i = 1, struc.n do
local section = struc[i] io.write(fHandle, "\n["..section.sName.."]\n")
for j = 1, section.tItems.n do io.write(fHandle, section.tItems[j].sName.."="..section.tItems[j].sValue.."\n") end
io.write(fHandle, "\n")
end io.close(fHandle) end
end

function parseINI(fName)
local table = {n=0}
local config = readfile(fName)
assert(config, "parseINI() "..fName)
local s, e, sec = string.find(config, "%[(%S+)%]%c", 1)
while sec do local section = { sName = sec, tItems = {n=0} }
s, ke, keys, sec = string.find(config, "(.-)%c%[(%S+)%]%c", e+1)
if not s then s, ke, keys = string.find(config, "(.*)", e+1) else e = ke end
section.tItems = parsekeys(keys) putitem(table, section)
end return table
end

function InitTable(fName,ctable,tname)
local config = readfile(fName)
local s, e, sec = string.find(config, "%[(%S+)%]%c", 1)
if sec then
local table = getitem(ctable, sec)
local stats = getvalue(table.tItems, "stats")
local s, e, chars = string.find(stats, "(%d+)")
tname[sec]=tonumber(chars)
end
while sec do local section = { sName = sec, tItems = {n=0} }
s, ke, keys, sec = string.find(config, "(.-)%c%[(%S+)%]%c", e+1)
if sec then
local table = getitem(ctable, sec)
local stats = getvalue(table.tItems, "stats")
local s, e, chars = string.find(stats, "(%d+)")
tname[sec]=tonumber(chars)
end
if not s then
s, ke, keys = string.find(config, "(.*)", e+1)
else
e = ke
end
end
end

function cntsmilies(str)
local tmp = {n=0}
    string.gsub(str, "(["..smEyes.."])(["..smNose.."]?)(["..smMouth.."])", function(e, n, m) table.insert(tmp, e..n..m) end)
string.gsub(str, "(["..smMouth.."])(["..smNose.."]?)(["..smEyes.."])", function(m, n, e) table.insert(tmp, m..n..e) end)
return tmp.n
end

function readfile(fName)
local fHandle = io.open(fName, "r")
if fHandle then local file = io.read(fHandle, "*a") io.close(fHandle) return file end
end

function iscommand(str)
return string.sub(str, 1, 1) == "!" or string.sub(str, 1, 1) == "?" or string.sub(str, 1, 1) == "+" or string.sub(str, 1, 1) == "#"
end

function OnTimer()
writeINI("Stats/OpStats.ini", tabOpStats)
writeINI("Stats/UserStats.ini", tabUserStats)
end

// NL
Title:
Post by: 6Marilyn6Manson6 on 11 May, 2005, 16:03:23
NightLitch the best NightLitch the best NightLitch the best NightLitch the best NightLitch the best NightLitch the best  hiihhi :D
Title:
Post by: digi on 11 May, 2005, 16:13:37
this is the one i'm looking for..
but still have same error ..
Syntax [string "---------------------------------------------------------------..."]:1163: unexpected symbol near `%'
Title:
Post by: d4niX on 11 May, 2005, 16:24:42
Ohh thats fast ... :-)

thx, but I have same error like digi

Syntax [string "---------------------------------------------------------------..."]:1163: unexpected symbol near `%'


If somebody make changes may send me a mail and I upload it on server.

d4niX.cz@gmail.com (d4nix.cz@gmail.com)

(uhh I must train my english)
Title:
Post by: jiten on 11 May, 2005, 16:43:27
QuoteOriginally posted by d4niX
Ohh thats fast ... :-)

thx, but I have same error like digi

Syntax [string "---------------------------------------------------------------..."]:1163: unexpected symbol near `%'


If somebody make changes may send me a mail and I upload it on server.

d4niX.cz@gmail.com (d4nix.cz@gmail.com)

(uhh I must train my english)
Just open the .lua file, go to that line (1163rd) and remove the % from it.

Cheers
Title:
Post by: d4niX on 11 May, 2005, 17:49:03
Ok,
I remove it and -> \testarea52\scripts\chatstats_v1.lua:1391: bad argument #1 to `read' (invalid option)
Title:
Post by: d4niX on 18 May, 2005, 10:45:43
hola,
somebody know what shall do with this ?