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

114 lines
2.8 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] = {
2015-02-02 23:00:08 +01:00
user_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
2015-02-02 23:00:08 +01:00
_stats[to_id][from_id].user_id = from_id
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()
2015-02-11 22:00:00 +01:00
-- Save stats to file
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 )
2015-02-11 22:00:00 +01:00
-- vardump(stats)
local text = ""
2015-02-02 23:00:08 +01:00
local to_id = tostring(msg.to.id)
2015-02-11 22:00:00 +01:00
local rank = {}
2014-12-14 21:28:32 +01:00
2015-02-11 22:00:00 +01:00
for id, user in pairs(_stats[to_id]) do
table.insert(rank, user)
end
2015-02-02 23:00:08 +01:00
2015-02-11 22:00:00 +01:00
table.sort(rank, function(a, b)
if a.msg_num and b.msg_num then
return a.msg_num > b.msg_num
end
end
)
2015-02-02 23:00:08 +01:00
2015-02-11 22:00:00 +01:00
for id, user in pairs(rank) do
2015-02-02 23:00:08 +01:00
-- Previous versions didn't save that
user_id = user.user_id or ''
2015-02-11 22:00:00 +01:00
print(">> ", id, user.name)
if user.last_name == nil then
text = text..user.name.." ["..user_id.."]: "..user.msg_num.."\n"
else
text = text..user.name.." "..user.last_name.." ["..user_id.."]: "..user.msg_num.."\n"
end
end
print("usuarios: "..text)
return text
2014-11-22 20:17:33 +01:00
end
2015-01-01 16:06:43 +01:00
local function run(msg, matches)
2015-02-11 22:00:00 +01:00
if matches[1] == "stats" then -- Hack
return get_stats_status(msg)
else
print ("update stats")
update_user_stats(msg)
2015-01-01 16:06:43 +01:00
save_stats()
2015-02-11 22:00:00 +01:00
end
2014-12-31 17:45:52 +01:00
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 = "Plugin to update user stats.",
usage = "!stats: Returns a list of Username [telegram_id]: msg_num",
patterns = {
".*",
"^!(stats)"
},
run = run
2015-01-01 16:06:43 +01:00
}
end