• 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
Line 3: Line 3:
 
local equipments = {}
 
local equipments = {}
  
 +
local U = require('Module:Utils')
 
local EquipmentData = require('Module:EquipmentData')
 
local EquipmentData = require('Module:EquipmentData')
  
Line 21: Line 22:
 
if not success or equipment_table == true then
 
if not success or equipment_table == true then
 
--create a EquipmentData with what we have
 
--create a EquipmentData with what we have
equipment_table = {_name = name}
+
equipment_table
 +
        = U.ifindBy(require('Module:Collection/Equipment'), function (e) return e._name == name or e.__name == name end)
 +
    or { _name = name }
 
end
 
end
 
local equipment = EquipmentData(equipment_table)
 
local equipment = EquipmentData(equipment_table)

Revision as of 15:22, 8 December 2017

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

local Equipment = {}

local equipments = {}

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

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

function Equipment:create(name)
	if name == nil then
		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
			equipment_table
		        = U.ifindBy(require('Module:Collection/Equipment'), function (e) return e._name == name or e.__name == name end)
			    or { _name = name }
		end
		local equipment = EquipmentData(equipment_table)
		equipments[name] = equipment
		return equipment
	end
end

function Equipment:get_module(name)
	if name == nil then
		return nil
	end
	return mw.ustring.format('Module:%s', name)
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