mirror of
https://gitlab.com/lunovox/e-urn.git
synced 2025-03-15 07:21:22 +00:00
This commit is contained in:
parent
7ba6a503c9
commit
68454c2124
3 changed files with 182 additions and 140 deletions
|
@ -34,7 +34,8 @@ Colect opinion of player with electronic urn.
|
|||
| ````/candidate```` or ````/campaign```` | ````[<candidate_name>]```` | Show the Campaign of candidate for president. |
|
||||
| ````/vote```` | ````[<candidate_name>]```` | Vote for a specific candidate for president. Need a minimum number of hours of online play. |
|
||||
| ````/unvote```` | | Unvote for presidetial candidate. |
|
||||
| ````/votes```` | | Show the name of all candidates for president with the amount of votes. Need privilege ````electoraljudge````.
|
||||
| ````/votes```` | | Show the name of all candidates for president with the amount of votes. Need the ````electoraljudge```` privilege. |
|
||||
| ````/election```` | | Apply presidential selection vote counting! Need the ````electoraljudge```` privilege. |
|
||||
|
||||
## **API:**
|
||||
|
||||
|
|
154
api.lua
154
api.lua
|
@ -53,10 +53,10 @@ modEUrn.unsortTableByValue = function(tbl)
|
|||
return tbl
|
||||
end
|
||||
|
||||
modEUrn.sortTableLenght = function(t)
|
||||
modEUrn.sortTableLenght = function(tbl)
|
||||
local count = 0
|
||||
--modEUrn.debug("t = "..dump(t))
|
||||
for _ in pairs(t) do
|
||||
for _ in pairs(tbl) do
|
||||
count = count + 1
|
||||
end
|
||||
return count
|
||||
|
@ -142,28 +142,24 @@ modEUrn.getPresidentName = function()
|
|||
return ""
|
||||
end
|
||||
|
||||
modEUrn.doPresident = function(playername, candidatename)
|
||||
if minetest.player_exists(candidatename) then
|
||||
local now = os.time() --Em milisegundos
|
||||
|
||||
--modEUrn.handler.elected
|
||||
if modEUrn.handler.elected==nil then
|
||||
modEUrn.handler.elected = { }
|
||||
end
|
||||
if modEUrn.handler.elected.president==nil then
|
||||
modEUrn.handler.elected.president = { }
|
||||
end
|
||||
modEUrn.handler.elected.president.name = candidatename
|
||||
modEUrn.handler.elected.president.when = now
|
||||
|
||||
|
||||
--modEUrn.handler.candidates
|
||||
if modEUrn.handler.candidates==nil then
|
||||
modEUrn.handler.candidates = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president==nil then
|
||||
modEUrn.handler.candidates.president = { }
|
||||
end
|
||||
modEUrn.doCheckDataBase = function(candidatename)
|
||||
--modEUrn.handler.elected
|
||||
if modEUrn.handler.elected==nil then
|
||||
modEUrn.handler.elected = { }
|
||||
end
|
||||
if modEUrn.handler.elected.president==nil then
|
||||
modEUrn.handler.elected.president = { }
|
||||
end
|
||||
|
||||
--modEUrn.handler.candidates
|
||||
if modEUrn.handler.candidates==nil then
|
||||
modEUrn.handler.candidates = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president==nil then
|
||||
modEUrn.handler.candidates.president = { }
|
||||
end
|
||||
|
||||
if type(candidatename)=="string" and candidatename~="" then
|
||||
if modEUrn.handler.candidates.president[candidatename]==nil then
|
||||
modEUrn.handler.candidates.president[candidatename] = { }
|
||||
end
|
||||
|
@ -173,12 +169,16 @@ modEUrn.doPresident = function(playername, candidatename)
|
|||
if modEUrn.handler.candidates.president[candidatename].voters==nil then
|
||||
modEUrn.handler.candidates.president[candidatename].voters = { }
|
||||
end
|
||||
for _, iCandPresident in ipairs(modEUrn.handler.candidates.president) do
|
||||
if type(modEUrn.handler.candidates.president[iCandPresident].voters[playername])~="nil" then
|
||||
modEUrn.handler.candidates.president[iCandPresident].voters[playername] = nil
|
||||
end
|
||||
end
|
||||
modEUrn.handler.candidates.president[candidatename].voters[playername] = now
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
modEUrn.doPresident = function(playername, candidatename)
|
||||
if minetest.player_exists(candidatename) then
|
||||
modEUrn.doCheckDataBase(candidatename)
|
||||
modEUrn.handler.elected.president.name = candidatename
|
||||
modEUrn.handler.elected.president.when = os.time() --Em milisegundos
|
||||
modEUrn.doVote(playername, candidatename)
|
||||
return true
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
|
@ -193,29 +193,15 @@ end
|
|||
|
||||
modEUrn.doPresidentCandidate = function(playername, candidatename, political_campaign)
|
||||
if minetest.player_exists(candidatename) then
|
||||
local now = os.time() --Em milisegundos
|
||||
--local now = os.time() --Em milisegundos
|
||||
if type(political_campaign)~="string"
|
||||
or political_campaign == ""
|
||||
then
|
||||
political_campaign = "There are no campaign data about this political candidate."
|
||||
end
|
||||
|
||||
--modEUrn.handler.candidates
|
||||
if modEUrn.handler.candidates==nil then
|
||||
modEUrn.handler.candidates = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president==nil then
|
||||
modEUrn.handler.candidates.president = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president[candidatename]==nil then
|
||||
modEUrn.handler.candidates.president[candidatename] = { }
|
||||
end
|
||||
|
||||
modEUrn.doCheckDataBase(candidatename)
|
||||
modEUrn.handler.candidates.president[candidatename].political_campaign = political_campaign
|
||||
|
||||
if modEUrn.handler.candidates.president[candidatename].voters==nil then
|
||||
modEUrn.handler.candidates.president[candidatename].voters = { }
|
||||
end
|
||||
|
||||
return true
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
|
@ -306,34 +292,76 @@ modEUrn.showPresCandCampaign = function(playername, candidatename)
|
|||
end
|
||||
end
|
||||
|
||||
modEUrn.doUnvote = function(playername)
|
||||
modEUrn.doCheckDataBase(candidatename)
|
||||
for iCandPresName, _ in pairs(modEUrn.handler.candidates.president) do
|
||||
--modEUrn.debug("iCandPresName = "..iCandPresName)
|
||||
if type(modEUrn.handler.candidates.president[iCandPresName].voters[playername])~="nil" then
|
||||
--modEUrn.debug("modEUrn.handler.candidates.president["..iCandPresName.."].voters = "..dump(modEUrn.handler.candidates.president[iCandPresName].voters))
|
||||
modEUrn.handler.candidates.president[iCandPresName].voters[playername] = nil
|
||||
return true, modEUrn.translate("You unvoted for the presidential candidate!")
|
||||
end
|
||||
end
|
||||
return false, modEUrn.translate("You can't have registered vote!")
|
||||
end
|
||||
|
||||
--[[ ]]
|
||||
modEUrn.doVote = function(playername, candidatename)
|
||||
if type(modEUrn.handler.voters[playername])=="table" then
|
||||
local voterTimePlayed = modEUrn.handler.voters[playername].times.played --in secounds
|
||||
if voterTimePlayed >= modEUrn.MinPlayedHours * (60*60) then
|
||||
if type(candidatename)=="string" and candidatename ~= "" then
|
||||
if minetest.player_exists(candidatename) then
|
||||
if type(modEUrn.handler.candidates.president[candidatename])=="table" then
|
||||
modEUrn.doUnvote(playername)
|
||||
modEUrn.handler.candidates.president[candidatename].voters[playername] = os.time() --Agora em milisegundos
|
||||
return true, modEUrn.translate("You voted for the candidate: ")..core.colorize("#00FF00", candidatename)
|
||||
else
|
||||
return false, modEUrn.translate("Player '%s' is not registered as a candidate for president!"):format(candidatename)
|
||||
end
|
||||
else
|
||||
return false, modEUrn.translate("The name '%s' is not the player name registered on the server!"):format(candidatename)
|
||||
end
|
||||
else
|
||||
return false, modEUrn.translate("Please enter the name of the presidential candidate you wish to vote!").."\n"..core.colorize("#888888", "Syntaxe: ")..core.colorize("#00FFFF", "/vote <"..modEUrn.translate("candidate_name")..">")
|
||||
end
|
||||
else
|
||||
return false, modEUrn.translate("You have not yet played the minimum number of hours to become a presidential voter. (Minimum %02d hours)"):format(modEUrn.MinPlayedHours)
|
||||
end
|
||||
else
|
||||
return false, modEUrn.translate("Your name is not registered as a voter.")
|
||||
end
|
||||
end
|
||||
--]]
|
||||
|
||||
modEUrn.doCheckPresident = function()
|
||||
--Verifica registra quem será o presidente pela maioria de voto do candidato.
|
||||
if #modEUrn.handler.candidates.president >= 1 then
|
||||
if modEUrn.sortTableLenght(modEUrn.handler.candidates.president) >= 1 then
|
||||
local maxVotes = 0
|
||||
local PresMaxVoted = ""
|
||||
for iCandPresName, _ in pairs(modEUrn.handler.candidates.president) do
|
||||
local numVotes = #modEUrn.handler.candidates.president[iCandPresName].voters
|
||||
local numVotes = modEUrn.sortTableLenght(modEUrn.handler.candidates.president[iCandPresName].voters)
|
||||
if numVotes >= 1 and maxVotes < numVotes then
|
||||
maxVotes = numVotes
|
||||
PresMaxVoted = iCandPresName
|
||||
end
|
||||
end
|
||||
if maxVotes>=1 and PresMaxVoted~="" then
|
||||
if modEUrn.handler.elected==nil then
|
||||
modEUrn.handler.elected = { }
|
||||
end
|
||||
if modEUrn.handler.elected.president==nil then
|
||||
modEUrn.handler.elected.president = { }
|
||||
end
|
||||
modEUrn.doCheckDataBase()
|
||||
modEUrn.handler.elected.president.name = PresMaxVoted
|
||||
modEUrn.handler.elected.president.when = os.time() --Agora em milisegundos
|
||||
|
||||
return true, modEUrn.translate("Player %s has been named the president of this server!"):format(dump(PresMaxVoted))
|
||||
--[[
|
||||
Falta:
|
||||
* enviar email para todos os jogadores anunciando quem ganhou a eleição do mês.
|
||||
* Faze o som de vitória
|
||||
--]]
|
||||
else
|
||||
return false, modEUrn.translate("There are no votes registered!")
|
||||
end
|
||||
else
|
||||
return false, modEUrn.translate("There are no candidates registered!")
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -389,18 +417,30 @@ minetest.register_on_shutdown(function()
|
|||
--minetest.log('action',"[E-URN] "..modEUrn.translate("Saving the database from all players in the file '%s'!"):format(modEUrn.urlTabela))
|
||||
end)
|
||||
|
||||
--[[ ]]
|
||||
minetest.after(3.5, function()
|
||||
minetest.register_globalstep(function(dtime)
|
||||
--local presname = modEUrn.handler.elected.president.name
|
||||
local presname = modEUrn.getPresidentName()
|
||||
if type(presname)=="string" and presname~="" then
|
||||
local mesAtual = os.date("%m", os.time())
|
||||
local mesChecked = os.date("%m", tonumber(modEUrn.handler.elected.president.when) or os.time())
|
||||
local mesChecked = os.date("%m", tonumber(modEUrn.handler.elected.president.when))
|
||||
if mesChecked ~= mesAtual then
|
||||
modEUrn.doCheckPresident()
|
||||
local result, cause = modEUrn.doCheckPresident()
|
||||
if result == true then
|
||||
minetest.chat_send_all(
|
||||
core.colorize("#00FF00", "[E-URN]").." "..cause
|
||||
)
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "..cause
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end)
|
||||
--]]
|
||||
|
||||
|
||||
|
|
165
commands.lua
165
commands.lua
|
@ -102,6 +102,49 @@ minetest.register_chatcommand(
|
|||
|
||||
--############################################################################
|
||||
|
||||
modEUrn.getPropCommDiscandidateMe = function()
|
||||
return {
|
||||
--privs = {electoraljudge=true},
|
||||
params = "[<".. modEUrn.translate("political campaign")..">]",
|
||||
description = modEUrn.translate("Unregister your campaign to run for server president."),
|
||||
func = function(playername, param)
|
||||
if modEUrn.handler.candidates==nil then
|
||||
modEUrn.handler.candidates = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president==nil then
|
||||
modEUrn.handler.candidates.president = { }
|
||||
end
|
||||
if modEUrn.handler.candidates.president[playername]~=nil then
|
||||
modEUrn.handler.candidates.president[playername] = nil
|
||||
end
|
||||
|
||||
--modEUrn.handler.elected
|
||||
if modEUrn.handler.elected==nil then
|
||||
modEUrn.handler.elected = { }
|
||||
end
|
||||
if modEUrn.handler.elected.president==nil then
|
||||
modEUrn.handler.elected.president = { }
|
||||
end
|
||||
modEUrn.handler.elected.president.name = ""
|
||||
modEUrn.handler.elected.president.when = 0
|
||||
|
||||
modEUrn.doSave()
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
|
||||
core.colorize("#00FF00", "[E-URN]").." "
|
||||
..modEUrn.translate("Player %s has been unregistered to run as a candidate for president of this server!"):format(dump(playername))
|
||||
)
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
minetest.register_chatcommand(
|
||||
"discandidateme",
|
||||
modEUrn.getPropCommCandidateMe()
|
||||
)
|
||||
--############################################################################
|
||||
|
||||
modEUrn.getPropCommCandidates = function()
|
||||
return {
|
||||
--privs = {electoraljudge=true},
|
||||
|
@ -130,21 +173,6 @@ modEUrn.getPropCommCandidates = function()
|
|||
core.colorize("#00FF00", "[E-URN]").." "
|
||||
..modEUrn.translate("Candidate List: %s"):format(candList)
|
||||
)
|
||||
|
||||
--minetest.chat_send_all("Numero de Candidatos: "..candCount)
|
||||
--minetest.chat_send_all("Lista de Candidatos: "..candList)
|
||||
--]]
|
||||
--[[
|
||||
if then
|
||||
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
|
||||
core.colorize("#00FF00", "[E-URN]").." "
|
||||
..modEUrn.translate("Player %s has been registered to run as a candidate for president of this server!"):format(dump(candidatename))
|
||||
)
|
||||
end
|
||||
--]]
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
@ -177,79 +205,22 @@ minetest.register_chatcommand(
|
|||
)
|
||||
|
||||
--############################################################################
|
||||
|
||||
--[[ ]]
|
||||
modEUrn.getPropCommCandVote = function()
|
||||
return {
|
||||
--privs = {electoraljudge=true},
|
||||
params = "<".. modEUrn.translate("candidate_name")..">",
|
||||
description = modEUrn.translate("Vote for a specific candidate for president."),
|
||||
func = function(playername, candidatename)
|
||||
if type(modEUrn.handler.voters[playername])=="table" then
|
||||
local voterTimePlayed = modEUrn.handler.voters[playername].times.played --in secounds
|
||||
if voterTimePlayed >= modEUrn.MinPlayedHours * (60*60) then
|
||||
|
||||
if type(candidatename)=="string" and candidatename ~= "" then
|
||||
if minetest.player_exists(candidatename) then
|
||||
if type(modEUrn.handler.candidates.president[candidatename])=="table" then
|
||||
--modEUrn.MinPlayedHours
|
||||
--modEUrn.handler.voters[playername].times.played
|
||||
|
||||
for iCandPresName, _ in pairs(modEUrn.handler.candidates.president) do
|
||||
--modEUrn.debug("iCandPresName = "..iCandPresName)
|
||||
if type(modEUrn.handler.candidates.president[iCandPresName].voters[playername])~="nil" then
|
||||
--modEUrn.debug("modEUrn.handler.candidates.president["..iCandPresName.."].voters = "..dump(modEUrn.handler.candidates.president[iCandPresName].voters))
|
||||
modEUrn.handler.candidates.president[iCandPresName].voters[playername] = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
modEUrn.handler.candidates.president[candidatename].voters[playername] = os.time() --Agora em milisegundos
|
||||
|
||||
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#00FF00", "[E-URN]").." "
|
||||
..modEUrn.translate("You voted for the candidate: ")
|
||||
..core.colorize("#00FF00", candidatename)
|
||||
)
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "
|
||||
..modEUrn.translate("Player '%s' is not registered as a candidate for president!"):format(candidatename)
|
||||
)
|
||||
end
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "
|
||||
..modEUrn.translate("The name '%s' is not the player name registered on the server!"):format(candidatename)
|
||||
)
|
||||
end
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "
|
||||
..modEUrn.translate("Please enter the name of the presidential candidate you wish to vote!").."\n"
|
||||
..core.colorize("#888888", "Syntaxe: ")
|
||||
..core.colorize("#00FFFF",
|
||||
"/vote <"..modEUrn.translate("candidate_name")..">"
|
||||
)
|
||||
)
|
||||
end
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "
|
||||
..modEUrn.translate("You have not yet played the minimum number of hours to become a presidential voter. (Minimum %02d hours)"):format(modEUrn.MinPlayedHours)
|
||||
)
|
||||
end
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN:ERRO]").." "
|
||||
..modEUrn.translate("Your name is not registered as a voter.")
|
||||
)
|
||||
end
|
||||
local resulte, cause = modEUrn.doVote(playername, candidatename)
|
||||
local color = "#FF0000"
|
||||
if resulte then
|
||||
color = "#00FF00"
|
||||
end
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize(color, "[E-URN]").." "..cause
|
||||
)
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
@ -258,6 +229,7 @@ minetest.register_chatcommand(
|
|||
"vote",
|
||||
modEUrn.getPropCommCandVote()
|
||||
)
|
||||
--]]
|
||||
|
||||
--############################################################################
|
||||
|
||||
|
@ -327,6 +299,35 @@ minetest.register_chatcommand(
|
|||
|
||||
--############################################################################
|
||||
|
||||
modEUrn.getPropCommElection = function()
|
||||
return {
|
||||
privs = {electoraljudge=true},
|
||||
--params = "<".. modEUrn.translate("candidate_name")..">",
|
||||
description = modEUrn.translate("Apply presidential selection vote counting! Need the 'electoraljudge' privilege."),
|
||||
func = function(playername, param)
|
||||
--modEUrn.debug("modEUrn.handler.candidates.president = "..dump(modEUrn.handler.candidates.president))
|
||||
--modEUrn.debug("modEUrn.sortTableLenght(modEUrn.handler.candidates.president) = "..modEUrn.sortTableLenght(modEUrn.handler.candidates.president))
|
||||
local result, cause = modEUrn.doCheckPresident()
|
||||
if result == true then
|
||||
minetest.chat_send_all(
|
||||
core.colorize("#00FF00", "[E-URN]").." "..cause
|
||||
)
|
||||
else
|
||||
minetest.chat_send_player(
|
||||
playername,
|
||||
core.colorize("#FF0000", "[E-URN]").." "..cause
|
||||
)
|
||||
end
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
minetest.register_chatcommand(
|
||||
"election",
|
||||
modEUrn.getPropCommElection()
|
||||
)
|
||||
--############################################################################
|
||||
|
||||
--[[
|
||||
minetest.register_chatcommand(
|
||||
"t",
|
||||
|
|
Loading…
Add table
Reference in a new issue