Halo Esports Wiki
Halo Esports Wiki
[checked revision][checked revision]
(Syncing content across wikis, if something seems broken as a result let me know)
 
Donut (talk | contribs)
m (Syncing content across wikis)
Line 1: Line 1:
 
local util_args = require ('Module:ArgsUtil')
 
local util_args = require ('Module:ArgsUtil')
  +
local binser = require('binser')
   
 
local p = {}
 
local p = {}
   
function p.getVar(var, f)
+
function p.getVar(var)
 
local val = mw.getCurrentFrame():callParserFunction('#var', var)
if not f then
 
f = mw.getCurrentFrame()
 
end
 
local val = f:callParserFunction('#var', var)
 
 
if val == '' then
 
if val == '' then
 
return nil
 
return nil
 
end
 
end
return util_args.norm(val)
+
return val
 
end
 
end
   
function p.setVar(var, val, f)
+
function p.getBool(var)
  +
local val = p.getVar(var)
if not f then
 
  +
return util_args.castAsBool(val)
f = mw.getCurrentFrame()
 
  +
end
  +
  +
function p.setBool(var, val)
  +
local bool = util_args.castAsBool(val)
  +
if bool then
  +
p.setVar(var, 'Yes')
  +
else
  +
p.setVar(var, 'No')
 
end
 
end
 
return bool
f:callParserFunction('#vardefine:' .. var, val)
 
return
 
 
end
 
end
   
function p.setVarOnlyIf(var, val, f)
+
function p.setVar(var, val)
 
mw.getCurrentFrame():callParserFunction('#vardefine:' .. var, val)
 
return val
  +
end
  +
  +
function p.setVarOnlyIf(var, val)
 
if not val then return end
 
if not val then return end
p.setVar(var, val, f)
+
p.setVar(var, val)
 
end
 
end
   
function p.resetGlobalIndex(name, f, val)
+
function p.getGlobalIndex(name)
 
local val = p.getVar('luaGlobalIndex' .. name)
if not f then
 
  +
return tonumber(val or 0) or 0
f = mw.getCurrentFrame()
 
  +
end
  +
  +
function p.setGlobalIndex(name)
  +
local n = p.getGlobalIndex(name) + 1
 
p.setVar('luaGlobalIndex' .. name, n)
 
return n
  +
end
  +
  +
function p.resetGlobalIndex(name, val)
  +
val = val or 0
  +
p.setVar('luaGlobalIndex' .. name, val)
  +
return val
  +
end
  +
  +
function p.appendToList(name, val, sep)
  +
-- this is just for helping to debug and shouldn't actually be used ever probably
  +
sep = sep or ', '
  +
local old_val = p.getVar(name) or ''
  +
p.setVar(name, old_val .. sep .. val)
  +
end
  +
  +
function p.log(val)
  +
p.appendToList('log', tostring(val))
  +
end
  +
  +
function p.logObject(val)
  +
local tbl = {}
  +
for k, v in pairs(val) do
  +
tbl[#tbl+1] = k .. ': ' .. v
 
end
 
end
  +
p.appendToList('log', table.concat(tbl,', '))
f:callParserFunction('#vardefine:luaGlobalIndex' .. name, val or 0)
 
return val or 0
 
 
end
 
end
   
function p.setGlobalIndex(name, f)
+
function p.clearAll(tbl)
  +
for _, v in ipairs(tbl) do
if not f then
 
  +
p.setVar(v, '')
f = mw.getCurrentFrame()
 
 
end
 
end
local n = (tonumber(f:callParserFunction('#var','luaGlobalIndex' .. name) or 0) or 0) + 1
 
f:callParserFunction('#vardefine:luaGlobalIndex' .. name, n)
 
return n
 
 
end
 
end
   
function p.getGlobalIndex(name, f)
+
function p.setTypedVar(var, val)
  +
local val_to_store = binser.serialize(val)
if not f then
 
  +
p.setVar(var, val_to_store)
f = mw.getCurrentFrame()
 
  +
end
  +
  +
function p.getTypedVar(var)
  +
local val = p.getVar(var)
 
if not val then
  +
return nil
 
end
  +
return binser.deserialize(val)[1]
  +
end
  +
  +
function p.setObject(var, val)
  +
local val_to_store = binser.serialize(val)
  +
p.setVar(var, val_to_store)
  +
end
  +
  +
function p.getObject(var)
  +
local val = p.getVar(var)
 
if not val then
  +
return nil
 
end
 
end
  +
return binser.deserialize(val)[1]
return tonumber(f:callParserFunction('#var','luaGlobalIndex' .. name) or 0) or 0
 
 
end
 
end
   

Revision as of 04:36, 16 April 2019

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

local util_args = require ('Module:ArgsUtil')
local binser = require('binser')

local p = {}

function p.getVar(var)
	local val = mw.getCurrentFrame():callParserFunction('#var', var)
	if val == '' then
		return nil
	end
	return val
end

function p.getBool(var)
	local val = p.getVar(var)
	return util_args.castAsBool(val)
end

function p.setBool(var, val)
	local bool = util_args.castAsBool(val)
	if bool then
		p.setVar(var, 'Yes')
	else
		p.setVar(var, 'No')
	end
	return bool
end

function p.setVar(var, val)
	mw.getCurrentFrame():callParserFunction('#vardefine:' .. var, val)
	return val
end

function p.setVarOnlyIf(var, val)
	if not val then return end
	p.setVar(var, val)
end

function p.getGlobalIndex(name)
	local val = p.getVar('luaGlobalIndex' .. name)
	return tonumber(val or 0) or 0
end

function p.setGlobalIndex(name)
	local n = p.getGlobalIndex(name) + 1
	p.setVar('luaGlobalIndex' .. name, n)
	return n
end

function p.resetGlobalIndex(name, val)
	val = val or 0
	p.setVar('luaGlobalIndex' .. name, val)
	return val
end

function p.appendToList(name, val, sep)
	-- this is just for helping to debug and shouldn't actually be used ever probably
	sep = sep or ', '
	local old_val = p.getVar(name) or ''
	p.setVar(name, old_val .. sep .. val)
end

function p.log(val)
	p.appendToList('log', tostring(val))
end

function p.logObject(val)
	local tbl = {}
	for k, v in pairs(val) do
		tbl[#tbl+1] = k .. ': ' .. v
	end
	p.appendToList('log', table.concat(tbl,', '))
end

function p.clearAll(tbl)
	for _, v in ipairs(tbl) do
		p.setVar(v, '')
	end
end

function p.setTypedVar(var, val)
	local val_to_store = binser.serialize(val)
	p.setVar(var, val_to_store)
end

function p.getTypedVar(var)
	local val = p.getVar(var)
	if not val then
		return nil
	end
	return binser.deserialize(val)[1]
end

function p.setObject(var, val)
	local val_to_store = binser.serialize(val)
	p.setVar(var, val_to_store)
end

function p.getObject(var)
	local val = p.getVar(var)
	if not val then
		return nil
	end
	return binser.deserialize(val)[1]
end

return p