Difference between revisions of "Module:Gear"

From BTAWiki
Jump to navigation Jump to search
Line 19: Line 19:
 
--
 
--
 
-- schema
 
-- schema
--
+
--  
 
core.schema = {}
 
core.schema = {}
  
Line 251: Line 251:
 
}
 
}
  
 +
core.schema.ammunition = {
 +
  table = 'Ammunition',
 +
  parent_table = core.schema.gear,
 +
  fields = {
 +
    {
 +
      field = 'Capacity',
 +
      type = 'Integer',
 +
    },
 +
  }
 +
}
  
 
--
 
--
Line 274: Line 284:
  
 
   if schema.parent_table ~= nil then
 
   if schema.parent_table ~= nil then
 +
    data[id_field.field] = tpl_args[id_field.field]
 
     core.cargo.store(frame, schema.parent_table, tpl_args)
 
     core.cargo.store(frame, schema.parent_table, tpl_args)
 
   end
 
   end
Line 331: Line 342:
 
p.table_jumpjet = core.cargo.declare(core.schema.jumpjet)
 
p.table_jumpjet = core.cargo.declare(core.schema.jumpjet)
 
p.table_weapon = core.cargo.declare(core.schema.weapon)
 
p.table_weapon = core.cargo.declare(core.schema.weapon)
 +
p.table_ammunition = core.cargo.declare(core.schema.ammunition)
  
 
function p.heatsink(frame)  
 
function p.heatsink(frame)  
Line 387: Line 399:
 
   core.cargo.store(frame, core.schema.weapon, tpl_args)
 
   core.cargo.store(frame, core.schema.weapon, tpl_args)
 
   return core.format_table('Weapon', tpl_args)
 
   return core.format_table('Weapon', tpl_args)
 +
end
 +
 +
function p.ammunition(frame)
 +
  local tpl_args = getArgs(frame, {parentFirst = true})
 +
 +
  core.cargo.store(frame, core.schema.weapon, tpl_args)
 +
  return core.format_table('Ammunition', tpl_args)
 
end
 
end
  
 
-- always return p
 
-- always return p
 
return p
 
return p

Revision as of 04:02, 16 January 2021

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

-- Module:Gear handles the myriad of gear types.

-- start with p, the package we expose
local p = {}

-- core is lua-only methods, not for use in the public intertace
local core = {}

local id_field = {
  field = 'Id',
  type = 'String'
}

--
-- imports
--
local getArgs = require('Module:Arguments').getArgs

--
-- schema
-- 
core.schema = {}

core.schema.gear = {
  table = 'Gear',
  fields = {
    id_field,
    {
      field = 'Cost',
      -- type defines the type of the field
      type = 'Integer',
    },{
      field = 'Rarity',
      type = 'Integer',
    },{
      field = 'Purchasable',
      type = 'Boolean',
    },{
      field = 'Manufacturer',
      type = 'String',
    },{
      field = 'Model',
      type = 'String',
    },{
      field = 'UIName',
      type = 'String',
    },{
      field = 'Name',
      type = 'String',
    },{
      field = 'Details',
      -- Details use the Text type, which is unindexed and intended for
      -- longer-form data.
      type = 'Text',
    },{
      field = 'Icon',
      type = 'String',
    },{
      field = 'ComponentType',
      type = 'String',
    },{
      field = 'ComponentSubType',
      type = 'String',
    },{
      field = 'InventorySize',
      type = 'Integer',
    },{
      field = 'Tonnage',
      type = 'Float',
    },{
      field = 'AllowedLocations',
      type = 'List (,) of String',
    },{
      field = 'DisallowedLocations',
      type = 'List (,) of String',
    },{
      field = 'BattleValue',
      type = 'Integer',
    },
  },
}

core.schema.heatsink = {
  table = 'Heatsinks',
  parent_table = core.schema.gear,
  fields = {
    {
      field = 'DissipationCapcity',
      type = 'Integer',
    },{
      field = 'EngineSlot',
      type = 'Boolean',
    },{
      field = 'EnginePart',
      type = 'Boolean',
    },
  },
}

core.schema.cooling = {
  table = 'Cooling',
  parent_table = core.schema.heatsink,
  fields = {
    {
      field = 'HeatsinkDefID',
      type = 'String',
    },
  },
}

core.schema.engineheatblock = {
  table = 'EngineHeatBlocks',
  parent_table = core.schema.heatsink,
  fields = {
    {
      field = 'HeatsinkCount',
      type = 'Integer',
    },
  },
}

core.schema.engineshield = {
  table = 'EngineShields',
  parent_table = core.schema.heatsink,
  fields = {
    {
      field = 'ReservedSlots',
      type = 'Integer',
    },{
      field = 'EngineFactor',
      type = 'Float',
    },
  },
}

core.schema.enginecore = {
  parent = core.schema.heatsink,
  table = 'EngineCore',
  fields = {
    {
      field = 'Rating',
      type = 'Integer',
    },
  },
}

core.schema.upgrade = {
  table = 'UpgradeDef',
  parent_table = core.schema.gear,
  fields = {},
}

core.schema.jumpjet = {
  table = 'JumpJets',
  parent_table = core.schema.gear,
  fields = {
    {
      field = 'MinTonnage',
      type = 'Integer',
    },{
      field = 'MaxTonnage',
      type = 'Integer',
    },{
      field = 'JumpCapcity',
      type = 'Float',
    },
  },
}

core.schema.weapon = {
  table = 'Weapons',
  parent_table = core.schema.gear,
  fields = {
    {
      field = 'Category',
      type = 'String',
    },{
      field = 'Type',
      type = 'String',
    },{
      field = 'WeaponSubType',
      type = 'String',
    },{
      field = 'MinRange',
      type = 'Integer',
    },{
      field = 'MaxRange',
      type = 'Integer',
    },{
      field = 'RangeSplit',
      type = 'List (,) of Integer',
    },{
      field = 'AmmoCategory',
      type = 'String',
    },{
      field = 'StartingAmmoCapacity',
      type = 'Integer',
    },{
      field = 'HeatGenerated',
      type = 'Integer',
    },{
      field = 'Damage',
      type = 'Integer',
    },{
      field = 'OverheatedDamageMultiplier',
      type = 'Float',
    },{
      field = 'EvasiveDamageMultiplier',
      type = 'Float',
    },{
      field = 'EvasivePipsIgnored',
      type = 'Integer',
    },{
      field = 'DamageVariance',
      type = 'Float',
    },{
      field = 'HeatDamage',
      type = 'Integer',
    },{
      field = 'AccuracyModifier',
      type = 'Float',
    },{
      field = 'CriticalChanceMultiplier',
      type = 'Float',
    },{
      field = 'AOECapable',
      type = 'Boolean',
    },{
      field = 'IndirectFireCapable',
      type = 'Boolean',
    },{
      field = 'RefireModifier',
      type = 'Integer',
    },{
      field = 'ShotsWhenFired',
      type = 'Integer',
    },{
      field = 'ProjectilesPerShot',
      type = 'Integer',
    },{
      field = 'AttackRecoil',
      type = 'Integer',
    },{
      field = 'Instability',
      type = 'Integer',
    },{
      field = 'WeaponEffectID',
      type = 'String',
    },
  },
}

core.schema.ammunition = {
  table = 'Ammunition',
  parent_table = core.schema.gear,
  fields = {
    {
      field = 'Capacity',
      type = 'Integer',
    },
  }
}

--
-- Cargo
--
-- Handles the connection between this module and the Cargo database.
core.cargo = {}

-- core.cargo.cargo_store stores the data for the current schema's fields, and
-- then recursively stores the data of the parent tables' fields.
function core.cargo.store(frame, schema, tpl_args)
  frame:expandTemplate{title=string.format('Template:Gear/cargo/attach/%s', schema.table), args={}}

  local data = {}
  data._table = schema.table

  for _, field in ipairs(schema.fields) do
    local arg = tpl_args[field.field]
    if arg ~= nil then
      data[field.field] = arg
    end
  end

  if schema.parent_table ~= nil then
    data[id_field.field] = tpl_args[id_field.field]
    core.cargo.store(frame, schema.parent_table, tpl_args)
  end

  frame:callParserFunction('#cargo_store:', data)
end

-- core.cargo.cargo_declare declares a given table. Additionally, if the table is
-- has a parent table, we add an Id field.
function core.cargo.declare(schema)
  return function(frame)
    local dcl_args = {}

    dcl_args._table = schema.table

    for _, field in ipairs(schema.fields) do
      dcl_args[field.field] = field.type
    end
    if schema.parent_table ~= nil then
      dcl_args[id_field.field] = id_field.type
    end
    frame:callParserFunction('#cargo_declare:', dcl_args)
  end
end

--
-- Helpers
-- 
function core.format_table(title, tpl_args)
  local rawTable = mw.html.create('table')
  rawTable:addClass('wikitable')

  rawTable:tag('tr'):tag('th'):attr('colspan', '2'):wikitext(title)

  for param, arg in pairs(tpl_args) do
    rawTable:tag('tr')
      :tag('th'):wikitext(param):done()
      :tag('td'):wikitext(arg)
  end

  return rawTable
end

--
-- Templates
--
-- these functions define templates.
--

p.table_gear = core.cargo.declare(core.schema.gear)
p.table_heatsink = core.cargo.declare(core.schema.heatsink)
p.table_cooling = core.cargo.declare(core.schema.cooling)
p.table_engineheatblock = core.cargo.declare(core.schema.engineheatblock)
p.table_engineshield = core.cargo.declare(core.schema.engineshield)
p.table_enginecore = core.cargo.declare(core.schema.enginecore)
p.table_upgrade = core.cargo.declare(core.schema.upgrade)
p.table_jumpjet = core.cargo.declare(core.schema.jumpjet)
p.table_weapon = core.cargo.declare(core.schema.weapon)
p.table_ammunition = core.cargo.declare(core.schema.ammunition)

function p.heatsink(frame) 
  local tpl_args = getArgs(frame, {parentFirst = true})
  core.cargo.store(frame, core.schema.heatsink, tpl_args)

  return core.format_table('Heat Sink', tpl_args)
end

function p.cooling(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})
  core.cargo.store(frame, core.schema.cooling, tpl_args)

  return core.format_table('Cooling', tpl_args)
end


function p.engineheatblock(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})
  core.cargo.store(frame, core.schema.engineheatblock, tpl_args)

  return core.format_table('Engine Heat Block', tpl_args)
end

function p.engineshield(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})
  core.cargo.store(frame, core.schema.engineshield, tpl_args)

  return core.format_table('Engine Shield', tpl_args)
end

function p.enginecore(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})
  core.cargo.store(frame, core.schema.enginecore, tpl_args)

  return core.format_table('Engine Core', tpl_args)
end

function p.upgrade(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})

  core.cargo.store(frame, core.schema.upgrade, tpl_args)
  return core.format_table('Upgrade', tpl_args)
end

function p.jumpjet(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})

  core.cargo.store(frame, core.schema.jumpjet, tpl_args)
  return core.format_table('Jump Jet', tpl_args)
end

function p.weapon(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})

  core.cargo.store(frame, core.schema.weapon, tpl_args)
  return core.format_table('Weapon', tpl_args)
end

function p.ammunition(frame)
  local tpl_args = getArgs(frame, {parentFirst = true})

  core.cargo.store(frame, core.schema.weapon, tpl_args)
  return core.format_table('Ammunition', tpl_args)
end

-- always return p
return p