Changes

delegation feature
Line 19: Line 19:  
return ships[name][model]
 
return ships[name][model]
 
else
 
else
--Catch a failed require
+
local ship = ShipData(self:get_table(name, 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] = {}
Line 40: Line 26:  
return ship
 
return ship
 
end
 
end
 +
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(ship_table))
 +
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 = mw.ustring.sub(ship_key, 1, split - 1)
 +
ship_suffix = mw.ustring.sub(ship_key, split + 1, -1)
 +
end
 +
return ship_base_name, ship_suffix
 
end
 
end
  
Anonymous user