• 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:ShipClass"

From Kancolle Wiki
Jump to navigation Jump to search
com>Ckwng
com>Ckwng
(Return barebones ShipClassData when module require fails.)
Line 8: Line 8:
 
model = ""
 
model = ""
 
end
 
end
 +
--check if we already have it
 
local ship_class = ship_classes[name]
 
local ship_class = ship_classes[name]
 
if ship_class then
 
if ship_class then
 
return ship_class
 
return ship_class
 
else
 
else
local ship_class_table = require(mw.ustring.format('Module:%s', name))
+
--Catch a failed require
ship_class = ShipClassData(ship_class_table.class)
+
local success, ship_class_table = pcall(function () return require(mw.ustring.format('Module:%s', name)) end)
 +
--require failed
 +
if success then
 +
ship_class_table = ship_class_table.class
 +
if not ship_class_table then
 +
--create a ShipClassData with what we have
 +
ship_class_table = {_name = name}
 +
end
 +
else
 +
--create a ShipClassData with what we have
 +
ship_class_table = {_name = name}
 +
end
 +
ship_class = ShipClassData(ship_class_table)
 
ship_classes[name] = ship_class
 
ship_classes[name] = ship_class
 
return ship_class
 
return ship_class

Revision as of 20:02, 11 March 2015

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

local ShipClass = {}
local ship_classes = {}

local ShipClassData = require('Module:ShipClassData')

function ShipClass:create(name)
	if model == nil then
		model = ""
	end
	--check if we already have it
	local ship_class = ship_classes[name]
	if ship_class then
		return ship_class
	else
		--Catch a failed require
		local success, ship_class_table = pcall(function () return require(mw.ustring.format('Module:%s', name)) end)
		--require failed
		if success then
			ship_class_table = ship_class_table.class
			if not ship_class_table then
				--create a ShipClassData with what we have
				ship_class_table = {_name = name}
			end
		else
			--create a ShipClassData with what we have
			ship_class_table = {_name = name}
		end
		ship_class = ShipClassData(ship_class_table)
		ship_classes[name] = ship_class
		return ship_class
	end
end

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

return ShipClass