Line 7: |
Line 7: |
| local U = require('Module:Core') | | local U = require('Module:Core') |
| local Equipment = require('Module:Equipment') | | local Equipment = require('Module:Equipment') |
| + | local Ship = require('Module:Ship') |
| + | local CollectionShips = require('Module:Collection/Ships') |
| local CollectionEquipment = require('Module:Collection/Equipment') | | local CollectionEquipment = require('Module:Collection/Equipment') |
| local EnemyShip = require('Module:EnemyShip') | | local EnemyShip = require('Module:EnemyShip') |
Line 82: |
Line 84: |
| end, | | end, |
| } | | } |
| + | end |
| + | |
| + | function Iterator.shipsBy(context, n, pred, pre, nItems) |
| + | local predKey = stringKey('pred', context, n) |
| + | local pred2 |
| + | if predKey then |
| + | pred2 = function(e) |
| + | local obj = Ship(e._name) |
| + | return obj[predKey](obj) |
| + | end |
| + | end |
| + | local preCollection |
| + | local collectionKey = stringKey('collection', context, n) |
| + | if collectionKey then |
| + | local _, CollectionData = U.requireModule(string.format("Collection/%s", collectionKey)) |
| + | preCollection = U.icopy(CollectionData) |
| + | else |
| + | preCollection = U.icopy(CollectionShips) |
| + | end |
| + | |
| + | local allowedRemodels = context['allowedRemodels'] |
| + | local listBase = (context['listBase'] == true) |
| + | local collection = {} |
| + | local index = 1 |
| + | for i = 1, #preCollection do |
| + | local _, CollectionData = U.requireModule(string.format("Data/Ship/%s", preCollection[i])) |
| + | if _ and CollectionData then |
| + | for j,v in pairs(CollectionData) do |
| + | if v._suffix then |
| + | if (allowedRemodels == nil or allowedRemodels[v._suffix]) then |
| + | local name = string.format("%s/%s", v._name, v._suffix) |
| + | collection[index] = v |
| + | index = index + 1 |
| + | end |
| + | elseif v._id then |
| + | if v._remodel_from or (v._name and listBase) then |
| + | collection[index] = v |
| + | index = index + 1 |
| + | end |
| + | end |
| + | end |
| + | end |
| + | end |
| + | |
| + | local sortKey = stringKey('sort', context, n) |
| + | if sortKey then |
| + | table.sort(collection, function(a, b) |
| + | if a[sortKey] ~= b[sortKey] then |
| + | return a[sortKey] < b[sortKey] |
| + | elseif a._id == b._id then |
| + | return a._api_id < b._api_id |
| + | else |
| + | return a._id < b._id |
| + | end |
| + | end) |
| + | end |
| + | |
| + | local i = 1 |
| + | local current = nil |
| + | local preFlag = true |
| + | local nCollection = nItems == true and #collection or nItems or #collection |
| + | return { |
| + | next = function() |
| + | for _ = i, nCollection do |
| + | local e |
| + | if nItems then |
| + | e = U.ifindBy(collection, function(e) return e._id == i end) or { _id = i } |
| + | else |
| + | e = collection[i] |
| + | end |
| + | if (pred and not pred(e, i)) or (pred2 and not pred2(e, i)) then |
| + | i = i + 1 |
| + | else |
| + | if pre and preFlag then |
| + | local value = pre(e, i) |
| + | if value then |
| + | current = value |
| + | preFlag = false |
| + | return true |
| + | end |
| + | end |
| + | current = e._name |
| + | if nItems and not current then |
| + | current = '-' |
| + | end |
| + | i = i + 1 |
| + | preFlag = true |
| + | return true |
| + | end |
| + | end |
| + | current = nil |
| + | return false |
| + | end, |
| + | current = function() |
| + | return current |
| + | end, |
| + | } |
| + | end |
| + | |
| + | function Iterator.shipsByType(context, n) |
| + | local type = numberKey('type', context, n) |
| + | return Iterator.shipsBy(context, n, function(e) |
| + | return e._type == type |
| + | end) |
| end | | end |
| | | |
Line 329: |
Line 435: |
| mw.log() | | mw.log() |
| end | | end |
| + | |
| + | testIterator('shipsByType', { type = '18', sort = '_id' , listBase = true}) |
| + | testIterator('shipsBy', { pred = 'is_auxiliary' , sort = '_name'}) |
| + | |
| testIterator('equipmentById', { from = '11', to = '20' }) | | testIterator('equipmentById', { from = '11', to = '20' }) |
| testIterator('equipmentByIdWithHeaders', { from = '1', to = '30' }) | | testIterator('equipmentByIdWithHeaders', { from = '1', to = '30' }) |
Line 335: |
Line 445: |
| testIterator('equipmentByTypeAndIcon', { type = '1', icon = '16' }) | | testIterator('equipmentByTypeAndIcon', { type = '1', icon = '16' }) |
| testIterator('equipmentBy', { pred = 'is_large_caliber_main_gun', sort = '_type' }) | | testIterator('equipmentBy', { pred = 'is_large_caliber_main_gun', sort = '_type' }) |
| + | |
| testIterator('enemiesByType', { type = '2' }) | | testIterator('enemiesByType', { type = '2' }) |
| end | | end |