2018-11-20 12:46:32 +02:00
|
|
|
local Event = require 'utils.event'
|
2018-09-23 00:25:13 +02:00
|
|
|
local Game = require 'utils.game'
|
2018-12-15 22:28:53 +02:00
|
|
|
local PlayerStats = require 'features.player_stats'
|
2018-12-16 03:28:00 +02:00
|
|
|
local Gui = require 'utils.gui'
|
2018-12-29 19:39:20 +02:00
|
|
|
local Color = require 'resources.color_presets'
|
2019-01-20 18:50:10 +02:00
|
|
|
local Server = require('features.server')
|
2018-04-06 21:58:50 +02:00
|
|
|
|
2019-01-21 22:37:07 +02:00
|
|
|
local concat = table.concat
|
2019-01-20 18:50:10 +02:00
|
|
|
|
|
|
|
local main_frame_name = Gui.uid_name()
|
|
|
|
local main_button_name = Gui.uid_name()
|
2017-06-13 13:16:07 +02:00
|
|
|
|
2019-03-01 11:18:39 +02:00
|
|
|
local descriptions = {
|
|
|
|
{disc = 'Satellites launched', icon = '[img=item.satellite]'},
|
|
|
|
{disc = 'Biters liberated', icon = '[img=entity.medium-biter]'},
|
|
|
|
{disc = 'Buildings by hand', icon = '[img=utility.hand]'},
|
|
|
|
{disc = 'Buildings by robots', icon = '[img=item.construction-robot]'},
|
|
|
|
{disc = 'Trees chopped', icon = '[img=entity.tree-02]'},
|
|
|
|
{disc = 'Rocks smashed', icon = '[img=entity.rock-huge]'},
|
|
|
|
{disc = 'Kills by train', icon = '[img=item.locomotive]'},
|
|
|
|
{disc = 'Coins spent', icon = '[img=item.coin]'},
|
|
|
|
}
|
|
|
|
|
2017-06-13 13:16:07 +02:00
|
|
|
local function create_score_gui(event)
|
2018-11-20 12:46:32 +02:00
|
|
|
local player = Game.get_player_by_index(event.player_index)
|
2019-01-20 18:50:10 +02:00
|
|
|
if not player then
|
|
|
|
return
|
|
|
|
end
|
2018-11-20 12:46:32 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
local top = player.gui.top
|
|
|
|
|
2019-01-20 20:25:53 +02:00
|
|
|
if not top[main_button_name] then
|
2019-01-21 22:37:07 +02:00
|
|
|
top.add({type = 'sprite-button', name = main_button_name, sprite = 'achievement/there-is-no-spoon'})
|
2018-11-20 12:46:32 +02:00
|
|
|
end
|
2017-06-13 13:16:07 +02:00
|
|
|
end
|
|
|
|
|
2018-11-06 13:55:52 +02:00
|
|
|
local function refresh_score()
|
2019-01-20 18:50:10 +02:00
|
|
|
local players = game.connected_players
|
2019-01-21 22:37:07 +02:00
|
|
|
local count = game.forces.player.get_item_launched('satellite')
|
2019-01-20 18:50:10 +02:00
|
|
|
|
2019-03-01 11:18:39 +02:00
|
|
|
local satellites_launched = concat {descriptions[1].icon .. ' ', count, ' '}
|
|
|
|
local biters_liberated = concat {descriptions[2].icon .. ' ', PlayerStats.get_total_biter_kills(), ' '}
|
|
|
|
local buildings_by_hand = concat {descriptions[3].icon .. ' ', PlayerStats.get_total_player_built_entities(), ' '}
|
|
|
|
local buildings_by_robot = concat {descriptions[4].icon .. ' ', PlayerStats.get_total_robot_built_entities(), ' '}
|
|
|
|
local trees_chopped = concat {descriptions[5].icon .. ' ', PlayerStats.get_total_player_trees_mined(), ' '}
|
|
|
|
local rocks_smashed = concat {descriptions[6].icon .. ' ', PlayerStats.get_total_player_rocks_mined(), ' '}
|
|
|
|
local kills_by_train = concat {descriptions[7].icon .. ' ', PlayerStats.get_total_train_kills(), ' '}
|
|
|
|
local coins_spent = concat {descriptions[8].icon .. ' ', PlayerStats.get_total_coins_spent(), ' '}
|
2019-01-20 18:50:10 +02:00
|
|
|
|
|
|
|
for i = 1, #players do
|
|
|
|
local player = players[i]
|
|
|
|
local frame = player.gui.top[main_frame_name]
|
|
|
|
|
|
|
|
if frame and frame.valid then
|
|
|
|
local score_table = frame.score_table
|
2019-01-21 22:37:07 +02:00
|
|
|
score_table.label_satellites_launched.caption = satellites_launched
|
2019-01-20 18:50:10 +02:00
|
|
|
score_table.label_biters_killed.caption = biters_liberated
|
|
|
|
score_table.label_player_built_entities.caption = buildings_by_hand
|
|
|
|
score_table.label_robot_built_entities.caption = buildings_by_robot
|
|
|
|
score_table.label_player_mined_trees.caption = trees_chopped
|
|
|
|
score_table.label_player_mined_stones.caption = rocks_smashed
|
|
|
|
score_table.label_kills_by_train.caption = kills_by_train
|
2019-01-21 22:37:07 +02:00
|
|
|
score_table.label_coins_spent.caption = coins_spent
|
2018-11-20 12:46:32 +02:00
|
|
|
end
|
|
|
|
end
|
2017-06-13 13:16:07 +02:00
|
|
|
end
|
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
local function score_label_style(label, color)
|
|
|
|
local style = label.style
|
|
|
|
style.font = 'default-bold'
|
|
|
|
style.font_color = color
|
|
|
|
end
|
2018-11-20 12:46:32 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
local function score_show(top)
|
2019-01-21 22:37:07 +02:00
|
|
|
local count = game.forces.player.get_item_launched('satellite')
|
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
local frame = top.add {type = 'frame', name = main_frame_name}
|
2019-03-01 11:18:39 +02:00
|
|
|
local score_table = frame.add {type = 'table', name = 'score_table', column_count = 8}
|
2019-01-21 22:37:07 +02:00
|
|
|
local style = score_table.style
|
|
|
|
style.vertical_spacing = 4
|
|
|
|
style.horizontal_spacing = 16
|
2019-01-20 18:50:10 +02:00
|
|
|
|
|
|
|
local label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
2019-01-21 22:37:07 +02:00
|
|
|
name = 'label_satellites_launched',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[1].icon .. ' ', count, ' '},
|
|
|
|
tooltip = descriptions[1].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2018-11-20 12:46:32 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_biters_killed',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[2].icon .. ' ', PlayerStats.get_total_biter_kills(), ' '},
|
|
|
|
tooltip = descriptions[2].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_player_built_entities',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[3].icon .. ' ', PlayerStats.get_total_player_built_entities(), ' '},
|
|
|
|
tooltip = descriptions[3].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_robot_built_entities',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[4].icon .. ' ', PlayerStats.get_total_robot_built_entities(), ' '},
|
|
|
|
tooltip = descriptions[4].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_player_mined_trees',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[5].icon .. ' ', PlayerStats.get_total_player_trees_mined(), ' '},
|
|
|
|
tooltip = descriptions[5].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_player_mined_stones',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[6].icon .. ' ', PlayerStats.get_total_player_rocks_mined(), ' '},
|
|
|
|
tooltip = descriptions[6].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_kills_by_train',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[7].icon .. ' ', PlayerStats.get_total_train_kills(), ' '},
|
|
|
|
tooltip = descriptions[7].disc
|
2019-01-20 18:50:10 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-21 22:37:07 +02:00
|
|
|
label =
|
|
|
|
score_table.add {
|
|
|
|
type = 'label',
|
|
|
|
name = 'label_coins_spent',
|
2019-03-01 11:18:39 +02:00
|
|
|
caption = concat {descriptions[8].icon .. ' ', PlayerStats.get_total_coins_spent(), ' '},
|
|
|
|
tooltip = descriptions[8].disc
|
2019-01-21 22:37:07 +02:00
|
|
|
}
|
2019-03-01 11:18:39 +02:00
|
|
|
score_label_style(label, Color.white)
|
2019-01-20 18:50:10 +02:00
|
|
|
end
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-21 22:37:07 +02:00
|
|
|
local function rocket_launched(event)
|
2019-01-24 01:33:43 +02:00
|
|
|
local entity = event.rocket
|
|
|
|
|
|
|
|
if not entity or not entity.valid or not entity.force == 'player' then
|
2019-01-21 22:37:07 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local inventory = entity.get_inventory(defines.inventory.rocket)
|
|
|
|
if not inventory or not inventory.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local count = inventory.get_item_count('satellite')
|
|
|
|
if count == 0 then
|
|
|
|
return
|
|
|
|
end
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-21 22:37:07 +02:00
|
|
|
count = game.forces.player.get_item_launched('satellite')
|
2018-12-15 22:28:53 +02:00
|
|
|
|
2019-01-24 01:33:43 +02:00
|
|
|
if (count < 10) or ((count < 50) and ((count % 5) == 0)) or ((count % 25) == 0) then
|
|
|
|
local message = 'A satellite has been launched! Total count: ' .. count
|
|
|
|
|
|
|
|
game.print(message)
|
|
|
|
Server.to_discord_bold(message)
|
|
|
|
end
|
2019-01-24 15:38:17 +02:00
|
|
|
|
|
|
|
refresh_score()
|
2017-06-13 13:16:07 +02:00
|
|
|
end
|
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
Gui.on_click(
|
|
|
|
main_button_name,
|
|
|
|
function(event)
|
|
|
|
local player = event.player
|
2018-11-20 12:46:32 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
local top = player.gui.top
|
|
|
|
local frame = top[main_frame_name]
|
2018-11-20 12:46:32 +02:00
|
|
|
|
2019-01-20 18:50:10 +02:00
|
|
|
if not frame then
|
|
|
|
score_show(top)
|
|
|
|
else
|
2018-11-20 12:46:32 +02:00
|
|
|
frame.destroy()
|
|
|
|
end
|
|
|
|
end
|
2019-01-20 18:50:10 +02:00
|
|
|
)
|
2017-06-13 13:16:07 +02:00
|
|
|
|
2019-01-25 13:23:42 +02:00
|
|
|
Gui.allow_player_to_toggle_top_element_visibility(main_button_name)
|
2018-12-16 03:28:00 +02:00
|
|
|
|
2018-04-06 21:58:50 +02:00
|
|
|
Event.add(defines.events.on_player_joined_game, create_score_gui)
|
|
|
|
Event.add(defines.events.on_rocket_launched, rocket_launched)
|
2018-12-15 22:28:53 +02:00
|
|
|
Event.on_nth_tick(300, refresh_score)
|