Module:CryptogullJr

From Caves of Qud Wiki
Jump to navigation Jump to search

local p = {}
local decode = require'Module:CryptogullJr/Codes'

function p.Main(frame)
	local buildcode = string.lower(frame.args[1])
    local beta = frame.args['beta'] or '1'
    return p.decode(buildcode, beta, frame)
end

function p.decode(buildcode, beta, frame)
    local genotype
    local subtype
    local classcalled
    local extname
    local attributes = {}
    local bonus = {}
    local exts = {}
    local beta = beta or '1'
    
	if buildcode == nil then
		error("A build code must be provided!")	
	end
    buildcode = string.lower(buildcode)
    if (string.len(buildcode) >= 8) then
        --[Check Genotype Code, and then subtype code]
        local genotypeC = string.match(buildcode, '%w')
        local subtypeC = string.sub(buildcode, 2, 2)
        if (genotypeC == 'a') then
            genotype = "True Kin"
            classcalled = "Caste: "
            extname = "Implants: "
            subtype = decode.TrueKinSubtype(subtypeC)
        elseif (genotypeC == 'b') then
            genotype = "Mutated Human"
            classcalled = "Calling: "
            extname = "Mutations: "
            subtype = decode.MutantSubtype(subtypeC)
        else
            error("The genotype code " .. genotypeC .. " does not exist!", 0)
        end

        --[Check if subtype worked]
        if (subtype == nil) then
            error("Couldn't find subtype " .. subtypeC, 0)
        else
        --[get attributes and put it in a table]
            local attributesC = string.sub(buildcode, 3, 8)
            local i = 1
            for char in string.gmatch(attributesC, "%w") do
                attributes[i] = tonumber(string.byte(char)) - 91
                i = i + 1
            end
            --[Mutations/Implants hooray]
            --if >8 chars, and have an even set of chars
            if (buildcode:len() >= 10 and buildcode:len() % 2 == 0) then
                local i = 1
                local extstring = buildcode:sub(9)
            	previouscode = nil
                for chars in string.gmatch(extstring, "[%w#]%w") do
                	varnum = tonumber(chars:match("#(%d)"))
                	if varnum ~= nil then
                		if previouscode == nil or (exts[i-1][2] == "") then
                			error("Unexpected variant code")
                		end
                		--  add 1 to the index because lua is silly
                		variant = exts[i-1][2][varnum + 1]
                		--  get the previous extension and append variant to it
                		exts[i - 1][1] = exts[i - 1][1] .. " (" .. variant .. ")"
                		previouscode = nil
                	else
                		previouscode = chars
                    	exts[i] = decode.getexts(chars, subtypeC)
                    	i = i + 1
                    end
                end
            end
            --calculate bonus
            bonus = p.getbonus(subtype, exts)
            if beta ~= '1' then
               attributes = p.fixattributes(bonus, attributes)
            end
            if not frame then
            	output = {buildcode, genotype, subtype, classcalled, extname, attributes, bonus, exts,}	
            else
            	output = p.MakeUpSheet(buildcode, genotype, subtype, classcalled, extname, attributes, bonus, exts, frame)
            end
            return output
        end
    else
        error("This build code is too short(<8 chars)!", 0)
    end
end

function p.getbonus(subtype, exts)
    --STR, AGI, TOUGH,INT, WILL, EGO
    local extbonuses = { 0, 0, 0, 0, 0, 0 }
    for x, y in pairs(exts) do
    	mw.logObject(y[1])
    	if decode.getStatBonus(y[1]) then
    		statbonus = decode.getStatBonus(y[1])
    		extbonuses[1] = extbonuses[1] + statbonus[1]
    		extbonuses[2] = extbonuses[2] + statbonus[2]
        	extbonuses[3] = extbonuses[3] + statbonus[3]
        	extbonuses[4] = extbonuses[4] + statbonus[4]
        	extbonuses[5] = extbonuses[5] + statbonus[5]
        	extbonuses[6] = extbonuses[6] + statbonus[6]
    	end
    end

    local bonusints = {}
    bonusints[1] = subtype[2] + extbonuses[1]
    bonusints[2] = subtype[3] + extbonuses[2]
    bonusints[3] = subtype[4] + extbonuses[3]
    bonusints[4] = subtype[5] + extbonuses[4]
    bonusints[5] = subtype[6] + extbonuses[5]
    bonusints[6] = subtype[7] + extbonuses[6]

    local i = 0
    local bonusT = {}
    for x, y in ipairs(bonusints) do
        local symbol = " "
        if y < 0 then
            symbol = ""
        elseif y > 0 then
            symbol = "+"
        end

        if symbol ~= nil and symbol ~= " " then
            bonusT[x] = symbol .. y
        else
            bonusT[x] = " "
        end
    end
    return bonusT
end

function p.fixattributes(bonus, attr)
    local newattr = {}
    for x, y in ipairs(bonus) do
        if y ~= " " then
            newattr[x] = tonumber(attr[x]) - tonumber(y)
        else
            newattr[x] = tonumber(attr[x])
        end
    end
    return newattr
end

function p.MakeUpSheet(codeIn, genoIn, subtype, classcalledIn, extnameIn, attributes, bonus, extsIn, frame)
    local mutstring = ""
    for _, ext in ipairs(extsIn) do
        mutstring = mutstring .. ", " .. ext[1]
    end

    return frame:expandTemplate {
        title = 'Crypto result',
        args = {
            code = codeIn:upper(),
            genotype = genoIn,
            classcalled = classcalledIn,
            class = subtype[1],
            strength = tostring(attributes[1]) .. bonus[1],
            agility = tostring(attributes[2]) .. bonus[2],
            toughness = attributes[3] .. bonus[3],
            intelligence = attributes[4] .. bonus[4],
            willpower = attributes[5] .. bonus[5],
            ego = attributes[6] .. bonus[6],
            extname = extnameIn,
            exts = mutstring:sub(3),
            skills = subtype[8],
            notes = subtype[14]
        }
    }
end

return p