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

Difference between revisions of "Module:Equipment"

From Kancolle Wiki
Jump to navigation Jump to search
m
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
local U = require('Module:Core')
 +
local EquipmentData = require('Module:EquipmentData')
 +
 +
local EquipmentCollection = mw.loadData('Module:Data/Equipment')
 +
 
local Equipment = {}
 
local Equipment = {}
  
 
local equipments = {}
 
local equipments = {}
  
local U = require('Module:Utils')
+
local function requireEquipmentModule(name, is_enemy)
local EquipmentData = require('Module:EquipmentData')
+
  local success, data
 +
  if not is_enemy then
 +
success, data = U.requireModule('Data/Equipment/' .. name)
 +
    if not success then
 +
      success, data = U.requireModule('Data/Item/' .. name)
 +
    end
 +
    if not success then
 +
      success, data = U.requireModule('Data/PseudoItem/' .. name)
 +
    end
 +
    if not success then
 +
      data = U.find(EquipmentCollection, name, '_name')
 +
      -- success = true
 +
    end
 +
  end
 +
  if not success then
 +
    success, data = U.requireModule('Data/EnemyEquipment/' .. name)
 +
  end
 +
  if not success then
 +
  return {_name = name}
 +
  end
 +
  data._is_enemy = is_enemy
 +
  return data
 +
end
  
function Equipment:get(stat, name)
+
function Equipment:create(name, is_enemy)
return self:create(name)[stat]()
+
  if not name then
 +
    return EquipmentData()
 +
  end
 +
  if not equipments[name] then
 +
    equipments[name] = EquipmentData(requireEquipmentModule(name, is_enemy))
 +
  end
 +
  return equipments[name]
 
end
 
end
  
function Equipment:create(name)
+
function Equipment:get(stat, name)
if name == nil then
+
  return self:create(name)[stat]()
return EquipmentData()
 
--check if we already have it
 
elseif equipments[name] then
 
return equipments[name]
 
else
 
--Catch a failed require
 
local success, equipment_table = pcall(function () return require(mw.ustring.format('Module:%s', name)) end)
 
--require failed, equipment_table can be true on a wikia bug (Ticket 356218)
 
if not success or equipment_table == true then
 
--create a EquipmentData with what we have
 
local eq = U.ifindBy(require('Module:Collection/Equipment'), function (e) return e._name == name or e.__name == name end)
 
if eq and eq.__name then
 
    eq._name = eq.__name
 
end
 
equipment_table = eq or { _name = name }
 
end
 
local equipment = EquipmentData(equipment_table)
 
equipments[name] = equipment
 
return equipment
 
end
 
 
end
 
end
  
function Equipment:get_module(name)
+
function Equipment:get_module(name, typeName)
if name == nil then
+
  return name and string.format('Module:Data/%s/%s', typeName or 'Equipment', name) or nil
return nil
 
end
 
return mw.ustring.format('Module:%s', name)
 
 
end
 
end
  
 
function Equipment:extend(data)
 
function Equipment:extend(data)
data = data or {}
+
  data = data or {}
setmetatable(data, data)
+
  setmetatable(data, data)
data.__index = self
+
  data.__index = self
data.__call = self.__call
+
  data.__call = self.__call
return data
+
  return data
 
end
 
end
  

Latest revision as of 15:13, 30 April 2023

Documentation for this module may be created at Module:Equipment/doc

local U = require('Module:Core')
local EquipmentData = require('Module:EquipmentData')

local EquipmentCollection = mw.loadData('Module:Data/Equipment')

local Equipment = {}

local equipments = {}

local function requireEquipmentModule(name, is_enemy)
  local success, data
  if not is_enemy then
	success, data = U.requireModule('Data/Equipment/' .. name)
    if not success then
      success, data = U.requireModule('Data/Item/' .. name)
    end
    if not success then
      success, data = U.requireModule('Data/PseudoItem/' .. name)
    end
    if not success then
      data = U.find(EquipmentCollection, name, '_name')
      -- success = true
    end
  end
  if not success then
    success, data = U.requireModule('Data/EnemyEquipment/' .. name)
  end
  if not success then
  	return {_name = name}
  end
  data._is_enemy = is_enemy
  return data
end

function Equipment:create(name, is_enemy)
  if not name then
    return EquipmentData()
  end
  if not equipments[name] then
    equipments[name] = EquipmentData(requireEquipmentModule(name, is_enemy))
  end
  return equipments[name]
end

function Equipment:get(stat, name)
  return self:create(name)[stat]()
end

function Equipment:get_module(name, typeName)
  return name and string.format('Module:Data/%s/%s', typeName or 'Equipment', name) or nil
end

function Equipment:extend(data)
  data = data or {}
  setmetatable(data, data)
  data.__index = self
  data.__call = self.__call
  return data
end

Equipment.__call = Equipment.create
setmetatable(Equipment, Equipment)

return Equipment