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

From Kancolle Wiki
Jump to navigation Jump to search
com>Ckwng
(Add module page title retrieval)
com>Ckwng
(Return a bare bones ShipData instead when module require fails.)
Line 15: Line 15:
 
model = ""
 
model = ""
 
end
 
end
 +
--check if we already have it
 
if ships[name] and ships[name][model] then
 
if ships[name] and ships[name][model] then
 
return ships[name][model]
 
return ships[name][model]
 
else
 
else
local ship_table = require(mw.ustring.format('Module:%s', name))
+
--Catch a failed require
local ship = ShipData(ship_table[model])
+
local success, ship_table = pcall(function () return require(mw.ustring.format('Module:%s', name)) end)
 +
--require succeeded
 +
if success then
 +
ship_table = ship_table[model]
 +
if not ship_table then
 +
--create a ShipData with what we have
 +
ship_table = {_name = name, _suffix = model}
 +
end
 +
--require failed
 +
else
 +
--create a ShipData with what we have
 +
ship_table = {_name = name, _suffix = model}
 +
end
 +
local ship = ShipData(ship_table)
 
if not ships[name] then
 
if not ships[name] then
 
ships[name] = {}
 
ships[name] = {}

Revision as of 07:09, 11 March 2015

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

local Ship = {}
local ships = {}

local ShipData = require('Module:ShipData')

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

function Ship:create(name, model)
	if name == nil then
		return ShipData()
	end
	if model == nil then
		model = ""
	end
	--check if we already have it
	if ships[name] and ships[name][model] then
		return ships[name][model]
	else
		--Catch a failed require
		local success, ship_table = pcall(function () return require(mw.ustring.format('Module:%s', name)) end)
		--require succeeded
		if success then
			ship_table = ship_table[model]
			if not ship_table then
				--create a ShipData with what we have
				ship_table = {_name = name, _suffix = model}
			end
		--require failed
		else
			--create a ShipData with what we have
			ship_table = {_name = name, _suffix = model}
		end
		local ship = ShipData(ship_table)
		if not ships[name] then
			ships[name] = {}
		end
		ships[name][model] = ship
		return ship
	end
end

function Ship:get_module(name)
	if name == nil then
		return nil
	end
	return mw.ustring.format('Module:%s', name)
end

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

return Ship