Module:UtilsVar: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
(Created page with "local p = {} local binser = require("binser") function p.set(name, val) local var = binser.serialize(val) mw.getCurrentFrame():callParserFunction("#vardefine", name, var)...")
 
(Future-proofing in case the extension is no longer available)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


local binser = require("binser")
VariablesLua = mw.ext.VariablesLua


function p.set(name, val)
function p.set(name, val)
local var = binser.serialize(val)
-- Future-proofing in case the extension is no longer available
mw.getCurrentFrame():callParserFunction("#vardefine", name, var)
if not VariablesLua then
return nil
end
 
local json = mw.text.jsonEncode(val)
VariablesLua.vardefine(name, json)
end
end


function p.get(name)
function p.get(name)
local var = mw.getCurrentFrame():callParserFunction("#var", name)
if not VariablesLua then
local val = binser.deserialize(var)[1]
return nil
end
 
local json = VariablesLua.var(name)
if json == "" then
return nil
end
local val = mw.text.jsonDecode(json)
return val
return val
end
end


function p.getFinal(name)
function p.add(name, val)
local var = mw.getCurrentFrame():callParserFunction("#var_final", name)
if not VariablesLua then
local val = binser.deserialize(var)[1]
return nil
return val
end
 
local array = p.get(name) or {}
table.insert(array, val)
p.set(name, array)
return array
end
end


function p.counter(name)
function p.counter(name, limit)
if not VariablesLua then
return {
increment = function() return 0 end,
value = function() return 0 end,
}
end
 
name = name or ""
name = name or ""
return {
return {
Line 27: Line 51:
val = val + 1
val = val + 1
p.set(name, val)
p.set(name, val)
return val
end,
end,
value = function()
value = function()
return p.get(name) or 0
return p.get(name) or 0
end,
end,
isLast = function()
limit = limit
return p.getFinal(name) == p.get(name)
end
}
}
end
end


return p
return p

Latest revision as of 13:06, 17 October 2022

This module allows for data to be shared between invocations of a module on a given page, using Extension:VariablesLua. Use cases for this include counters and previewing Cargo storage.

Not to be confused with Module:UtilsArg.


local p = {}

VariablesLua = mw.ext.VariablesLua

function p.set(name, val)
	-- Future-proofing in case the extension is no longer available
	if not VariablesLua then
		return nil
	end

	local json = mw.text.jsonEncode(val)
	VariablesLua.vardefine(name, json)
end

function p.get(name)
	if not VariablesLua then
		return nil
	end

	local json = VariablesLua.var(name)
	if json == "" then
		return nil
	end
	local val = mw.text.jsonDecode(json)
	return val
end

function p.add(name, val)
	if not VariablesLua then
		return nil
	end

	local array = p.get(name) or {}
	table.insert(array, val)
	p.set(name, array)
	return array
end

function p.counter(name, limit)
	if not VariablesLua then
		return {
			increment = function() return 0 end,
			value = function() return 0 end,
		}
	end

	name = name or ""
	return {
		increment = function()
			local val = p.get(name) or 0
			val = val + 1
			p.set(name, val)
			return val
		end,
		value = function()
			return p.get(name) or 0
		end,
		limit = limit
	}
end

return p