mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-03-03 14:53:01 +02:00
moved format_time to utils
This commit is contained in:
parent
b7a83be250
commit
0f0df92cd0
@ -3,6 +3,7 @@ local Global = require 'utils.global'
|
|||||||
local Gui = require 'utils.gui'
|
local Gui = require 'utils.gui'
|
||||||
local UserGroups = require 'user_groups'
|
local UserGroups = require 'user_groups'
|
||||||
local PlayerStats = require 'player_stats'
|
local PlayerStats = require 'player_stats'
|
||||||
|
local Utils = require 'utils.utils'
|
||||||
|
|
||||||
local poke_messages = require 'resources.poke_messages'
|
local poke_messages = require 'resources.poke_messages'
|
||||||
local player_sprites = require 'resources.player_sprites'
|
local player_sprites = require 'resources.player_sprites'
|
||||||
@ -75,36 +76,6 @@ local function lighten_color(color)
|
|||||||
color.a = 1
|
color.a = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
local minutes_to_ticks = 60 * 60
|
|
||||||
local hours_to_ticks = 60 * 60 * 60
|
|
||||||
local ticks_to_minutes = 1 / minutes_to_ticks
|
|
||||||
local ticks_to_hours = 1 / hours_to_ticks
|
|
||||||
|
|
||||||
local function format_time(ticks)
|
|
||||||
local result = {}
|
|
||||||
|
|
||||||
local hours = math.floor(ticks * ticks_to_hours)
|
|
||||||
if hours > 0 then
|
|
||||||
ticks = ticks - hours * hours_to_ticks
|
|
||||||
table.insert(result, hours)
|
|
||||||
if hours == 1 then
|
|
||||||
table.insert(result, 'hour')
|
|
||||||
else
|
|
||||||
table.insert(result, 'hours')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local minutes = math.floor(ticks * ticks_to_minutes)
|
|
||||||
table.insert(result, minutes)
|
|
||||||
if minutes == 1 then
|
|
||||||
table.insert(result, 'minute')
|
|
||||||
else
|
|
||||||
table.insert(result, 'minutes')
|
|
||||||
end
|
|
||||||
|
|
||||||
return table.concat(result, ' ')
|
|
||||||
end
|
|
||||||
|
|
||||||
local function format_distance(tiles)
|
local function format_distance(tiles)
|
||||||
return math.round(tiles * 0.001, 1) .. ' km'
|
return math.round(tiles * 0.001, 1) .. ' km'
|
||||||
end
|
end
|
||||||
@ -224,7 +195,7 @@ local column_builders = {
|
|||||||
return label
|
return label
|
||||||
end,
|
end,
|
||||||
draw_cell = function(parent, cell_data)
|
draw_cell = function(parent, cell_data)
|
||||||
local text = format_time(cell_data)
|
local text = Utils.format_time(cell_data)
|
||||||
|
|
||||||
local label = parent.add {type = 'label', name = time_cell_name, caption = text}
|
local label = parent.add {type = 'label', name = time_cell_name, caption = text}
|
||||||
local label_style = label.style
|
local label_style = label.style
|
||||||
@ -660,6 +631,7 @@ end
|
|||||||
Gui.on_click(
|
Gui.on_click(
|
||||||
poke_cell_name,
|
poke_cell_name,
|
||||||
function(event)
|
function(event)
|
||||||
|
game.print(serpent.dump(event.element.parent.type))
|
||||||
local element = event.element
|
local element = event.element
|
||||||
local button_data = Gui.get_data(element)
|
local button_data = Gui.get_data(element)
|
||||||
local poke_player = button_data.player
|
local poke_player = button_data.player
|
||||||
|
283
utils/utils.lua
283
utils/utils.lua
@ -1,152 +1,131 @@
|
|||||||
local Module = {}
|
local Module = {}
|
||||||
|
|
||||||
Module.distance = function(pos1, pos2)
|
Module.distance = function(pos1, pos2)
|
||||||
local dx = pos2.x - pos1.x
|
local dx = pos2.x - pos1.x
|
||||||
local dy = pos2.y - pos1.y
|
local dy = pos2.y - pos1.y
|
||||||
return math.sqrt(dx * dx + dy * dy)
|
return math.sqrt(dx * dx + dy * dy)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- rounds number (num) to certain number of decimal places (idp)
|
-- rounds number (num) to certain number of decimal places (idp)
|
||||||
math.round = function(num, idp)
|
math.round = function(num, idp)
|
||||||
local mult = 10 ^ (idp or 0)
|
local mult = 10 ^ (idp or 0)
|
||||||
return math.floor(num * mult + 0.5) / mult
|
return math.floor(num * mult + 0.5) / mult
|
||||||
end
|
end
|
||||||
|
|
||||||
function math.clamp(num, min, max)
|
function math.clamp(num, min, max)
|
||||||
if num < min then
|
if num < min then
|
||||||
return min
|
return min
|
||||||
elseif num > max then
|
elseif num > max then
|
||||||
return max
|
return max
|
||||||
else
|
else
|
||||||
return num
|
return num
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Module.print_except = function(msg, player)
|
Module.print_except = function(msg, player)
|
||||||
for _, p in pairs(game.players) do
|
for _, p in pairs(game.players) do
|
||||||
if p.connected and p ~= player then
|
if p.connected and p ~= player then
|
||||||
p.print(msg)
|
p.print(msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Module.print_admins = function(msg)
|
Module.print_admins = function(msg)
|
||||||
for _, p in pairs(game.players) do
|
for _, p in pairs(game.players) do
|
||||||
if p.connected and p.admin then
|
if p.connected and p.admin then
|
||||||
p.print(msg)
|
p.print(msg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Module.get_actor = function()
|
Module.get_actor = function()
|
||||||
if game.player then
|
if game.player then
|
||||||
return game.player.name
|
return game.player.name
|
||||||
end
|
end
|
||||||
return '<server>'
|
return '<server>'
|
||||||
end
|
end
|
||||||
|
|
||||||
Module.cast_bool = function(var)
|
Module.cast_bool = function(var)
|
||||||
if var then
|
if var then
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Module.find_entities_by_last_user =
|
Module.find_entities_by_last_user =
|
||||||
function(player, surface, filters)
|
function(player, surface, filters)
|
||||||
if type(player) == 'string' or not player then
|
if type(player) == 'string' or not player then
|
||||||
error(
|
error(
|
||||||
"bad argument #1 to '" ..
|
"bad argument #1 to '" ..
|
||||||
debug.getinfo(1, 'n').name .. "' (number or LuaPlayer expected, got " .. type(player) .. ')',
|
debug.getinfo(1, 'n').name .. "' (number or LuaPlayer expected, got " .. type(player) .. ')',
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if type(surface) ~= 'table' and type(surface) ~= 'number' then
|
if type(surface) ~= 'table' and type(surface) ~= 'number' then
|
||||||
error(
|
error(
|
||||||
"bad argument #2 to '" ..
|
"bad argument #2 to '" ..
|
||||||
debug.getinfo(1, 'n').name .. "' (number or LuaSurface expected, got " .. type(surface) .. ')',
|
debug.getinfo(1, 'n').name .. "' (number or LuaSurface expected, got " .. type(surface) .. ')',
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local entities = {}
|
local entities = {}
|
||||||
local surface = surface
|
local surface = surface
|
||||||
local player = player
|
local player = player
|
||||||
local filters = filters or {}
|
local filters = filters or {}
|
||||||
if type(surface) == 'number' then
|
if type(surface) == 'number' then
|
||||||
surface = game.surfaces[surface]
|
surface = game.surfaces[surface]
|
||||||
end
|
end
|
||||||
if type(player) == 'number' then
|
if type(player) == 'number' then
|
||||||
player = game.players[player]
|
player = game.players[player]
|
||||||
end
|
end
|
||||||
filters.force = player.force.name
|
filters.force = player.force.name
|
||||||
for _, e in pairs(surface.find_entities_filtered(filters)) do
|
for _, e in pairs(surface.find_entities_filtered(filters)) do
|
||||||
if e.last_user == player then
|
if e.last_user == player then
|
||||||
table.insert(entities, e)
|
table.insert(entities, e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return entities
|
return entities
|
||||||
end
|
end
|
||||||
|
|
||||||
local Gui = require("utils.gui")
|
Module.ternary = function(c, t, f)
|
||||||
local alert_frame_name = Gui.uid_name()
|
if c then
|
||||||
local alert_close_button_name = Gui.uid_name()
|
return t
|
||||||
function Module.alert(player, lines)
|
else
|
||||||
if type(lines) == string then
|
return f
|
||||||
lines = {lines}
|
end
|
||||||
end
|
end
|
||||||
local center = player.gui.center
|
|
||||||
local alert_frame = center[alert_frame_name]
|
|
||||||
if alert_frame and alert_frame.valid then
|
local minutes_to_ticks = 60 * 60
|
||||||
Gui.remove_data_recursivly(alert_frame)
|
local hours_to_ticks = 60 * 60 * 60
|
||||||
alert_frame.destroy()
|
local ticks_to_minutes = 1 / minutes_to_ticks
|
||||||
end
|
local ticks_to_hours = 1 / hours_to_ticks
|
||||||
alert_frame =
|
Module.format_time = function(ticks)
|
||||||
center.add {
|
local result = {}
|
||||||
type = 'frame',
|
|
||||||
name = alert_frame_name,
|
local hours = math.floor(ticks * ticks_to_hours)
|
||||||
direction = 'vertical',
|
if hours > 0 then
|
||||||
caption = 'Alert'
|
ticks = ticks - hours * hours_to_ticks
|
||||||
}
|
table.insert(result, hours)
|
||||||
alert_frame.style.maximal_width = 500
|
if hours == 1 then
|
||||||
player.opened = alert_frame
|
table.insert(result, 'hour')
|
||||||
for _,line in pairs(lines) do
|
else
|
||||||
local frame = alert_frame.add {
|
table.insert(result, 'hours')
|
||||||
type = 'label',
|
end
|
||||||
caption = line
|
end
|
||||||
}
|
|
||||||
frame.style.single_line = false
|
local minutes = math.floor(ticks * ticks_to_minutes)
|
||||||
end
|
table.insert(result, minutes)
|
||||||
alert_frame.add {type = 'button', name = alert_close_button_name, caption = 'Close'}
|
if minutes == 1 then
|
||||||
|
table.insert(result, 'minute')
|
||||||
end
|
else
|
||||||
|
table.insert(result, 'minutes')
|
||||||
Gui.on_custom_close(
|
end
|
||||||
alert_frame_name,
|
|
||||||
function(event)
|
return table.concat(result, ' ')
|
||||||
Gui.remove_data_recursivly(event.element)
|
end
|
||||||
event.element.destroy()
|
|
||||||
end
|
return Module
|
||||||
)
|
|
||||||
|
|
||||||
Gui.on_click(
|
|
||||||
alert_close_button_name,
|
|
||||||
function(event)
|
|
||||||
Gui.remove_data_recursivly(event.element)
|
|
||||||
event.element.parent.destroy()
|
|
||||||
end
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Module.ternary = function(c, t, f)
|
|
||||||
if c then
|
|
||||||
return t
|
|
||||||
else
|
|
||||||
return f
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return Module
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user