Page 1 of 1

Question about voting system in control.lua:

Posted: Wed Jan 03, 2018 8:27 am
by Greave
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?

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()
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?

Re: Question about voting system in control.lua:

Posted: Wed Jan 03, 2018 10:08 am
by darkfrei

Re: Question about voting system in control.lua:

Posted: Wed Jan 03, 2018 2:32 pm
by Greave
Ive looked into that actually, I think its overkill for what im trying to accomplish.
I also dont want to give players the option to adjust settings, since the server runs on very specific variables for difficulty balancing.
Isnt it possible to just restart the scenario on a trigger?

Re: Question about voting system in control.lua:

Posted: Wed Jan 03, 2018 3:48 pm
by darkfrei
Greave wrote: Isnt it possible to just restart the scenario on a trigger?
Create new surface, teleport all players to it, delete old surface.

If scenario has some memory, it must be reset. For example, amount of rockets. If you change by all players forces, then it must help you.

Re: Question about voting system in control.lua:

Posted: Fri Jan 05, 2018 9:52 am
by eradicator
darkfrei wrote:
Greave wrote: Isnt it possible to just restart the scenario on a trigger?
Create new surface, teleport all players to it, delete old surface.

If scenario has some memory, it must be reset. For example, amount of rockets. If you change by all players forces, then it must help you.
There's no built-in way to "restart" a scenario on the same map. You'll have to implement that manually, for example by deleting all player characters, deleting all surface chunks and then regenerating/respawning the starting chunks/players.
If you don't care about the landscape (i.e. landfill, trees) you can regenerate ore manually without deleting chunks first:
http://lua-api.factorio.com/latest/LuaG ... ate_entity
(trees/rocks might actually work, but not sure about that.)
Also don't forget to clear the forces chart (i.e. reset "fog of war").

Creating new surfaces for that purpose brings it's own set of problems, such as incompatible mods. And you have to properly manage/remove old surfaces anyway, or risk memory/cpu issues or even more mod incompatibility.