2022-01-14 22:58:43 +02:00
local Event = require ' utils.event '
local Gui = require ' utils.gui '
local Public = require ' modules.wave_defense.table '
local Token = require ' utils.token '
local Task = require ' utils.task '
local Server = require ' utils.server '
2022-02-14 00:32:57 +02:00
local SpamProtection = require ' utils.spam_protection '
2022-01-14 22:58:43 +02:00
local main_frame_name = Gui.uid_name ( )
local save_button_name = Gui.uid_name ( )
local discard_button_name = Gui.uid_name ( )
2022-01-14 23:50:49 +02:00
local random = math.random
2022-01-14 22:58:43 +02:00
2022-01-14 23:50:49 +02:00
local random_greetings = {
' Dear defender ' ,
' Defenders ' ,
' Dear players ' ,
' Fellow players '
}
local random_greetings_size = # random_greetings
function Public . main_gui ( player , text )
2022-01-14 22:58:43 +02:00
local main_frame = player.gui . screen [ main_frame_name ]
if main_frame and main_frame.valid then
main_frame.destroy ( )
end
main_frame =
player.gui . screen.add (
{
type = ' frame ' ,
name = main_frame_name ,
caption = ' A stretch is needed. ' ,
direction = ' vertical '
}
)
main_frame.auto_center = true
local main_frame_style = main_frame.style
main_frame_style.width = 500
local inside_frame = main_frame.add { type = ' frame ' , style = ' inside_shallow_frame ' }
local inside_frame_style = inside_frame.style
inside_frame_style.padding = 0
local inside_table = inside_frame.add { type = ' table ' , column_count = 1 }
local inside_table_style = inside_table.style
inside_table_style.vertical_spacing = 0
inside_table.add ( { type = ' line ' } )
local info_main =
inside_table.add (
{
type = ' label ' ,
2022-01-14 23:50:49 +02:00
caption = ' [color=yellow] ' .. text .. ' ,[/color] '
2022-01-14 22:58:43 +02:00
}
)
local info_main_style = info_main.style
info_main_style.font = ' default-large-bold '
info_main_style.padding = 0
info_main_style.left_padding = 10
info_main_style.horizontal_align = ' left '
info_main_style.vertical_align = ' bottom '
info_main_style.single_line = false
info_main_style.font_color = { 0.55 , 0.55 , 0.99 }
inside_table.add ( { type = ' line ' } )
local info_sub =
inside_table.add (
{
type = ' label ' ,
2022-04-05 12:00:13 +02:00
caption = ' We have played for ' .. Server.format_time ( game.ticks_played ) .. ' now. \n If you want to take a quick break, \n please vote to pause the waves for 5 minutes. '
2022-01-14 22:58:43 +02:00
}
)
local info_sub_style = info_sub.style
info_sub_style.font = ' default-game '
info_sub_style.padding = 0
info_sub_style.left_padding = 10
info_sub_style.horizontal_align = ' left '
info_sub_style.vertical_align = ' bottom '
info_sub_style.single_line = false
inside_table.add ( { type = ' line ' } )
local bottom_flow = main_frame.add ( { type = ' flow ' , direction = ' horizontal ' } )
local left_flow = bottom_flow.add ( { type = ' flow ' } )
left_flow.style . horizontal_align = ' left '
left_flow.style . horizontally_stretchable = true
local close_button = left_flow.add ( { type = ' button ' , name = discard_button_name , caption = ' I cannot rest! ' } )
close_button.style = ' back_button '
local right_flow = bottom_flow.add ( { type = ' flow ' } )
right_flow.style . horizontal_align = ' right '
local save_button = right_flow.add ( { type = ' button ' , name = save_button_name , caption = ' I need to stretch ' } )
save_button.style = ' confirm_button '
player.opened = main_frame
end
2022-01-14 23:50:49 +02:00
function Public . display_pause_wave ( player , text )
2022-01-14 22:58:43 +02:00
if not player then
return
end
2022-01-14 23:50:49 +02:00
if not text then
return
end
return Public.main_gui ( player , text )
2022-01-14 22:58:43 +02:00
end
local function pause_waves_state ( state )
if state then
2022-01-14 23:50:49 +02:00
game.print ( ' [color=blue][Wave Defense][/color] New waves will not spawn for 5 minutes! ' , { r = 0.98 , g = 0.66 , b = 0.22 } )
2022-01-14 22:58:43 +02:00
Public.set ( ' paused ' , true )
else
2022-01-14 23:50:49 +02:00
game.print ( ' [color=blue][Wave Defense][/color] Waves will spawn normally again. ' , { r = 0.98 , g = 0.66 , b = 0.22 } )
2022-01-14 22:58:43 +02:00
Public.set ( ' paused ' , false )
end
end
local pause_waves_state_token = Token.register ( pause_waves_state )
Gui.on_click (
save_button_name ,
function ( event )
2022-02-14 00:32:57 +02:00
local is_spamming = SpamProtection.is_spamming ( event.player , nil , ' WD Save Button ' )
if is_spamming then
return
end
2022-01-14 22:58:43 +02:00
local player = event.player
if not player or not player.valid or not player.character then
return
end
local total_players = # game.connected_players
local pause_waves = Public.get ( ' pause_waves ' )
if not pause_waves [ player.index ] then
pause_waves [ player.index ] = true
pause_waves.index = pause_waves.index + 1
end
2022-01-14 23:50:49 +02:00
local divided = total_players / 2
if pause_waves.index >= divided then
Public.set ( ' pause_waves ' , { index = 0 } )
2022-01-14 22:58:43 +02:00
local players = game.connected_players
for i = 1 , # players do
local p = players [ i ]
local screen = p.gui . screen
local frame = screen [ main_frame_name ]
2022-01-14 23:50:49 +02:00
p.surface . play_sound ( { path = ' utility/new_objective ' , position = p.position , volume_modifier = 0.75 } )
2022-01-14 22:58:43 +02:00
if frame and frame.valid then
Gui.remove_data_recursively ( frame )
frame.destroy ( )
end
end
pause_waves_state ( true )
Task.set_timeout_in_ticks ( 18000 , pause_waves_state_token , false ) -- 5 minutes
return
end
local screen = player.gui . screen
local frame = screen [ main_frame_name ]
if frame and frame.valid then
Gui.remove_data_recursively ( frame )
frame.destroy ( )
end
end
)
Gui.on_click (
discard_button_name ,
function ( event )
2022-02-14 00:32:57 +02:00
local is_spamming = SpamProtection.is_spamming ( event.player , nil , ' WD Discard Button ' )
if is_spamming then
return
end
2022-01-14 22:58:43 +02:00
local player = event.player
local screen = player.gui . screen
local frame = screen [ main_frame_name ]
if not player or not player.valid or not player.character then
return
end
if frame and frame.valid then
Gui.remove_data_recursively ( frame )
frame.destroy ( )
end
end
)
Event.on_nth_tick (
216000 , -- 1 hour
function ( )
2022-01-14 23:50:49 +02:00
if game.ticks_played < 100 then
2022-01-14 22:58:43 +02:00
return
end
2022-04-05 12:00:13 +02:00
if Server.format_time ( game.ticks_played ) == 0 then
return
end
2022-01-14 23:50:49 +02:00
local greeting = random_greetings [ random ( 1 , random_greetings_size ) ]
2022-01-14 22:58:43 +02:00
local players = game.connected_players
for i = 1 , # players do
local player = players [ i ]
2022-01-14 23:50:49 +02:00
Public.display_pause_wave ( player , greeting )
2022-01-14 22:58:43 +02:00
end
end
)
return Public