railcarts/data_storage.lua
Ciaran Gultnieks 80bbb57df2 Initial commit
2014-03-26 19:10:30 +00:00

110 lines
3.2 KiB
Lua

-------------------------------------------------------------------------------
-- Monorail Mod by Sapier
--
-- You may copy, use, modify or do nearly anything except removing this
-- copyright notice.
-- And of course you are NOT allow to pretend you have written it.
--
--! @file data_storage.lua
--! @brief generic functions used in many different places
--! @copyright Sapier
--! @author Sapier
--! @date 2013-02-04
--!
-- Contact sapier a t gmx net
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- name: railcarts_get_current_time()
--
--! @brief alias to get current time
--
--! @return current time in seconds
-------------------------------------------------------------------------------
function railcarts_get_current_time()
return os.time(os.date('*t'))
--return minetest.get_time()
end
-------------------------------------------------------------------------------
-- name: railcarts_global_data_store(value)
--
--! @brief save data and return unique identifier
--
--! @param value to save
--
--! @return unique identifier
-------------------------------------------------------------------------------
railcarts_global_data_identifier = 0
railcarts_global_data = {}
railcarts_global_data.cleanup_index = 0
railcarts_global_data.last_cleanup = railcarts_get_current_time()
function railcarts_global_data_store(value)
local current_id = railcarts_global_data_identifier
railcarts_global_data_identifier = railcarts_global_data_identifier + 1
railcarts_global_data[current_id] = {
value = value,
added = railcarts_get_current_time(),
}
return current_id
end
-------------------------------------------------------------------------------
-- name: railcarts_global_data_store(value)
--
--! @brief pop data from global store
--
--! @param id to pop
--
--! @return stored value
-------------------------------------------------------------------------------
function railcarts_global_data_get(id)
local dataid = tonumber(id)
if dataid == nil or
railcarts_global_data[dataid] == nil then
return nil
end
local retval = railcarts_global_data[dataid].value
railcarts_global_data[dataid] = nil
return retval
end
-------------------------------------------------------------------------------
-- name: railcarts_global_data_store(value)
--
--! @brief pop data from global store
--
--! @param id to pop
--
--! @return stored value
-------------------------------------------------------------------------------
function railcarts_global_data_cleanup(id)
if railcarts_global_data.last_cleanup + 500 < railcarts_get_current_time() then
for i=1,50,1 do
if railcarts_global_data[railcarts_global_data.cleanup_index] ~= nil then
if railcarts_global_data[railcarts_global_data.cleanup_index].added <
railcarts_get_current_time() - 300 then
railcarts_global_data[railcarts_global_data.cleanup_index] = nil
end
railcarts_global_data.cleanup_index = railcarts_global_data.cleanup_index +1
if railcarts_global_data.cleanup_index > #railcarts_global_data then
railcarts_global_data.cleanup_index = 0
break
end
end
end
railcarts_global_data.last_cleanup = railcarts_get_current_time()
end
end