mirror of
https://github.com/ComfyFactory/ComfyFactorio.git
synced 2025-01-06 00:23:49 +02:00
new command - playtime
returns a value from the webpanel and prints it out to the player that ran the command
This commit is contained in:
parent
c6a58e565d
commit
6a262c01a2
@ -171,6 +171,67 @@ function Public.format_time(ticks)
|
||||
return concat(result, ' ')
|
||||
end
|
||||
|
||||
--- Takes a time and returns it in days, hours, minutes etc.
|
||||
function Public.get_formatted_playtime(ticks)
|
||||
if ticks < 5184000 then
|
||||
local y = ticks / 216000
|
||||
y = tostring(y)
|
||||
local h = ''
|
||||
for i = 1, 10, 1 do
|
||||
local z = string.sub(y, i, i)
|
||||
|
||||
if z == '.' then
|
||||
break
|
||||
else
|
||||
h = h .. z
|
||||
end
|
||||
end
|
||||
|
||||
local m = ticks % 216000
|
||||
m = m / 3600
|
||||
m = math.floor(m)
|
||||
m = tostring(m)
|
||||
|
||||
if h == '0' then
|
||||
local str = m .. ' minutes'
|
||||
return str
|
||||
else
|
||||
local str = h .. ' hours '
|
||||
str = str .. m
|
||||
str = str .. ' minutes'
|
||||
return str
|
||||
end
|
||||
else
|
||||
local y = ticks / 5184000
|
||||
y = tostring(y)
|
||||
local h = ''
|
||||
for i = 1, 10, 1 do
|
||||
local z = string.sub(y, i, i)
|
||||
|
||||
if z == '.' then
|
||||
break
|
||||
else
|
||||
h = h .. z
|
||||
end
|
||||
end
|
||||
|
||||
local m = ticks % 5184000
|
||||
m = m / 216000
|
||||
m = math.floor(m)
|
||||
m = tostring(m)
|
||||
|
||||
if h == '0' then
|
||||
local str = m .. ' days'
|
||||
return str
|
||||
else
|
||||
local str = h .. ' days '
|
||||
str = str .. m
|
||||
str = str .. ' hours'
|
||||
return str
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Prints a message letting the player know they cannot run a command
|
||||
-- @param name string name of the command
|
||||
function Public.cant_run(name)
|
||||
|
@ -1,4 +1,6 @@
|
||||
local Global = require 'utils.global'
|
||||
local Core = require 'utils.core'
|
||||
local Color = require 'utils.color_presets'
|
||||
local Game = require 'utils.game'
|
||||
local Token = require 'utils.token'
|
||||
local Task = require 'utils.task'
|
||||
@ -19,6 +21,7 @@ local settings = {
|
||||
}
|
||||
local set_data = Server.set_data
|
||||
local try_get_data = Server.try_get_data
|
||||
local try_get_data_and_print = Server.try_get_data_and_print
|
||||
local concat = table.concat
|
||||
|
||||
Global.register(
|
||||
@ -42,7 +45,7 @@ local Public = {
|
||||
}
|
||||
}
|
||||
|
||||
local try_download_data =
|
||||
local try_download_data_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
local key = data.key
|
||||
@ -64,7 +67,7 @@ local try_download_data =
|
||||
end
|
||||
)
|
||||
|
||||
local try_upload_data =
|
||||
local try_upload_data_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
local key = data.key
|
||||
@ -110,6 +113,34 @@ local try_upload_data =
|
||||
end
|
||||
)
|
||||
|
||||
local get_total_playtime_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
if not data then
|
||||
return
|
||||
end
|
||||
if not data.to_print then
|
||||
return
|
||||
end
|
||||
|
||||
local key = data.key
|
||||
local value = data.value
|
||||
local to_print = data.to_print
|
||||
local player = game.get_player(to_print)
|
||||
if player and player.valid then
|
||||
if key then
|
||||
if value then
|
||||
player.play_sound {path = 'utility/scenario_message', volume_modifier = 1}
|
||||
player.print('[color=blue]' .. key .. '[/color] has a total playtime of: ' .. Core.get_formatted_playtime(value))
|
||||
else
|
||||
player.play_sound {path = 'utility/cannot_build', volume_modifier = 1}
|
||||
player.print('[color=red]' .. key .. '[/color] was not found.')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
local nth_tick_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
@ -148,6 +179,34 @@ function Public.format_time(ticks, h, m)
|
||||
end
|
||||
end
|
||||
|
||||
--- Tries to get data from the webpanel and prints it out to the player
|
||||
-- @param <LuaPlayer>
|
||||
-- @param <TargetPlayer>
|
||||
function Public.get_and_print_to_player(player, TargetPlayer)
|
||||
if not (player and player.valid) then
|
||||
return
|
||||
end
|
||||
|
||||
local p = player.print
|
||||
|
||||
if not TargetPlayer then
|
||||
p('[ERROR] No player was provided.', Color.fail)
|
||||
return
|
||||
end
|
||||
|
||||
if not player.admin then
|
||||
p("[ERROR] You're not admin.", Color.fail)
|
||||
return
|
||||
end
|
||||
|
||||
local secs = Server.get_current_time()
|
||||
if secs == nil then
|
||||
return
|
||||
else
|
||||
try_get_data_and_print(session_data_set, TargetPlayer, player.name, get_total_playtime_token)
|
||||
end
|
||||
end
|
||||
|
||||
--- Tries to get data from the webpanel and updates the local table with values.
|
||||
-- @param data_set player token
|
||||
function Public.try_dl_data(key)
|
||||
@ -157,7 +216,7 @@ function Public.try_dl_data(key)
|
||||
session[key] = game.players[key].online_time
|
||||
return
|
||||
else
|
||||
try_get_data(session_data_set, key, try_download_data)
|
||||
try_get_data(session_data_set, key, try_download_data_token)
|
||||
end
|
||||
end
|
||||
|
||||
@ -169,7 +228,7 @@ function Public.try_ul_data(key)
|
||||
if secs == nil then
|
||||
return
|
||||
else
|
||||
try_get_data(session_data_set, key, try_upload_data)
|
||||
try_get_data(session_data_set, key, try_upload_data_token)
|
||||
end
|
||||
end
|
||||
|
||||
@ -301,6 +360,27 @@ Server.on_data_set_changed(
|
||||
end
|
||||
)
|
||||
|
||||
commands.add_command(
|
||||
'playtime',
|
||||
'Fetches a player total playtime or nil.',
|
||||
function(cmd)
|
||||
local player = game.player
|
||||
if not (player and player.valid) then
|
||||
return
|
||||
end
|
||||
|
||||
local p = player.print
|
||||
|
||||
local param = cmd.parameter
|
||||
if not param then
|
||||
p('[ERROR] No player was provided.', Color.fail)
|
||||
return
|
||||
end
|
||||
|
||||
Public.get_and_print_to_player(player, param)
|
||||
end
|
||||
)
|
||||
|
||||
Public.upload_data = upload_data
|
||||
|
||||
return Public
|
||||
|
@ -63,6 +63,7 @@ local stop_scenario_tag = '[STOP-SCENARIO]'
|
||||
local ping_tag = '[PING]'
|
||||
local data_set_tag = '[DATA-SET]'
|
||||
local data_get_tag = '[DATA-GET]'
|
||||
local data_get_and_print_tag = '[DATA-GET-AND-PRINT]'
|
||||
local data_get_all_tag = '[DATA-GET-ALL]'
|
||||
local data_tracked_tag = '[DATA-TRACKED]'
|
||||
local ban_sync_tag = '[BAN-SYNC]'
|
||||
@ -384,6 +385,15 @@ local function send_try_get_data(data_set, key, callback_token)
|
||||
raw_print(message)
|
||||
end
|
||||
|
||||
local function send_try_get_data_and_print(data_set, key, to_print, callback_token)
|
||||
data_set = double_escape(data_set)
|
||||
key = double_escape(key)
|
||||
to_print = double_escape(to_print)
|
||||
|
||||
local message = concat {data_get_and_print_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '",to_print:"', to_print, '"}'}
|
||||
raw_print(message)
|
||||
end
|
||||
|
||||
local cancelable_callback_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
@ -436,6 +446,13 @@ function Public.try_get_data(data_set, key, callback_token)
|
||||
send_try_get_data(data_set, key, callback_token)
|
||||
end
|
||||
|
||||
--- Same as try_get_data but prints the returned value to the given player who ran the command.
|
||||
function Public.try_get_data_and_print(data_set, key, to_print, callback_token)
|
||||
validate_arguments(data_set, key, callback_token)
|
||||
|
||||
send_try_get_data_and_print(data_set, key, to_print, callback_token)
|
||||
end
|
||||
|
||||
local function try_get_data_cancelable(data_set, key, callback_token)
|
||||
local keys = requests[data_set]
|
||||
if not keys then
|
||||
|
Loading…
Reference in New Issue
Block a user