1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-10 00:43:27 +02:00
ComfyFactorio/utils/datastore/current_time_data.lua

132 lines
3.2 KiB
Lua
Raw Normal View History

2023-11-12 00:59:17 +02:00
-- created by Gerkiz for ComfyFactorio
local Server = require 'utils.server'
local Event = require 'utils.event'
2022-04-05 19:28:08 +02:00
local Gui = require 'utils.gui'
local Color = require 'utils.color_presets'
local current_time_label = 'current_time_label'
local function validate_player(player)
2020-10-24 14:46:14 +02:00
if not player then
return false
end
if not player.valid then
return false
end
return true
end
2020-10-24 14:46:14 +02:00
local function set_location(player)
local gui = player.gui
local label = gui.screen[current_time_label]
2020-10-22 12:44:27 +02:00
if not label or not label.valid then
return
end
local res = player.display_resolution
local uis = player.display_scale
label.location = {x = res.width - 423 * uis, y = 50 * uis}
end
2020-10-24 14:46:14 +02:00
local function create_label(player)
2022-01-05 23:49:07 +02:00
local date = Server.get_current_date_with_time()
if not date then
date = '1970-01-01'
end
2020-10-24 14:46:14 +02:00
local label =
player.gui.screen.add(
{
type = 'label',
name = current_time_label,
caption = date
2020-10-24 14:46:14 +02:00
}
)
local style = label.style
style.font = 'default-game'
return label
2020-10-21 23:17:17 +02:00
end
Event.add(
defines.events.on_player_joined_game,
function(event)
local player = game.get_player(event.player_index)
local label = player.gui.screen[current_time_label]
if not label or not label.valid then
2020-10-24 14:46:14 +02:00
label = create_label(player)
end
2020-10-24 14:46:14 +02:00
set_location(player)
label.visible = false
end
)
-- Update the value each second
Event.on_nth_tick(
60,
function()
2022-01-05 23:49:07 +02:00
local date = Server.get_current_date_with_time()
if not date then
date = '1969-01-01 00:00'
end
local players = game.connected_players
2020-10-24 14:46:14 +02:00
for i = 1, #players do
local player = players[i]
local label = player.gui.screen[current_time_label]
if label and label.valid then
label.caption = date
2020-10-24 14:46:14 +02:00
set_location(player)
end
end
end
)
commands.add_command(
'server-date',
'Toggle to show the date',
function()
local player = game.player
local secs = Server.get_current_time()
if validate_player(player) then
if not secs then
return player.print('Not running on Comfy backend.', Color.warning)
end
local label = player.gui.screen[current_time_label]
if not label or not label.valid then
2020-10-24 14:46:14 +02:00
label = create_label(player)
end
if label.visible then
label.visible = false
player.print('Removed date-label.', Color.warning)
else
label.visible = true
2020-10-24 14:46:14 +02:00
set_location(player)
player.print('Added date-label.', Color.success)
end
end
end
)
2022-04-05 19:28:08 +02:00
Gui.screen_to_bypass(current_time_label)
2021-02-13 14:04:22 +02:00
2020-10-24 14:46:14 +02:00
Event.add(
defines.events.on_player_display_resolution_changed,
function(event)
local player = game.get_player(event.player_index)
set_location(player)
end
)
Event.add(
defines.events.on_player_display_scale_changed,
function(event)
local player = game.get_player(event.player_index)
set_location(player)
end
)