From f4e224811e9a9ad942241cbfc0cb091457e3ceeb Mon Sep 17 00:00:00 2001 From: Lunovox Date: Mon, 19 Feb 2024 11:05:38 -0300 Subject: [PATCH] =?UTF-8?q?[feat]=20c=C3=B3digo=20n=C3=A3o=20testado.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.lua | 41 +++++++++++++++++++++++- commands.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ settingtypes.txt | 24 ++++++++++++++ 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 settingtypes.txt diff --git a/api.lua b/api.lua index 15e00f8..40487cf 100644 --- a/api.lua +++ b/api.lua @@ -17,8 +17,11 @@ modEUrn.handler = { }, --]] voters = { }, --modEUrn.handler.voters[playername] + --month_counted = 0 } +modEUrn.MinPlayedHours = tonumber(minetest.settings:get("eurn.voter.min_played_hours") or 90) -- value in hours + modEUrn.doSave = function() local file = io.open(modEUrn.urlTabela, "w") if file then @@ -176,6 +179,31 @@ modEUrn.getPresidentCandidates = function() return cands end +modEUrn.doCheckPresident = function() + --Verifica registra quem serĂ¡ o presidente pela maioria de voto do candidato. + if #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 + if numVotes >= 1 and maxVotes < numVotes then + maxVotes = numVotes + PresMaxVoted = iCandPresName + end + end + if 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.handler.elected.president.name = PresMaxVoted + modEUrn.handler.elected.president.when = os.time() --Agora em milisegundos + end + end +end + minetest.register_on_leaveplayer(function(player) modEUrn.on_leaveplayer(player) modEUrn.doSave() @@ -228,4 +256,15 @@ 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 + 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()) + if mesChecked ~= mesAtual then + modEUrn.doCheckPresident() + end + end + end) +end) diff --git a/commands.lua b/commands.lua index aa33893..8198401 100644 --- a/commands.lua +++ b/commands.lua @@ -215,6 +215,87 @@ minetest.register_chatcommand( modEUrn.getPropCommCandCampaign() ) +--############################################################################ + +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" + 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 + if type(modEUrn.handler.candidates.president[iCandPresName].voters[playername])~="nil" then + 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:ERRO]").." " + ..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 + end, + } +end + +minetest.register_chatcommand( + "vote", + modEUrn.getPropCommCandVote() +) + + --############################################################################ --[[ diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..cd67617 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,24 @@ +# E-URN: +# Allows you to print the debug information of this mod on the screen. +# Default: false +eurn.debug (Show E-Urn Debug) bool false + +# E-URN: +# Whether the database will be BASE64 compressed. +# If enabled will save database bank in file '.db64', +# else in file '.tbl' without compression. +# Default: true +eurn.save_compressed (Database Compressed) bool true + +# E-URN: +# Minimum game time in hours to become a voter. +# Default: 90 | Min: 0 | Max: 8760 +eurn.voter.min_played_hours (Minimum Game Hours) int 90 0 8760 + +# E-URN: +# Time interval in seconds to check the presidential candidate with the highest number of votes. +# WARNING: Decreasing this value too much can cause server lag. +# Default: 1800 | Min: 300 | Max: 86400 +eurn.president.intervalcheck (Interval Check President) int 1800 300 86400 + +