Module:Sandbox/DutyS12345

-- local p = {} local chart = {} local color = {} color = p chart = p

-- basic args function function arguments(origArgs) local args = {}

for k, v in pairs(origArgs) do       if v ~= '' then args[k] = v       end end

return args end

local factions = { "TR", "NC", "VS", "NSO", "NS", }

local WD = require("Module:WeaponData") local weaponList = WD.getWeaponList

function p.print1(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) return string.sub(args[1], 1, 5) end

function p.verifyWeaponList(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) frame = mw.getCurrentFrame local weaponListIn = args[1] or frame:preprocess("") mw.log(weaponListIn) local found = {} local notFound = {} for weaponGroup, weaponGroupList in pairs(weaponList) do		for _, faction in ipairs(factions) do			if weaponGroupList[faction] then for _, weapon in ipairs(weaponGroupList[faction]) do					if string.find(weaponListIn, weapon, 1, true) then found[weapon] = true else notFound[weapon] = true end end end end end local foundSorted = {} for k, v in pairs(found) do		table.insert(foundSorted, k)	end table.sort(foundSorted) local notFoundSorted = {} for k, v in pairs(notFound) do		table.insert(notFoundSorted, k)	end table.sort(notFoundSorted) return "Not found:\n" .. table.concat(notFoundSorted, ", ") .. "\n\nFound:\n" .. table.concat(foundSorted, ", ") end

function p.testCombinedData(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local combinedData = mw.loadData("Module:WeaponData/data") local weaponNameList = {} for k, v in pairs(combinedData) do		table.insert(weaponList, k)	end table.sort(weaponList) return table.concat(weaponList, ", ") end

function p.testModuleTable(frame) local weaponList = mw.loadData("Module:Sandbox/DutyS12345/data1") local result = {} for weaponGroup, weaponGroupList in pairs(weaponList) do		for _, faction in ipairs(factions) do			if weaponGroupList[faction] then for _, weapon in ipairs(weaponGroupList[faction]) do					table.insert(result, weapon) end end end end return table.concat(result, ", ") end

function p.testDataTable(frame) local weaponList = require("Module:WeaponData").getWeaponList local result = {} for weaponGroup, weaponGroupList in pairs(weaponList) do		for _, faction in ipairs(factions) do			if weaponGroupList[faction] then for _, weapon in ipairs(weaponGroupList[faction]) do					table.insert(result, weapon) end end end end return table.concat(result, ", ") end

function color.hex2rgb(hex) hex = hex:gsub("#","") return tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) end

function color.rgb2hsl(r,g,b) local cmax = math.max(r,g,b) local cmin = math.min(r,g,b) local c = cmax - cmin local l = (cmax + cmin) / 2 if c == 0 then return 0, 0, l / 255 end local h	local s = c / (255 - math.abs(2 * l - 255)) if cmax == r then h = 60 * (((g - b) / c) % 6) elseif cmax == g then h = 60 * (((b - r) / c) + 2) elseif cmax == b then h = 60 * (((r - g) / c) + 4) end return h, s, l / 255 end

function color.hsl2rgb(h,s,l) local c = (1 - math.abs(2 * l - 1)) * s	local x = c * (1 - math.abs(((h / 60) % 2) - 1)) local m = l - (c / 2) if h < 60 then return math.floor((c+m)*255+0.5), math.floor((x+m)*255+0.5), math.floor(m*255+0.5) elseif h < 120 then return math.floor((x+m)*255+0.5), math.floor((c+m)*255+0.5), math.floor(m*255+0.5) elseif h < 180 then return math.floor(m*255+0.5), math.floor((c+m)*255+0.5), math.floor((x+m)*255+0.5) elseif h < 240 then return math.floor(m*255+0.5), math.floor((x+m)*255+0.5), math.floor((c+m)*255+0.5) elseif h < 300 then return math.floor((x+m)*255+0.5), math.floor(m*255+0.5), math.floor((c+m)*255+0.5) elseif h < 360 then return math.floor((c+m)*255+0.5), math.floor(m*255+0.5), math.floor((x+m)*255+0.5) end end

function color.hsl2rgb2(h,s,l) local c = (255 - math.abs(510 * l - 255)) * s	local x = c * (1 - math.abs(((h / 60) % 2) - 1)) local m = (255 * l) - (c / 2) if h < 60 then return math.floor(c+m+0.5), math.floor(x+m+0.5), math.floor(m+0.5) elseif h < 120 then return math.floor(x+m+0.5), math.floor(c+m+0.5), math.floor(m+0.5) elseif h < 180 then return math.floor(m+0.5), math.floor(c+m+0.5), math.floor(x+m+0.5) elseif h < 240 then return math.floor(m+0.5), math.floor(x+m+0.5), math.floor(c+m+0.5) elseif h < 300 then return math.floor(x+m+0.5), math.floor(m+0.5), math.floor(c+m+0.5) elseif h < 360 then return math.floor(c+m+0.5), math.floor(m+0.5), math.floor(x+m+0.5) end end

function color.rgb2hex(r,g,b) return string.format("#%x%x%x", r, g, b) end

function color.interpolate(hex1, hex2, num_colors) local colors = {} local h1, s1, l1 = color.rgb2hsl(color.hex2rgb(hex1)) local h2, s2, l2 = color.rgb2hsl(color.hex2rgb(hex2)) local single = false if num_colors == 1 then num_colors = 3 single = true end local h_increment = h2 - h1	if h_increment >= 180 then h_increment = h_increment - 360 elseif h_increment < -180 then h_increment = htest + 360 end h_increment = h_increment / (num_colors - 1) local s_increment = (s2 - s1) / (num_colors - 1) local l_increment = (l2 - l1) / (num_colors - 1) if single then local h_new = h1 + h_increment local s_new = s1 + s_increment local l_new = l1 + l_increment if h_new >= 360 then h_new = h_new - 360 elseif h_new < 0 then h_new = h_new + 360 end table.insert(colors, color.rgb2hex(color.hsl2rgb(h_new, s_new, l_new))) else for i = 0, num_colors - 1 do			local h_new = h1 + i * h_increment local s_new = s1 + i * s_increment local l_new = l1 + i * l_increment if h_new >= 360 then h_new = h_new - 360 elseif h_new < 0 then h_new = h_new + 360 end table.insert(colors, color.rgb2hex(color.hsl2rgb(h_new, s_new, l_new))) end end return colors end

function interpolate(hex1, hex2, num_colors) local colors = require("Dev:Colors") end

function p.weaponDamageComparisonChart(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local Weapon = require("Module:WeaponData/weapon") local weaponList = {} local data = {} local minDamageRange = 0 for _, weaponName in ipairs(args) do		local weapon = Weapon:new(weaponName) table.insert(weaponList, weapon) local tempMinDamageRange = weapon:get("min_damage_range") if tempMinDamageRange > minDamageRange then minDamageRange = tempMinDamageRange end end minDamageRange = minDamageRange + 20 for i, weapon in ipairs(weaponList) do		local tempMaxDamage = weapon:get("max_damage") local tempMinDamage = weapon:get("min_damage") local dataset = { ["label"] = weapon:get("name"), ["data"] = { {["x"] = 0, ["y"] = tempMaxDamage}, {["x"] = weapon:get("max_damage_range"), ["y"] = tempMaxDamage}, {["x"] = weapon:get("min_damage_range"), ["y"] = tempMinDamage}, {["x"] = minDamageRange, ["y"] = tempMinDamage} }		}		table.insert(data, dataset) end return chart.createLineChart(data, "Distance (m)", "Damage", "Damage vs Distance") end function chart.createLineChart(data, xlabel, ylabel, title) local ret = {' ', chart.getLineChartConfig(data, xlabel, ylabel, title), ' '} return table.concat(ret, '') end

function chart.getLineChartConfig(data, xlabel, ylabel, title) local JSON = require("Dev:Json") -- coloring local color1 = "#47a9b8" -- local color2 = "#12ed54" local color2 = "#ed7512" local colorSet = color.interpolate(color1, color2, #data) for i, dataset in ipairs(data) do		dataset["borderColor"] = colorSet[i] dataset["backgroundColor"] = colorSet[i] .. "1a" end local options = { ["elements"] = { ["line"] = { ["showLine"] = true, ["tension"] = 0 },			["point"] = { ["radius"] = 1, ["hoverRadius"] = 3, ["hitRadius"] = 15, ["borderWidth"] = 6 }		},		["scales"] = {} }	if type(xlabel) == "string" then options["scales"]["xAxes"] = else options["scales"]["xAxes"] = end if type(ylabel) == "string" then options["scales"]["yAxes"] = end if type(title) == "string" then options["title"] = { ["display"] = true, ["text"] = title }	end local chartConfig = { ["type"] = "scatter", ["data"] = { ["datasets"] = data },		["options"] = options }	-- local temp = {{x=0,y=1},{x=0.25,y=1},{x=0.5,y=0.5},{x=0.75,y=0},{x=1,y=0}} return JSON.encode(chartConfig) end

function chart.chartConfig(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local func = args[1] for i, v in ipairs(args) do		if i == 1 then else args[i - 1] = v			args[i] = nil end end return (string.gsub(p[func]{args=args}, ' class=".-"', '')) end

function chart.weaponComparisonAccuracyChart(frame)

end

-- function chart.createLineChart(data, xlabel, ylabel, title) function chart.createRadialChart(data)

end

function p.queryData(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local Weapon = require("Module:WeaponData/weapon") local combinedData = mw.loadData("Module:WeaponData/data") local stats = {

}	local results = {} for _, statName in ipairs(stats) do		results[statName] = {} end for k, weaponName in pairs(combinedData) do		local weapon = Weapon:new(weaponName) for _, statName in ipairs(stats) do			results[statName][weapon:get(statName)] = true end end for _, statName in ipairs(stats) do		local sorted = {} for k, v in pairs(results[statName]) do			table.insert(sorted, k)		end table.sort(sorted) results[statName] = sorted end return luaprettyprint(weaponList, ", ") end

function p.prettyprint_test(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local t = mw.loadData("Module:WeaponData/data/Pulsar_VS1") return luaprettyprint(t) end

function luaprettyprint(t, ordering, level) local ret = {} luaprettyprint_h(t, ordering, level or 0, ret, 0) return table.concat(ret, "") end

function luaprettyprint_h(t, ordering, level, ret, retn) retn = retn + 1 ret[retn] = "{\n" local orderedComplete = {} if ordering == nil then else for _, k in ipairs(ordering[1]) do			retn = luaprettyprint_hkv(t, ordering, level + 1, ret, retn, k)			orderedComplete[k] = true end end for k, v in pairs(t) do		if orderedComplete[k] then else retn = luaprettyprint_hkv(t, ordering, level + 1, ret, retn, k)		end end for i = 1,level do		ret[retn + i] = "\t" end retn = retn + level + 1 ret[retn] = "},\n" return retn end

function luaprettyprint_hkv(t, ordering, level, ret, retn, k)	local k_type = type(k) local v_type = type(t[k]) if v_type == "nil" then return retn end for i = 1,level do		ret[retn + i] = "\t" end retn = retn + level if k_type == "number" then retn = retn + 1 ret[retn] = '[' .. k .. '] = '	elseif k_type == "string" then retn = retn + 1 ret[retn] = '["' .. k .. '"] = ' elseif k_type == "nil" then elseif k_type == "table" then elseif k_type == "function" then else end if v_type == "number" then retn = retn + 1 ret[retn] = t[k] .. ',\n' elseif v_type == "string" then retn = retn + 1 ret[retn] = '"' .. t[k] .. '",\n' elseif v_type == "nil" then elseif v_type == "table" then retn = luaprettyprint_h(t[k], ordering and ordering[k] or nil, level, ret, retn) elseif v_type == "function" then else end return retn end

function p.dumpWeaponTemplateCall(frame) -- local args = (frame.args and arguments(frame.args)) or arguments(frame) local args = arguments(frame:getParent.args) local ret = {} local stats = { ["name"] = "Name", -- Image = "image", -- Caption = "caption", ["description"] = "Description", ["cert_cost"] = "Cert Cost", ["seed_cost"] = "Seed Cost", ["a7_cost"] = "A7 Cost", ["merit_cost"] = "Merit Cost", ["dc_cost"] = "SC Cost", ["weapon_type"] = "Weapon Type", ["faction"] = "Empire", ["fire_modes"] = "Fire Modes", ["range"] = "Range",

["move_modifier"] = "Move Speed Multiplier", ["equip_ms"] = "Equip Time", ["damage_head_multiplier"] = "Headshot", ["fire_rate"] = "Fire Rate", ["max_damage"] = "MaxDamage", ["min_damage"] = "MinDamage", ["muzzle_velocity"] = "Velocity", ["short_reload"] = "Reload Speed", ["clip_size"] = "Ammunition", -- ["chamber_time"] = "Chamber Time", -- ["muzzle_velocity"] = "Velocity2", ["hip_accuracy"] = "Hip Accuracy", ["aim_accuracy"] = "Aim Accuracy", -- ["muzzle_velocity"] = "Velocity3", -- ["max_damage"] = "MaxDamage2", -- ["min_damage"] = "MinDamage2", -- ["max_damage"] = "MaxDamage3", -- ["min_damage"] = "MinDamage3", -- [""] = "DamageType", -- [""]		-- [""] = "Heat Reload", -- [""] = "Pellets"

-- [""] = "Heat Ammo" -- [""] = "Vehicle Accuracy" ["recoil_magnitude_min"] = "Vertical Recoil", ["recoil_horizontal_min"] = "Horizontal Recoil", ["recoil_horizontal_tolerance"] = "Horizontal Tolerance", ["recoil_angle_min"] = "Recoil Angle", ["recoil_recovery_rate"] = "Recoil Decrease", ["recoil_first_shot_modifier"] = "First Shot", -- ["Sources"] = -- ["note"] = "Note"

}	local ordering = {{ "id", "name", "description", "cert_cost", "seed_cost", "a7_cost", "merit_cost", "dc_cost", "weapon_type", "faction", "fire_modes", "range", "stats", "move_modifier", "equip_ms", "damage_head_multiplier", "fire_rate", "fire_refire_ms", "max_damage", "max_damage_range", "min_damage", "min_damage_range", "muzzle_velocity", "short_reload", "long_reload", "clip_size", "capacity", "hip_accuracy", "aim_accuracy", "recoil_magnitude_max", "recoil_magnitude_min", "recoil_horizontal_max", "recoil_horizontal_min", "recoil_horizontal_tolerance", "recoil_angle_max", "recoil_angle_min", "recoil_recovery_rate", "recoil_first_shot_modifier", "variants"}, ["hip_accuracy"] = , ["aim_accuracy"] = , }	for k, v in pairs(stats) do		args[k] = tonumber(args[v]) if not args[k] then args[k] = args[v] end args[v] = nil end args["id"] = 0 args["stats"] = {} args["fire_refire_ms"] = 0 if args["short_reload"] and args["short_reload"]:find("/") then args["short_reload"] = args["short_reload"]:gsub("s", "") local i, j = args["short_reload"]:find("/") args["long_reload"] = tonumber(args["short_reload"]:sub(i + 1, -1)) args["short_reload"] = tonumber(args["short_reload"]:sub(1, i - 1)) end if args["max_damage"] then local i, j = args["max_damage"]:find("/") args["max_damage_range"] = tonumber(args["max_damage"]:sub(i + 1, -1)) args["max_damage"] = tonumber(args["max_damage"]:sub(1, i - 1)) end if args["min_damage"] then local i, j = args["min_damage"]:find("/") args["min_damage_range"] = tonumber(args["min_damage"]:sub(i + 1, -1)) args["min_damage"] = tonumber(args["min_damage"]:sub(1, i - 1)) end if args["clip_size"] then local i, j = args["clip_size"]:find("/") args["capacity"] = tonumber(args["clip_size"]:sub(i + 1, -1)) args["clip_size"] = tonumber(args["clip_size"]:sub(1, i - 1)) end if args["hip_accuracy"] then local i, j = args["hip_accuracy"]:find("/") local acc = {} acc["crouch_still"] = tonumber(args["hip_accuracy"]:sub(1, j - 1)) _, j = args["hip_accuracy"]:find("/", j + 1) acc["crouch_move"] = tonumber(args["hip_accuracy"]:sub(i + 1, j - 1)) i = j		_, j = args["hip_accuracy"]:find("/", j + 1) acc["stand_still"] = tonumber(args["hip_accuracy"]:sub(i + 1, j - 1)) i = j		_, j = args["hip_accuracy"]:find("/", j + 1) acc["stand_move"] = tonumber(args["hip_accuracy"]:sub(i + 1, j - 1)) acc["bloom_per_shot"] = tonumber(args["hip_accuracy"]:sub(j + 1, -1)) args["hip_accuracy"] = acc end if args["aim_accuracy"] then local i, j = args["aim_accuracy"]:find("/") local acc = {} acc["crouch_still"] = tonumber(args["aim_accuracy"]:sub(1, j - 1)) _, j = args["aim_accuracy"]:find("/", j + 1) acc["crouch_move"] = tonumber(args["aim_accuracy"]:sub(i + 1, j - 1)) i = j		_, j = args["aim_accuracy"]:find("/", j + 1) acc["stand_still"] = tonumber(args["aim_accuracy"]:sub(i + 1, j - 1)) i = j		_, j = args["aim_accuracy"]:find("/", j + 1) acc["stand_move"] = tonumber(args["aim_accuracy"]:sub(i + 1, j - 1)) acc["bloom_per_shot"] = tonumber(args["aim_accuracy"]:sub(j + 1, -1)) args["aim_accuracy"] = acc end if type(args["recoil_magnitude_min"]) == "number" then args["recoil_magnitude_max"] = args["recoil_magnitude_min"] else if args["recoil_magnitude_min"] then local i, j = args["recoil_magnitude_min"]:find("/") args["recoil_magnitude_max"] = tonumber(args["recoil_magnitude_min"]:sub(i + 1, -1)) args["recoil_magnitude_min"] = tonumber(args["recoil_magnitude_min"]:sub(1, i - 1)) end end if type(args["recoil_horizontal_min"]) == "number" then args["recoil_horizontal_max"] = args["recoil_horizontal_min"] else if args["recoil_horizontal_min"] then local i, j = args["recoil_horizontal_min"]:find("/") args["recoil_horizontal_max"] = tonumber(args["recoil_horizontal_min"]:sub(i + 1, -1)) args["recoil_horizontal_min"] = tonumber(args["recoil_horizontal_min"]:sub(1, i - 1)) end end if args["recoil_angle_min"] then local i, j = args["recoil_angle_min"]:find("/") args["recoil_angle_max"] = tonumber(args["recoil_angle_min"]:sub(i + 1, -1)) args["recoil_angle_min"] = tonumber(args["recoil_angle_min"]:sub(1, i - 1)) end args["variants"] = {} -- local options = { -- 	["nil"] = true, -- }	return ' ' .. luaprettyprint(args, ordering, 1 --, options) .. ' ' end

function p.getListOfVariants(frame) local args = (frame.args and arguments(frame.args)) or arguments(frame) local ret = {} local ordering = – local combinedData = mw.loadData("Module:WeaponData/data") local Weapon = require("Module:WeaponData/weapon") local sorted = {} for k, v in pairs(weaponList) do		table.insert(sorted, k)	end table.sort(sorted) for _, weaponClass in ipairs(sorted) do		for _, faction in ipairs(factions) do			for _, weaponName in ipairs(weaponList[weaponClass][faction]) do				if combinedData[weaponName] then local variants = Weapon:new(weaponName):get("variants") if variants then local c = 0 for _, v in pairs(variants) do							c = c + 1 for _, v in pairs(variants) do								if v["idx"] == c then local variantName = v["name"] table.insert(ordering[1], variantName) ret[variantName] = weaponName end end end end end end end end local prettystring = luaprettyprint(ret, ordering, 0) prettystring = prettystring:gsub('%["([- %w]-"[- %w]-"[- %w]-)"%]', '[\'%1\']') prettystring = "local variants = " .. prettystring:gsub(",\n$", "\n") return' ' .. prettystring .. ' ' end

return p