2019-11-23 11:45:43 +01:00
local bb_config = require " maps.biter_battles_v2.config "
2019-10-28 17:38:36 +01:00
local event = require ' utils.event '
local Server = require ' utils.server '
2019-08-14 21:16:56 +02:00
local difficulties = {
[ 1 ] = { name = " Peaceful " , str = " 25% " , value = 0.25 , color = { r = 0.00 , g = 0.45 , b = 0.00 } , print_color = { r = 0.00 , g = 0.9 , b = 0.00 } } ,
2019-08-20 22:33:47 +02:00
[ 2 ] = { name = " Piece of cake " , str = " 50% " , value = 0.5 , color = { r = 0.00 , g = 0.35 , b = 0.00 } , print_color = { r = 0.00 , g = 0.7 , b = 0.00 } } ,
[ 3 ] = { name = " Easy " , str = " 75% " , value = 0.75 , color = { r = 0.00 , g = 0.25 , b = 0.00 } , print_color = { r = 0.00 , g = 0.5 , b = 0.00 } } ,
2019-08-14 21:16:56 +02:00
[ 4 ] = { name = " Normal " , str = " 100% " , value = 1 , color = { r = 0.00 , g = 0.00 , b = 0.25 } , print_color = { r = 0.0 , g = 0.0 , b = 0.7 } } ,
[ 5 ] = { name = " Hard " , str = " 150% " , value = 1.5 , color = { r = 0.25 , g = 0.00 , b = 0.00 } , print_color = { r = 0.5 , g = 0.0 , b = 0.00 } } ,
2019-11-28 03:53:19 +01:00
[ 6 ] = { name = " Nightmare " , str = " 250% " , value = 2.5 , color = { r = 0.35 , g = 0.00 , b = 0.00 } , print_color = { r = 0.7 , g = 0.0 , b = 0.00 } } ,
[ 7 ] = { name = " Insane " , str = " 500% " , value = 5 , color = { r = 0.45 , g = 0.00 , b = 0.00 } , print_color = { r = 0.9 , g = 0.0 , b = 0.00 } }
2019-08-14 21:16:56 +02:00
}
2019-09-01 02:42:14 +02:00
local timeout = 18000
2019-08-15 12:38:24 +02:00
2019-08-14 21:16:56 +02:00
local function difficulty_gui ( )
for _ , player in pairs ( game.connected_players ) do
if player.gui . top [ " difficulty_gui " ] then player.gui . top [ " difficulty_gui " ] . destroy ( ) end
local str = table.concat ( { " Global map difficulty is " , difficulties [ global.difficulty_vote_index ] . name , " . Mutagen has " , difficulties [ global.difficulty_vote_index ] . str , " effectiveness. " } )
local b = player.gui . top.add { type = " sprite-button " , caption = difficulties [ global.difficulty_vote_index ] . name , tooltip = str , name = " difficulty_gui " }
b.style . font = " heading-2 "
b.style . font_color = difficulties [ global.difficulty_vote_index ] . print_color
b.style . minimal_height = 38
b.style . minimal_width = 96
end
end
local function poll_difficulty ( player )
if player.gui . center [ " difficulty_poll " ] then player.gui . center [ " difficulty_poll " ] . destroy ( ) return end
2019-09-08 00:21:02 +02:00
2019-11-29 05:56:24 +01:00
if global.bb_settings . only_admins_vote or global.tournament_mode then
2019-09-08 00:21:02 +02:00
if not player.admin then return end
end
2020-04-21 15:13:19 +02:00
local tick = game.ticks_played
if tick > timeout then
2019-08-15 12:38:24 +02:00
if player.online_time ~= 0 then
2020-04-21 15:13:19 +02:00
local t = math.abs ( math.floor ( ( timeout - tick ) / 3600 ) )
2019-08-15 12:38:24 +02:00
local str = " Votes have closed " .. t
str = str .. " minute "
if t > 1 then str = str .. " s " end
str = str .. " ago. "
player.print ( str )
end
2019-08-14 21:16:56 +02:00
return
end
local frame = player.gui . center.add { type = " frame " , caption = " Vote global difficulty: " , name = " difficulty_poll " , direction = " vertical " }
for i = 1 , 7 , 1 do
local b = frame.add ( { type = " button " , name = tostring ( i ) , caption = difficulties [ i ] . name .. " ( " .. difficulties [ i ] . str .. " ) " } )
b.style . font_color = difficulties [ i ] . color
b.style . font = " heading-2 "
b.style . minimal_width = 180
end
local b = frame.add ( { type = " label " , caption = " - - - - - - - - - - - - - - - - - - - - " } )
2020-04-21 15:13:19 +02:00
local b = frame.add ( { type = " button " , name = " close " , caption = " Close ( " .. math.floor ( ( timeout - tick ) / 3600 ) .. " minutes left) " } )
2019-08-14 21:16:56 +02:00
b.style . font_color = { r = 0.66 , g = 0.0 , b = 0.66 }
b.style . font = " heading-3 "
b.style . minimal_width = 96
end
local function set_difficulty ( )
local a = 0
local vote_count = 0
for _ , d in pairs ( global.difficulty_player_votes ) do
a = a + d
vote_count = vote_count + 1
end
2019-09-08 00:21:02 +02:00
if vote_count == 0 then return end
2019-08-14 21:16:56 +02:00
a = a / vote_count
local new_index = math.round ( a , 0 )
if global.difficulty_vote_index ~= new_index then
2019-09-08 00:21:02 +02:00
local message = table.concat ( { " >> Map difficulty has changed to " , difficulties [ new_index ] . name , " difficulty! " } )
game.print ( message , difficulties [ new_index ] . print_color )
2019-10-28 17:38:36 +01:00
Server.to_discord_embed ( message )
2019-08-14 21:16:56 +02:00
end
global.difficulty_vote_index = new_index
global.difficulty_vote_value = difficulties [ new_index ] . value
end
local function on_player_joined_game ( event )
if not global.difficulty_vote_value then global.difficulty_vote_value = 1 end
if not global.difficulty_vote_index then global.difficulty_vote_index = 4 end
if not global.difficulty_player_votes then global.difficulty_player_votes = { } end
2019-09-08 00:21:02 +02:00
local player = game.players [ event.player_index ]
2020-04-21 15:13:19 +02:00
if game.ticks_played < timeout then
2019-09-08 00:21:02 +02:00
if not global.difficulty_player_votes [ player.name ] then
2019-11-29 05:56:24 +01:00
if global.bb_settings . only_admins_vote or global.tournament_mode then
2019-09-08 00:21:02 +02:00
if player.admin then poll_difficulty ( player ) end
else
poll_difficulty ( player )
end
end
2019-09-14 03:00:43 +02:00
else
if player.gui . center [ " difficulty_poll " ] then player.gui . center [ " difficulty_poll " ] . destroy ( ) end
2019-09-08 00:21:02 +02:00
end
2019-08-14 21:16:56 +02:00
difficulty_gui ( )
end
2019-09-08 00:21:02 +02:00
local function on_player_left_game ( event )
2020-04-21 15:13:19 +02:00
if game.ticks_played > timeout then return end
2019-09-08 00:21:02 +02:00
local player = game.players [ event.player_index ]
if not global.difficulty_player_votes [ player.name ] then return end
global.difficulty_player_votes [ player.name ] = nil
set_difficulty ( )
end
2019-08-14 21:16:56 +02:00
local function on_gui_click ( event )
if not event then return end
if not event.element then return end
if not event.element . valid then return end
local player = game.players [ event.element . player_index ]
if event.element . name == " difficulty_gui " then
poll_difficulty ( player )
return
end
2019-08-15 12:38:24 +02:00
if event.element . type ~= " button " then return end
2019-08-14 21:16:56 +02:00
if event.element . parent.name ~= " difficulty_poll " then return end
2019-10-05 04:18:18 +02:00
if event.element . name == " close " then event.element . parent.destroy ( ) return end
2020-04-21 15:13:19 +02:00
if game.ticks_played > timeout then event.element . parent.destroy ( ) return end
2019-11-29 04:38:33 +01:00
local i = tonumber ( event.element . name )
2019-11-29 05:56:24 +01:00
if global.bb_settings . only_admins_vote or global.tournament_mode then
2019-11-29 04:38:33 +01:00
if player.admin then
game.print ( player.name .. " has voted for " .. difficulties [ i ] . name .. " difficulty! " , difficulties [ i ] . print_color )
global.difficulty_player_votes [ player.name ] = i
set_difficulty ( )
difficulty_gui ( )
end
event.element . parent.destroy ( )
return
end
2019-08-14 21:16:56 +02:00
game.print ( player.name .. " has voted for " .. difficulties [ i ] . name .. " difficulty! " , difficulties [ i ] . print_color )
global.difficulty_player_votes [ player.name ] = i
set_difficulty ( )
2019-10-05 04:18:18 +02:00
difficulty_gui ( )
2019-08-14 21:16:56 +02:00
event.element . parent.destroy ( )
end
event.add ( defines.events . on_gui_click , on_gui_click )
2019-09-08 00:21:02 +02:00
event.add ( defines.events . on_player_left_game , on_player_left_game )
2019-08-14 21:16:56 +02:00
event.add ( defines.events . on_player_joined_game , on_player_joined_game )