Help with chatstat script
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Help with chatstat script

Started by suikerblondie, 24 April, 2005, 23:34:14

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

suikerblondie

Hi all. i'm trying to convert this script to lua 5, but it gives me many errors on Syntax I:\PtokaxLua5\scripts\Chatstats.lua:102: attempt to call field `sort' (a nil value)
Syntax I:\PtokaxLua5\scripts\Chatstats.lua:166: attempt to call global `write' (a nil value). What i'm doing wrong?
--Chat stats by chill
--Serialisation by RabidWombat

----------------------------------------------------------
----------------------------------------------------------
ChatConf = {
	bot = "chatstats",		-- The Name of the bot.
	cmd1 = "!showstats",		-- Shows the Stats.
	Max1 = 10,			-- How Many top chatters are shown of each genre.
	howToSort = 2,		-- Stands for the sorting either 1= least first, 2 = highest first.
	howToSend = 1,		-- 1 = Sends to all, 2 = Sends to user, 3 = PM to the user, 4 = PM to the user and regs a bot.
	StatsFile = "ChatStats.txt",	-- The Filename
	StatsFolder = "txt",		-- The Folder
}
----------------------------------------------------------
----------------------------------------------------------

---------------------------------------------------------------------------------------

--	MAIN SCRIPT

---------------------------------------------------------------------------------------
ChatStats = {}
dofile(ChatConf.StatsFolder.."/"..ChatConf.StatsFile)
----------------------------------------------------------
chatEmotions = {
	[":)"] = 1,
	[":-)"] = 1,
	[":("] = 1,
	[":-("] = 1,
	[";)"] = 1,
	[";-)"] = 1,
	[":-p"] = 1,
	[":p"] = 1,
	[":D"] = 1,
	[":-D"] = 1,
}
----------------------------------------------------------
function Main()
	frmHub:UnregBot(ChatConf.bot)
	if ChatConf.howToSend == 4 then
		frmHub:RegBot(ChatConf.bot)
	end
	SetTimer(1000*60*5)
	StartTimer()
end
----------------------------------------------------------
function OnTimer()
	WriteTable(ChatStats,"ChatStats",ChatConf.StatsFile)
end
----------------------------------------------------------
function ChatArrival(curUser,data)

	if string.sub(data,1,1) == "<" then

		if not ChatStats[curUser.sName] then
			ChatStats[curUser.sName] = {}
			ChatStats[curUser.sName].words = 0
			ChatStats[curUser.sName].chars = 0
			ChatStats[curUser.sName].emotions = 0
		end

		data = string.sub(data,string.len(curUser.sName)+4,string.len(data)-1)

		string.gsub(data,"(%S+)",function(w)
			ChatStats[curUser.sName].chars = ChatStats[curUser.sName].chars + string.len(w)
			if chatEmotions[w] then
				ChatStats[curUser.sName].emotions = ChatStats[curUser.sName].emotions + 1
			else
				ChatStats[curUser.sName].words = ChatStats[curUser.sName].words + 1
			end
		end)

		local _,_,word1 = string.find(data,"^(%S+)")

		if word1 and STATSFUNC[word1] then
			if ChatConf.howToSend == 1 then
				SendToAll(ChatConf.bot,STATSFUNC[word1]())
			elseif ChatConf.howToSend == 2 then
				curUser:SendData(ChatConf.bot,STATSFUNC[word1]())
			elseif (ChatConf.howToSend == 3) or (ChatConf.howToSend == 4) then
				curUser:SendPM(ChatConf.bot,STATSFUNC[word1]())
			end
		end

	end
end
---------------------------------------------------------------------------------------

--	SORTING TABLE

---------------------------------------------------------------------------------------
function doSortTable(table,field)
	if ChatConf.howToSort == 1 then
		if field == "ALL" then
			table.sort(table, function(a,b) return((a[2].chars + a[2].words + a[2].emotions)  < (b[2].chars + b[2].words + b[2].emotions)) end)
		else
			table.sort(table, function(a,b) return(a[2][field] < b[2][field]) end)
		end
	elseif ChatConf.howToSort == 2 then
		if field == "ALL" then
			table.sort(table, function(a,b) return((a[2].chars + a[2].words + a[2].emotions)  > (b[2].chars + b[2].words + b[2].emotions)) end)
		else
			table.sort(table, function(a,b) return(a[2][field] > b[2][field]) end)
		end
	end
	return table
end
---------------------------------------------------------------------------------------

--	TABLE FUNCTIONS

---------------------------------------------------------------------------------------
STATSFUNC = {
	[ChatConf.cmd1] = function()

		local sTable = {}
		table.foreach(ChatStats, function(i,v) table.insert(sTable,{i,v}) end)
		local msg = "\r\n\r\n-----  Current Chat Stats ------\r\n"

		sTable = doSortTable(sTable,"ALL")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Overall Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", "..(sTable[i][2].chars + sTable[i][2].words + sTable[i][2].emotions).." overall,  "..sTable[i][2].chars.." chars, "..sTable[i][2].words.." words, "..sTable[i][2].emotions.." emotions.\r\n"
			end
		end
		sTable = doSortTable(sTable,"chars")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Char Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].chars.." chars.\r\n"
			end
		end
		sTable = doSortTable(sTable,"words")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Word Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].words.." words.\r\n"
			end
		end
		sTable = doSortTable(sTable,"emotions")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Emotion Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].emotions.." emotions.\r\n"
			end
		end
		return(msg)

	end,
}
---------------------------------------------------------------------------------------

--	Write Tables

---------------------------------------------------------------------------------------
function WriteTable(table,tablename,file)
	local handle = io.open(ChatConf.StatsFolder.."/"..file,"w")
	Serialize(table,tablename,handle)
  	io.close(handle)
end
--------------------------------------------
function Serialize(tTable,sTableName,hFile,sTab)
	sTab = sTab or "";
	write(hFile,sTab..sTableName.." = {\n");
	for key,value in tTable do
		local sKey = (type(key) == "string") and format("[%q]",key) or format("[%d]",key);
		if(type(value) == "table") then
			Serialize(value,sKey,hFile,sTab.."\t");
		else
			local sValue = (type(value) == "string") and format("%q",value) or tostring(value);
			write(hFile,sTab.."\t"..sKey.." = "..sValue);
		end
		write(hFile,",\n");
	end
	write(hFile,sTab.."}");
end
 


Anyone who can help me???

Dessamator

Ignorance is Bliss.

suikerblondie

Sorry Dessamator, i like the old chatstats more. I wanna use it. The new one is not the one i wanna use. Anyone els can help me please?

jiten

#3
Try this:
-- Conversion to Lua 5 by jiten
--Chat stats by chill
--Serialisation by RabidWombat

----------------------------------------------------------
----------------------------------------------------------
ChatConf = {
	bot = "-ChatStats-",		-- The Name of the bot.
	cmd1 = "+showstats",		-- Shows the Stats.
	Max1 = 10,			-- How Many top chatters are shown of each genre.
	howToSort = 2,		-- Stands for the sorting either 1= least first, 2 = highest first.
	howToSend = 1,		-- 1 = Sends to all, 2 = Sends to user, 3 = PM to the user, 4 = PM to the user and regs a bot.
	StatsFile = "ChatStats.txt",	-- The Filename
	StatsFolder = "logs",		-- The Folder
}
----------------------------------------------------------
----------------------------------------------------------

---------------------------------------------------------------------------------------

--	MAIN SCRIPT

---------------------------------------------------------------------------------------
ChatStats = {}

----------------------------------------------------------
chatEmotions = {
	[":)"] = 1,
	[":-)"] = 1,
	[":("] = 1,
	[":-("] = 1,
	[";)"] = 1,
	[";-)"] = 1,
	[":-p"] = 1,
	[":p"] = 1,
	[":D"] = 1,
	[":-D"] = 1,
}
----------------------------------------------------------
function Main()
	local f = io.open( ChatConf.StatsFolder.."/"..ChatConf.StatsFile )
	if f then
		f:close();
		dofile(ChatConf.StatsFolder.."/"..ChatConf.StatsFile)
	end
	if ChatConf.howToSend == 4 then
		frmHub:RegBot(ChatConf.bot)
	end
	SetTimer(1000*60*5)
	StartTimer()
end
----------------------------------------------------------
function OnTimer()
	WriteTable(ChatStats,"ChatStats",ChatConf.StatsFolder.."/"..ChatConf.StatsFile)
end
----------------------------------------------------------
function ChatArrival(curUser,data)

	if not ChatStats[curUser.sName] then
		ChatStats[curUser.sName] = {}
		ChatStats[curUser.sName].words = 0
		ChatStats[curUser.sName].chars = 0
		ChatStats[curUser.sName].emotions = 0
	end

	data = string.sub(data,string.len(curUser.sName)+4,string.len(data)-1)

	string.gsub(data,"(%S+)",function(w)
		ChatStats[curUser.sName].chars = ChatStats[curUser.sName].chars + string.len(w)
		if chatEmotions[w] then
			ChatStats[curUser.sName].emotions = ChatStats[curUser.sName].emotions + 1
		else
			ChatStats[curUser.sName].words = ChatStats[curUser.sName].words + 1
		end
	end)

	local _,_,word1 = string.find(data,"^(%S+)")

	if word1 and STATSFUNC[word1] then
		if ChatConf.howToSend == 1 then
			SendToAll(ChatConf.bot,STATSFUNC[word1]())
		elseif ChatConf.howToSend == 2 then
			curUser:SendData(ChatConf.bot,STATSFUNC[word1]())
		elseif (ChatConf.howToSend == 3) or (ChatConf.howToSend == 4) then
			curUser:SendPM(ChatConf.bot,STATSFUNC[word1]())
		end
		return 1
	end
end
---------------------------------------------------------------------------------------

--	SORTING TABLE

---------------------------------------------------------------------------------------
function doSortTable(Table,field)
	if ChatConf.howToSort == 1 then
		if field == "ALL" then
			table.sort(Table, function(a,b) return((a[2].chars + a[2].words + a[2].emotions)  < (b[2].chars + b[2].words + b[2].emotions)) end)
		else
			table.sort(Table, function(a,b) return(a[2][field] < b[2][field]) end)
		end
	elseif ChatConf.howToSort == 2 then
		if field == "ALL" then
			table.sort(Table, function(a,b) return((a[2].chars + a[2].words + a[2].emotions)  > (b[2].chars + b[2].words + b[2].emotions)) end)
		else
			table.sort(Table, function(a,b) return(a[2][field] > b[2][field]) end)
		end
	end
	return Table
end
---------------------------------------------------------------------------------------

--	TABLE FUNCTIONS

---------------------------------------------------------------------------------------
STATSFUNC = {
	[ChatConf.cmd1] = function()

		local sTable = {}
		table.foreach(ChatStats, function(i,v) table.insert(sTable,{i,v}) end)
		local msg = "-----  Current Chat Stats ------\r\n"

		sTable = doSortTable(sTable,"ALL")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Overall Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", "..(sTable[i][2].chars + sTable[i][2].words + sTable[i][2].emotions).." overall,  "..sTable[i][2].chars.." chars, "..sTable[i][2].words.." words, "..sTable[i][2].emotions.." emotions.\r\n"
			end
		end
		sTable = doSortTable(sTable,"chars")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Char Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].chars.." chars.\r\n"
			end
		end
		sTable = doSortTable(sTable,"words")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Word Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].words.." words.\r\n"
			end
		end
		sTable = doSortTable(sTable,"emotions")
		msg = msg.."\r\n\tTop "..ChatConf.Max1.." Emotion Stats\r\n\r\n"
		for i = 1,ChatConf.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].emotions.." emotions.\r\n"
			end
		end
		return(msg)

	end,
}
---------------------------------------------------------------------------------------

--	Write Tables

---------------------------------------------------------------------------------------
function Serialize(tTable, sTableName, sTab) 
        assert(tTable, "tTable equals nil"); 
        assert(sTableName, "sTableName equals nil"); 
 
        assert(type(tTable) == "table", "tTable must be a table!"); 
        assert(type(sTableName) == "string", "sTableName must be a string!"); 
 
        sTab = sTab or ""; 
        sTmp = "" 
 
        sTmp = sTmp..sTab..sTableName.." = {\n" 
 
        for key, value in tTable do 
                local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key); 
 
                if(type(value) == "table") then 
                        sTmp = sTmp..Serialize(value, sKey, sTab.."\t"); 
                else 
                        local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value); 
                        sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue 
                end 
 
                sTmp = sTmp..",\n" 
        end 
 
        sTmp = sTmp..sTab.."}" 
        return sTmp 
end 
 
function WriteTable(table,tablename,file) 
	local handle = io.open(file,"w+") 
	handle:write(Serialize(table, tablename)) 
	handle:flush() 
	handle:close() 
end
*Edit* Fixed read/write bug
Cheers

Herodes

try this ..
--Chat stats by chill
--Serialisation by RabidWombat
-- Lua5 conversion by Herodes
--- optimised it a bit..
tChat = {
	tC = { ---------------------------------------------------------- Configure the script here
		bot = "chatstats",		-- The Name of the bot.
		Max1 = 10, 				-- How Many top chatters are shown of each genre
		howToSort = 2, 			-- Stands for the sorting either 1:least first / 2:highest first
		howToSend = 1, 		-- 1:Sends to all / 2:Sends to user / 3:PM to the user / 4:PM to the user and regs a bot.
		StatsFile = "ChatStats.txt",	-- The Filename
	},
	tS = {},
}
---------------------------------------------------------------------------------------
--	MAIN SCRIPT
---------------------------------------------------------------------------------------
dofile( tChat.tC.StatsFile)
----------------------------------------------------------
tEmoticons = {
	[":)"] = 1,
	[":-)"] = 1,
	[":("] = 1,
	[":-("] = 1,
	[";)"] = 1,
	[";-)"] = 1,
	[":-p"] = 1,
	[":p"] = 1,
	[":D"] = 1,
	[":-D"] = 1,
}
----------------------------------------------------------
function Main()
	local f = io.open( tChat.tC.StatsFile )
	if f then f:close(); dofile(tChat.tC.StatsFile) end

	if (tChat.tC.howToSend == 4) then frmHub:RegBot( tChat.tC.bot ) end
	SetTimer(1000*60*5)
	StartTimer()
end
----------------------------------------------------------
function OnTimer()
	local handle = io.open( tChat.tC.StatsFile ,"w+" )
	handle:write( Serialize( tChat.tS , "tChat.tS" ) ); handle:close()
end
----------------------------------------------------------
function ChatArrival( user, data )
	if not tChat.tS[user.sName] then
		tChat.tS[user.sName] = { words = 0, chars = 0, emoticons = 0 };
	end

	local t = tChat.tS[user.sName]

	local data = string.sub(data, 1, -1)

	local s,e, message = string.find( data, "%b<>%s(.+)" )

	tChat.tS[user.sName].chars = tChat.tS[user.sName].chars + string.len( message )

	for s in string.gfind( data, "%s*(%S+)%s*") do
		if tEmoticons[s] then
			tChat.tS[user.sName].emoticons = tChat.tS[user.sName].emoticons + 1
		else
			tChat.tS[user.sName].words = tChat.tS[user.sName].words + 1
		end
	end

	if string.find( data, "%b<>%s+[%+%!%?%-]showstats" ) then

		local function doSortTable( tTable, field )
			if tChat.tC.howToSort == 1 then
				if field == "all" then
					table.sort( tTable, function( a, b ) return((a[2].chars + a[2].words + a[2].emoticons)  < (b[2].chars + b[2].words + b[2].emoticons)) end ); return tTable;
				end
				table.sort( tTable, function( a, b ) return(a[2][field] < b[2][field]) end ); return tTable;
			elseif tChat.tC.howToSort == 2 then
				if field == "all" then
					table.sort( tTable, function( a, b ) return((a[2].chars + a[2].words + a[2].emoticons)  > (b[2].chars + b[2].words + b[2].emoticons)) end ); return tTable;
				end
				table.sort( tTable, function( a, b ) return(a[2][field] > b[2][field]) end ); return tTable;
			end
		end;

		local sTable = {}
		table.foreach( tChat.tS , function( i, v ) table.insert( sTable, { i, v } ) end );

		local msg = "\r\n\r\n-----  Current Chat Stats ------\r\n"
		sTable = doSortTable( sTable, "all" )
		msg = msg.."\r\n\tTop "..tChat.tC.Max1.." Overall Stats\r\n\r\n"
		for i = 1,tChat.tC.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", "..(sTable[i][2].chars + sTable[i][2].words + sTable[i][2].emoticons).." overall,  "..sTable[i][2].chars.." chars, "..sTable[i][2].words.." words, "..sTable[i][2].emoticons.." emoticons.\r\n"
			end
		end

		sTable = doSortTable( sTable, "chars" )
		msg = msg.."\r\n\tTop "..tChat.tC.Max1.." Char Stats\r\n\r\n"
		for i = 1,tChat.tC.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].chars.." chars.\r\n"
			end
		end

		sTable = doSortTable( sTable, "words" )
		msg = msg.."\r\n\tTop "..tChat.tC.Max1.." Word Stats\r\n\r\n"
		for i = 1,tChat.tC.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].words.." words.\r\n"
			end
		end

		sTable = doSortTable( sTable, "emoticons" )
		msg = msg.."\r\n\tTop "..tChat.tC.Max1.." Emotion Stats\r\n\r\n"
		for i = 1,tChat.tC.Max1 do
			if sTable[i] then
				msg = msg.."\t# "..i.."  -  "..sTable[i][1]..", with "..sTable[i][2].emoticons.." emoticons.\r\n"
			end
		end

		if tChat.tC.howToSend == 1 then  		SendToAll( tChat.tC.bot, msg )
		elseif tChat.tC.howToSend == 2 then 	user:SendData( tChat.tC.bot, msg )
		elseif (tChat.tC.howToSend == 3) or (tChat.tC.howToSend == 4) then  user:SendPM( tChat.tC.bot, msg )
		end
		return 1
	end
end

---------------------------------------------------------------------------------------
--	Write Tables
---------------------------------------------------------------------------------------
function Serialize(tTable, sTableName, sTab)
	sTab = sTab or "";
	sTmp = ""
	sTmp = sTmp..sTab..sTableName.." = {\n"
	for key, value in tTable do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
		if(type(value) == "table") then
			sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
			sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
		end

		sTmp = sTmp..",\n"
	end
	sTmp = sTmp..sTab.."}"
	return sTmp
end

suikerblondie

Thnx for helping me. The script jiten fixed do not read, write and save anything to the chatstats.txt. After the command in the main it shows me only the stats without the rest i typed before in the main  ?(

The one herodes write has the same problem with lin 102 sort by table like the script i had.

Is it possible to fix it?

jiten

QuoteOriginally posted by suikerblondie
Thnx for helping me. The script jiten fixed do not read, write and save anything to the chatstats.txt. After the command in the main it shows me only the stats without the rest i typed before in the main  ?(

The one herodes write has the same problem with lin 102 sort by table like the script i had.

Is it possible to fix it?
I've tested Herodes one, and it's working well. It doesn't give me that error.
Mine needs some changes...

Cheers

jiten

First post updated with fixed script.

Best regards,

jiten

suikerblondie

QuoteFirst post updated with fixed script.

Best regards,

jiten
 

Thnx m8. It works now and have found what you changed. I will remember it for when i see the same troubles in another script that i will change for lua 5.

Grtzzz
suikerblondie

jiten


SMF spam blocked by CleanTalk