1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00

Merge pull request #32 from Quadrum1/patch-2

Add tool: fancy_time.lua
This commit is contained in:
Gerkiz 2021-05-01 21:25:10 +02:00 committed by GitHub
commit dc7e594025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 154 additions and 10 deletions

View File

@ -9,6 +9,7 @@ local Utils = require 'utils.core'
local Color = require 'utils.color_presets'
local Server = require 'utils.server'
local Jail = require 'utils.datastore.jail_data'
local FancyTime = require 'tools.fancy_time'
local Public = {}
local match = string.match
@ -261,7 +262,8 @@ local function on_player_built_tile(event)
if #this.landfill_history > 1000 then
this.landfill_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' at X:'
str = str .. placed_tiles[1].position.x
@ -365,7 +367,8 @@ local function on_player_used_capsule(event)
this.capsule_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. msg
str = str .. ' at X:'
@ -420,7 +423,8 @@ local function on_entity_died(event)
chest = '[color=yellow]' .. event.entity.name .. '[/color]'
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. name .. ' destroyed '
str = str .. chest
@ -437,7 +441,8 @@ local function on_entity_died(event)
return
end
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
if cause and cause.name == 'character' and cause.player then
str = str .. cause.player.name .. ' destroyed '
@ -482,7 +487,8 @@ local function on_player_mined_entity(event)
if #this.mining_history > 1000 then
this.mining_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' mined '
str = str .. '[color=yellow]' .. entity.name .. '[/color]'
@ -516,7 +522,8 @@ local function on_player_mined_entity(event)
this.mining_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' mined '
str = str .. '[color=yellow]' .. event.entity.name .. '[/color]'
@ -563,7 +570,8 @@ local function on_gui_opened(event)
this.corpse_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' opened '
str = str .. '[color=yellow]' .. corpse_owner.name .. '[/color] body'
@ -617,7 +625,8 @@ local function on_pre_player_mined_item(event)
this.corpse_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' mined '
str = str .. '[color=yellow]' .. corpse_owner.name .. '[/color] body'
@ -709,7 +718,8 @@ local function on_player_cancelled_crafting(event)
this.cancel_crafting_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. player.name .. ' canceled '
str = str .. ' item [color=yellow]' .. event.recipe.name .. '[/color]'
@ -930,7 +940,8 @@ function Public.insert_into_capsule_history(player, position, msg)
if #this.capsule_history > 1000 then
this.capsule_history = {}
end
local t = math.abs(math.floor((game.tick) / 3600))
local t = math.abs(math.floor((game.tick) / 60))
t = FancyTime.short_fancy_time(t)
local str = '[' .. t .. '] '
str = str .. '[color=yellow]' .. msg .. '[/color]'
str = str .. ' at X:'

133
tools/fancy_time.lua Normal file
View File

@ -0,0 +1,133 @@
--luacheck: ignore
local Public = {}
--- Turns seconds into a fancy table, each index being a different modulos (second, minute, etc.)
---@param seconds <number>
---@param short <boolean> - True if strings should be kept as short as possible
function Public.seconds_to_fancy(seconds, short)
short = short or false
local time_left = seconds
if time_left < 1 then
return {"0 seconds"}
end
local names = {
{"second", "s"},
{"minute", "min"},
{"hour", "h"},
{"day", "d"},
{"week", "w"},
{"month", "m"},
{"year", "y"}
}
local modulos = {
1,
60,
3600,
3600*24,
3600*24*7,
3660*24*30,
3660*24*365.25
}
local values = {}
local pretty = {}
-- Iterates modulos in reverse
-- Adds fitting time to values
-- Subtracts time_left with added time
for i = #modulos, 1, -1 do
local fit_time = math.floor(time_left/modulos[i])
table.insert(values, fit_time)
time_left = time_left - fit_time*modulos[i]
end
local internal_name_index = 1
if not short then
internal_name_index = 1
else
internal_name_index = 2
end
-- Connects name with index value
-- #values-i+1 because the values array is flipped upside down and this converts the indices back!
for i = #names, 1, -1 do
if values[#values-i+1] > 0 then
local _name = names[i][internal_name_index]
-- Adds 's' to name if applicable. Example: 2 minute -> 2 minutes
if values[#values-i+1] > 1 and not short then
_name = _name .. "s"
end
table.insert(pretty, values[#values-i+1] .." ".._name)
end
end
return pretty
end
--- Turns a fancy table into a fancy string
---@param fancy_array <table>
function Public.fancy_time_formatting(fancy_array)
if #fancy_array == 0 then return end
local fancy_string = ""
if #fancy_array > 1 then
for i = 1, #fancy_array, 1 do
if i == 1 then
fancy_string = fancy_array[1]
else
fancy_string = fancy_string .. ", " .. fancy_array[i]
end
end
return fancy_string
else
return fancy_array[1]
end
end
--- Filters a fancy table for specific words. Entries within will be purged or kept based on the content and the filter mode
---@param mode <boolean> - True acts as whitelist, only time data in the filter will be kept. False acts a blacklist
function Public.filter_time(fancy_array, filter_words, mode)
local filtered_array = {}
for i=1, #fancy_array, 1 do
local _subject = fancy_array[i]
for fi=1, #filter_words, 1 do
local result = string.find(_subject, filter_words[fi]) -- nil if not found
result = type(result) ~= type(nil) -- true if words contained in string
if result == true and mode == true then
table.insert(filtered_array, _subject)
break
elseif result == true and mode == false then
break
elseif result == false and mode == false and fi == #filter_words then
table.insert(filtered_array, _subject)
end
end
end
return filtered_array
end
--- Quick function to turn seconds into a full string of time
--- Example: short_fancy_time(1803) --> "30 minutes, 3 seconds"
---@param seconds <number>
function Public.fancy_time(seconds)
local fancy = Public.seconds_to_fancy(seconds,false)
fancy = Public.fancy_time_formatting(fancy)
return fancy
end
--- Quick function to turn seconds into a shortend string of time, excluding seconds
--- Example: short_fancy_time(1803) --> "30 min"
---@param seconds <number>
function Public.short_fancy_time(seconds)
local fancy = Public.seconds_to_fancy(seconds,true)
fancy = Public.filter_time(fancy, {"seconds","s"}, false)
fancy = Public.fancy_time_formatting(fancy)
return fancy
end
return Public