Anonymous
×
Create a new article
Write your page title here:
We currently have 97 articles on The Dream Shrine. Type your article name above or click on one of the titles below and start writing!



The Dream Shrine

Documentation for this module may be created at Module:UtilsVar/doc

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