1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

more rewrite

This commit is contained in:
grilledham 2018-06-28 18:28:47 +01:00
parent 1c2f699676
commit a56559ddc0

View File

@ -4,6 +4,9 @@ local Gui = require 'utils.gui'
local UserGroups = require 'user_groups'
local PlayerStats = require 'player_stats'
local poke_messages = require 'resources.poke_messages'
local poke_cooldown_time = 240 -- in ticks.
local symbol_asc = ''
local symbol_desc = ''
local normal_color = {r = 1, g = 1, b = 1}
@ -12,13 +15,20 @@ local focus_color = {r = 1, g = 0.55, b = 0.1}
local player_poke_cooldown = {}
local player_pokes = {}
local player_settings = {}
local no_notify_players = {}
Global.register(
{player_poke_cooldown = player_poke_cooldown, player_pokes = player_pokes, player_settings = player_settings},
{
player_poke_cooldown = player_poke_cooldown,
player_pokes = player_pokes,
player_settings = player_settings,
no_notify_players = no_notify_players
},
function(tbl)
player_poke_cooldown = tbl.player_poke_cooldown
player_pokes = player_pokes
player_settings = tbl.player_settings
no_notify_players = tbl.no_notify_players
end
)
@ -84,6 +94,21 @@ local function format_distance(tiles)
return math.round(tiles * 0.001, 1) .. ' km'
end
local function do_poke_spam_protection(player, poke_player_index)
if player.admin then
return true
end
local tick = player_poke_cooldown[poke_player_index] or 0
if tick < game.tick then
player_poke_cooldown[poke_player_index] = game.tick + poke_cooldown_time
return true
else
return false
end
end
local function apply_heading_style(style)
style.font_color = focus_color
style.font = 'default-bold'
@ -246,7 +271,7 @@ local column_builders = {
return a.count > b.count
end,
draw_heading = function(parent)
local label = parent.add {type = 'label', name = deaths_cell_name, caption = 'Deaths'}
local label = parent.add {type = 'label', name = deaths_heading_name, caption = 'Deaths'}
local label_style = label.style
apply_heading_style(label_style)
label_style.width = 100
@ -275,42 +300,56 @@ local column_builders = {
},
[poke_name_heading_name] = {
create_data = function(player)
return player_pokes[player.index] or 0
return {poke_count = player_pokes[player.index] or 0, player = player}
end,
sort = function(a, b)
return a > b
return a.poke_count > b.poke_count
end,
draw_heading = function(parent)
local label = parent.add {type = 'label', name = poke_name_heading_name, caption = 'Poke'}
local label_style = label.style
apply_heading_style(label_style)
label_style.width = 100
label_style.width = 64
return label
end,
draw_cell = function(parent, cell_data, data)
local label = parent.add {type = 'button', name = poke_cell_name, caption = cell_data}
local parent_style = parent.style
parent_style.width = 64
parent_style.align = 'center'
local label = parent.add {type = 'button', name = poke_cell_name, caption = cell_data.poke_count}
local label_style = label.style
label_style.align = 'center'
label_style.width = 100
label_style.minimal_width = 32
label_style.height = 24
label_style.font = 'default-bold'
label_style.top_padding = 0
label_style.bottom_padding = 0
label_style.left_padding = 0
label_style.right_padding = 0
Gui.set_data(label, {data = data, player = cell_data.player})
return label
end
}
}
local default_player_settings = {
columns = {
player_name_heading_name,
time_heading_name,
rank_heading_name,
distance_heading_name,
fish_heading_name,
deaths_heading_name,
poke_name_heading_name
},
sort = -2
}
local function get_default_player_settings()
return {
columns = {
player_name_heading_name,
time_heading_name,
rank_heading_name,
distance_heading_name,
fish_heading_name,
deaths_heading_name,
poke_name_heading_name
},
sort = -2
}
end
local function redraw_headings(data)
local settings = data.settings
@ -337,7 +376,7 @@ local function redraw_headings(data)
heading.caption = heading.caption .. sort_symbol
end
Gui.set_data(heading, data)
Gui.set_data(heading, {data = data, index = i})
end
end
@ -400,16 +439,17 @@ local function draw_main_frame(left, player)
local data = {
heading_table_flow = heading_table_flow,
cell_table_scroll_pane = cell_table_scroll_pane,
settings = default_player_settings
settings = get_default_player_settings()
}
redraw_headings(data)
redraw_cells(data)
Gui.set_data(frame, data)
end
local function remove_main_frame(frame)
Gui.remove_data_recursivly(frame)
frame.destroy()
Gui.destroy(frame)
end
local function toggle(event)
@ -430,13 +470,105 @@ local function player_joined(event)
return
end
local top = player.gui.top
local gui = player.gui
local top = gui.top
if not top[main_button_name] then
top.add {type = 'sprite-button', name = main_button_name, sprite = 'item/heavy-armor'}
top.add {type = 'sprite-button', name = main_button_name, sprite = 'entity/player'}
end
local frame = gui.left[main_frame_name]
if frame and frame.valid then
local data = Gui.get_data(frame)
redraw_cells(data)
end
end
local function tick()
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left[main_frame_name]
if frame and frame.valid then
local data = Gui.get_data(frame)
redraw_cells(data)
end
end
end
Event.add(defines.events.on_player_joined_game, player_joined)
Event.on_nth_tick(1800, tick)
Gui.on_click(main_button_name, toggle)
local function headings_click(event)
local heading_data = Gui.get_data(event.element)
local data = heading_data.data
local settings = data.settings
local index = heading_data.index
local sort = settings.sort
local sort_column = math.abs(sort)
if sort_column == index then
sort = -sort
else
sort = -index
end
settings.sort = sort
redraw_headings(data)
redraw_cells(data)
end
for name, _ in pairs(column_builders) do
Gui.on_click(name, headings_click)
end
Gui.on_click(
poke_cell_name,
function(event)
local element = event.element
local button_data = Gui.get_data(element)
local poke_player = button_data.player
local player = event.player
if poke_player == player then
return
end
local poke_player_index = poke_player.index
if not do_poke_spam_protection(player, poke_player_index) then
return
end
local count = (player_pokes[poke_player_index] or 0) + 1
player_pokes[poke_player_index] = count
local poke_str = poke_messages[math.random(#poke_messages)]
local message = table.concat({'>> ', player.name, ' has poked ', poke_player.name, ' with ', poke_str, ' <<'})
for _, p in ipairs(game.connected_players) do
local frame = p.gui.left[main_frame_name]
if frame and frame.valid then
local frame_data = Gui.get_data(frame)
local settings = frame_data.settings
local columns = settings.columns
local sort = settings.sort
local sorted_column = columns[math.abs(sort)]
if sorted_column == poke_name_heading_name then
redraw_cells(frame_data)
else
element.caption = count
end
end
if not no_notify_players[p.index] then
p.print(message)
end
end
end
)