mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-27 11:35:22 +09:00
Added Store
This commit is contained in:
@@ -7,12 +7,33 @@
|
||||
Public.type_check_error(value,test_type,error_message,level) --- Raises an error if the value is of the incorrect type
|
||||
Public.param_check(value,test_type,param_name,param_number) --- Raises an error when the value is the incorrect type, uses a consistent error message format
|
||||
|
||||
Public.extract_keys(tbl,...) --- Extracts certain keys from a table
|
||||
|
||||
Public.player_return(value,colour,player) --- Will return a value of any type to the player/server console, allows colour for in-game players
|
||||
|
||||
Public.opt_require(path) --- Calls a require that will not error if the file is not found
|
||||
Public.ext_require(path,...) --- Calls a require and returns only the keys given, file must return a table
|
||||
|
||||
Public.format_time(ticks,options) --- Formats tick into a clean format, denominations from highest to lowest
|
||||
|
||||
Public.move_items(items,surface,position,radius,chest_type) --- Moves items to the position and stores them in the closest entity of the type given
|
||||
|
||||
Public.print_grid_value(value, surface, position, scale, offset, immutable) --- Prints a colored value on a location.
|
||||
Public.print_colored_grid_value(value, surface, position, offset, immutable,
|
||||
color_value, base_color, delta_color, under_bound, over_bound) --- Prints a colored value on a location. with extra settings.
|
||||
Public.clear_flying_text(surface) --- Clears all flying text entites on a surface
|
||||
|
||||
Public.string_contains(s, contains) --- Tests if a string contains a given substring.
|
||||
|
||||
Public.extract_keys(tbl,...) --- Extracts certain keys from a table
|
||||
Public.enum(tbl) --- Converts a table to an enum
|
||||
Public.auto_complete(options,input,use_key,rtn_key) --- Returns the closest match to the input
|
||||
Public.table_keys(tbl) --- Returns all the keys of a table
|
||||
Public.table_values(tbl) --- Returns all the values of a table
|
||||
Public.table_alphanumsort(tbl) --- Returns the list is a sorted way that would be expected by people (this is by key)
|
||||
Public.table_keysort(tbl) --- Returns the list is a sorted way that would be expected by people (this is by key) (faster alterative than above)
|
||||
|
||||
Public.format_chat_colour(message,color) --- Returns a message with valid chat tags to change its colour
|
||||
Public.format_chat_colour_localized(message,color) --- Returns a message with valid chat tags to change its colour, using localization
|
||||
Public.format_chat_player_name(player,raw_string) --- Returns the players name in the players color
|
||||
]]
|
||||
|
||||
local Colours = require 'resources.color_presets'
|
||||
@@ -61,19 +82,6 @@ function Public.param_check(value,test_type,param_name,param_number)
|
||||
return true
|
||||
end
|
||||
|
||||
--- Extracts certain keys from a table
|
||||
-- @usage local key_three, key_one = extract({key_one='foo',key_two='bar',key_three=true},'key_three','key_one')
|
||||
-- @tparam tbl table the table which contains the keys
|
||||
-- @tparam ... string the names of the keys you want extracted
|
||||
-- @return the keys in the order given
|
||||
function Public.extract_keys(tbl,...)
|
||||
local values = {}
|
||||
for _,key in pairs({...}) do
|
||||
table.insert(values,tbl[key])
|
||||
end
|
||||
return unpack(values)
|
||||
end
|
||||
|
||||
--- Will return a value of any type to the player/server console, allows colour for in-game players
|
||||
-- @usage player_return('Hello, World!') -- returns 'Hello, World!' to game.player or server console
|
||||
-- @usage player_return('Hello, World!','green') -- returns 'Hello, World!' to game.player with colour green or server console
|
||||
@@ -413,6 +421,8 @@ function Public.print_colored_grid_value(value, surface, position, offset, immut
|
||||
}.active = false
|
||||
end
|
||||
|
||||
--- Clears all flying text entites on a surface
|
||||
-- @tparam surface LuaSurface the surface to clear
|
||||
function Public.clear_flying_text(surface)
|
||||
local entities = surface.find_entities_filtered{name ='flying-text'}
|
||||
for _,entity in pairs(entities) do
|
||||
@@ -430,7 +440,38 @@ function Public.string_contains(s, contains)
|
||||
return s and string.find(s, contains) ~= nil
|
||||
end
|
||||
|
||||
--- Returns the closest match to a key
|
||||
--- Extracts certain keys from a table
|
||||
-- @usage local key_three, key_one = extract({key_one='foo',key_two='bar',key_three=true},'key_three','key_one')
|
||||
-- @tparam tbl table the table which contains the keys
|
||||
-- @tparam ... string the names of the keys you want extracted
|
||||
-- @return the keys in the order given
|
||||
function Public.extract_keys(tbl,...)
|
||||
local values = {}
|
||||
for _,key in pairs({...}) do
|
||||
table.insert(values,tbl[key])
|
||||
end
|
||||
return unpack(values)
|
||||
end
|
||||
|
||||
--- Converts a table to an enum
|
||||
-- @tparam tbl table the table that will be converted
|
||||
-- @treturn table the new table that acts like an enum
|
||||
function Public.enum(tbl)
|
||||
local rtn = {}
|
||||
for k,v in pairs(tbl) do
|
||||
if type(k) ~= 'number' then
|
||||
rtn[v]=k
|
||||
end
|
||||
end
|
||||
for k,v in pairs(tbl) do
|
||||
if type(k) == 'number' then
|
||||
table.insert(rtn,v)
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
--- Returns the closest match to the input
|
||||
-- @tparam options table a table of options for the auto complete
|
||||
-- @tparam input string the input string that will be completed
|
||||
-- @tparam[opt=false] use_key boolean when true the keys of options will be used as the options
|
||||
|
||||
72
expcore/store.lua
Normal file
72
expcore/store.lua
Normal file
@@ -0,0 +1,72 @@
|
||||
--- A system which stores peristent data and makes it easy to sync updates between changes
|
||||
local Global = require 'utils.global'
|
||||
local Enum = ext_require('expcore.common','enum')
|
||||
|
||||
local Store = {
|
||||
data = {},
|
||||
locations = {},
|
||||
types = Enum{
|
||||
'local', -- no persistent data, only triggers update_callback
|
||||
'player', -- each player has they own sub_location
|
||||
'force', -- each force has its own sub_location
|
||||
'surface', -- each surface has its own sub_location
|
||||
'game', -- the entrie game has a single store of data
|
||||
'global' -- not yet impimented, data will sync between all servers
|
||||
}
|
||||
}
|
||||
Global.register(Store.data,function(tbl)
|
||||
Store.data = tbl
|
||||
end)
|
||||
|
||||
--- Registers a new store location
|
||||
-- @tparam location string a unique location string that will hold the data
|
||||
-- @tparam type string see Store.types
|
||||
-- @tparam update_callback function the function which will be called with the new value that is set
|
||||
function Store.register_location(location,store_type,update_callback)
|
||||
if Store.locations[location] then
|
||||
store_type = Store.locations[location]
|
||||
store_type = type(store_type) == 'number' and Store.types(store_type) or store_type
|
||||
return error('The location is already registed: '..location..' and is type: '..store_type,2)
|
||||
end
|
||||
if not Store.type[type] then
|
||||
return error('Attempted to set invlid type: '..type..' for location: '..location,2)
|
||||
end
|
||||
store_type = type(store_type) == 'string' and Store.types(store_type) or store_type
|
||||
Store.locations[location] = {
|
||||
location=location,
|
||||
type=store_type,
|
||||
update_callback=update_callback
|
||||
}
|
||||
if store_type ~= Store.types['local'] and store_type ~= Store.types.global then
|
||||
Store.data[location] = {}
|
||||
end
|
||||
end
|
||||
|
||||
--- Sets a new value for a location, will trigger the update callback
|
||||
function Store.set_location(location,sub_location,value,...)
|
||||
if not Store.locations[location] then
|
||||
return error('Invalid store location: '..location,2)
|
||||
end
|
||||
location = Store.locations[location]
|
||||
if location.type == Store.types.game then
|
||||
Store.data[location.location] = sub_location
|
||||
elseif location.type ~= Store.types['local'] and location.type ~= Store.types.global then
|
||||
Store.data[location.location][sub_location] = value
|
||||
end
|
||||
location.update_callback(sub_location,value,...)
|
||||
end
|
||||
|
||||
--- Gets the value for a location
|
||||
function Store.get_location(location,sub_location)
|
||||
if not Store.locations[location] then
|
||||
return error('Invalid store location: '..location,2)
|
||||
end
|
||||
location = Store.locations[location]
|
||||
if location.type == Store.types.game then
|
||||
return Store.data[location.location]
|
||||
elseif location.type ~= Store.types['local'] and location.type ~= Store.types.global then
|
||||
return Store.data[location.location][sub_location]
|
||||
end
|
||||
end
|
||||
|
||||
return Store
|
||||
Reference in New Issue
Block a user