2024-02-21 23:22:08 -03:00
modEUrn.MinPlayedHours = tonumber ( minetest.settings : get ( " eurn.voter.min_played_hours " ) or 90 ) -- value in hours
modEUrn.save_compressed = minetest.settings : get_bool ( " eurn.save_compressed " ) ~= false
2023-07-20 22:44:51 -03:00
minetest.register_privilege ( " electoraljudge " , {
description = modEUrn.translate ( " Allows you to configure the Electronic Urn. " ) ,
give_to_singleplayer = false ,
} )
2024-02-20 23:03:09 -03:00
modEUrn.debug = function ( text , playername )
if text ~= nil
and type ( text ) == " string "
and text ~= " "
then
if minetest.settings : get_bool ( " eurn.debug " ) then
if playername ~= nil
and type ( playername ) == " string "
and playername ~= " "
then
local player = minetest.get_player_by_name ( playername )
if player ~= nil and player : is_player ( ) then
minetest.chat_send_player (
playername , text
)
minetest.log ( ' action ' , " [ " .. modEUrn.modname : upper ( ) .. " :DEBUG: " .. playername .. " ] " .. text )
else
minetest.log ( ' error ' ,
" [ " .. modEUrn.modname : upper ( ) .. " :DEBUG] "
.. modEUrn.translate (
" Unable to address debug for player '%s'. "
) : format ( dump ( playername ) )
)
minetest.log ( ' action ' , " [ " .. modEUrn.modname : upper ( ) .. " :DEBUG] " .. text )
end
else
minetest.chat_send_all ( text )
minetest.log ( ' action ' , " [ " .. modEUrn.modname : upper ( ) .. " :DEBUG] " .. text )
end
end
end
end
modEUrn.unsortTableByValue = function ( tbl )
if type ( tbl ) ~= " table " then
tbl = { }
end
--modEUrn.debug("modEUrn.unsortTableByValue(): tbl = "..dump(tbl))
table.sort ( tbl , function ( a , b )
local aaa = " a(%s) > b(%s) "
aaa = aaa : format ( dump ( a ) , dump ( b ) )
modEUrn.debug ( aaa )
return a >= b
end )
--modEUrn.debug("modEUrn.unsortTableByValue(): tbl = "..dump(tbl))
return tbl
end
2024-02-21 09:51:15 -03:00
modEUrn.sortTableLenght = function ( tbl )
2024-02-20 23:03:09 -03:00
local count = 0
--modEUrn.debug("t = "..dump(t))
2024-02-21 09:51:15 -03:00
for _ in pairs ( tbl ) do
2024-02-20 23:03:09 -03:00
count = count + 1
end
return count
end
2023-07-20 22:44:51 -03:00
modEUrn.handler = {
elected = {
president = {
name = " " , --name of the elected candidate.
when = 0 , --when the candidate was elected.
2024-02-19 14:31:33 -03:00
--voters = { }, --names of voters who voted for this elected candidate.
2023-07-20 22:44:51 -03:00
} ,
} ,
candidates = {
president = { } , --modEUrn.handler.candidates.president[<candidatename>] = <when> | If the candidate has not updated his candidacy for more than 6 months, he loses his candidacy application.
} ,
--]]
voters = { } , --modEUrn.handler.voters[playername]
2024-02-19 11:05:38 -03:00
--month_counted = 0
2023-07-20 22:44:51 -03:00
}
modEUrn.doSave = function ( )
local file = io.open ( modEUrn.urlTabela , " w " )
if file then
2024-02-21 02:38:24 -03:00
local handler = minetest.serialize ( modEUrn.handler )
if modEUrn.save_compressed then
handler = minetest.encode_base64 ( handler )
end
file : write ( handler )
2023-07-20 22:44:51 -03:00
file : close ( )
minetest.log ( ' action ' , " [E-URN] " .. modEUrn.translate ( " Database saved in file '%s'! " ) : format ( modEUrn.urlTabela ) )
else
minetest.log ( ' error ' , " [E-URN:ERRO] " .. modEUrn.translate ( " The file '%s' is not in table format! " ) : format ( modEUrn.urlTabela ) )
end
end
modEUrn.doLoad = function ( )
local file = io.open ( modEUrn.urlTabela , " r " )
if file then
2024-02-21 02:38:24 -03:00
local handler = file : read ( " *all " )
if modEUrn.save_compressed then
handler = minetest.decode_base64 ( handler )
end
handler = minetest.deserialize ( handler ) --transforma string to table
2023-07-20 22:44:51 -03:00
file : close ( )
if not handler or type ( handler ) ~= " table " then
minetest.log ( ' error ' , " [E-URN:ERRO] " .. modEUrn.translate ( " The file '%s' is not in table format! " ) : format ( modEUrn.urlTabela ) )
return { }
else
modEUrn.handler = handler
minetest.log ( ' action ' , " [E-URN] " .. modEUrn.translate ( " Database '%s' loaded! " ) : format ( modEUrn.urlTabela ) )
end
end
end
modEUrn.on_leaveplayer = function ( player )
local playername = player : get_player_name ( )
local now = os.time ( ) --Em milisegundos
if modEUrn.handler . voters [ playername ] ~= nil and type ( modEUrn.handler . voters [ playername ] ) == " table " then
if modEUrn.handler . voters [ playername ] . times.login ~= nil and type ( modEUrn.handler . voters [ playername ] . times.login ) == " number " and modEUrn.handler . voters [ playername ] . times.login > 0 then
if modEUrn.handler . voters [ playername ] . times.played ~= nil and type ( modEUrn.handler . voters [ playername ] . times.played ) == " number " and modEUrn.handler . voters [ playername ] . times.played > 0 then
modEUrn.handler . voters [ playername ] . times.played = modEUrn.handler . voters [ playername ] . times.played + ( now - modEUrn.handler . voters [ playername ] . times.login )
else
modEUrn.handler . voters [ playername ] . times.played = ( now - modEUrn.handler . voters [ playername ] . times.login )
end
modEUrn.handler . voters [ playername ] . times.login = 0
modEUrn.handler . voters [ playername ] . times.logout = now
end
else
minetest.log ( ' error ' , " [E-URN:ERROR:ONLEAVEPLAYER] " .. modEUrn.translate ( " Impossible to register logout for player '%s' ! " ) : format ( dump ( player ) ) )
end
end
2024-02-18 18:06:19 -03:00
modEUrn.getPresidentName = function ( )
if type ( modEUrn.handler . elected.president . name ) ~= " nil "
and modEUrn.handler . elected.president . name ~= " "
then
return modEUrn.handler . elected.president . name
end
2024-02-20 23:03:09 -03:00
return " "
2024-02-18 18:06:19 -03:00
end
2024-02-21 09:51:15 -03:00
modEUrn.doCheckDataBase = function ( candidatename )
--modEUrn.handler.elected
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . elected ) == " nil " then
2024-02-21 09:51:15 -03:00
modEUrn.handler . elected = { }
end
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . elected.president ) == " nil " then
2024-02-21 09:51:15 -03:00
modEUrn.handler . elected.president = { }
end
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . elected.president . name ) == " nil " then
modEUrn.handler . elected.president . name = " "
end
if type ( modEUrn.handler . elected.president . when ) == " nil " then
modEUrn.handler . elected.president . when = 0
end
2024-02-21 09:51:15 -03:00
--modEUrn.handler.candidates
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . candidates ) == " nil " then
2024-02-21 09:51:15 -03:00
modEUrn.handler . candidates = { }
end
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . candidates.president ) == " nil " then
2024-02-21 09:51:15 -03:00
modEUrn.handler . candidates.president = { }
end
if type ( candidatename ) == " string " and candidatename ~= " " then
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . candidates.president [ candidatename ] ) == " nil " then
2024-02-18 18:06:19 -03:00
modEUrn.handler . candidates.president [ candidatename ] = { }
end
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . candidates.president [ candidatename ] . political_campaign ) == " nil " then
2024-02-18 18:06:19 -03:00
modEUrn.handler . candidates.president [ candidatename ] . political_campaign = " There are no campaign data about this political candidate. "
end
2024-02-21 11:02:52 -03:00
if type ( modEUrn.handler . candidates.president [ candidatename ] . voters ) == " nil " then
2024-02-18 18:06:19 -03:00
modEUrn.handler . candidates.president [ candidatename ] . voters = { }
end
2024-02-21 09:51:15 -03:00
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 )
2024-02-18 18:06:19 -03:00
return true
else
minetest.chat_send_player (
playername ,
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
core.colorize ( " #FF0000 " , " [E-URN] " ) .. " "
.. modEUrn.translate ( " The name '%s' is not the player name registered on the server! " ) : format ( candidatename )
)
end
return false
end
modEUrn.doPresidentCandidate = function ( playername , candidatename , political_campaign )
if minetest.player_exists ( candidatename ) then
2024-02-21 09:51:15 -03:00
--local now = os.time() --Em milisegundos
2024-02-18 18:06:19 -03:00
if type ( political_campaign ) ~= " string "
or political_campaign == " "
then
political_campaign = " There are no campaign data about this political candidate. "
end
2024-02-21 09:51:15 -03:00
modEUrn.doCheckDataBase ( candidatename )
modEUrn.handler . candidates.president [ candidatename ] . political_campaign = political_campaign
2024-02-18 18:06:19 -03:00
return true
else
minetest.chat_send_player (
playername ,
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
core.colorize ( " #FF0000 " , " [E-URN] " ) .. " "
.. modEUrn.translate ( " The name '%s' is not the player name registered on the server! " ) : format ( candidatename )
)
end
return false
end
modEUrn.getPresCandCampaign = function ( candidatename )
2024-02-22 17:01:36 -03:00
local political_campaign = modEUrn.translate ( " There are no campaign data about this political candidate. " )
2024-02-20 23:03:09 -03:00
--modEUrn.debug("candidatename="..dump(candidatename))
if type ( modEUrn.handler . candidates.president [ candidatename ] ) == " table "
and type ( modEUrn.handler . candidates.president [ candidatename ] . political_campaign ) == " string "
2024-02-18 18:06:19 -03:00
and modEUrn.handler . candidates.president [ candidatename ] . political_campaign ~= " "
then
2024-02-20 23:03:09 -03:00
political_campaign = modEUrn.handler . candidates.president [ candidatename ] . political_campaign
2024-02-18 18:06:19 -03:00
end
return political_campaign
end
modEUrn.getPresidentCandidates = function ( )
local cands = { }
for iCandPresName , iCandPresValue in pairs ( modEUrn.handler . candidates.president ) do
table.insert ( cands , iCandPresName )
--cands:insert(iCandPresName)
end
return cands
end
2024-02-25 11:28:25 -03:00
modEUrn.getVoterPlayedTime = function ( playername )
if type ( playername ) == " string "
and playername ~= " "
and type ( modEUrn.handler . voters ) == " table "
and type ( modEUrn.handler . voters [ playername ] ) == " table "
and type ( modEUrn.handler . voters [ playername ] . times ) == " table "
and type ( modEUrn.handler . voters [ playername ] . times.played ) == " number "
then
return modEUrn.handler . voters [ playername ] . times.played --in secounds
end
end
2024-02-20 23:03:09 -03:00
modEUrn.showPresCandCampaign = function ( playername , candidatename )
if type ( candidatename ) == " string " and candidatename ~= " " then --Se o nome do candidato foi dito
if minetest.player_exists ( candidatename ) then --Se existe algum jogador o nome que foi dito
if type ( modEUrn.handler . candidates.president [ candidatename ] ) == " table " then --Se foi algum candidato registrado.
local player = minetest.get_player_by_name ( playername )
if player : is_player ( ) then --verifica se o jogador está online ou por terminal
modEUrn.FormSpecs . showFormInfoCampaign ( playername , candidatename )
else
minetest.chat_send_player (
playername ,
core.colorize ( " #00FF00 " , " [E-URN] " ) .. " "
.. core.colorize ( " #888888 " ,
modEUrn.translate ( " Candidate Name " ) .. " : "
)
.. core.colorize ( " #00FF00 " , candidatename )
)
local Campaign = modEUrn.getPresCandCampaign ( candidatename )
if type ( Campaign ) == " string " and Campaign ~= " " then
minetest.chat_send_player (
playername ,
core.colorize ( " #00FF00 " , " [E-URN] " ) .. " "
.. core.colorize ( " #888888 " ,
modEUrn.translate ( " Candidate Campaign " ) .. " : "
)
.. Campaign
)
end
end
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 ,
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
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 ,
--os.date("%Y-%m-%d %Hh:%Mm:%Ss", now) ..
core.colorize ( " #FF0000 " , " [E-URN] " ) .. " "
.. modEUrn.translate ( " Please enter the name of the candidate you wish to display the campaign! " ) .. " \n "
.. core.colorize ( " #888888 " , " Syntaxe: " )
.. core.colorize ( " #00FFFF " ,
" /campaign < " .. modEUrn.translate ( " candidate_name " ) .. " > "
)
)
end
end
2024-02-21 09:51:15 -03:00
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 )
2024-02-25 11:28:25 -03:00
local voterTimePlayed = modEUrn.getVoterPlayedTime ( playername )
--modEUrn.debug("modEUrn.doVote(): voterTimePlayed = "..dump(voterTimePlayed))
if type ( voterTimePlayed ) == " number " then
--if type(modEUrn.handler.voters[playername])=="table" then
--local voterTimePlayed = modEUrn.handler.voters[playername].times.played --in secounds
2024-02-21 09:51:15 -03:00
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
--]]
2024-02-19 11:05:38 -03:00
modEUrn.doCheckPresident = function ( )
--Verifica registra quem será o presidente pela maioria de voto do candidato.
2024-02-21 09:51:15 -03:00
if modEUrn.sortTableLenght ( modEUrn.handler . candidates.president ) >= 1 then
2024-02-19 11:05:38 -03:00
local maxVotes = 0
local PresMaxVoted = " "
for iCandPresName , _ in pairs ( modEUrn.handler . candidates.president ) do
2024-02-21 09:51:15 -03:00
local numVotes = modEUrn.sortTableLenght ( modEUrn.handler . candidates.president [ iCandPresName ] . voters )
2024-02-19 11:05:38 -03:00
if numVotes >= 1 and maxVotes < numVotes then
maxVotes = numVotes
PresMaxVoted = iCandPresName
end
end
2024-02-19 11:22:10 -03:00
if maxVotes >= 1 and PresMaxVoted ~= " " then
2024-02-21 09:51:15 -03:00
modEUrn.doCheckDataBase ( )
2024-02-19 11:05:38 -03:00
modEUrn.handler . elected.president . name = PresMaxVoted
modEUrn.handler . elected.president . when = os.time ( ) --Agora em milisegundos
2024-02-19 11:22:10 -03:00
2024-02-21 09:51:15 -03:00
return true , modEUrn.translate ( " Player %s has been named the president of this server! " ) : format ( dump ( PresMaxVoted ) )
2024-02-19 11:22:10 -03:00
--[[
Falta :
* enviar email para todos os jogadores anunciando quem ganhou a eleição do mês .
* Faze o som de vitória
--]]
2024-02-21 09:51:15 -03:00
else
return false , modEUrn.translate ( " There are no votes registered! " )
2024-02-19 11:05:38 -03:00
end
2024-02-21 09:51:15 -03:00
else
return false , modEUrn.translate ( " There are no candidates registered! " )
2024-02-19 11:05:38 -03:00
end
end
2023-07-20 22:44:51 -03:00
minetest.register_on_leaveplayer ( function ( player )
modEUrn.on_leaveplayer ( player )
modEUrn.doSave ( )
end )
--[[
minetest.register_on_newplayer ( function ( player )
end )
--]]
--[[
minetest.register_on_prejoinplayer ( function ( name , ip )
end )
--]]
minetest.register_on_joinplayer ( function ( player )
local playername = player : get_player_name ( )
local now = os.time ( ) --Em milisegundos
if playername ~= nil and type ( playername ) == " string " and playername ~= " " then
if modEUrn.handler . voters [ playername ] == nil then
modEUrn.handler . voters [ playername ] = {
times = {
register = now ,
played = 0 ,
login = now ,
logout = 0 ,
} ,
}
end
modEUrn.handler . voters [ playername ] . times.login = now
modEUrn.handler . voters [ playername ] . times.logout = 0
else
minetest.log ( ' error ' , " [E-URN:ERROR:ONJOINPLAYER] " .. modEUrn.translate ( " Impossible to register login for player '%s' ! " ) : format ( dump ( player ) ) )
end
end )
minetest.register_on_shutdown ( function ( )
local players = minetest.get_connected_players ( )
if # players >= 1 then
for _ , player in ipairs ( players ) do
modEUrn.on_leaveplayer ( player )
2024-02-16 21:28:25 -03:00
local playername = player : get_player_name ( )
--minetest.disconnect_player(name, [reason])
minetest.disconnect_player ( playername )
2023-07-20 22:44:51 -03:00
end
end
modEUrn.doSave ( )
2023-07-21 01:58:24 -03:00
--minetest.log('action',"[E-URN] "..modEUrn.translate("Saving the database from all players in the file '%s'!"):format(modEUrn.urlTabela))
2023-07-20 22:44:51 -03:00
end )
2024-02-21 09:51:15 -03:00
--[[ ]]
2024-02-19 11:05:38 -03:00
minetest.after ( 3.5 , function ( )
minetest.register_globalstep ( function ( dtime )
2024-02-20 23:03:09 -03:00
--local presname = modEUrn.handler.elected.president.name
local presname = modEUrn.getPresidentName ( )
2024-02-19 11:05:38 -03:00
if type ( presname ) == " string " and presname ~= " " then
local mesAtual = os.date ( " %m " , os.time ( ) )
2024-02-21 09:51:15 -03:00
local mesChecked = os.date ( " %m " , tonumber ( modEUrn.handler . elected.president . when ) )
2024-02-19 11:05:38 -03:00
if mesChecked ~= mesAtual then
2024-02-21 09:51:15 -03:00
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
2024-02-19 11:05:38 -03:00
end
end
end )
end )
2024-02-21 09:51:15 -03:00
--]]
2024-02-20 23:03:09 -03:00