Модуль:Entity Lookup: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показаны 4 промежуточные версии этого же участника) | |||
| Строка 3: | Строка 3: | ||
local function safeLoad(page) | local function safeLoad(page) | ||
local moduleName = JsonPaths.get(page) | |||
local ok, data = pcall(mw.loadData, moduleName) | |||
return ok and (data or {}) or {} | |||
end | end | ||
p.entities = safeLoad("entity prototypes.json") | p.entities = safeLoad("entity prototypes.json") | ||
p.name_overrides = safeLoad("entity name overrides.json") | p.name_overrides = safeLoad("entity name overrides.json") | ||
local function buildName(entity, useSuffix) | |||
local name = entity.name or "" | |||
local label = mw.ustring.lower(entity.label or "") | |||
local suffix = mw.ustring.lower(entity.suffix or "") | |||
if useSuffix and suffix ~= "" then | |||
local parts = {} | |||
for part in tostring(suffix):gmatch("[^,;]+") do | |||
part = part | |||
if part ~= "" then | |||
parts[#parts + 1] = part | |||
end | |||
end | |||
suffix = table.concat(parts, ", ") | |||
end | |||
if label ~= "" and suffix ~= "" and label == suffix then | |||
suffix = "" | |||
end | |||
if label == "" and suffix == "" then | |||
return name | |||
elseif label == "" then | |||
return name .. " (" .. suffix .. ")" | |||
elseif suffix == "" then | |||
return name .. " (" .. label .. ")" | |||
else | |||
return name .. " (" .. label .. ") (" .. suffix .. ")" | |||
end | |||
end | |||
| Строка 18: | Строка 46: | ||
function p.getname(frame, entid) -- {{#invoke:Entity Lookup|getname|ProtoID}} | function p.getname(frame, entid) -- {{#invoke:Entity Lookup|getname|ProtoID}} | ||
local id = frame.args and frame.args[1] or entid | local id = frame.args and frame.args[1] or entid | ||
if not id then return "Не найдено" end | if not id then | ||
return "Не найдено" | |||
end | |||
local override = p.name_overrides[id] | local override = p.name_overrides[id] | ||
if override then | if override then | ||
return override | return lower(override) | ||
end | end | ||
local entity = p.entities[id] | local entity = p.entities[id] | ||
if entity then | if not entity then | ||
return "Не найдено" | |||
end | end | ||
local useSuffix = frame.args and frame.args.suffix and frame.args.suffix ~= "" | |||
return buildName(entity, useSuffix) | |||
end | end | ||
| Строка 50: | Строка 77: | ||
for id, entity in pairs(p.entities) do | for id, entity in pairs(p.entities) do | ||
if entity.name == name then | if entity.name == name or buildName(entity) == name then | ||
return id | return id | ||
end | end | ||
| Строка 57: | Строка 84: | ||
return "Не найдено" | return "Не найдено" | ||
end | end | ||
-- p.getname{args={"FreedomImplant"}} | -- p.getname{args={"FreedomImplant"}} | ||
function p.getdesc(frame, entid) -- {{#invoke:Entity Lookup|getdesc|ProtoID}} | function p.getdesc(frame, entid) -- {{#invoke:Entity Lookup|getdesc|ProtoID}} | ||
local out = "" | |||
local id = "" | |||
if frame.args[1] ~= nil then id = frame.args[1] | |||
else id = entid end | |||
local entity = p.entities[id] | |||
if entity ~= nil then | |||
out = entity.desc | |||
else | |||
out = "Не найдено" | |||
end | |||
return out | return out | ||
end | end | ||