Module:CryptogullJrOld: Difference between revisions

From Caves of Qud Wiki
Jump to navigation Jump to search
(set 'beta' to default)
(update for mutation rework)
Line 1: Line 1:
local p = {}
local p = {}
local decode = require'Module:CryptogullJr/Codes'
local decode = require'Module:Sandbox/User:Teamtoto/LuaModule2/Codes'


function p.Main(frame)
function p.Main(frame)
    local buildcode = string.lower(frame.args[1])
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 genotype
     local subtype
     local subtype
Line 11: Line 16:
     local bonus = {}
     local bonus = {}
     local exts = {}
     local exts = {}
     local beta = frame.args['beta'] or '1'
     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
     if (string.len(buildcode) >= 8) then
         --[Check Genotype Code, and then subtype code]
         --[Check Genotype Code, and then subtype code]
Line 46: Line 56:
                 local i = 1
                 local i = 1
                 local extstring = buildcode:sub(9)
                 local extstring = buildcode:sub(9)
                 for chars in string.gmatch(extstring, "%w%w") do
            previouscode = nil
                    exts[i] = decode.getexts(chars, subtypeC)
                 for chars in string.gmatch(extstring, "[%w#]%w") do
                    i = i + 1
                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
             end
             end
Line 56: Line 80:
               attributes = p.fixattributes(bonus, attributes)
               attributes = p.fixattributes(bonus, attributes)
             end
             end
             output = p.MakeUpSheet(buildcode, genotype, subtype, classcalled, extname, attributes, bonus, exts, frame)
             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
             return output
         end
         end
Line 66: Line 94:
function p.getbonus(subtype, exts)
function p.getbonus(subtype, exts)
     --STR, AGI, TOUGH,INT, WILL, EGO
     --STR, AGI, TOUGH,INT, WILL, EGO
     local extbonuses = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
     local extbonuses = { 0, 0, 0, 0, 0, 0 }
     for x, y in ipairs(exts) do
     for x, y in pairs(exts) do
        extbonuses[1] = extbonuses[1] + y[2]
    mw.logObject(y[1])
        extbonuses[2] = extbonuses[2] + y[3]
    if decode.getStatBonus(y[1]) then
        extbonuses[3] = extbonuses[3] + y[4]
    statbonus = decode.getStatBonus(y[1])
        extbonuses[4] = extbonuses[4] + y[5]
    extbonuses[1] = extbonuses[1] + statbonus[1]
        extbonuses[5] = extbonuses[5] + y[6]
    extbonuses[2] = extbonuses[2] + statbonus[2]
        extbonuses[6] = extbonuses[6] + y[7]
        extbonuses[3] = extbonuses[3] + statbonus[3]
        extbonuses[7] = extbonuses[7] + y[8]
        extbonuses[4] = extbonuses[4] + statbonus[4]
        extbonuses[8] = extbonuses[8] + y[9]
        extbonuses[5] = extbonuses[5] + statbonus[5]
        extbonuses[9] = extbonuses[9] + y[10]
        extbonuses[6] = extbonuses[6] + statbonus[6]
        extbonuses[10] = extbonuses[10] + y[11]
    end
     end
     end


Line 144: Line 172:
         }
         }
     }
     }
end
function p.test()
return p.decode("BCIKMKSEBFBH#0B2")
end
end


return p
return p

Revision as of 00:49, 28 January 2021

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

local p = {}
local decode = require'Module:Sandbox/User:Teamtoto/LuaModule2/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

function p.test()
	return p.decode("BCIKMKSEBFBH#0B2")
end

return p