Jump to content

Module:ApplyBonus

From Conan Exiles Wiki

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

local util_args = require("Module:ArgsUtil")
local util_cache = require("Module:CacheUtil")
local util_vars = require("Module:VarsUtil")
local cache = require('mw.ext.LuaCache')
local lang = mw.getLanguage('en')

local p = {}
local h = {}

local data = {}
data.armorer = {}
--                 float 27/28  int 6        int 0     float 29              float 30          float 31
data.armorer.t1 = {damage=0.5,  stamina=1, health=2, carryingcapacity=1.5, followerdamage=0.5, concussivedamage=0.5}
data.armorer.t2 = {damage=1,    stamina=1, health=4, carryingcapacity=3,   followerdamage=1,   concussivedamage=1}
data.armorer.t3 = {damage=1.5,  stamina=2, health=6, carryingcapacity=4.5, followerdamage=1.5, concussivedamage=1.5}
data.armorer.t4 = {damage=2,    stamina=2, health=8, carryingcapacity=6,   followerdamage=2,   concussivedamage=2}

function p.Main(frame)
	local args = util_args.mergeKeepEmpty()
	local text = ""
	
	local bonus = args[1] or ''
	local profession = args[2]
	local tier = args[3]
	
	-- TODO Fehlerhandling
	if data[profession] == nil or data[profession][tier] == nil then
		return bonus	
	end
	
	local results = {}
	
	for attribute in string.gmatch(bonus, '([^,]+)') do
		-- mit Prozent?
		if string.find(attribute, "%", 1, true) then
			-- in this case we always use "damage"
			local tstart = string.find(attribute, "+", 1, true)
			local tend = string.find(attribute, "%", 1, true)
			
			local ttext = string.sub(attribute, 1, tstart) -- includes the '+'
			local tvalue = string.sub(attribute, tstart+1, tend-1)
				
			-- error handling for missing data
			text = dataExists(profession, tier, "damage")
			if data[profession][tier]["damage"] ~= nil then
				local result = ttext .. (tonumber(tvalue) + data[profession][tier]["damage"]) .. "%"
				table.insert(results, result)
			else
				table.insert(results, attribute) 
			end
		else
			local tstart = string.find(attribute, "+", 1, true)
			
			local ttext = string.sub(attribute, 1, tstart) -- without the '+'
			local key = prepareKey(ttext)
			local tvalue = string.sub(attribute, tstart+1)
			
			-- error handling for missing data
			if data[profession][tier][key] ~= nil then
				local result = ttext .. (tonumber(tvalue) + data[profession][tier][key])
				table.insert(results, result)
			else
				table.insert(results, attribute) 
			end
		end
	end
	
	return table.concat(results, ", ")
end

function prepareKey(text)
	return string.gsub(text, "+", ""):gsub(" ", ""):lower()
end

function dataExists(profession, tier, attribute)
	return data[profession][tier][key] or true
end

return p