2015-03-31 20:17:14 +02:00
|
|
|
do
|
|
|
|
|
2015-02-25 21:03:25 +01:00
|
|
|
local _file_votes = './data/votes.lua'
|
|
|
|
|
|
|
|
function read_file_votes ()
|
2015-03-31 20:17:14 +02:00
|
|
|
local f = io.open(_file_votes, "r+")
|
|
|
|
if f == nil then
|
|
|
|
print ('Created voting file '.._file_votes)
|
|
|
|
serialize_to_file({}, _file_votes)
|
|
|
|
else
|
|
|
|
print ('Values loaded: '.._file_votes)
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
return loadfile (_file_votes)()
|
2015-02-25 21:03:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function clear_votes (chat)
|
2015-03-31 20:17:14 +02:00
|
|
|
local _votes = read_file_votes ()
|
|
|
|
_votes [chat] = {}
|
|
|
|
serialize_to_file(_votes, _file_votes)
|
2015-02-25 21:03:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function votes_result (chat)
|
2015-03-31 20:17:14 +02:00
|
|
|
local _votes = read_file_votes ()
|
|
|
|
local results = {}
|
|
|
|
local result_string = ""
|
|
|
|
if _votes [chat] == nil then
|
|
|
|
_votes[chat] = {}
|
|
|
|
end
|
|
|
|
for user,vote in pairs (_votes[chat]) do
|
|
|
|
if (results [vote] == nil) then
|
|
|
|
results [vote] = user
|
|
|
|
else
|
|
|
|
results [vote] = results [vote] .. ", " .. user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for vote,users in pairs (results) do
|
|
|
|
result_string = result_string .. vote .. " : " .. users .. "\n"
|
|
|
|
end
|
|
|
|
return result_string
|
2015-02-25 21:03:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function save_vote(chat, user, vote)
|
2015-03-31 20:17:14 +02:00
|
|
|
local _votes = read_file_votes ()
|
|
|
|
if _votes[chat] == nil then
|
|
|
|
_votes[chat] = {}
|
|
|
|
end
|
|
|
|
_votes[chat][user] = vote
|
2015-02-25 21:03:25 +01:00
|
|
|
|
2015-03-31 20:17:14 +02:00
|
|
|
serialize_to_file(_votes, _file_votes)
|
|
|
|
|
2015-02-25 21:03:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function run(msg, matches)
|
2015-03-31 20:17:14 +02:00
|
|
|
if (matches[1] == "ing") then
|
|
|
|
if (matches [2] == "reset") then
|
|
|
|
clear_votes (tostring(msg.to.id))
|
|
|
|
return "Voting statistics reset.."
|
|
|
|
elseif (matches [2] == "stats") then
|
|
|
|
local votes_result = votes_result (tostring(msg.to.id))
|
|
|
|
if (votes_result == "") then
|
|
|
|
votes_result = "[No votes registered]\n"
|
|
|
|
end
|
|
|
|
return "Voting statistics :\n" .. votes_result
|
|
|
|
end
|
|
|
|
else
|
|
|
|
save_vote(tostring(msg.to.id), msg.from.print_name, tostring(tonumber(matches[2])))
|
|
|
|
return "Vote registered : " .. msg.from.print_name .. " " .. tostring(tonumber(matches [2]))
|
|
|
|
end
|
2015-02-25 21:03:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-03-31 20:17:14 +02:00
|
|
|
description = "Plugin for voting in groups.",
|
|
|
|
usage = {
|
|
|
|
"!voting reset: Reset all the votes.",
|
|
|
|
"!vote [number]: Cast the vote.",
|
|
|
|
"!voting stats: Shows the statistics of voting."
|
|
|
|
},
|
|
|
|
patterns = {
|
|
|
|
"^!vot(ing) (reset)",
|
|
|
|
"^!vot(ing) (stats)",
|
|
|
|
"^!vot(e) ([0-9]+)$"
|
|
|
|
},
|
|
|
|
run = run
|
2015-02-25 21:03:25 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:17:14 +02:00
|
|
|
end
|