Better docs for store

This commit is contained in:
Cooldude2606
2019-09-22 17:35:32 +01:00
parent ce88e0a296
commit 4f4d8d9bd5
96 changed files with 261 additions and 134 deletions

View File

@@ -14,7 +14,7 @@ local Gui = require 'expcore.gui.core'
@tparam ?string|Concepts.LocalisedString caption the message that is shown on the button
@tparam ?string|Concepts.LocalisedString tooltip the tooltip that shows when a player hovers over the button
@tparam SpritePath sprite upto three sprites in the order: default, hovered, clicked
@tparam Concepts.SpritePath sprite upto three sprites in the order: default, hovered, clicked
@usage-- Making a basic button
local basic_button =

View File

@@ -40,6 +40,11 @@ Store.register(team_scores,true,function(value,key)
game.print('Team '..key..' now has a score of '..value)
end)
-- If you want multiple handlers on one store location then you can register to the raw event
Event.add(Store.events.on_value_changed,function(event)
game.print('Store '..event.location..'/'..event.key..' was updated to: '..event.value)
end)
]]
local Global = require 'utils.global' --- @dep utils.global
@@ -67,11 +72,19 @@ local function error_not_table(value)
end
end
--- Registers a new location with an update callback which is triggered when the value updates
-- @tparam[opt] string location string a unique that points to the data, string used rather than token to allow migration
-- @tparam[opt=false] boolean synced when true will output changes to a file so it can be synced
-- @tparam[opt] function callback when given the callback will be automatically registered to the update of the value
-- @treturn string the location that is being used
--[[-- Registers a new location with an update callback which is triggered when the value updates
@tparam[opt] string location string a unique that points to the data, string used rather than token to allow migration
@tparam[opt=false] boolean synced when true will output changes to a file so it can be synced
@tparam[opt] function callback when given the callback will be automatically registered to the update of the value
@treturn string the location that is being used
@usage-- Registering a new store location
local store_id = Store.register()
@usage-- Registering a new store location, with custom update callback
local store_id = Store.uid_location()
Store.register(store_id,function(value,key)
game.print('Store '..store_id..'/'..key..' was updated to: '..value)
end)
]]
function Store.register(location,synced,callback)
if _LIFECYCLE ~= _STAGE.control then
return error('Can only be called during the control stage', 2)
@@ -99,10 +112,14 @@ function Store.register(location,synced,callback)
return location
end
--- Gets the value stored at a location, this location must be registered
-- @tparam string location the location to get the data from
-- @tparam[opt] string key the key location if used
-- @treturn any the data which was stored at the location
--[[-- Gets the value stored at a location, this location must be registered
@tparam string location the location to get the data from
@tparam[opt] string key the key location if used
@treturn any the data which was stored at the location
@usage-- Getting the data at a store location
local data = Store.get(store_id_no_keys)
local data = Store.get(store_id_with_keys,'key_one')
]]
function Store.get(location,key)
if not Store.registered[location] then
return error('Location is not registered', 2)
@@ -117,13 +134,17 @@ function Store.get(location,key)
return data
end
--- Sets the value at a location, this location must be registered
-- @tparam string location the location to set the data to
-- @tparam[opt] string key the key location if used
-- @tparam any value the new value to set at the location, value may be reverted if there is a watch callback, cant be nil
-- @tparam[opt=false] boolean from_sync set this true to avoid an output to the sync file
-- @tparam[opt=false] boolean from_internal set this true to add one to the error stack offset
-- @treturn boolean true if it was successful
--[[-- Sets the value at a location, this location must be registered
@tparam string location the location to set the data to
@tparam[opt] string key the key location if used
@tparam any value the new value to set at the location, value may be reverted if there is a watch callback, cant be nil
@tparam[opt=false] boolean from_sync set this true to avoid an output to the sync file
@tparam[opt=false] boolean from_internal set this true to add one to the error stack offset
@treturn boolean true if it was successful
@usage-- Setting the data at a store location
Store.set(store_id_no_keys,'Hello, World!')
Store.set(store_id_with_keys,'key_one','Hello, World!')
]]
function Store.set(location,key,value,from_sync,from_internal)
if not Store.registered[location] then
return error('Location is not registered', from_internal and 3 or 2)
@@ -157,10 +178,25 @@ function Store.set(location,key,value,from_sync,from_internal)
return true
end
--- Allows for updating a value based on the current value; only valid way to change tables in a store
-- @tparam string location the location to set the data to
-- @tparam[opt] string key the key location if required
-- @tparam[opt] function update_callback the function called to update the value stored, rtn value to set new value
--[[-- Allows for updating a value based on the current value; only valid way to change tables in a store
@tparam string location the location to set the data to
@tparam[opt] string key the key location if required
@tparam[opt] function update_callback the function called to update the value stored, rtn value to set new value
@usage-- Updating a value stored at a location
Store.update(store_id_no_keys,function(value)
return value + 1
end)
Store.update(store_id_with_keys,'key_one',function(value)
return value + 1
end)
@usage-- Updating a table stored at a location
Store.update(store_id_no_keys,function(value)
value.ctn = value.ctn + 1
end)
Store.update(store_id_with_keys,'key_one',function(value)
value.ctn = value.ctn + 1
end)
]]
function Store.update(location,key,update_callback,...)
local value = Store.get(location,key)
@@ -194,9 +230,18 @@ function Store.update(location,key,update_callback,...)
end
--- Allows for updating all values at a location based on the current value; only valid way to change tables in a store
-- @tparam string location the location to set the data to
-- @tparam[opt] function update_callback the function called to update the value stored
--[[-- Allows for updating all values at a location based on the current value; only valid way to change tables in a store
@tparam string location the location to set the data to
@tparam[opt] function update_callback the function called to update the value stored
@usage-- Updating all values at a location
Store.update(store_id_with_keys,function(value)
return value + 1
end)
@usage-- Updating all tables at a location
Store.update(store_id_with_keys,function(value)
value.ctn = value.ctn + 1
end)
]]
function Store.update_all(location,update_callback,...)
local data = Store.get(location)
@@ -223,11 +268,15 @@ function Store.update_all(location,update_callback,...)
end
--- Sets the value at a location to nil, this location must be registered
-- @tparam string location the location to set the data to
-- @tparam[opt] string key the key location if used
-- @tparam[opt=false] boolean from_sync set this true to avoid an output to the sync file
-- @treturn boolean true if it was successful
--[[-- Sets the value at a location to nil, this location must be registered
@tparam string location the location to set the data to
@tparam[opt] string key the key location if used
@tparam[opt=false] boolean from_sync set this true to avoid an output to the sync file
@treturn boolean true if it was successful
@usage-- Clear the data at a location
Store.clear(store_id_no_keys)
Store.clear(store_id_with_keys,'key_one')
]]
function Store.clear(location,key,from_sync)
if not Store.callbacks[location] then
return error('Location is not registered', 2)
@@ -252,24 +301,33 @@ function Store.clear(location,key,from_sync)
return true
end
--- Gets all non nil keys at a location, keys can be added and removed during runtime
-- this is similar to Store.get but will always return a table even if it is empty
-- @tparam string location the location to get the keys of
-- @treturn table a table containing all the keys names
--[[-- Gets all non nil keys at a location, keys can be added and removed during runtime
this is similar to Store.get but will always return a table even if it is empty
@tparam string location the location to get the keys of
@treturn table a table containing all the keys names
@usage-- Get all keys at a store location
local keys = Store.get_keys(store_id_with_keys)
]]
function Store.get_keys(location)
local data = Store.get(location)
return type(data) == 'table' and table_keys(data) or {}
end
--- Check for if a location is registered
-- @tparam string location the location to test for
-- @treturn boolean true if registered
--[[-- Check for if a location is registered
@tparam string location the location to test for
@treturn boolean true if registered
@usage-- Check that a store is registered
local registerd = Store.is_registered(store_id)
]]
function Store.is_registered(location)
return Store.registered[location]
end
--- Returns a unique name that can be used for a store
-- @treturn string a unique name
--[[-- Returns a unique name that can be used for a store
@treturn string a unique name
@usage-- Get a new unique store id
local store_id = Store.uid_location()
]]
function Store.uid_location()
return tostring(Token.uid())
end

View File

@@ -102,13 +102,16 @@ end
--[[-- The base element to be used with the toolbar, others can be used but this is recomented
@element toolbar-button
@tparam string permission_alias the alias used with Toolbar.allowed
@usage-- Adding a basic button to the toolbar, note no need to call Toolbar.add_button_concept
Gui.new_concept('toolbar-button')
:set_caption('Click Me')
:on_click(function(event)
event.player.print('You Clicked Me!!')
end)
]]
Toolbar.button =
Gui.new_concept('button')
@@ -176,16 +179,20 @@ end
--[[-- The base toolbar frame, others can be used but this is recomented
@element toolbar-frame
@param on_update fired when the frame is to have its content updated
@tparam boolean open_by_default weather the frame should be open when a player first joins
@tparam boolean use_container true by default and will place a container inside the frame for content
@tparam string direction the direction that the items in the frame are added
@usage-- Adding a basic player list
local player_list =
Gui.new_concept('toolbar-frame')
:set_permission_alias('player_list')
:set_caption('Player List')
:toggle_with_click()
:define_draw(function(properties,parent,element)
local list_area =
element.add{
@@ -206,6 +213,7 @@ Gui.new_concept('toolbar-frame')
}
end
end)
:on_update(function(event)
local list_area = event.element.scroll
list_area.clear()
@@ -217,6 +225,7 @@ end)
}
end
end)
]]
Toolbar.frame =
Gui.new_concept('toolbar-button')