Editing Module:Test
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
− | + | ENV = "wiki" -- "dev" or "wiki" | |
− | + | INFO = true | |
− | + | --------------- | |
− | + | -- load data -- | |
− | + | --------------- | |
− | + | data = {} | |
− | |||
− | |||
− | if mw | + | if ENV == "dev" then |
+ | data["Biomes"] = loadfile("./data/BiomeDefs.lua")() | ||
+ | data["Races"] = loadfile("./data/ThingDefs_Races.lua")() | ||
+ | elseif ENV == "wiki" then | ||
+ | data["Biomes"] = mw.loadData('Module:Test/data/biomes') | ||
+ | data["Races"] = mw.loadData('Module:Test/data/races') | ||
+ | end | ||
− | + | ------------------ | |
− | + | -- virtual keys -- | |
+ | ------------------ | ||
− | + | -- this could be implemented with metatable events | |
− | + | -- they get added in get(id_pair) | |
− | |||
− | local | + | local virtual_keys = { |
− | + | ["Races"] = { | |
+ | ["lives_in"] = function (race, biomes) | ||
+ | local list = {} | ||
+ | for biome_key, biome in pairs(biomes) do | ||
+ | for _,animal in ipairs(biome.wildAnimals) do | ||
+ | if race.defName == animal then | ||
+ | table.insert(list, biome_key) | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | return list | ||
+ | end | ||
+ | } | ||
+ | } | ||
− | + | -------------------- | |
− | + | -- load libraries -- | |
− | + | -------------------- | |
− | + | mw = mw or {} | |
+ | log_entries = {} | ||
− | + | if ENV == "dev" then | |
+ | inspect = require './lib/inspect' | ||
+ | util = require("./lib/util") | ||
+ | search = require("./lib/search") | ||
− | + | function pinspect(tbl, title) | |
+ | if INFO then | ||
+ | util.hl(title) | ||
+ | print(inspect(tbl)) | ||
+ | end | ||
+ | end | ||
− | + | mw.logObject = function(arg) | |
− | table.insert( | + | table.insert(log_entries, inspect(arg)) |
end | end | ||
− | + | mw.dumpObject = function(arg) | |
− | + | return inspect(arg) | |
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | function | + | log = function(arg) |
− | + | table.insert(log_entries, arg) | |
− | |||
end | end | ||
+ | end | ||
− | + | if ENV == "wiki" then | |
− | + | INFO = false | |
− | + | log = mw.log | |
− | + | util = require("Module:Test/lib/util") | |
− | + | search = require("Module:Test/lib/search") | |
− | + | function pinspect() end | |
+ | end | ||
− | + | ------------- | |
− | + | -- private -- | |
− | + | ------------- | |
− | |||
− | + | local function vardefine(name, value) | |
+ | local f_name = "vardefine" | ||
+ | assert(var_name, string.format("bad argument #1 to '%s' (argument missing, name of variable to define)", f_name)) | ||
+ | assert(var_name == "string", string.format("bad argument #1 to '%s' (string expected, got %s)", f_name, type(var_name))) | ||
+ | assert(var_value, string.format("bad argument #2 to '%s' (argument missing, value to assign to variable)", f_name)) | ||
+ | assert(var_value == "string" or var_value == "number", string.format("bad argument #2 to '%s' (string or number expected, got %s)", f_name, type(var_value))) | ||
+ | frame:callParserFunction('#vardefine', var_name, var_value) | ||
end | end | ||
− | |||
− | |||
− | |||
− | function | + | local function search_parent_def_table(key, def_table) |
− | + | local ParentName = getParentName(def_table) | |
− | + | if not ParentName then return nil end | |
− | + | local parentdef_table = search_table_recursive(ParentName, data) | |
− | + | if not parentdef_table then return nil end | |
− | |||
− | |||
− | end | ||
− | + | local found = search_table_recursive(key, parentdef_table) | |
− | if | + | if found then return found |
− | + | else | |
− | + | found = search_parent_def_table(key, parentdef_table) | |
− | if | + | if found then return found end |
− | |||
− | |||
end | end | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | + | local function merge_def(base_def_table, def_category, ignore_keys) | |
− | |||
− | |||
− | |||
− | |||
− | + | local ancestors = {} | |
− | + | local parent_name = base_def_table["ParentName"] | |
+ | local parent_table = data[def_category][parent_name] | ||
− | + | while parent_name do | |
− | + | table.insert(ancestors, parent_name) | |
+ | parent_name = parent_table["ParentName"] | ||
+ | parent_table = data[def_category][parent_name] | ||
end | end | ||
− | + | local inheritance_chain = util.shallowcopy(util.reverse_numeric_table(ancestors)) | |
− | + | table.insert(inheritance_chain, base_def_table.defName) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | for | + | local merged = {} |
− | + | for i,v in ipairs(inheritance_chain) do | |
− | + | util.overwrite_first_table_with_second(merged, data[def_category][inheritance_chain[i]], ignore_keys) | |
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | return merged | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | + | local function get(id_pair) | |
− | + | local res = search.conductor(id_pair, data) | |
− | + | if not res then | |
− | + | log("Def not found") | |
− | + | return nil | |
− | |||
− | |||
end | end | ||
− | + | local base_def_table = res.parent.table | |
− | + | local def = merge_def(base_def_table, res.ancestors[2], {"ParentName", "Abstract"}) | |
− | + | -- add virtual keys | |
− | for k, | + | if virtual_keys[res.ancestors[2]] then |
− | + | def._virtual = {} | |
− | + | for k,func in pairs(virtual_keys[res.ancestors[2]]) do | |
− | + | def._virtual[k] = func(def, data.Biomes) | |
− | |||
− | |||
− | |||
end | end | ||
end | end | ||
− | |||
− | + | return def | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | ------------ | |
+ | -- public -- | ||
+ | ------------ | ||
− | + | local p = {} | |
− | if not frame.args[ | + | function p.query(frame) |
− | + | if not frame.args[1] then | |
+ | log("missing argument #1 (needed to identify a Def, defName or label can be used)") | ||
return nil | return nil | ||
end | end | ||
− | local def = | + | local def = get({"defName", frame.args[1]}) or get({"label", frame.args[1]}) |
+ | local pruned = def | ||
− | if not | + | for i,arg in ipairs(frame.args) do |
− | + | if i > 1 then | |
− | + | pruned = search.conductor(arg, pruned) | |
+ | if not pruned then | ||
+ | log(string.format("bad argument #%i ('%s' not found in '%s')", i, frame.args[i], frame.args[i-1])) | ||
+ | return nil | ||
+ | else | ||
+ | pruned = pruned.value | ||
+ | end | ||
+ | end | ||
end | end | ||
− | if | + | if type(pruned) == "table" then mw.logObject(pruned) end |
− | + | return pruned | |
− | + | end | |
− | |||
− | + | function p.logObject(frame) | |
+ | mw.logObject(get(frame)) | ||
+ | end | ||
− | + | function p.dumpObject(frame) | |
− | + | return mw.dumpObject(get(frame)) | |
+ | end | ||
− | + | ------------------------------------------------------------------- | |
− | + | -- simulate MediaWiki/Scribunto module invocation using 'frame' -- | |
− | + | ------------------------------------------------------------------- | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | local simframe = { ["args"] = {} } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | simframe.args[1] = "Fox_Fennec" | |
+ | simframe.args[2] = "description" | ||
+ | --~ simframe.args[3] = "12" | ||
− | + | frame = frame or simframe | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | -- frame.args are always strings on MediaWiki so convert them back to numbers | |
− | + | for k,arg in pairs(frame.args) do | |
+ | frame.args[k] = tonumber(arg) or arg | ||
end | end | ||
− | ------------------------------------ | + | ---------------------------------------- |
− | -- simulate | + | -- simulate wiki log while developing -- |
− | ------------------------------------ | + | ---------------------------------------- |
− | + | local clock = string.format("os.clock(): %i ms", os.clock() * 1000) | |
− | + | log("--" .. string.rep("-", #clock) .. "--") | |
− | + | log("- " .. clock .. " -") | |
− | -- | + | log("--" .. string.rep("-", #clock) .. "--") |
− | -- | ||
− | |||
− | |||
− | if | + | if ENV == "dev" then |
− | + | util.hl("log") | |
− | for _,v in ipairs( | + | for _,v in ipairs(log_entries) do |
print(v) | print(v) | ||
end | end | ||
+ | |||
end | end | ||
− | + | return p | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |