Question about voting system in control.lua:
Posted: Wed Jan 03, 2018 8:27 am
Can anyone give me some advice here?
Ive gotten almost everything working, intro MOTD is printed to chat, items are autospawned in inventory and research is unlocked, but the vote system doesnt seem to be completing.
I can initiate a vote poll with "/over", but when I or another player use "/yes" or "/no" to vote, nothing happens, at least nothing is printed out to chat confirming the vote went in and tallying things.
Any ideas as to why this is happening?
Also, follow up question, once I get the voting system working, what would be the most efficient way to trigger a restart (including re-map generation) of the scenario?
Ive gotten almost everything working, intro MOTD is printed to chat, items are autospawned in inventory and research is unlocked, but the vote system doesnt seem to be completing.
I can initiate a vote poll with "/over", but when I or another player use "/yes" or "/no" to vote, nothing happens, at least nothing is printed out to chat confirming the vote went in and tallying things.
Any ideas as to why this is happening?
Code: Select all
require("silo-script")
local version = 1
script.on_event(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
player.print("Welcome to Grim Galaxy!")
player.print("---------------------------------")
player.print("Youre goal is to survive.")
player.print("The current highscore is 2.5 hours.")
player.force.recipes.loader.enabled=true
player.force.technologies["stone-walls"].researched = true
player.force.technologies["turrets"].researched = true
player.force.technologies["military"].researched=true
player.insert{name="iron-plate", count=8}
player.insert{name="pistol", count=1}
player.insert{name="stone-wall", count=50}
player.insert{name="firearm-magazine", count=40}
player.insert{name="burner-mining-drill", count = 2}
player.insert{name="gun-turret", count = 1}
player.insert{name="stone-furnace", count = 1}
player.force.chart(player.surface, {{player.position.x - 200, player.position.y - 200}, {player.position.x + 200, player.position.y + 200}})
end)
local function count_votes()
global.voters = global.voters or {}
local yes, no = 0, 0
for _, vote in pairs(global.voters) do
if vote == true then
yes = yes + 1
elseif vote == false then
no = no + 1
end
end
return yes, no, yes > no
end
local function end_voting()
if global.vote_type then
if global.vote_type == "end_vote" then
if select(3, count_votes()) then
game.print("The majority has voted to end the round.")
else
game.print("The majority has voted to continue the round.")
end
end
end
global.voters = nil
global.voting_ends = nil
global.voting_type = nil
end
local function process_vote(player_index, yes_or_no)
global.voters = global.voters or {}
if global.voters[player_index] ~= nil then
global.voters[player_index] = yes_or_no
local yes, no = count_votes()
game.print(".name" .. "voted to " .. (yes_or_no and "end" or "continue") .. " the round.")
game.print("For: " .. yes .. " Against: " .. no)
end
end
commands.add_command(
"yes",
"Vote Yes",
function(e)
process_vote(e.player_index, true)
end
)
commands.add_command(
"no",
"Vote No",
function(e)
process_vote(e.player_index, false)
end
)
commands.add_command(
"over",
"Start a vote to end the map.",
function(e)
--first argument to /over is the time to wait before declaring a winner in minutes, defaults to 1 minute
global.voting_ends = game.tick + ((e.parameter and (tonumber(e.parameter)) or 1 ) * 60 * 60)
global.voting_type = "end_map"
global.voters = {}
game.print(game.players[e.player_index].name .. " has started a vote to end the map, type /yes or /no to vote.")
game.print("Voting will end in 60 seconds!")
end
)
-- register tick handler, or add it your current tick handler
script.on_event(
defines.events.on_tick,
function(e)
if e.tick == global.voting_ends then
end_voting()
end
end
)
script.on_event(defines.events.on_player_respawned, function(event)
local player = game.players[event.player_index]
player.insert{name="pistol", count=1}
player.insert{name="firearm-magazine", count=10}
end)
script.on_event(defines.events.on_gui_click, function(event)
silo_script.on_gui_click(event)
end)
script.on_init(function()
global.version = version
silo_script.init()
end)
script.on_event(defines.events.on_rocket_launched, function(event)
silo_script.on_rocket_launched(event)
end)
script.on_configuration_changed(function(event)
if global.version ~= version then
global.version = version
end
silo_script.on_configuration_changed(event)
end)
silo_script.add_remote_interface()