FANDOM


--Module:Weapon Upgrades
--Table
--[[THIS SCRIPT IS LIVE. DO NOT EDIT IT DIRECTLY.
    Make sure to also deploy changes to Module:Weapon_Upgrades_Titan
    First use its dedicated workspace to test the changes:
 
    Module:Weapon Upgrade
]]
local p = {}
 
--Constants___________________________________________________________________
local level_cap=12
local ico="AgIcon.png"
 
local error_msg={"{|style=\"width:100%;text-align:center;font-weight:bold;font-size:200%;line-height:1.25;border:3px dotted orange\" id=\"error\"\n|colspan=\"2\" style=\"color:red;\"|ERROR\n|-\n|colspan=\"2\" style=\"color:red\"|Table Could Not Be Generated\n|-\n|style=\"color:yellow;\"|Reason:\n|style=\"text-align:left;color:white;\"|",
	"\n|}\n",
	mode = "missing: <ruby><u>weapon-mode</u><rt style=\"color:magenta;\">string</rt>",
	mode_invalid = "Invalid mode. Must be one of the following:\n|-\n|colspan=\"2\" style=\"text-align:center;color:cyan;\"|<u>normal</u> <u>accelerated</u> <u>rwf</u>/<u>fwr</u> <u>charge</u> <u>burst</u> <u>constant</u>",
	mk2 = "missing: <ruby><u>mark-2</u><rt style=\"color:magenta;\">boolean</rt></ruby>",
	base_level = "missing: <ruby><u>base-level</u><rt style=\"color:magenta;\">number</rt></ruby>",
	rounds = "missing: <ruby><u>rounds</u><rt style=\"color:magenta;\">number</rt></ruby>",
	cost_total = "missing: <ruby><u>total-upgrade-cost</u><rt style=\"color:magenta;\">string</rt></ruby>",
	time_total = "missing: <ruby><u>total-upgrade-time</u><rt style=\"color:magenta;\">string</rt></ruby>",
--Normal
	time_cyc = "missing: <ruby><u>cycle-time</u><rt style=\"color:magenta;\">number</rt></ruby>",
	time_reload = "missing: <ruby><u>reload-time</u><rt style=\"color:magenta;\">number</rt></ruby>",
--accelerated
	rof_low = "missing: <ruby><u>initial-rate-of-fire</u><rt style=\"color:magenta;\">number</rt></ruby>",
	rof_high = "missing: <ruby><u>rate-of-fire</u><rt style=\"color:magenta;\">number</rt></ruby>",
--rwf
	time_burst = "missing: <ruby><u>burst-time</u><rt style=\"color:magenta;\">number</rt></ruby>",
	rounds_burst = "missing: <ruby><u>burst-rounds</u><rt style=\"color:magenta;\">number</rt></ruby>",
--charge
	time_charge = "missing: <ruby><u>charge-time</u><rt style=\"color:magenta;\">number</rt></ruby>",
--level
	level = {"missing: <ruby><u>level-",
		_time = "-time</u><rt style=\"color:magenta;\">string</rt></ruby>",
		_cost = "-cost</u><rt style=\"color:magenta;\">string</rt></ruby>",
		_dmg = "-damage</u><rt style=\"color:magenta;\">number</rt></ruby>",
	}
}
 
 
---------------------------BUILD FUNCTION---------------------------
function p.Upgrade(frame)
	--Import Arguments
	--This call is necessary to read in data from the page
	data = frame:getParent().args
 
	--Read in for all weapons, pass to sub-functions
	--just a bunch of fetch requests, storing them locally
	--All of these exist for every weapon, regardless of mode
	mode = pfetch("weapon-mode") --the firing mode of the weapon
	if not mode then return error_msg[1] .. error_msg["mode"] .. error_msg[2]  --failsafe
	else mode = string.lower(mode) end
 
	mk2 = pfetch("mark-2") -- is this mark-2?
	if not mk2 then return error_msg[1] .. error_msg["mk2"] .. error_msg[2]
	else mk2 = Logic(mk2) end --process boolean after failsafe (interferes if done first)
 
	level_base = pfetch("base-level")--check error
	if not level_base then return error_msg[1] .. error_msg["level_base"] .. error_msg[2]
	else level_base = formatnum(level_base) end --failsafe
 
	rounds = pfetch("rounds") --the weapon's clip size
	if not rounds then return error_msg[1] .. error_msg["rounds"] .. error_msg[2]
	else rounds = formatnum(rounds) end --failsafe
 
	cost_total = pfetch_str("total-upgrade-cost") --total upgrade cost
	if cost_total == nil then return error_msg[1] .. error_msg["cost_total"] .. error_msg[2]
	elseif cost_total == "" then cost_total = "\'\'Unknown\'\'" end -- fail to "Unknown", won't break program
 
	time_total = pfetch_str("total-upgrade-time") --total upgrade time
	if time_total == nil then return error_msg[1] .. error_msg["time_total"] .. error_msg[2]
	elseif time_total == "" then time_total = "\'\'Unknown\'\'" end -- fail to "Unknown", won't break program
 
	--Create output variable
	--Leave it blank for now
	local tab = ""
 
	--Mode select
	--Modes are divided according to firing mechanics
	--While all 6 modes listed have basically Identical code, they were divided here because the necessary if statments would make the thing an unreadable mess.
 
	--Normal mode
	if mode == "normal" then
		--read in mode-specific values
		--more fetch requests, mode-specific this time
		local time_cyc = pfetch("cycle-time") --cycle time
		if not time_cyc then return error_msg[1] .. error_msg["time_cyc"] .. error_msg[2]
		else time_cyc = formatnum(time_cyc) end --failsafe and conversion
 
 
		local time_reload = pfetch("reload-time") --time not firing
		if not time_reload then return error_msg[1] .. error_msg["time_reload"] .. error_msg[2]
		else time_reload = formatnum(time_reload) end --failsafe and conversion
 
		local time_unload = time_cyc - time_reload --generated value, cannot fail
 
		--get Heading
		--using a function to return a constant string. See Header functions section below.
		local h = header_normal()
 
		--Write purchase heading and purchase table header to output
		--This is the first major portion of table construction. The ".." operator concatenates the strings together.
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		--level 1 is done separately from the rest due to mk2 having a different format
		--For a small handful of weapons, level 5 is the starting level. These still loop to Mk2 level 1.
		local r = level_data(level_base)
 
		-- Run Calc
		--this function call does all the math. The output of a lua function is always a multi-type array, hence the element selector.
		local c = calc_normal(r[3],rounds,time_cyc,time_unload)
 
		--Write purchase table to output
		--Take the calculated values and add them to the table. the "\n|" characters are wikiText, as tab has to be a table in wikitext for  injecton into the page.
		tab = tab..r[1].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n|}\n"
 
		--Test Mk2, add notice
		--This is the extra message for mk2 weapons about needing to max level one first
		if mk2 then
			tab = tab..Rank_up_msg()
		end
 
		--Write Header for main table
		--Header for the second section of the table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_normal(r[3],rounds,time_cyc,time_unload)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n"
 
 
			--for all levels except final, end row
			--This adds a row end marker into the text flow. Wikitext stuff. On the last row it puts in the table end and a new line instead.
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
	--From here, skip the next six sections, the only real difference is a change in the math that needed per weapon type. See my blog.
	--User_blog:Piisfun/How_DPS_is_calculated_for_various_weapons
 
 
 
	--Accelerated mode
	--for dealing with the speed up of machine guns and autocannons
	elseif mode == "accelerated" then
		--read in mode-specific values
		local rof_low = pfetch("initial-rate-of-fire")
		if not rof_low then return error_msg[1] .. error_msg["rof_low"] .. error_msg[2]
		else rof_low = formatnum(rof_low) end --failsafe and conversion
 
		local rof_high = pfetch("rate-of-fire")
		if not rof_high then return error_msg[1] .. error_msg["rof_high"] .. error_msg[2]
		else rof_high = formatnum(rof_high) end --failsafe and conversion
 
		--get Heading
		local h = header_accel()
 
		--Write purchase heading and purchase table header to output
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		local r = level_data(level_base)
 
		-- Run Calc
		local c = calc_accel(r[3],rounds,rof_low,rof_high)
 
		--Write purchase table to output
		tab = tab..r[1].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n|}\n"
 
		--Test Mk2, add notice
		if mk2 then
			tab= tab..Rank_up_msg()
		end
 
		--Write Header for main table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_accel(r[3],rounds,rof_low,rof_high)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n"
 
			--for all levels except final, end row
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
 
 
	--FWR mode
	--Also known as RWF, or Reload While Firing
	elseif (mode == "fwr" or mode == "rwf") then
		--read in mode-specific values
		local time_burst = pfetch("burst-time")
		if not time_burst then return error_msg[1] .. error_msg["time_burst"] .. error_msg[2]
		else time_burst = formatnum(time_burst) end --failsafe and conversion
 
		local time_reload = pfetch("reload-time")
		if not time_reload then return error_msg[1] .. error_msg["time_reload"] .. error_msg[2]
		else time_reload = formatnum(time_reload) end --failsafe and conversion
 
		local rounds_burst = pfetch("burst-rounds")
		if not rounds_burst then return error_msg[1] .. error_msg["rounds_burst"] .. error_msg[2]
		else rounds_burst = formatnum(rounds_burst) end --failsafe and conversion
 
		--get Heading
		local h = header_fwr()
 
		--Write purchase heading and purchase table header to output
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		local r = level_data(level_base)
 
		-- Run Calc
		local c = calc_fwr(r[3],rounds,rounds_burst,time_burst,time_reload)
 
		--Write purchase table to output
		tab = tab..r[1].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n|}\n"
 
		--Test Mk2, add notice
		if mk2 then
			tab= tab..Rank_up_msg()
		end
 
		--Write Header for main table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_fwr(r[3],rounds,rounds_burst,time_burst,time_reload)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n"
 
			--for all levels except final, end row
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
 
 
	--Charge mode
	--Mainly for energy snipers
	elseif mode == "charge" then
		--read in mode-specific values
		local time_reload = pfetch("reload-time")
		if not time_reload then return error_msg[1] .. error_msg["time_reload"] .. error_msg[2]
		else time_reload = formatnum(time_reload) end --failsafe and conversion
 
		local time_charge = pfetch("charge-time")
		if not time_charge then return error_msg[1] .. error_msg["time_charge"] .. error_msg[2]
		else time_charge = formatnum(time_charge) end --failsafe and conversion
 
		--get Heading
		local h = header_charge()
 
		--Write purchase heading and purchase table header to output
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		local r = level_data(level_base)
 
		-- Run Calc
		local c = calc_charge(r[3],rounds,time_reload,time_charge)
 
		--Write purchase table to output
		tab = tab..r[1].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n|\n"..c[4].."\n|}\n"
 
		--Test Mk2, add notice
		if mk2 then
			tab= tab..Rank_up_msg()
		end
 
		--Write Header for main table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_charge(r[3],rounds,time_reload,time_charge)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|"..c[3].."\n|"..c[4].."\n"
 
			--for all levels except final, end row
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
 
 
	--Burst mode
	--Physical Snipers and Aphid/Vortex/Thermite. Also Spiral.
	elseif mode == "burst" then
		--read in mode-specific values
		local time_reload = pfetch("reload-time")
		if not time_reload then return error_msg[1] .. error_msg["time_reload"] .. error_msg[2]
		else time_reload = formatnum(time_reload) end --failsafe and conversion
 
		--get Heading
		local h = header_burst()
 
		--Write purchase heading and purchase table header to output
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		local r = level_data(level_base)
 
		-- Run Calc
		local c = calc_burst(r[3],rounds,time_reload)
 
		--Write purchase table to output
		tab = tab..r[1].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n|}\n"
 
		--Test Mk2, add notice
		if mk2 then
			tab= tab..Rank_up_msg()
		end
 
		--Write Header for main table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_burst(r[3],rounds,time_reload)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c[1].."\n|"..c[2].."\n"
 
			--for all levels except final, end row
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
 
	--Constant mode
	--Because Magnum and Atomizer exist.
	elseif mode == "constant" then
		--read in mode-specific values
		local time_cyc = pfetch("cycle-time")
		if not time_cyc then return error_msg[1] .. error_msg["time_cyc"] .. error_msg[2]
		else time_cyc = formatnum(time_cyc) end --failsafe and conversion
 
		--get Heading
		local h = header_const()
 
		--Write purchase heading and purchase table header to output
		tab = "===Purchase Information===\n"..h[1]..level_base.."\n|"
 
		--Retrieve Statisics for base level
		local r = level_data(level_base)
 
		-- Run Calc
		local c = calc_const(r[3],time_cyc)
 
		--Write purchase table to output
	   tab = tab..r[1].."\n|"..r[3].."\n|"..c.."\n|}\n"
 
		--Test Mk2, add notice
		if mk2 then
			tab= tab..Rank_up_msg()
		end
 
		--Write Header for main table
		tab = tab.."===Upgrade Information===\n"..h[2]
 
		--Loop accross remaining levels, and write each
		for n = (level_base + 1),level_cap do
			--read in statistics and run calc
			r = level_data(n)
			c = calc_const(r[3],time_cyc)
 
			--Write to output
			tab = tab.."!"..n.."\n|"..r[1].."\n|"..r[2].."\n|"..r[3].."\n|"..c.."\n"
 
			--for all levels except final, end row
			if n == level_cap then
				tab = tab.."|}\n"
			--otherwise start new row
			else
				tab = tab.."|-\n"
			end
		end
 
 
	--Error mode
	--This is only shown if somebody puts in a bad mode selector. It is a lot more descriptive than just getting "Script Error"
	else
		--what to return if the mode selector is invalid
		return error_msg[1] .. error_msg["mode_invalid"] .. error_msg[2]
 
	end
 
 
	--Create totals table
	tab = tab..header_totals()..cost_total.."\n|"..time_total.."\n|}\n"
 
 
	--DEBUG OUTPUT, ONLY ENABLE IF THERE IS AN ERROR
	--This dumps the table source to the page, rather than the formatted table
	--return "<nowiki>"..tab.."</nowiki>"
 
	--Normal output
	return tab
 
end
 
---------------------------FETCH FUNCTIONS--------------------------
--This function retrieves level-specific data, provided it is given
--the target level
function level_data(level)
	--convert input in a fixed way
	level = string.format( "%02d", level)
 
	--Retrive Statistics from template
 
	--The upgrade cost for this level
	local cost = pfetch("level-"..level.."-cost")
	if not cost then cost = "''unknown''" end --failsafe: return Unkown
 
	--The upgrade time for this level
	local time_upgrade = pfetch("level-"..level.."-time")
	if not time_upgrade then time_upgrade="''unkown''" end --failsafe: return Unkown
 
	--The damage output at this level
	local dmg = pfetch("level-"..level.."-damage")
	if not dmg then dmg = 0
		else --failsafe: return zero. prevents breakage.
		dmg = formatnum(dmg) end
 
	return {cost, time_upgrade, dmg}
end
 
 
------------------------CALCULATOR FUNCTIONS------------------------
--SEE BLOG.  User_blog:Piisfun/How_DPS_is_calculated_for_various_weapons
 
--Normal mode calculator
function calc_normal(dmg,rounds,time_cyc,time_unload)
	local dmg_cyc = round(dmg * rounds)
	local dps_burst = round(dmg_cyc / time_unload)
	local dps_cyc = round(dmg_cyc / time_cyc)
	return {dmg_cyc, dps_burst, dps_cyc}
end
 
--Accelerated mode Calculator (Machine guns, Auto-cannons)
function calc_accel(dmg,rounds,rof_low,rof_high)
	local dmg_cyc = round(dmg * rounds)
	local dps_low = round(dmg * rof_low)
	local dps_high = round(dmg * rof_high)
	return {dmg_cyc, dps_low, dps_high}
end
 
--FWR mode Calculator (Rockets, Shotguns, Lasers, Dragoon)
function calc_fwr(dmg,rounds,rounds_burst,time_burst,time_reload)
	local dmg_burst = round(dmg * rounds_burst)
	local dps_burst = round(dmg_burst / time_burst)
	local dps_const = round((rounds * dmg) / time_reload)
	return {dmg_burst, dps_burst, dps_const}
end
 
--Charge mode Calculator (Energy Snipers, Shocktrain)
function calc_charge(dmg,rounds,time_reload,time_charge)
	local dmg_cyc = round(dmg * rounds)
	local dps_cyc = round(dmg_cyc / (time_reload + time_charge))
	local dmg_cyc_low = round(dmg)
	local dps_cyc_low = round(dmg_cyc_low / time_reload)
	return {dmg_cyc,dps_cyc,dmg_cyc_low,dps_cyc_low}
end
 
--Burst mode Calculator (Burst Missiles, Kinetic Snipers, Lightning Cannons, Spiral, Noricum)
function calc_burst(dmg,rounds,time_reload)
	local dmg_cyc = round(dmg * rounds)
	local dps_cyc = round(dmg_cyc / time_reload)
	return {dmg_cyc,dps_cyc}
end
 
--Constant mode Calculator (Magnum)
function calc_const(dmg, time_cyc)
	local dps = round(dmg / time_cyc)
	return dps
end
 
 
--------------------------HEADER FUNCTIONS--------------------------
--STRING STORAGE FOR THE VARIOUS HEADERS
--the lines saying class are the css class for visuals, just like HTML.
 
--The Mark 2 notice
function Rank_up_msg()
	return "Note: Level 12 Mark I is required to enhance (purchase) to Mark II.\n"
end
 
--The header for the totals table, which is shared
function header_totals()
	return "{|class=\"equip_table\"\n!\n!Cost\n!Time\n|-\n!Totals\n|"
end
 
--The header strings for the NORMAL mode
function header_normal()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!Cycle<br />Damage\n!Burst<br />DPS\n!Cycle<br />DPS\n|-\n!"
	local normal = "{| class=\"equip_table\"\n! Level\n! Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n! Damage\n!Cycle<br />Damage\n!Burst<br />DPS\n!Cycle<br />DPS\n|-\n"
	return {purchase, normal}
end
 
--The header strings for the ACCELERATED mode
function header_accel()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!Cycle<br />Damage\n!Intial<br />DPS\n!Max<br />DPS\n|-\n!"
	local normal = "{| class=\"equip_table\"\n! Level\n! Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n! Damage\n!Cycle<br />Damage\n!Intial<br />DPS\n!Max<br />DPS\n|-\n"
	return {purchase, normal}
end
 
--The header strings for the FIRE-WHILE-RELOADING mode
function header_fwr()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!Burst<br />Damage\n!Burst<br />DPS\n!Continuous<br />DPS\n|-\n!"
	local normal = "{|class=\"equip_table\"\n!Level\n!Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n!Damage\n!Burst<br />Damage\n!Burst<br />DPS\n!Continuous<br />DPS\n|-\n"
	return {purchase, normal}
end
 
--The header strings for the CHARGE mode
function header_charge()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!Cycle<br />Damage\n!Cycle DPS\n!Rapid Cycle<br />Damage\n!Rapid<br />Cycle DPS\n|-\n!"
	local normal = "{|class=\"equip_table\"\n!Level\n!Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n!Damage\n!Cycle<br />Damage\n!Cycle DPS\n!Rapid Cycle<br />Damage\n!Rapid<br />Cycle DPS\n|-\n"
	return {purchase, normal}
end
 
--The header strings for the BURST mode
function header_burst()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!Cycle<br />Damage\n!Cycle<br />DPS\n|-\n!"
	local normal = "{|class=\"equip_table\"\n!Level\n!Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n!Damage\n!Cycle<br />Damage\n!Cycle<br />DPS\n|-\n"
	return {purchase, normal}
end
 
--The header strings for the CONSTANT mode
function header_const()
	local purchase = "{|class=\"equip_table\"\n!Level\n!Cost\n!Damage\n!DPS\n|-\n!"
	local normal = "{| class=\"equip_table\"\n! Level\n! Cost \[\[File:"..ico.."|26px|link=Silver\]\]\n!Time\n! Damage\n!DPS\n|-\n"
	return {purchase, normal}
end
 
 
------------------MISCELANEOUS REQUIRED FUNCTIONS-------------------
--Function for rounding
function round(x)
	return math.floor(x+.5)
end
 
--Function for converting strings to numbers
function formatnum(str)
	return tonumber(string.gsub(str,",",""),10)
end
 
--Function for converting Logic-string inputs to logic values
function Logic(val)
	if val == "true" then
		return true
	else
		return false
	end
end
 
--Protected fetch function-reads in data and returns errors as false
function pfetch(target)
	local temp --temporary variable
	if (select(1,pcall(function() temp=data[target] end))) then
		--attempt fetch, use pcall() to catch any errors, and return the requested data on success.
 
		if temp == "" then return nil end --treat this as a failed attempt
 
		return temp
	else
		--if read attempt failed, return nil.
		return nil
	end
end
 
--[[ pfetch_str()
	Differs from pfetch() in that empty strings are allowed.
 
]]
function pfetch_str(target)
	local temp --temporary variable
	if (select(1,pcall(function() temp=data[target] end))) then
		--attempt fetch, use pcall() to catch any errors, and return the requested data on success.
 
		return temp
	else
		--if read attempt failed, return nil.
		return nil
	end
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.