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

Module:EnemyShip

From Kancolle Wiki
Revision as of 20:54, 14 April 2015 by Nanamin (talk | contribs) (Created page with "local EnemyShip = {} local ships = {} local EnemyShipData = require('Module:EnemyShipData') function EnemyShip:get(stat, name, model) return self:create(name, model)[stat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local EnemyShip = {}
local ships = {}
 
local EnemyShipData = require('Module:EnemyShipData')
 
function EnemyShip:get(stat, name, model)
	return self:create(name, model)[stat]()
end
 
function EnemyShip:create(name, model)
	if name == nil then
		return EnemyShipData()
	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
				--Instantiate EnemyShipData with what we have
				ship_table = {_name = name, _suffix = model}
			end
		--require failed
		else
			--Instantiate EnemyShipData with what we have
			ship_table = {_name = name, _suffix = model}
		end
		local ship = EnemyShipData(ship_table)
		if not ships[name] then
			ships[name] = {}
		end
		ships[name][model] = ship
		return ship
	end
end
 
function EnemyShip:get_module(name)
	if name == nil then
		return nil
	end
	return mw.ustring.format('Module:%s', name)
end
 
EnemyShip.__call = EnemyShip.create
setmetatable(EnemyShip, EnemyShip)
 
return EnemyShip