This is the documentation page for Module:StatObject
Usage
local StatObject = require('Module:StatObject')
StatObject.default = {
-- Getter functions:
-- Arrays of default values and format strings
key = { 'Default Value', '%.2f format' },
-- Can generate values/formatting with functions
-- 'val, ...' is the return from map[1]
key = { function(obj) end, function(self, val, ...) end },
-- If format is a table the return values will be passed to each function/format string
-- in the same order they are returned (nil is a pass-through)
key = {
function(obj) return a, b, c, d end,
{ function(self, val) return val end, '%s', nil, '%d', sep = '' }
},
-- Can omit second entry, can omit table
key = 'Default Value',
-- nil means default get (same as omitting)
key = nil,
-- Add additional key-value pairs below to define raw getters and format getter functions for data
-- To use the getters use StatObject.statRead(dataEntry, key) and StatObject.statFormat(dataEntry, key)
}
local ModData = mw.loadData('Module:Mods/data') -- Importing sample database
StatObject.statRead(ModData['Serration'], 'Name') -- Example of getting the raw Name value of Serration mod entry
StatObject.statFormat(ModData['Serration'], 'Name') -- Example of getting the formatted Name value of Serration mod entry
-- Can also use StatObject.default to define computed/derived fields from raw data
Documentation
Package items
StatObject.default(table)- Map of getter functions to specific attributes of an object. Includes string formatting info for displaying stats to readers.
StatObject.cacheIn(obj, key, val)(function)- Memoization of calculated value; value is passed through.
- Parameters:
- Returns: Value that is memorized (...)
StatObject.ucachein(obj, key, val)(function)- Memoization of calculated value; value assumed to be a table and will return unpacked.
- Parameters:
- Returns: Table value that is memorized but unpacked (...)
StatObject.pucacheIn(obj, key, val, obj, key, val, obj, k)(function)- Memoization of calculated values; values will be memorized as a table and are passed through as is.
- Parameters:
- Returns:
StatObject.statRead(obj, k)(function)- Main getter function to access any raw/computed attribute/column/key of an object entry. See default table in M:Weapons for examples.
- Parameters:
- Returns: Return value from the appropriate attribute-getting function (...)
StatObject.statFormat(obj, k, ...)(function)- Main getter function to access any formatted attribute/column/key of a Object entry. See default table in Module:Weapons for examples.
- Parameters:
- Returns: Format function (function)
StatObject.meta(table)- Contains metafunctions to return functions with certain parameters for getter maps[1].
StatObject.meta.gets(k)(function)- Returns a getter function that returns the value of a particular key from an object table entry in the data.
- Parameter:
kKey name (string) - Returns: Getter function (function)
StatObject.meta.unpacks(k)(function)- Returns a function that unpacks a table value of a particular key.
- Parameter:
kKey name (string) - Returns: Unpacking function (function)
StatObject.meta.ors(...)(function)- Returns a getter function that looks through multiple keys/columns/attributes until a non-nil value is found. ors('Key1', 'Key2')(obj) == get(obj,'Key1') or get(obj,'Key2')
- Parameter:
...Names of keys stored in default table to search through (string) - Returns: Getter function that returns the first non-nil value out of multiple keys in default table (function)
StatObject.meta.indexes(k, index)(function)- Returns a getter function that gets a single element from a table value or tuple (return statements with multiple values). indexes('Key', 2)(obj) == get(obj, 'Key')[2]
- Parameters:
- Returns: Getter function that returns the specified indexed element from a table or tuple value (function)
StatObject.meta.selects(k, index)(function)- Tuples only (also returns elements after that selected)
- Parameters:
- Returns: Getter function that returns the specified indexed element from a tuple value (function)
StatObject.meta.selects1(k, index)(function)- Tuples only (only returns one)
- Parameters:
- Returns: Getter function that returns the specified indexed element from a tuple value (function)
StatObject.meta.tindexes(k, index)(function)- Tables only
- Parameters:
- Returns: Getter function that returns the specified indexed element from a table value (function)
StatObject.meta.passes(func)(function)- Wrapper function to match definition of maps[2] functions created by makeFormat(). This helps standardizes how all format functions are called by p.statFormat() regardless of whether or not they are created in makeFormat().
- Parameter:
funcFunction to be wrapped (function) - Returns: Returns the contents of the called function (function)
StatObject.meta.percent()(function)- Formats decimal value as a percentage.
- Returns: Formatted value as a percentage rounded to two decimal places (string)
StatObject.meta.percents(s)(function)- More customizable percentage formatting.
- Parameter:
sFormat string (string) - Returns: Returns formatted value as a percentage based on format string (function)
StatObject.getObjects(data, validateFunc, sortFunc)(function)- Returns a subset of data based on a validation function.
- Parameters:
- Returns: Table entries as seen in
/data(table) StatObject.getStruct[... N23](function)- Returns a structure that gets a value from any input table, in the reverse order passed. (i.e. getStruct3(a, b, c)[k] == (c[k] or b[k] or a[k]))
- Parameter:
...Data tables to retrieve data from (table) - Returns: The structure (table)
StatObject.objIter(data, validateFunc)(function)- Iterates through a subset of data based on a validation function. (e.g.
for k,obj in p.objIter(data) do end) - Parameters:
- Returns: Table entries as seen in
/data(table) StatObject.shortLinkList(objs, variant, linkdisplay)(function)- Builds a list of objects, with variants being next to base object name inside parentheses (e. g.
Braton (
MK1,
Prime)). (WIP) - Parameters:
objsTable of objects to list (table)variantA function which takes an object and returns its variant (string), base name (string), and full name (string) (function)linkdisplayA function which takes an object's full name, a display string, and the object, and returns wikitext displaying the object (function)
- Returns: Wikitext of resultant list (string)
- Created with Docbunto
See Also