stats with local scope

This commit is contained in:
yago 2015-01-01 16:06:43 +01:00
parent a889d410f1
commit e22d2670fd

View File

@ -1,5 +1,14 @@
-- Saves the number of messages from a user
-- Can check the number of messages with !stats
do
local socket = require('socket')
local _file_stats = './res/stats.lua'
local _stats
function update_user_stats(msg) function update_user_stats(msg)
-- Save user to _users table -- Save user to stats table
local from_id = tostring(msg.from.id) local from_id = tostring(msg.from.id)
local to_id = tostring(msg.to.id) local to_id = tostring(msg.to.id)
local user_name = get_name(msg) local user_name = get_name(msg)
@ -7,54 +16,53 @@ function update_user_stats(msg)
-- If last name is nil dont save last_name. -- If last name is nil dont save last_name.
local user_last_name = msg.from.last_name local user_last_name = msg.from.last_name
local user_print_name = msg.from.print_name local user_print_name = msg.from.print_name
if _users[to_id] == nil then if _stats[to_id] == nil then
_users[to_id] = {} print ('New stats key to_id: '..to_id)
_stats[to_id] = {}
end end
if _users[to_id][from_id] == nil then if _stats[to_id][from_id] == nil then
_users[to_id][from_id] = { print ('New stats key from_id: '..to_id)
_stats[to_id][from_id] = {
name = user_name, name = user_name,
last_name = user_last_name, last_name = user_last_name,
print_name = user_print_name, print_name = user_print_name,
msg_num = 1 msg_num = 1
} }
else else
local actual_num = _users[to_id][from_id].msg_num print ('Updated '..to_id..' '..from_id)
_users[to_id][from_id].msg_num = actual_num + 1 local actual_num = _stats[to_id][from_id].msg_num
_stats[to_id][from_id].msg_num = actual_num + 1
-- And update last_name -- And update last_name
_users[to_id][from_id].last_name = user_last_name _stats[to_id][from_id].last_name = user_last_name
end end
end end
function load_user_stats() function read_file_stats( )
local f = io.open('res/users.json', "r+") local f = io.open(_file_stats, "r+")
-- If file doesn't exists -- If file doesn't exists
if f == nil then if f == nil then
f = io.open('res/users.json', "w+") -- Create a new empty table
f:write("{}") -- Write empty table print ('Created user stats file '.._file_stats)
f:close() serialize_to_file({}, _file_stats)
return {}
else else
local c = f:read "*a" print ('Stats loaded: '.._file_stats)
f:close() f:close()
return json:decode(c)
end end
return loadfile (_file_stats)()
end end
function save_stats()
local function save_stats()
-- Save stats to file -- Save stats to file
local json_users = json:encode_pretty(_users) serialize_to_file(_stats, _file_stats)
vardump(json_users)
file_users = io.open ("./res/users.json", "w")
file_users:write(json_users)
file_users:close()
end end
function get_stats_status( msg ) local function get_stats_status( msg )
-- vardump(_users) -- vardump(stats)
local text = "" local text = ""
local to_id = tostring(msg.to.id) local to_id = tostring(msg.to.id)
for id, user in pairs(_users[to_id]) do for id, user in pairs(_stats[to_id]) do
if user.last_name == nil then if user.last_name == nil then
text = text..user.name.." ["..id.."]: "..user.msg_num.."\n" text = text..user.name.." ["..id.."]: "..user.msg_num.."\n"
else else
@ -65,25 +73,28 @@ function get_stats_status( msg )
return text return text
end end
function run(msg, matches) local function run(msg, matches)
-- TODO: I need to know wich patterns matches. if matches[1] == "stats" then -- Hack
if matches[1] == "!stats" then return get_stats_status(msg)
return get_stats_status(msg)
else else
print ("update stats") print ("update stats")
update_user_stats(msg) update_user_stats(msg)
print(socket.gettime())
save_stats()
print(socket.gettime())
end end
end end
-- TODO: local vars _stats = read_file_stats()
_users = load_user_stats()
return { return {
description = "Numer of messages by user", description = "Numer of messages by user",
usage = "!stats", usage = "!stats",
patterns = { patterns = {
".*", ".*",
"^!stats" "^!(stats)"
}, },
run = run run = run
} }
end