Module:CryptogullJrOld

From Caves of Qud Wiki
Jump to navigation Jump to search

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

local p = {}
local decode = require'Module:CryptogullJr/Codes'
local utilTable = require('Module:Table Utility')

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

function p.decode(buildcode, beta, title)
    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)[1]
                    	i = i + 1
                    end
                end
            end
            --calculate bonus
            bonus = p.getbonus(subtype, exts)
            if beta ~= '1' then
               attributes = p.fixattributes(bonus, attributes)
            end
			output = p.MakeUpSheet(buildcode, genotype, subtype,
            		classcalled, extname, attributes, bonus, exts, title)
            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, exts, title)
	local frame = mw.getCurrentFrame()
    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 = utilTable.concat(exts, "</br>"),
            skills = subtype[8],
            notes = subtype[14],
            buildname = title,
            author = "Legacy Build Code"
        }
    }
end
function p.test()
	return p.Main({["args"]={"BGMMMKMKBABJBPD1", ["beta"]=1}})	
end
return p