This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/plugins/stats.lua

98 lines
2.3 KiB
Lua
Raw Normal View History

2015-01-01 16:06:43 +01:00
-- Saves the number of messages from a user
-- Can check the number of messages with !stats
do
local socket = require('socket')
2015-01-06 21:04:45 +01:00
local _file_stats = './data/stats.lua'
2015-01-01 16:06:43 +01:00
local _stats
2014-12-31 17:45:52 +01:00
function update_user_stats(msg)
2015-01-01 16:06:43 +01:00
-- Save user to stats table
2014-12-31 17:45:52 +01:00
local from_id = tostring(msg.from.id)
local to_id = tostring(msg.to.id)
local user_name = get_name(msg)
print ('New message from '..user_name..'['..from_id..']'..' to '..to_id)
2014-12-31 17:45:52 +01:00
-- If last name is nil dont save last_name.
local user_last_name = msg.from.last_name
local user_print_name = msg.from.print_name
2015-01-01 16:06:43 +01:00
if _stats[to_id] == nil then
print ('New stats key to_id: '..to_id)
_stats[to_id] = {}
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
if _stats[to_id][from_id] == nil then
print ('New stats key from_id: '..to_id)
_stats[to_id][from_id] = {
2014-12-31 17:45:52 +01:00
name = user_name,
last_name = user_last_name,
print_name = user_print_name,
msg_num = 1
}
else
2015-01-01 16:06:43 +01:00
print ('Updated '..to_id..' '..from_id)
local actual_num = _stats[to_id][from_id].msg_num
_stats[to_id][from_id].msg_num = actual_num + 1
2014-12-31 17:45:52 +01:00
-- And update last_name
2015-01-01 16:06:43 +01:00
_stats[to_id][from_id].last_name = user_last_name
2014-12-31 17:45:52 +01:00
end
end
2015-01-01 16:06:43 +01:00
function read_file_stats( )
local f = io.open(_file_stats, "r+")
2014-12-31 17:45:52 +01:00
-- If file doesn't exists
if f == nil then
2015-01-01 16:06:43 +01:00
-- Create a new empty table
print ('Created user stats file '.._file_stats)
serialize_to_file({}, _file_stats)
2014-12-31 17:45:52 +01:00
else
2015-01-01 16:06:43 +01:00
print ('Stats loaded: '.._file_stats)
f:close()
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
return loadfile (_file_stats)()
2014-12-31 17:45:52 +01:00
end
2015-01-01 16:06:43 +01:00
local function save_stats()
2014-11-23 00:31:22 +01:00
-- Save stats to file
2015-01-01 16:06:43 +01:00
serialize_to_file(_stats, _file_stats)
2014-12-31 17:45:52 +01:00
end
2014-11-23 00:31:22 +01:00
2015-01-01 16:06:43 +01:00
local function get_stats_status( msg )
-- vardump(stats)
2014-11-22 20:17:33 +01:00
local text = ""
2014-12-14 21:28:32 +01:00
local to_id = tostring(msg.to.id)
2015-01-01 16:06:43 +01:00
for id, user in pairs(_stats[to_id]) do
2014-11-23 14:34:46 +01:00
if user.last_name == nil then
2014-12-14 21:28:32 +01:00
text = text..user.name.." ["..id.."]: "..user.msg_num.."\n"
2014-11-23 14:34:46 +01:00
else
2014-12-14 21:28:32 +01:00
text = text..user.name.." "..user.last_name.." ["..id.."]: "..user.msg_num.."\n"
2014-11-23 14:34:46 +01:00
end
2014-11-22 20:17:33 +01:00
end
2014-12-31 17:45:52 +01:00
print("usuarios: "..text)
2014-11-22 20:17:33 +01:00
return text
end
2015-01-01 16:06:43 +01:00
local function run(msg, matches)
if matches[1] == "stats" then -- Hack
return get_stats_status(msg)
2014-12-31 17:45:52 +01:00
else
print ("update stats")
update_user_stats(msg)
2015-01-01 16:06:43 +01:00
save_stats()
2014-12-31 17:45:52 +01:00
end
end
2015-01-01 16:06:43 +01:00
_stats = read_file_stats()
2014-12-31 17:45:52 +01:00
2014-11-22 20:17:33 +01:00
return {
description = "Numer of messages by user",
usage = "!stats",
2014-12-31 17:45:52 +01:00
patterns = {
2015-01-01 16:06:43 +01:00
".*",
"^!(stats)"
2014-12-31 17:45:52 +01:00
},
2014-11-22 20:17:33 +01:00
run = run
2015-01-01 16:06:43 +01:00
}
end