Модуль:Entity Lookup: различия между версиями

м поддержка суффиксов
Метка: отменено
Нет описания правки
 
(не показано 13 промежуточных версий 2 участников)
Строка 1: Строка 1:
local prototypes = mw.loadData("Module:Entity Lookup/data")
local p = {}
local overrides = mw.loadData("Module:Entity Lookup/names")
local JsonPaths = require('Module:JsonPaths')
 
local function safeLoad(page)
local moduleName = JsonPaths.get(page)
local ok, data = pcall(mw.loadData, moduleName)
return ok and (data or {}) or {}
end
 
p.entities = safeLoad("entity prototypes.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 = ""
 
if useSuffix then
suffix = mw.ustring.lower(entity.suffix or "")
 
if suffix ~= "" then
local parts = {}
for part in tostring(suffix):gmatch("[^,;]+") do
part = mw.text.trim(part)
if part ~= "" then
parts[#parts + 1] = part
end
end
suffix = table.concat(parts, ", ")
end
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


local p = {}
p.entities = prototypes.entities
p.name_overrides = overrides.name_overrides or {}


-- p.getname{args={"FreedomImplant"}}
-- p.getname{args={"FreedomImplant"}}
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


if p.name_overrides[id] then
local override = p.name_overrides[id]
return p.name_overrides[id]
if override then
return lower(override)
end
end


local entity = p.entities[id]
local entity = p.entities[id]
if entity then
if not entity then
local name = entity.name or ""
return "Не найдено"
if entity.suffix and entity.suffix ~= "" then
name = name .. " (" .. entity.suffix .. ")"
end
return name
end
end


return "Не найдено"
local useSuffix = frame.args and frame.args.suffix and frame.args.suffix ~= ""
return buildName(entity, useSuffix)
end
end


-- p.getidbyname{args={"Швабра"}}
-- p.getidbyname{args={"Швабра"}}
Строка 39: Строка 81:


for id, entity in pairs(p.entities) do
for id, entity in pairs(p.entities) do
local actualName = entity.name
if entity.name == name or buildName(entity) == name then
if entity.suffix and entity.suffix ~= "" then
actualName = actualName .. " (" .. entity.suffix .. ")"
end
if actualName == name then
return id
return id
end
end
Строка 51: Строка 89:
end
end


-- p.getdesc{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 id = frame.args and frame.args[1] or entid
local out = ""
local id = ""
if frame.args[1] ~= nil then id = frame.args[1]  
else id = entid end
local entity = p.entities[id]
local entity = p.entities[id]
if entity then
if entity ~= nil then
return entity.desc or ""
out = entity.desc
else
out = "Не найдено"
end
end
return "Не найдено"
return out
end
end


-- p.createtexttooltip{args={"Верхний текст", "FreedomImplant", "Файл:Honks.gif"}}
 
-- p.createtexttooltip{args={"Верхний текст", "FreedomImplant", "Файл:Horns.png"}}
function p.createtexttooltip(frame) -- {{#invoke:Entity Lookup|createtexttooltip|Текст|ProtoID|img=картиночка}}
function p.createtexttooltip(frame) -- {{#invoke:Entity Lookup|createtexttooltip|Текст|ProtoID|img=картиночка}}
local args = frame.args
local out = " "
local label, id, img = args[1], args[2], args.img
local entity = p.entities[frame.args[2]]
local entity = p.entities[id]
local stats = frame:preprocess("{{Особое|".. frame.args[2] .."}}")
local stats = frame:preprocess("{{Особое|" .. id .. "}}")
if entity ~= nil then do
if entity then
if frame.args["img"] ~= nil then do
local displayName = entity.name
out = frame:preprocess("{{Tooltip|".. frame.args[1] .."|".. entity.name .."|".. string.gsub(entity.desc, "\n", "<br>") .."|".. stats .."|img=".. frame.args["img"] .."}}")
if entity.suffix and entity.suffix ~= "" then
end else
displayName = displayName .. " (" .. entity.suffix .. ")"
out = frame:preprocess("{{Tooltip|".. frame.args[1] .."|".. entity.name .."|".. string.gsub(entity.desc, "\n", "<br>") .."|".. stats .."}}")
end
local desc = string.gsub(entity.desc or "", "\n", "<br>")
if img then
return frame:preprocess("{{Tooltip|" .. label .. "|" .. displayName .. "|" .. desc .. "|" .. stats .. "|img=" .. img .. "}}")
else
return frame:preprocess("{{Tooltip|" .. label .. "|" .. displayName .. "|" .. desc .. "|" .. stats .. "}}")
end
end
-- {{Tooltip|Текст|[[картиночка|64x64px|left|middle|link=]] Название|Описание}}
end else
  out = frame:preprocess("{{Tooltip|".. frame.args[1] .."|Не найден|Предмет не найден|img=MobMouse2.png}}")
end
end
return frame:preprocess("{{Tooltip|" .. label .. "|Не найден|Предмет не найден|img=Mousegif.gif}}")
return out
end
end


-- p.createimagetooltip{args={"Файл:Honks.gif", "FreedomImplant", Статы="Имеет 3 заряда"}}
 
-- p.createimagetooltip{args={"Файл:Horns.png", "FreedomImplant", Статы="Имеет 3 заряда"}}
function p.createimagetooltip(frame) -- {{#invoke:Entity Lookup|createimagetooltip|Картинка|ProtoID|Мета=мета|Статы=статы}}
function p.createimagetooltip(frame) -- {{#invoke:Entity Lookup|createimagetooltip|Картинка|ProtoID|Мета=мета|Статы=статы}}
local picFile, id = frame.args[1], frame.args[2]
local out = " "
local meta = frame.args["Мета"] or "64x64px|link="
local entity = p.entities[frame.args[2]]
local stats = frame:preprocess("{{Особое|" .. id .. "}}")
local pic = frame.args[1]
local meta = frame.args["Мета"]
local stats = frame:preprocess("{{Особое|".. frame.args[2] .."}}")
if meta == nil then
meta = "64x64px|link="
end
meta = meta:gsub(',', '|')
meta = meta:gsub(',', '|')
local pic = string.format("[[%s|%s]]", picFile, meta)
pic = "[[" .. frame.args[1] .. "|" .. meta .. "]]"
local entity = p.entities[id]
if entity ~= nil then do
if entity then
if frame.args[1] ~= nil then do
local displayName = entity.name
out = frame:preprocess("{{Tooltip|".. pic .. "|" .. entity.name .. "|" .. entity.desc .. "|" .. stats .."|no-text-decoration=1}}")
if entity.suffix and entity.suffix ~= "" then
end else
displayName = displayName .. " (" .. entity.suffix .. ")"
out = frame:preprocess("{{Tooltip|[[Файл:MobMouse2.png|64x64px|left|middle|link=]]|" .. entity.name .. "|" .. entity.desc .. "|" .. stats .."|no-text-decoration=1}}")
end
end
local desc = entity.desc or ""
-- {{Tooltip|Текст|[[картиночка|64x64px|left|middle|link=]] Название|Описание}}
return frame:preprocess(string.format("{{Tooltip|%s|%s|%s|%s|no-text-decoration=1}}", pic, displayName, desc, stats))
end else
  out = frame:preprocess("{{Tooltip|" .. pic .. "|Не найден|Предмет не найден|img=MobMouse2.png|no-text-decoration=1}}")
end
end
-- {{Tooltip|Текст|[[картиночка|64x64px|left|middle|link=]] Название|Описание}}
return out
return frame:preprocess(string.format("{{Tooltip|%s|Не найден|Предмет не найден|img=Mousegif.gif|no-text-decoration=1}}", pic))
end
end


return p
return p