• 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
com>Ckwng
(Undo revision 342923 by Ckwng (talk) On second thought, not needed)
Line 1: Line 1:
local BaseData = require('Module:BaseData')
+
local Ship = {}
 +
local ships = {}
  
local Ship = BaseData{
+
local ShipData
_ships = {},
 
_data_class = false,
 
}
 
  
 
function Ship:_prepareShipData()
 
function Ship:_prepareShipData()
self._data_class = self._data_class or require('Module:ShipData')
+
ShipData = ShipData or require('Module:ShipData')
 
end
 
end
  
Line 17: Line 15:
 
self:_prepareShipData()
 
self:_prepareShipData()
 
if name == nil then
 
if name == nil then
return self._data_class()
+
return ShipData()
 
end
 
end
 
if model == nil then
 
if model == nil then
Line 26: Line 24:
 
end
 
end
 
--check if we already have it
 
--check if we already have it
if self._ships[name] and self._ships[name][model] then
+
if ships[name] and ships[name][model] then
return self._ships[name][model]
+
return ships[name][model]
 
else
 
else
local ship = self._data_class(self:get_table(name, model))
+
local ship = ShipData(self:get_table(name, model))
if not self._ships[name] then
+
if not ships[name] then
self._ships[name] = {}
+
ships[name] = {}
 
end
 
end
self._ships[name][model] = ship
+
ships[name][model] = ship
 
return ship
 
return ship
 
end
 
end
Line 93: Line 91:
 
end
 
end
 
return mw.ustring.format('Module:%s', name)
 
return mw.ustring.format('Module:%s', name)
end
 
 
function Ship:extend(data)
 
data = data or {}
 
setmetatable(data, data)
 
data.__index = self
 
data.__call = self.__call
 
return data
 
 
end
 
end
  

Revision as of 08:40, 28 December 2015

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

local Ship = {}
local ships = {}

local ShipData

function Ship:_prepareShipData()
	ShipData = ShipData or require('Module:ShipData')
end

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

function Ship:create(name, model)
	self:_prepareShipData()
	if name == nil then
		return ShipData()
	end
	if model == nil then
		name, model = self:process_ship_key(name)
	end
	if not model then
		model = ""
	end
	--check if we already have it
	if ships[name] and ships[name][model] then
		return ships[name][model]
	else
		local ship = ShipData(self:get_table(name, model))
		if not ships[name] then
			ships[name] = {}
		end
		ships[name][model] = ship
		return ship
	end
end

function Ship:process_ship_key_as_reference(reference, base)
	name, model = self:process_ship_key(reference)
	if #name == 0 then
		name = base
	end
	return name, model
end

function Ship:create_from_reference(reference, base)
	name, model = self:process_ship_key(reference)
	if #name == 0 then
		name = base:base_name()
	end
	return self(name, model)
end

function Ship:get_table(name, model)
	--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}
		elseif type(ship_table) == "string" then
			ship_table = self:get_table(self:process_ship_key_as_reference(ship_table, name))
		end
	--require failed
	else
		--create a ShipData with what we have
		ship_table = {_name = name, _suffix = model}
	end
	return ship_table
end

function Ship:process_ship_key(ship_key)
	local split = mw.ustring.find(ship_key, '/')
	local ship_base_name, ship_suffix
	if split == nil then
		ship_base_name = ship_key
	else
		ship_base_name = split - 1 > 0 and mw.ustring.sub(ship_key, 1, split - 1) or ''
		ship_suffix = mw.ustring.sub(ship_key, split + 1, -1)
	end
	return ship_base_name, ship_suffix
end

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

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

return Ship