模块:Salvage

来自乐园数据管理室

此模块的文档可以在模块:Salvage/doc创建

local p = {}
local loadJson = require("Module:JSON").loadJson

function p.getDataByGmkId(gmkId)
  return loadJson("SalvagePoint/" .. gmkId)
end

function p.parseSalvageTable(SalvageTable)
  -- parse collection
  local collection = {}
  for ColleTableIndex = 1, 3 do
    local ColleTable = SalvageTable["ColleTable" .. ColleTableIndex]
    local ColleTablePercent = SalvageTable["ColleTablePercent" .. ColleTableIndex] / 100

    if ColleTablePercent > 0 then
      local collectionStaticPopTable = {}
      local collectionRandomPopTable = {}

      for itmIndex = 1, 8 do -- 单道具行
        if #ColleTable["itm" .. itmIndex .. "ID"] > 0 then
          local itemName = ColleTable["itm" .. itmIndex .. "ID"]
          local itemNum = ColleTable["itm" .. itmIndex .. "Num"]
          local itemPer = ColleTable["itm" .. itmIndex .. "Per"]

          local itemTitle = mw.title.new(itemName, "物品")
          local itemLink = "[[" .. itemTitle.fullText .. "|" .. itemTitle.text .. "]]"

          if tonumber(itemNum) > 0 then
            table.insert(collectionStaticPopTable, itemLink)
            mw.smw.set {["SalvagePop"] = itemTitle.fullText}
          end
          if tonumber(itemPer) > 0 then
            table.insert(collectionRandomPopTable, {itemLink, itemPer})
            mw.smw.set {["SalvagePop"] = itemTitle.fullText}
          end
        end
      end

      local randitmPopNum = {ColleTable["randitmPopMin"], ColleTable["randitmPopMax"]}
      if ColleTable["randitmPopMin"] == ColleTable["randitmPopMax"] then
        randitmPopNum = {ColleTable["randitmPopMin"]}
      end

      if #collectionStaticPopTable > 0 or #collectionRandomPopTable > 0 then
        table.insert(
          collection,
          {
            ["colleTablePercent"] = ColleTablePercent,
            ["staticPop"] = collectionStaticPopTable,
            ["randomPop"] = collectionRandomPopTable,
            ["randitmPopNum"] = randitmPopNum
          }
        )
      end
    end
  end

  -- parse treasure
  local treasure = {}
  for TresureTableIndex = 1, 3 do
    local TresureTable = SalvageTable["TresureTable" .. TresureTableIndex]
    local TresureTablePercent = SalvageTable["TresureTablePercent" .. TresureTableIndex] / 100

    if TresureTablePercent > 0 then
      local treasureStaticPopTable = {}
      local treasureRandomPopTable = {}
      local treasureGold = {0}

      for itmIndex = 1, 8 do -- 单道具行
        if #TresureTable["itm" .. itmIndex .. "ID"] > 0 then
          local itemName = TresureTable["itm" .. itmIndex .. "ID"]
          local itemNum = TresureTable["itm" .. itmIndex .. "Num"]
          local itemPer = TresureTable["itm" .. itmIndex .. "Per"]

          local itemTitle = mw.title.new(itemName, "物品")
          local itemLink = "[[" .. itemTitle.fullText .. "|" .. itemTitle.text .. "]]"

          if tonumber(itemNum) > 0 then
            table.insert(treasureStaticPopTable, itemLink)
            mw.smw.set {["SalvagePop"] = itemTitle.fullText}
          end
          if tonumber(itemPer) > 0 then
            table.insert(treasureRandomPopTable, {itemLink, itemPer})
            mw.smw.set {["SalvagePop"] = itemTitle.fullText}
          end
          if tonumber(TresureTable["goldPopMin"]) > 0 and tonumber(TresureTable["goldMax"]) > 0 then
            treasureGold = {TresureTable["goldMin"], TresureTable["goldMax"]}
          end
        end
      end

      local tboxImage
      if TresureTable["RSC_ID"] == "4" then
        tboxImage = "[[文件:Mnu064 image 1.png|136px]]"
      elseif TresureTable["RSC_ID"] == "5" then
        tboxImage = "[[文件:Mnu064 image 2.png|136px]]"
      elseif TresureTable["RSC_ID"] == "6" then
        tboxImage = "[[文件:Mnu064 image 4.png|136px]]"
      else
        error("No valid RSC_ID")
      end

      table.insert(
        treasure,
        {
          ["treasureTablePercent"] = TresureTablePercent,
          ["staticPop"] = treasureStaticPopTable,
          ["randomPop"] = treasureRandomPopTable,
          ["randitmPopNum"] = TresureTable["randitmPopMin"] == TresureTable["randitmPopMax"] and
            {TresureTable["randitmPopMin"]} or
            {TresureTable["randitmPopMin"], TresureTable["randitmPopMax"]},
          ["tboxImage"] = tboxImage,
          ["treasureGold"] = treasureGold
        }
      )
    end
  end

  -- enemy
  local enemy = {}
  for EnemyPopTableIndex = 1, 3 do
    local EnemyPopTable = SalvageTable["EnemyPopTable" .. EnemyPopTableIndex]
    local EnemyPopTablePercent = SalvageTable["EnemyPopTablePercent" .. EnemyPopTableIndex] / 100
    local enemyPop = {}

    if EnemyPopTablePercent > 0 then
      for eneIndex = 1, 4 do
        if #EnemyPopTable["ene" .. eneIndex .. "ID"] > 0 then
          local eneName = EnemyPopTable["ene" .. eneIndex .. "ID"]
          local eneLv = EnemyPopTable["ene" .. eneIndex .. "Lv"]
          local enePer = EnemyPopTable["ene" .. eneIndex .. "Per"]

          local eneTitle = mw.title.new(eneName, "敌人")
          local eneLink = "[[" .. eneTitle.fullText .. "|" .. eneTitle.text .. "]]"

          if tonumber(enePer) > 0 then
            table.insert(enemyPop, {eneLink, enePer})
            mw.smw.set {["SalvageEnemyPop"] = eneTitle.fullText}
          end
        end
      end

      table.insert(
        enemy,
        {
          ["EnemyPopTablePercent"] = EnemyPopTablePercent,
          ["enemyPop"] = enemyPop
        }
      )
    end
  end

  return {
    ["collection"] = collection,
    ["treasure"] = treasure,
    ["TresureBoxHit"] = SalvageTable["TresureBoxHit"] / 100,
    ["enemy"] = enemy,
    ["EnemyPopHit"] = SalvageTable["EnemyPopHit"] / 100
  }
end

function DisplayTable(SalvageTable)
  local tableBody = ""
  local result = p.parseSalvageTable(SalvageTable)

  -- collection
  local collectionTable = ""
  if #result["treasure"] > 0 then
    for i, v in ipairs(result["collection"]) do
      local ColleTablePercent = v["colleTablePercent"]
      local StaticPop = table.concat(v["staticPop"], "<br>")
      local randitmPopNum = table.concat(v["randitmPopNum"], " ~ ")
      local RandomPop = {}
      for i, v in ipairs(v["randomPop"]) do
        table.insert(RandomPop, table.concat(v, " ") .. "%")
      end
      RandomPop = table.concat(RandomPop, "<br>")

      tableBody =
        tableBody ..
        mw.ustring.format([[|-
|%s%%
|%s
|%s
|%s
]], ColleTablePercent, StaticPop, randitmPopNum, RandomPop)
    end

    collectionTable =
      mw.ustring.format(
      [[{| class="wikitable"
|+
! colspan="4" |收藏道具
|+
!概率
!必出道具
! colspan="2" |额外道具
%s|}
]],
      tableBody
    )
  end

  -- treasure
  local treasureTable = ""
  if #result["treasure"] > 0 then
    tableBody = ""
    for i, v in ipairs(result["treasure"]) do
      local StaticPop = table.concat(v["staticPop"], "<br>")
      local randitmPopNum = table.concat(v["randitmPopNum"], " ~ ")
      local treasureGold = table.concat(v["treasureGold"], " ~ ")
      local RandomPop = {}
      for i, v in ipairs(v["randomPop"]) do
        table.insert(RandomPop, table.concat(v, " ") .. "%")
      end
      RandomPop = table.concat(RandomPop, "<br>")

      tableBody =
        tableBody ..
        mw.ustring.format(
          [[|-
|%s%%
|%s
|%s
|%s
|%s
|%s
]],
          v["treasureTablePercent"],
          v["tboxImage"],
          treasureGold,
          StaticPop,
          randitmPopNum,
          RandomPop
        )
    end

    treasureTable =
      mw.ustring.format(
      [[{| class="wikitable"
|+
! colspan="6" |宝藏 基础出现率:%s%%
|+
!概率
!宝箱类型
!金币
!必出道具
! colspan="2" |额外道具
%s|}
]],
      result["TresureBoxHit"],
      tableBody
    )
  end

  -- enemy
  local enemyTable = ""
  if #result["enemy"] > 0 and result["EnemyPopHit"] > 0 then
    tableBody = ""
    for i, v in ipairs(result["enemy"]) do
      local EnemyPopTablePercent = v["EnemyPopTablePercent"]
      local enemyPop = {}
      for i, v in ipairs(v["enemyPop"]) do
        table.insert(enemyPop, table.concat(v, " ") .. "%")
      end
      enemyPop = table.concat(enemyPop, "<br>")

      tableBody = tableBody .. mw.ustring.format([[|-
|%s%%
|%s
]], EnemyPopTablePercent, enemyPop)
    end

    enemyTable =
      mw.ustring.format(
      [[{| class="wikitable"
|+
! colspan="2" |敌人 基础出现率:%s%%
|+
!概率
!名称
%s|}
]],
      result["EnemyPopHit"],
      tableBody
    )
  end

  return '<div style="display:flex;flex-wrap:wrap;">\n' .. collectionTable .. enemyTable .. treasureTable .. "</div>\n"
end

function getMapinfo(gmkId)
  local area
  local gmkData = loadJson("Gmk/salvage")
  for _,v in ipairs(gmkData) do
    if v.Name == gmkId then
      area = v.areas[1]
    end
  end
  return loadJson("Mapinfo/" .. area)
end

function p.popTable(frame)
  local args = frame.args
  local gmkId = args[1]

  local mapinfo = getMapinfo(gmkId)
  mw.smw.set {["地域"] = mapinfo.mapName}

  local data = p.getDataByGmkId(gmkId)
  local output = ""

  -- 按键
  local button1 = data["BtnChallenge0"]["BtnType1"]
  local button2 = data["BtnChallenge1"]["BtnType1"]
  local button3 = data["BtnChallenge2"]["BtnType1"]

  if button1 == "Random" then
    output = output .. "按键:随机" .. "\n\n"
  else
    output = output .. "按键:" .. button1 .. button2 .. button3 .. "\n\n"
  end

  output = output .. "=== 普通气瓶 ===\n\n"
  output = output .. DisplayTable(data["SalvageTable1"])
  output = output .. "=== 白银气瓶 ===\n\n"
  output = output .. DisplayTable(data["SalvageTable2"])
  output = output .. "=== 黄金气瓶 ===\n\n"
  output = output .. DisplayTable(data["SalvageTable3"])
  output = output .. "=== 极品气瓶 ===\n\n"
  output = output .. DisplayTable(data["SalvageTable4"])

  if data["SalvageTable5"] ~= "" then
    output = output .. "=== 增幅气瓶 ===\n\n"
    output = output .. DisplayTable(data["SalvageTable5"])
  end
  return output
end

return p