• Welcome to the Kancolle Wiki!
  • If you have any questions regarding site content, account registration, etc., please visit the KanColle Wiki Discord

Changes

Jump to navigation Jump to search
no edit summary
Line 2: Line 2:  
-- more generic interface and compositions (filtering, grouping, mapping, sorting, etc.)
 
-- more generic interface and compositions (filtering, grouping, mapping, sorting, etc.)
 
-- prevent clients from infinite loops
 
-- prevent clients from infinite loops
 +
 +
local EquipmentData = require('Module:EquipmentData')
    
local Iterator = {}
 
local Iterator = {}
    +
-- legacy, not a proper iterator
 
function Iterator.array(arr, i, n)
 
function Iterator.array(arr, i, n)
 
     i = i or 0
 
     i = i or 0
Line 19: Line 22:  
end
 
end
   −
function Iterator.equipmentById(context, n)
+
function stringKey(name, context, n, default)
     local fromKey = "from" .. (n and tostring(n) or "")
+
     return context and context[name .. (n and tostring(n) or '')] or default
     local from = context and context[fromKey] and tonumber(context[fromKey]) or 1
+
end
    local toKey = "to" .. (n and tostring(n) or "")
+
 
     local to = context and context[toKey] and tonumber(context[toKey]) or 500
+
function numberKey(name, context, n, default)
     local sortKey = "sort" .. (n and tostring(n) or "")
+
     return context and tonumber(context[name .. (n and tostring(n) or '')]) or default
    local sort = context and context[sortKey]
+
end
 +
 
 +
function Iterator.equipmentBy(context, n, pred)
 +
     local predKey = stringKey('pred', context, n)
 +
     if predKey then
 +
        pred = function(e)
 +
            local obj = EquipmentData(e)
 +
            return obj[predKey](obj)
 +
        end
 +
    end
 
     local collection = require('Module:Collection/Equipment')
 
     local collection = require('Module:Collection/Equipment')
     if sort then
+
    local sortKey = stringKey('sort', context, n)
         table.sort(collection, function(a, b) return a[sort] < b[sort] end)
+
     if sortKey then
 +
         table.sort(collection, function(a, b) return a[sortKey] and b[sortKey] and a[sortKey] < b[sortKey] end)
 
     end
 
     end
 
     local i = 1
 
     local i = 1
     local current
+
     local current = nil
 
     return {
 
     return {
 
         next = function()
 
         next = function()
 
             for _ = i, #collection do
 
             for _ = i, #collection do
                 if i >= from and i <= to then
+
                local e = collection[i]
                     current = collection[i]._name
+
                 if pred(e, i) then
 +
                     current = e._name
 
                     i = i + 1
 
                     i = i + 1
 
                     return true
 
                     return true
Line 51: Line 65:  
end
 
end
   −
function Iterator.equipment(n)
+
function Iterator.equipmentById(context, n)
     return Iterator.equipmentById(nil, n)
+
    local from = numberKey('from', context, n, 1)
 +
    local to = numberKey('to', context, n, 500)
 +
     return Iterator.equipmentBy(context, n, function(_, i)
 +
        -- use _id?
 +
        return i >= from and i <= to
 +
    end)
 
end
 
end
    
function Iterator.equipmentByType(context, n)
 
function Iterator.equipmentByType(context, n)
     local typeKey = "type" .. (n and tostring(n) or "")
+
     local type = numberKey('type', context, n)
    local type = context and context[typeKey] and tonumber(context[typeKey])
+
     return Iterator.equipmentBy(context, n, function(e)
     local sortKey = "sort" .. (n and tostring(n) or "")
+
        return e._type == type
    local sort = context and context[sortKey]
+
    end)
    local collection = require('Module:Collection/Equipment')
  −
    if sort then
  −
        table.sort(collection, function(a, b) return a[sort] < b[sort] end)
  −
    end
  −
    local i = 1
  −
    local current
  −
    return {
  −
        next = function()
  −
            for _ = i, #collection do
  −
                if collection[i]._type == type then
  −
                    current = collection[i]._name
  −
                    i = i + 1
  −
                    return true
  −
                end
  −
                i = i + 1
  −
            end
  −
            current = nil
  −
            return false
  −
        end,
  −
        current = function()
  −
            return current
  −
        end,
  −
    }
   
end
 
end
    
function Iterator.equipmentByTypeAndIcon(context, n)
 
function Iterator.equipmentByTypeAndIcon(context, n)
     local typeKey = "type" .. (n and tostring(n) or "")
+
     local type = numberKey('type', context, n)
    local iconKey = "icon" .. (n and tostring(n) or "")
+
     local icon = numberKey('icon', context, n)
    local type = context and context[typeKey] and tonumber(context[typeKey])
+
     return Iterator.equipmentBy(context, n, function(e)
     local icon = context and context[iconKey] and tonumber(context[iconKey])
+
        return e._type == type and e._icon == icon
     local sortKey = "sort" .. (n and tostring(n) or "")
+
    end)
    local sort = context and context[sortKey]
  −
    local collection = require('Module:Collection/Equipment')
  −
    if sort then
  −
        table.sort(collection, function(a, b) return a[sort] < b[sort] end)
  −
    end
  −
    local i = 1
  −
    local current
  −
    return {
  −
        next = function()
  −
            for _ = i, #collection do
  −
                if collection[i]._type == type and collection[i]._icon == icon then
  −
                    current = collection[i]._name
  −
                    i = i + 1
  −
                    return true
  −
                end
  −
                i = i + 1
  −
            end
  −
            current = nil
  −
            return false
  −
        end,
  −
        current = function()
  −
            return current
  −
        end,
  −
    }
   
end
 
end
   −
--[[
   
function Iterator.test()
 
function Iterator.test()
     local iterator = Iterator.equipmentById({ from = "11", to = "20" })
+
     function testIterator(name, args)
    while iterator.next() do
+
        local iterator = Iterator[name](args)
        mw.log(iterator.current())
+
        mw.log(name)
 +
        while iterator.next() do
 +
            mw.log('  ' .. iterator.current())
 +
        end
 +
        mw.log()
 
     end
 
     end
 +
    testIterator('equipmentById', { from = '11', to = '20' })
 +
    testIterator('equipmentByType', { type = '2' })
 +
    testIterator('equipmentByTypeAndIcon', { type = '1', icon = '16' })
 +
    testIterator('equipmentBy', { pred = 'is_large_caliber_main_gun', sort = '_type' })
 
end
 
end
 
-- p.test()
 
-- p.test()
]]--
      
return Iterator
 
return Iterator
cssedit, gkautomate
7,060

edits

Navigation menu