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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
 
(не показано 7 промежуточных версий 1 участника)
Строка 2: Строка 2:


local p = {}
local p = {}
p.store = prototypes.store
p.store = prototypes.store or {}


-- Возвращает значение поля (без ошибок, если не найдено)
function p.readscalar(frame)
function p.readscalar(frame)
return mw.text.nowiki(p.store[frame.args[1]][frame.args[2]])
    local prototype = frame.args[1]
    local field = frame.args[2]
    if not prototype or not p.store[prototype] then
        return ""
    end
    return mw.text.nowiki( tostring(p.store[prototype][field] or "") )
end
end


-- Собирает список шаблонов для указанной группы (или всех, если group == nil)
function p.buildlists(frame)
function p.buildlists(frame)
local out = ""
    local group = frame.args[1]
local group = frame.args[1]
     local keys = {}
     local keys = {}
for k in pairs(p.store) do
 
if p.store[k].group == group or group == nil then
    for k in pairs(p.store) do
table.insert(keys, k)
        if not group or p.store[k].group == group then
end
            table.insert(keys, k)
end
        end
   
    end
     local function compareByCost(a, b)
 
         return p.store[a].cost < p.store[b].cost
     local function costOrMax(key)
         local entry = p.store[key]
        if not entry then return math.huge end
        local n = tonumber(entry.cost)
        return n or math.huge
     end
     end


     table.sort(keys, compareByCost)
     table.sort(keys, function(a, b)
      
        return costOrMax(a) < costOrMax(b)
    end)
 
     local out = {}
     for _, k in ipairs(keys) do
     for _, k in ipairs(keys) do
         out = out .. frame:expandTemplate{ title = "Uplink", args = { prototype = k }}
        -- передаём прототип как именованный параметр шаблона
         out[#out + 1] = frame:expandTemplate{ title = "Uplink", args = { prototype = k } }
     end
     end
      
 
return out
     return table.concat(out, "")
end
end


return p
return p

Текущая версия от 16:07, 16 октября 2025

Для документации этого модуля может быть создана страница Модуль:Uplink Lookup/doc

local prototypes = mw.loadData("Module:Uplink Lookup/data")

local p = {}
p.store = prototypes.store or {}

-- Возвращает значение поля (без ошибок, если не найдено)
function p.readscalar(frame)
    local prototype = frame.args[1]
    local field = frame.args[2]
    if not prototype or not p.store[prototype] then
        return ""
    end
    return mw.text.nowiki( tostring(p.store[prototype][field] or "") )
end

-- Собирает список шаблонов для указанной группы (или всех, если group == nil)
function p.buildlists(frame)
    local group = frame.args[1]
    local keys = {}

    for k in pairs(p.store) do
        if not group or p.store[k].group == group then
            table.insert(keys, k)
        end
    end

    local function costOrMax(key)
        local entry = p.store[key]
        if not entry then return math.huge end
        local n = tonumber(entry.cost)
        return n or math.huge
    end

    table.sort(keys, function(a, b)
        return costOrMax(a) < costOrMax(b)
    end)

    local out = {}
    for _, k in ipairs(keys) do
        -- передаём прототип как именованный параметр шаблона
        out[#out + 1] = frame:expandTemplate{ title = "Uplink", args = { prototype = k } }
    end

    return table.concat(out, "")
end

return p