Module:DetailsTableBuilder

From MetalStorm Wiki
Jump to navigation Jump to search

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

local p = {}
local cargo = mw.ext.cargo

function p.build(frame)
    tables = {
    	planes = {
    		tableName = "Planes",
    		fields = 'name, role, hangarLevel, hitpoints, topSpeed, acceleration, turnRate, idealTurningSpeed, afterburnerFuel',
    		args = { orderBy = 'Planes.hangarLevel' },
    		headers = {
    			metric = "<tr><th>Name</th><th>Role</th><th>Hangar Level</th><th>Hitpoints</th><th>Top Speed (km/h)</th><th>Acceleration (m/s²)</th><th>Turn Rate (°/s)</th><th>Ideal Turning Speed (km/h)</th><th>Afterburner Fuel (s)</th></tr>",
    			imperial = "<tr><th>Name</th><th>Role</th><th>Hangar Level</th><th>Hitpoints</th><th>Top Speed (mph)</th><th>Acceleration (ft/s²)</th><th>Turn Rate (°/s)</th><th>Ideal Turning Speed (mph)</th><th>Afterburner Fuel (s)</th></tr>",
    		},
    	},
    	cannons = {
    		tableName = "Cannons",
    		fields = 'plane, type, maxDamage, idealRange, reticleRange, spinupTime, spread, overheatTime, ammo, armorPiercing',
    		args = { orderBy = 'Cannons.type' },
    		headers = {
    			metric = "<tr><th>Plane</th><th>Type</th><th>Max Damage (DPS)</th><th>Ideal Range (km)</th><th>Reticle Range (km)</th><th>Spin-up Time (s)</th><th>Spread</th><th>Overheat Time (s)</th><th>Ammo</th><th>Armor Piercing</th></tr>",
    			imperial = "<tr><th>Plane</th><th>Type</th><th>Max Damage (DPS)</th><th>Ideal Range (mi)</th><th>Reticle Range (mi)</th><th>Spin-up Time (s)</th><th>Spread</th><th>Overheat Time (s)</th><th>Ammo</th><th>Armor Piercing</th></tr>",
    		},
    	},
    	missiles = {
    		tableName = "Missiles",
    		fields = 'plane, quantity, guidance, rangeType, rangeValue, damage, speed, turnRate, lockAngle, lockTime, blastRadius, rateOfFire',
    		args = { orderBy = 'Missiles.guidance' },
    		headers = {
    			metric = "<tr><th>Plane</th><th>Quantity</th><th>Guidance</th><th>Range Type</th><th>Range (km)</th><th>Damage</th><th>Speed (km/h)</th><th>Turn Rate (°/s)</th><th>Lock Angle (°)</th><th>Lock Time (s)</th><th>Blast Radius (m)</th><th>Rate of Fire (r/s)</th></tr>",
    			imperial = "<tr><th>Plane</th><th>Quantity</th><th>Guidance</th><th>Range Type</th><th>Range (mi)</th><th>Damage</th><th>Speed (mph)</th><th>Turn Rate (°/s)</th><th>Lock Angle (°)</th><th>Lock Time (s)</th><th>Blast Radius (ft)</th><th>Rate of Fire (r/s)</th></tr>",
    		},    		
    	},
    	rockets = {
    		tableName = "Rockets",
    		fields = 'plane, quantity, type, rangeValue, damage, speed, blastRadius, rateOfFire',
    		args = { orderBy = 'Rockets.type' },
    		headers = {
    			metric = "<tr><th>Plane</th><th>Quantity</th><th>Type</th><th>Range (km)</th><th>Damage</th><th>Speed (km/h)</th><th>Blast Radius (m)</th><th>Rate of Fire (r/s)</th></tr>",
    			imperial = "<tr><th>Plane</th><th>Quantity</th><th>Type</th><th>Range (mi)</th><th>Damage</th><th>Speed (mph)</th><th>Blast Radius (ft)</th><th>Rate of Fire (r/s)</th></tr>",
    		},    		
    	},
    }
    
	function interpolate(value, level)
		if tonumber(level) == 1 then
			return value
		else
			return value + level * value / 20
		end
	end
	
	local m_ft = 3.28084
	local km_mi = 0.62137
	
	local function rowBuilder(item, type, system, level)
		if type == 	tables.planes.tableName then
			-- name, role, hangarLevel, hitpoints, topSpeed, acceleration, turnRate, idealTurningSpeed, afterburnerFuel
			return string.format("<tr><td>%s</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%d</td><td>%.1f</td></tr>",
				item.name,
				item.role,
				item.hangarLevel,
				interpolate(item.hitpoints, level),
				system == "metric" and item.topSpeed or item.topSpeed * km_mi,
				system == "metric" and item.acceleration or item.acceleration * m_ft,
				item.turnRate,
				system == "metric" and item.idealTurningSpeed or item.idealTurningSpeed * km_mi,
				item.afterburnerFuel)
		elseif type == 	tables.cannons.tableName then
			-- plane, type, maxDamage, idealRange, reticleRange, spinupTime, spread, overheatTime, ammo, armorPiercing
			return string.format("<tr><td>%s</td><td>%s</td><td>%d</td><td>%.1f</td><td>%.1f</td><td>%.1f</td><td>%s</td><td>%.1f</td><td>%s</td><td>%s</td></tr>",
				item.plane,
				item.type,
				interpolate(item.maxDamage, level),
				system == "metric" and item.idealRange or item.idealRange * km_mi,
				system == "metric" and item.reticleRange or item.reticleRange * km_mi,
				item.spinupTime,
				item.spread,
				item.overheatTime,
				item.ammo,
				item.armorPiercing)
		elseif type == tables.missiles.tableName then
			-- plane, quantity, guidance, rangeType, rangeValue, damage, speed, turnRate, lockAngle, lockTime, blastRadius, rateOfFire
			mw.log(item.damage)
			return string.format("<tr><td>%s</td><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
				item.plane,
				item.quantity,
				item.guidance,
				item.rangeType and item.rangeType or "",
				item.rangeValue and string.format("%.1f", system == "metric" and item.rangeValue or item.rangeValue * km_mi) or "",
				interpolate(item.damage, level),
				system == "metric" and item.speed or item.speed * km_mi,
				item.turnRate,
				item.lockAngle and item.lockAngle or "",
				item.lockTime and item.lockTime or "",
				item.blastRadius and string.format("%.1f", system == "metric" and item.blastRadius or item.blastRadius * m_ft) or "",
				item.rateOfFire and item.rateOfFire or "")
		elseif type == tables.rockets.tableName then
			-- plane, quantity, type, rangeValue, damage, speed, blastRadius, rateOfFire
			return string.format("<tr><td>%s</td><td>%d</td><td>%s</td><td>%.1f</td><td>%d</td><td>%d</td><td>%d</td><td>%.1f</td></tr>",
				item.plane,
				item.quantity,
				item.type,
				system == "metric" and item.rangeValue or item.rangeValue * km_mi,
				interpolate(item.damage, level),
				system == "metric" and item.speed or item.speed * km_mi,
				system == "metric" and item.blastRadius or item.blastRadius * m_ft,
				item.rateOfFire)
		end
	end
	
	selectedTable = tables[frame.args.type] or tables.planes
    local results = cargo.query(selectedTable.tableName, selectedTable.fields, selectedTable.args)
    local system = frame.args.system or "metric"
    local level = frame.args.level or 1
    
    local output = '<table class="wikitable sortable">' .. selectedTable.headers[system]
    for _, item in ipairs(results) do
		output = output .. rowBuilder(item, selectedTable.tableName, system, level)
	end
	output = output .. "</table>"

	return tostring(output)
end

return p