• 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 1: Line 1:  
local U = require("Module:Core")
 
local U = require("Module:Core")
local format = require("Module:Core").format
   
local Formatting = require("Module:Formatting")
 
local Formatting = require("Module:Formatting")
 
local Equipment = require("Module:Equipment")
 
local Equipment = require("Module:Equipment")
 
local Iterator = require("Module:Iterator")
 
local Iterator = require("Module:Iterator")
local ShipIterator = require("Module:ShipIterator")
   
local ShipCapabilities = require("Module:ShipCapabilities")
 
local ShipCapabilities = require("Module:ShipCapabilities")
 
local ShipCardKai = require("Module:ShipCardKai")
 
local ShipCardKai = require("Module:ShipCardKai")
Line 11: Line 9:  
local EquipmentCardKai = require("Module:EquipmentCardKai")
 
local EquipmentCardKai = require("Module:EquipmentCardKai")
 
local EquipmentGraphicKai = require("Module:EquipmentGraphicKai")
 
local EquipmentGraphicKai = require("Module:EquipmentGraphicKai")
local EquipmentCollection = require("Module:Collection/Equipment")
+
local EquipmentCollection = require("Module:Data/Equipment")
local AllEquipmentCollection = require("Module:Collection/EquipmentByApiId")
+
-- local AllEquipmentCollection = require("Module:Collection/EquipmentByApiId")
 
local Development = require("Module:Development")
 
local Development = require("Module:Development")
 
local ShipsByApiId = require("Module:Collection/ShipsByApiId")
 
local ShipsByApiId = require("Module:Collection/ShipsByApiId")
 +
local Ship = require("Module:Ship")
   −
local Ship = nil
  −
  −
local frame = nil
   
local args = nil
 
local args = nil
 +
-- local Ship = nil
 
local ship = nil
 
local ship = nil
 +
local shipCapabilities = {}
 
local target = nil
 
local target = nil
local shipCapabilities = {}
   
local equipment = nil
 
local equipment = nil
local filterArg = {}
+
local sequence = nil
local sorted = false
+
local sequence_length = nil
 
+
local sequence_position = nil
-- $('.calc-debug').show().appendTo('#mw-content-text')
+
-- local sorted = false
local debug = {}
+
-- local env = {}
 
  −
function debugLog(k, v)
  −
    if args.debug then
  −
        k = U.trim(tostring(k))
  −
        v = U.trim(tostring(v))
  −
        table.insert(debug, k .. " = " .. v)
  −
    end
  −
end
  −
 
  −
function debugString()
  −
    return #debug > 0 and args.debug and ([[<pre class="calc-debug" style="text-align:left;background:transparent;display:none"><code>]] .. table.concat(debug, "\n") .. [[</code></pre>]]) or ""
  −
end
  −
 
  −
function format_lua(lua)
  −
    if type(lua) == "table" then
  −
        return tostring(table.concat(lua, args.concat_value or ", "))
  −
    else
  −
        return tostring(lua)
  −
    end
  −
end
      
local enumerating_functions = {
 
local enumerating_functions = {
 
+
     args = function() return mw.text.split(args.args, "%s*,%s*") end,
     args = function()
+
     base_names = function() return require('Module:Collection/ShipsBase') end,
        return mw.text.split(args.args, "%s*,%s*")
+
     all_names = function() return require('Module:Collection/Ships') end,
    end,
+
    remodel_names = function()
 
+
    local all_names = require('Module:Collection/Ships')
     base_names = function()
+
    local base_names = require('Module:Collection/ShipsBase')
        return ShipIterator.baseForms
+
    local result = {}
     end,
+
    for _, name in ipairs(all_names) do
 
+
    if not U.includes(base_names, name) then
    all_names = function()
+
    local ship = Ship(name)
        return ShipIterator.allForms
+
    if ship._implementation_date then
    end,
+
    table.insert(result, name)
   
+
    end
    enemy = function()
+
    end
        return ShipIterator.enemyForms
+
    end
 +
    return result
 
     end,
 
     end,
 
+
    enemy = function() return require('Module:Collection/EnemyShips') end,
 
     equipment = function()
 
     equipment = function()
        return U.imap(EquipmentCollection, function(e) return e._name end)
+
    equipment = true
 +
    return U.imap(EquipmentCollection, function(e) return e._name end)
 
     end,
 
     end,
      
+
     --[[
 
     allEquipment = function()
 
     allEquipment = function()
 +
    equipment = true
 
         local result = {}
 
         local result = {}
 
         for i = 1, 700 do
 
         for i = 1, 700 do
Line 81: Line 61:  
         return result
 
         return result
 
     end,
 
     end,
   
     enemyEquipment = function()
 
     enemyEquipment = function()
 +
    equipment = true
 
         local result = {}
 
         local result = {}
 
         for i = 501, 700 do
 
         for i = 501, 700 do
Line 91: Line 71:  
         return result
 
         return result
 
     end,
 
     end,
 +
    ]]--
 +
}
   −
}
+
local function format_lua(lua)
 +
return tostring(type(lua) == "table" and table.concat(lua, args.concat_value or ", ") or lua)
 +
end
   −
-- TODO: have a syntax for calling Module:Formatting functions?
   
local formatting_functions = {
 
local formatting_functions = {
 
+
     air_power = function(ship) return ship.air_power and ship:air_power() or -1 end,
     air_power = function(ship)
+
     equipment_range = function(ship) return U.imax(U.imap(ship._equipment or {}, function (e) return Equipment(e.equipment):range() or 0 end), 0) end,
        return ship.air_power and ship:air_power() or -1
  −
    end,
  −
   
  −
     equipment_range = function(ship)
  −
        return U.imax(U.imap(ship._equipment or {}, function (e) return Equipment(e.equipment):range() or 0 end), 0)
  −
    end,
  −
   
   
     equipment_range_diff = function(ship)
 
     equipment_range_diff = function(ship)
 
         local equipment_range = U.imax(U.imap(ship._equipment or {}, function (e) return Equipment(e.equipment):range() or 0 end), 0)
 
         local equipment_range = U.imax(U.imap(ship._equipment or {}, function (e) return Equipment(e.equipment):range() or 0 end), 0)
 
         return (ship:range() or 0) - equipment_range
 
         return (ship:range() or 0) - equipment_range
 
     end,
 
     end,
 
+
     night_battle_power = function(ship) return (ship:firepower_max() or 0) + (ship:torpedo_max() or 0) end,
     night_battle_power = function(ship)
  −
        return (ship:firepower_max() or 0) + (ship:torpedo_max() or 0)
  −
    end,
  −
   
   
     format_day_battle = function(ship)
 
     format_day_battle = function(ship)
 
         local mode, attack_power = shipCapabilities:day_battle()
 
         local mode, attack_power = shipCapabilities:day_battle()
 
         return shipCapabilities:format_day_battle(mode, attack_power)
 
         return shipCapabilities:format_day_battle(mode, attack_power)
 
     end,
 
     end,
   
     format_night_battle = function(ship)
 
     format_night_battle = function(ship)
 
         local mode, attack_power = shipCapabilities:night_battle()
 
         local mode, attack_power = shipCapabilities:night_battle()
 
         return shipCapabilities:format_night_battle(mode, attack_power)
 
         return shipCapabilities:format_night_battle(mode, attack_power)
 
     end,
 
     end,
   
     format_opening_torpedo = function(ship)
 
     format_opening_torpedo = function(ship)
 
         return shipCapabilities:format_torpedo(shipCapabilities:opening_torpedo())
 
         return shipCapabilities:format_torpedo(shipCapabilities:opening_torpedo())
 
     end,
 
     end,
   
     format_closing_torpedo = function(ship)
 
     format_closing_torpedo = function(ship)
 
         return shipCapabilities:format_torpedo(shipCapabilities:closing_torpedo())
 
         return shipCapabilities:format_torpedo(shipCapabilities:closing_torpedo())
 
     end,
 
     end,
   
     format_asw_attack = function(ship)
 
     format_asw_attack = function(ship)
 
         local attack_power, opening, day, night, uncertain = shipCapabilities:asw_attack()
 
         local attack_power, opening, day, night, uncertain = shipCapabilities:asw_attack()
 
         return shipCapabilities:format_asw_attack(attack_power, opening, day, night, uncertain)
 
         return shipCapabilities:format_asw_attack(attack_power, opening, day, night, uncertain)
 
     end,
 
     end,
   
     format_opening_airstrike = function(ship)
 
     format_opening_airstrike = function(ship)
 
         return shipCapabilities:format_opening_airstrike(shipCapabilities:opening_airstrike())
 
         return shipCapabilities:format_opening_airstrike(shipCapabilities:opening_airstrike())
 
     end,
 
     end,
 
+
     slots = function(ship) return format_lua(U.imap(ship._equipment or {}, function (e) return e and e.size or '?' end)) end,
     slots = function(ship)
  −
        return format_lua(U.imap(ship._equipment or {}, function (e) return e and e.size or '?' end))
  −
    end,
  −
 
   
     equips = function(ship)
 
     equips = function(ship)
 
         return 'style="text-align:left"|' .. U.ijoin(U.imap(ship._equipment or {}, function (e)
 
         return 'style="text-align:left"|' .. U.ijoin(U.imap(ship._equipment or {}, function (e)
Line 158: Line 121:  
         end), "<br>")
 
         end), "<br>")
 
     end,
 
     end,
 
+
     code = function(obj) return equipment and Formatting:format_equipment_type(obj:type()) or Formatting:format_ship_code(obj:type()) end,
     code = function(obj)
+
     code_link = function(obj) return string.format("[[%s]]", Formatting:format_ship_code(obj:type())) end,
        return equipment and
+
     type = function(obj) return equipment and Formatting:format_equipment_type(obj:type()) or Formatting:format_ship_type(obj:type()) end,
            Formatting:format_equipment_type(obj:type()) or
  −
            Formatting:format_ship_code(obj:type())
  −
    end,
  −
 
  −
     -- overrides ShipData:type
  −
     type = function(obj)
  −
        return equipment and
  −
            Formatting:format_equipment_type(obj:type()) or
  −
            Formatting:format_ship_type(obj:type())
  −
    end,
  −
 
   
     icon = function(obj)
 
     icon = function(obj)
 
         if obj.hp then
 
         if obj.hp then
Line 180: Line 132:  
             end
 
             end
 
         else
 
         else
             return obj.icon and ([[<span data-sort-value="]] .. (obj._type or '0') .. [=[">[[File:]=] .. Formatting:format_equipment_icon(obj:icon()) .. "]]</span>") or ''
+
        return obj.icon and ([=[[[File:]=] .. Formatting:format_equipment_icon(obj:icon()) .. "]]") or ''
        end
+
             -- return obj.icon and ([[<span data-sort-value="]] .. (obj._type or '0') .. [=[">[[File:]=] .. Formatting:format_equipment_icon(obj:icon()) .. "]]</span>") or ''
    end,
  −
 
  −
    icon_damaged = function(obj)
  −
        return string.format("[[File:Ship Icon %s Damaged.png|100px]]", obj:name())
  −
    end,
  −
 
  −
    -- overrides ShipData:link
  −
    link = function(obj)
  −
        if obj._dummy then
  −
            local link = ShipsByApiId[obj._api_id]
  −
            if link then
  −
                link = link:gsub('/', ' ')
  −
            end
  −
            return link and Formatting:format_link(link) or ''
  −
        else
  −
            return Formatting:format_link(obj:link())
   
         end
 
         end
 
     end,
 
     end,
 
+
    icon_damaged = function(obj) return string.format("[[File:Ship Icon %s Damaged.png|100px]]", obj:name()) end,
     -- overrides ShipData:class
+
     link = function(obj) return string.format("[[%s]]", equipment and obj:name() or obj:unique_name()) end,
     class = function(ship)
+
     class = function(ship) return ship:class() and ship:class():name() or "?" end,
        return ship:class() and ship:class():name() or "?"
  −
    end,
  −
 
  −
    -- overrides ShipData:implementation_date
   
     implementation_date = function(ship)
 
     implementation_date = function(ship)
 
         local date = ship:implementation_date()
 
         local date = ship:implementation_date()
         return not date and "??" or format{
+
         return not date and "??" or string.format("%s/%s/%s", date[1], date[2] < 10 and "0" .. date[2] or date[2], date[3] < 10 and "0" .. date[3] or date[3])
            "${year}/${month}/${day}",
  −
            year = date[1],
  −
            month = date[2] < 10 and "0" .. date[2] or date[2],
  −
            day = date[3] < 10 and "0" .. date[3] or date[3],
  −
        }
   
     end,
 
     end,
   
     banner = function(obj)
 
     banner = function(obj)
 
     if not obj or not obj.lua_name then
 
     if not obj or not obj.lua_name then
Line 223: Line 149:  
         return ShipBattleCardKai:Asset({ obj:lua_name(), hd = true, size = "160px" })
 
         return ShipBattleCardKai:Asset({ obj:lua_name(), hd = true, size = "160px" })
 
     end,
 
     end,
   
     card = function(obj)
 
     card = function(obj)
 
     if not obj or not obj.lua_name then
 
     if not obj or not obj.lua_name then
Line 234: Line 159:  
         end
 
         end
 
     end,
 
     end,
 
+
     item = function(obj) return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", link = "nil" }) end,
     item = function(obj)
+
     fairy = function(obj) return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", fairy = "only", link = "nil" }) end,
        return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", link = "nil" })
+
     full = function(obj) return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", fairy = "true", link = "nil" }) end,
    end,
  −
 
  −
     fairy = function(obj)
  −
        return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", fairy = "only", link = "nil" })
  −
    end,
  −
 
  −
     full = function(obj)
  −
        return EquipmentGraphicKai:Asset({ obj:name(), size = "75px", fairy = "true", link = "nil" })
  −
    end,
  −
 
   
     card_ = function(o) return string.format("[[File:%s Card.png|75px]]", o:name()) end,
 
     card_ = function(o) return string.format("[[File:%s Card.png|75px]]", o:name()) end,
 
     equipment_ = function(o) return string.format("[[File:%s Equipment.png|75px]]", o:name()) end,
 
     equipment_ = function(o) return string.format("[[File:%s Equipment.png|75px]]", o:name()) end,
 
     character_ = function(o) return string.format("[[File:%s Character.png|75px]]", o:name()) end,
 
     character_ = function(o) return string.format("[[File:%s Character.png|75px]]", o:name()) end,
 
     full_ = function(o) return string.format("[[File:%s Full.png|75px]]", o:name()) end,
 
     full_ = function(o) return string.format("[[File:%s Full.png|75px]]", o:name()) end,
   
     format_morale = function(ship)
 
     format_morale = function(ship)
 
         local morale = ship:morale() or 49
 
         local morale = ship:morale() or 49
 
         return morale <= 19 and "Red" or morale <= 29 and "Orange" or morale <= 49 and "Normal" or "Sparkle"
 
         return morale <= 19 and "Red" or morale <= 29 and "Orange" or morale <= 49 and "Normal" or "Sparkle"
 
     end,
 
     end,
   
     hit_rate = function(ship, target)
 
     hit_rate = function(ship, target)
 
         local r = Combat.hit_rate(ship, target)
 
         local r = Combat.hit_rate(ship, target)
Line 266: Line 179:  
         end
 
         end
 
     end,
 
     end,
   
     critical_hit_rate = function(ship, target)
 
     critical_hit_rate = function(ship, target)
 
         local r = Combat.critical_hit_rate(ship, target)
 
         local r = Combat.critical_hit_rate(ship, target)
Line 276: Line 188:  
         end
 
         end
 
     end,
 
     end,
 
+
     scrap_string = function(eq)
     scrap = function(eq)
   
         local scrap = eq:scrap()
 
         local scrap = eq:scrap()
         return format{
+
         return string.format("%s/%s/%s/%s", scrap.fuel or 0, scrap.ammo or 0, scrap.steel or 0, scrap.bauxite or 0)
            "${fuel}/${ammo}/${steel}/${bauxite}",
  −
            fuel = scrap.fuel or 0,
  −
            ammo = scrap.ammo or 0,
  −
            steel = scrap.steel or 0,
  −
            bauxite = scrap.bauxite or 0,
  −
        }
  −
    end,
  −
 
  −
    development = function(eq)
  −
        return Development.formatResources(eq)
  −
    end,
  −
 
  −
    development_rate = function(eq)
  −
        return Development.formatRates(eq)
  −
    end,
  −
   
  −
    development_hq = function(eq)
  −
        return Development.formatHQ(eq)
  −
    end,
  −
   
  −
    backMinusRarity = function(ship)
  −
        return ship:back() - ship:rarity()
   
     end,
 
     end,
      
+
     development = function(eq) return Development.formatResources(eq) end,
 +
    development_rate = function(eq) return Development.formatRates(eq) end,
 +
    development_hq = function(eq) return Development.formatHQ(eq) end,
 +
    backMinusRarity = function(ship) return (ship:back() or 0) - (ship:rarity() or 0) end,
 
     rarity_bg = function(ship)
 
     rarity_bg = function(ship)
 
         return string.format('style="background:%s"|%s<br>%s', Formatting:format_ship_back(ship:rarity()), ship:rarity() or '??', Formatting:format_ship_rarity(ship:rarity()))
 
         return string.format('style="background:%s"|%s<br>%s', Formatting:format_ship_back(ship:rarity()), ship:rarity() or '??', Formatting:format_ship_rarity(ship:rarity()))
 
     end,
 
     end,
   
     back_bg = function(ship)
 
     back_bg = function(ship)
 
         return string.format('style="background:%s"|%s<br>%s', Formatting:format_ship_back(ship:back()), ship:back() or '??', Formatting:format_ship_rarity(ship:back()))
 
         return string.format('style="background:%s"|%s<br>%s', Formatting:format_ship_back(ship:back()), ship:back() or '??', Formatting:format_ship_rarity(ship:back()))
 
     end,
 
     end,
 
+
    same_day_remodel_links = function(ship)
 +
    local result = { string.format("[[%s]]", ship:name()) }
 +
    local visited = {}
 +
visited[ship:name()] = true
 +
    while true do
 +
    local next = ship:remodel_to()
 +
    if not next then break end
 +
    ship = Ship(next)
 +
    if ship._implementation_date then break end
 +
    if visited[ship:name()] then break end
 +
    visited[ship:name()] = true
 +
    table.insert(result, string.format("[[%s]]", ship:name()))
 +
    end
 +
    return table.concat(result, ", ")
 +
    end,
 
}
 
}
   −
function addFormattingFunctions(name, table)
+
--[[
 +
local function addFormattingFunctions(name, table)
 
     for k, v in pairs(table) do
 
     for k, v in pairs(table) do
 
         formatting_functions[name .. "." .. k] = v
 
         formatting_functions[name .. "." .. k] = v
Line 322: Line 228:  
addFormattingFunctions("FitData", require("Module:CalcFit"))
 
addFormattingFunctions("FitData", require("Module:CalcFit"))
 
addFormattingFunctions("Assets", require("Module:CalcAsset"))
 
addFormattingFunctions("Assets", require("Module:CalcAsset"))
 +
]]--
   −
function format_value(key, ship, target)
+
local function format_value(key, ship, target)
    local formatting_function = formatting_functions[key]
+
local keys = mw.text.split(key, "%s*%.%s*")
    if formatting_function then
+
local result = ship
        return formatting_function(ship, target)
+
for _, key in ipairs(keys) do
    end
+
    local formatting_function = formatting_functions[key]
    if shipCapabilities[key] then
+
    if formatting_function then
        local a, b = shipCapabilities[key](shipCapabilities)
+
        result = formatting_function(result, target)
        return format_lua(b or a)
+
    else
    end
+
    local lua = result[key] or result['_' .. key]
    if ship then
+
    if type(lua) == "function" then
        local lua = ship[key]
+
        result = lua(result)
        if type(lua) == "function" then
+
    else
            return format_lua(lua(ship))
+
        result = lua
        else
+
    end
            return format_lua(lua)
+
    end
        end
+
if type(result) ~= 'table' then
    end
+
return format_lua(result)
 +
end
 +
    --[[
 +
    if shipCapabilities[key] then
 +
        local a, b = shipCapabilities[key](shipCapabilities)
 +
        return format_lua(b or a)
 +
    end
 +
    ]]--
 +
end
 +
return format_lua(result)
 
end
 
end
   −
local sequence = nil
+
local function format_arg(arg)
local sequence_position = nil
  −
 
  −
function format_arg(arg)
   
     return args.format == "table" and ("| " .. arg .. "\n") or arg
 
     return args.format == "table" and ("| " .. arg .. "\n") or arg
 
end
 
end
   −
local env = {}
+
--[[
 
+
local function interpret_setter(s)
function interpret_setter(s)
   
     local kv = mw.text.split(s, "%s*~%s*")
 
     local kv = mw.text.split(s, "%s*~%s*")
 
     local k = kv[1]
 
     local k = kv[1]
Line 394: Line 306:  
end
 
end
   −
local implicitTrigger = true
+
local function trigger()
 
  −
local function trigger(implicit)
  −
implicitTrigger = implicit
   
     local ship_key = sequence[sequence_position]
 
     local ship_key = sequence[sequence_position]
    ship_key = tonumber(ship_key) or ship_key
   
     sequence_position = sequence_position + 1
 
     sequence_position = sequence_position + 1
 
     ship = Ship(ship_key)
 
     ship = Ship(ship_key)
Line 411: Line 319:  
end
 
end
   −
function interpret_arg(arg, args)
+
local function interpret_arg(arg)
 
     local prefix = string.sub(arg, 1, 1)
 
     local prefix = string.sub(arg, 1, 1)
 
     local prefix2 = string.sub(arg, 1, 2)
 
     local prefix2 = string.sub(arg, 1, 2)
     if arg == "_" then
+
     if arg == "-" then return format_arg("")
        return false
+
elseif arg == "!@" then trigger(false)
    elseif arg == "-" then
  −
        return format_arg("")
  −
    elseif prefix == "~" then
  −
    table.insert(filterArg, string.sub(arg, 2))
  −
    elseif prefix == "@" then
  −
        local enumerator = string.sub(arg, 2)
  −
        local enumerating_function = enumerating_functions[enumerator]
  −
        if enumerating_function and not sequence then
  −
            sequence = enumerating_function()
  −
            sequence_position = 1
  −
        elseif not sequence then
  −
            local buildIterator = Iterator[enumerator]
  −
            if buildIterator then
  −
                sequence = {}
  −
                sequence_position = 1
  −
                local iterator = buildIterator(args)
  −
                while iterator.next() do
  −
                    table.insert(sequence, iterator.current())
  −
                end
  −
            end
  −
        end
  −
        if args.sort and sequence and not sorted then
  −
        sorted = true
  −
        table.sort(sequence, function(a, b)
  −
        local a = Ship(a)
  −
        local b = Ship(b)
  −
      local x = a[args.sort](a)
  −
      local y = b[args.sort](b)
  −
if x < y then return true
  −
elseif x == y then return a:id() < b:id()
  −
else return false end
  −
end)
  −
        end
  −
    elseif arg == "!@" then
  −
trigger(false)
   
     elseif prefix2 == "!!" then
 
     elseif prefix2 == "!!" then
 
         local key = string.sub(arg, 3)
 
         local key = string.sub(arg, 3)
Line 478: Line 351:  
     elseif prefix == "#" and args.format == "table" then
 
     elseif prefix == "#" and args.format == "table" then
 
         return "|-\n"
 
         return "|-\n"
     elseif prefix == "?" then
+
     if prefix == "?" then
    if implicitTrigger and sequence then
  −
    trigger(true)
  −
    end
   
         local prefix2 = string.sub(arg, 1, 2)
 
         local prefix2 = string.sub(arg, 1, 2)
         if prefix2 == "??" then
+
         if prefix2 == "??" then return format_arg(format_value(string.sub(arg, 3), target, ship))
            return format_arg(format_value(string.sub(arg, 3), target, ship))
+
         elseif prefix2 == "?#" then return "?# is unimplemented" -- frame:preprocess(format{string.sub(arg, 3), this = env[this] or ""})
         elseif prefix2 == "?#" then
  −
            return frame:preprocess(format{string.sub(arg, 3), this = env[this] or ""})
   
         else
 
         else
        if #filterArg > 0 then
+
if test then
        local test = true
+
return format_arg(format_value(string.sub(arg, 2), ship, target))
        for _, e in ipairs(filterArg) do
+
else
        local kv = mw.text.split(e, "%s*~%s*")
+
return nil
        local xs = mw.text.split(kv[2], "%s*,%s*")
+
end
        local test2 = false
  −
        for _, e2 in ipairs(xs) do
  −
if tostring(ship[kv[1]](ship)) == e2 then
  −
        test2 = true
  −
        break
  −
        end
  −
        end
  −
        if not test2 then
  −
        test = false
  −
        break
  −
        end
  −
        end
  −
        if test then
  −
        return format_arg(format_value(string.sub(arg, 2), ship, target))
  −
        else
  −
        return nil
  −
        end
  −
        else
  −
            return format_arg(format_value(string.sub(arg, 2), ship, target))
  −
            end
  −
        end
   
     else
 
     else
 
         return format_arg(arg)
 
         return format_arg(arg)
Line 519: Line 366:  
end
 
end
   −
function interpret_args()
   
     Ship = args.enemy and require("Module:EnemyShip") or require("Module:Ship")
 
     Ship = args.enemy and require("Module:EnemyShip") or require("Module:Ship")
    debugLog("#args", #args)
  −
    debugLog("type(args)", type(args))
   
     if args.from and args.to then
 
     if args.from and args.to then
 
         sequence = {}
 
         sequence = {}
Line 530: Line 374:  
         sequence_position = 1
 
         sequence_position = 1
 
     end
 
     end
    local i = 1
  −
    local values = {}
  −
    repeat
  −
        for _, arg in ipairs(args) do
  −
            debugLog("i", i)
  −
            debugLog("arg", arg)
  −
            local value = interpret_arg(arg, args)
  −
            debugLog("value", value)
  −
            if value then
  −
                table.insert(values, value)
  −
            end
  −
            i = i + 1
  −
        end
  −
    until not sequence or sequence_position > #sequence
  −
    debugLog("#values", #values)
  −
    return table.concat(values, args.concat or (args.format == "table" and "" or ", ")) .. debugString()
  −
end
     −
local Calc = {}
+
        else
 +
            local buildIterator = Iterator[enumerator]
 +
            if buildIterator then
 +
                sequence = {}
 +
                sequence_position = 1
 +
                local iterator = buildIterator(args)
 +
                while iterator.next() do
 +
                    table.insert(sequence, iterator.current())
 +
                end
 +
                sequence_length = #sequence
 +
            end
 +
 
 +
]]--
   −
function Calc.format(frame_, args_)
+
local function interpret(args_)
    frame = frame_
+
args = args_
    args = args_ or U.getTemplateArgs(frame).explicit
+
local filterArgs = {}
 
     for _, arg in ipairs(args) do
 
     for _, arg in ipairs(args) do
        debugLog("arg", arg)
+
    local prefix = string.sub(arg, 1, 1)
 +
    if prefix == "~" then
 +
    -- ~x.y.z~a.b.c means obj.x.y.z == a or obj.x.y.z == b or obj.x.y.z == c
 +
    -- obj.x.y.z can be obj:x():y():z(), etc.
 +
    local ksvs = mw.text.split(string.sub(arg, 2), "%s*~%s*")
 +
    local ks = mw.text.split(ksvs[1], "%s*%.%s*")
 +
    local vs = mw.text.split(ksvs[2], "%s*,%s*")
 +
    table.insert(filterArgs, {ks, vs})
 +
    end
 
     end
 
     end
     return interpret_args()
+
     local actions = {}
end
+
    for _, arg in ipairs(args) do
 +
    local prefix = string.sub(arg, 1, 1)
 +
    if prefix == "@" then
 +
    local enumerator = string.sub(arg, 2)
 +
        local enumerating_function = enumerating_functions[enumerator]
 +
        if enumerating_function then
 +
        sequence = {}
 +
        for _, e in ipairs(enumerating_function()) do
 +
        local ship
 +
        if equipment then
 +
        ship = Equipment(e)
 +
        else
 +
        ship = Ship(e)
 +
        end
 +
        if #filterArgs > 0 then
 +
    local test = true
 +
for _, ksvs in ipairs(filterArgs) do
 +
local test2 = false
 +
 
 +
local obj = ship
 +
for _, k in ipairs(ksvs[1]) do
 +
local g = obj[k]
 +
if type(g) == 'function' then
 +
obj = g(obj)
 +
elseif type(g) == 'table' then
 +
obj = g
 +
else
 +
obj = g
 +
break
 +
end
 +
end
 +
local v1 = tostring(obj)
 +
 
 +
for _, v2 in ipairs(ksvs[2]) do
 +
if v1 == v2 then
 +
test2 = true
 +
break
 +
end
 +
end
   −
function Calc.test()
+
if not test2 then
    mw.log(
+
test = false
        Calc.format(nil, {
+
break
            format = "table",
+
end
            debug = "true",
+
end
            "!Nagato/Kai Ni",
+
if test then
            "!_equipment ~ 41cm Triple Gun Mount Kai, 41cm Triple Gun Mount Kai, Type 13 Air Radar Kai",
+
table.insert(sequence, ship)
            "!_level ~ 99",
+
end
            "!_luck ~ nil",
+
        else
            "!_morale ~ 25",
+
        table.insert(sequence, ship)
            "!!Destroyer Ro-Class",
+
        end
            "!cl0 ~ 56",
+
        end
            "!cl1 ~ 807",
+
        sequence_position = 1
            "!cl2 ~ 137",
+
sequence_length = #sequence
            "?card",
+
        end
            "?level",
+
    elseif prefix == '!' then
            "?format_morale",
+
    local name = string.sub(arg, 2)
            "?accuracy",
+
    local obj = Ship(name)
            "?luck",
+
    if not obj:hp() then
            "??card",
+
    equipment = true
            "?cl0",
+
    obj = Equipment(name)
            "?cl1",
+
    end
            "?cl2",
+
    sequence = {}
            "",
+
    table.insert(sequence, obj)
            "",
+
        sequence_position = 1
            "",
+
sequence_length = 1
            "",
+
    elseif arg == "#" or prefix == "?" then
            "-",
+
    table.insert(actions, string.sub(arg, 2))
            "?FitData.hit",
+
    end
            "?FitData.error",
+
    end
             "?FitData.crit",
+
    if args.sort and sequence then
            "?hit_rate",
+
    local sorts = mw.text.split(args.sort, "%s*,%s*")
            "?critical_hit_rate",
+
    table.sort(sequence, function(a, b)
            "?FitData.difference",
+
    for _, sort in ipairs(sorts) do
            "?FitData.critical_difference",
+
  local x = a[sort](a)
            "?FitData.accuracy_value",
+
  local y = b[sort](b)
            "?FitData.fit",
+
  if x == nil then return true end
        })
+
  if y == nil then return false end
    )
+
  if x < y then return true end
 +
  if x > y then return false end
 +
    end
 +
return false
 +
end)
 +
    end
 +
    local values = {}
 +
    repeat
 +
    if sequence then
 +
    ship = sequence[sequence_position]
 +
    sequence_position = sequence_position + 1
 +
    end
 +
        for _, arg in ipairs(actions) do
 +
        if arg == '' then
 +
        if args.format == "table" then
 +
        table.insert(values, "|-\n")
 +
        end
 +
        else
 +
            local value = ship and format_arg(format_value(arg, ship)) -- interpret_arg(arg)
 +
            if value then
 +
                table.insert(values, value)
 +
            end
 +
             end
 +
        end
 +
    until not sequence_length or sequence_position > sequence_length
 +
    return table.concat(values, args.concat or (args.format == "table" and "" or ", "))
 
end
 
end
   −
function Calc.test2()
+
local function test()
    mw.log(
+
mw.log(interpret({"@base_names", "~is_battleship~true", "?link", sort = "type,id"}))
        Calc.format(nil, {
+
mw.log(interpret({"@base_names", "~class.name~Kagerou Class", "?link", sort = "type,id"}))
            "@all_names",
+
mw.log(interpret({"!Saiun", "?icon"}))
            "~hp_mod_married ~ 1",
+
mw.log(interpret({"!Ayanami", "?scrap_string", "?scrap.fuel", "?scrap_fuel", "?_scrap_fuel"}))
            -- "!@",
  −
            "?link"
  −
        })
  −
    )
   
end
 
end
   −
-- print(p.test2())
+
-- print(p.test())
 
+
return {
return Calc
+
format = function(frame) return interpret(U.getTemplateArgs(frame).explicit) end,
 +
test = test,
 +
}
cssedit, gkautomate
7,064

edits

Navigation menu