Module:Protection banner: Difference between revisions
save progress in simplifying the attempt order algorithm
(more efficient p.matchNamespace logic) |
(save progress in simplifying the attempt order algorithm) |
||
Line 59: | Line 59: | ||
} | } | ||
local | local behaviors = { | ||
vandalism = 'namespaceFirst', | vandalism = 'namespaceFirst', | ||
dispute = 'reasonFirst', | dispute = 'reasonFirst', | ||
Line 83: | Line 83: | ||
-- Define often-used functions as local variables. | -- Define often-used functions as local variables. | ||
local tconcat = table.concat | local tconcat = table.concat | ||
local floor = math.floor | |||
local p = {} | local p = {} | ||
Line 119: | Line 120: | ||
expiry = 'temp' | expiry = 'temp' | ||
end | end | ||
local properties = {expiry, namespace, protLevel, protType, reason} | |||
local behavior = behaviors[reason] | |||
if behavior == 'namespaceFirst' then | |||
tinsert(properties, table.remove(properties, 2)) -- move namespace to the end | |||
elseif behavior ~= 'reasonFirst' and reason ~= 'all' then | |||
local | |||
local | |||
if | |||
elseif | |||
error(reason .. ' is not a valid reason') | error(reason .. ' is not a valid reason') | ||
end | end | ||
local activeProperties = {} | |||
for i, property in ipairs(properties) do | |||
if property ~= 'all' then | |||
activeProperties[#activeProperties + 1] = property | |||
end | |||
end | |||
local noActiveProperties = #activeProperties | |||
-- Try successively generic matches until we run out of key combinations | -- Try successively generic matches until we run out of key combinations | ||
for i, | for i = 1, 2^noActiveProperties do | ||
local key = {} | local key = {} | ||
for | for pos = 1, 5 do | ||
if | if pos > noActiveProperties then | ||
key[ | key[pos] = 'all' | ||
else | else | ||
key[j] = 'all' | local quotient = i / 2 ^ (pos - 1) | ||
quotient = floor(quotient) | |||
if quotient % 2 == 1 then | |||
key[pos] = protectionProperties[j] | |||
else -- quotient % 2 == 0 | |||
key[pos] = 'all' | |||
end | |||
end | end | ||
end | end | ||
Line 199: | Line 161: | ||
end | end | ||
error('No category match found; please define the category for key "all-all-all-all-all"') | error('No category match found; please define the category for key "all-all-all-all-all"') | ||
end | end | ||
return p | return p |