1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-16 10:19:27 +02:00
RedMew/features/gui/score.lua

167 lines
4.6 KiB
Lua
Raw Normal View History

local Global = require 'utils.global'
2018-11-20 12:46:32 +02:00
local Event = require 'utils.event'
local Token = require 'utils.token'
local Schedule = require 'utils.task'
local Gui = require 'utils.gui'
2018-12-29 19:39:20 +02:00
local Color = require 'resources.color_presets'
local Server = require 'features.server'
local ScoreTracker = require 'utils.score_tracker'
2019-05-31 23:14:22 +02:00
local format_number = require 'util'.format_number
local pairs = pairs
2019-06-01 13:41:12 +02:00
local concat = table.concat
local scores_to_show = global.config.score.global_to_show
local set_timeout_in_ticks = Schedule.set_timeout_in_ticks
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
local memory = {
redraw_score_scheduled = false,
player_last_position = {},
player_death_causes = {}
}
Global.register(memory, function(tbl) memory = tbl end)
---Creates a map of score name => {captain, tooltip}
local function get_global_score_labels()
2019-06-01 13:41:12 +02:00
local scores = ScoreTracker.get_global_scores_with_metadata(scores_to_show)
local score_labels = {}
2019-06-01 13:41:12 +02:00
for index = 1, #scores do
local score_data = scores[index]
score_labels[score_data.name] = {
caption = concat({score_data.icon, format_number(score_data.value, true)}, ' '),
tooltip = score_data.locale_string
}
end
2019-06-01 13:41:12 +02:00
return score_labels
end
local do_redraw_score = Token.register(function()
local players = game.connected_players
local scores = get_global_score_labels()
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
for score_name, textual_display in pairs(scores) do
score_table[score_name].caption = textual_display.caption
end
end
end
memory.redraw_score_scheduled = false
end)
local function schedule_redraw_score()
if memory.redraw_score_scheduled then
return
end
-- throttle redraws
set_timeout_in_ticks(30, do_redraw_score)
memory.redraw_score_scheduled = true
end
local function player_joined_game(event)
local player = game.get_player(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-05-26 13:21:40 +02:00
top.add({
type = 'sprite-button',
name = main_button_name,
sprite = 'achievement/there-is-no-spoon',
tooltip = {'score.tooltip'}
})
2018-11-20 12:46:32 +02:00
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)
local scores = get_global_score_labels()
2019-01-20 18:50:10 +02:00
local frame = top.add {type = 'frame', name = main_frame_name}
local score_table = frame.add {type = 'table', name = 'score_table', column_count = 8}
local style = score_table.style
style.vertical_spacing = 4
style.horizontal_spacing = 16
2019-01-20 18:50:10 +02:00
for score_name, textual_display in pairs(scores) do
local label = score_table.add({
type = 'label',
name = score_name,
caption = textual_display.caption,
tooltip = textual_display.tooltip
})
score_label_style(label, Color.white)
end
2019-01-20 18:50:10 +02:00
end
local function global_score_changed(event)
local found = false
for index = 1, #scores_to_show do
if scores_to_show[index] then
found = true
end
end
if not found then
return
end
schedule_redraw_score()
if event.score_name ~= 'satellites-launched' then
return
end
2019-05-31 21:42:15 +02:00
local count = ScoreTracker.get_for_global('satellites-launched')
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
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
local top = player.gui.top
local frame = top[main_frame_name]
2019-06-20 13:39:18 +02:00
local main_button = top[main_button_name]
2018-11-20 12:46:32 +02:00
2019-01-20 18:50:10 +02:00
if not frame then
score_show(top)
2019-06-20 13:39:18 +02:00
main_button.style = 'selected_slot_button'
local style = main_button.style
style.width = 38
style.height = 38
2019-01-20 18:50:10 +02:00
else
2018-11-20 12:46:32 +02:00
frame.destroy()
2019-06-20 13:39:18 +02:00
main_button.style = 'icon_button'
2018-11-20 12:46:32 +02:00
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)
Event.add(defines.events.on_player_joined_game, player_joined_game)
Event.add(ScoreTracker.events.on_global_score_changed, global_score_changed)