Module:Creature Mutation Query

From Caves of Qud Wiki
Jump to navigation Jump to search

local util_args = require('Module:Args Utility')
local util_cargo = require('Module:Cargo Utility')

local p = {}

function p.calculate(mutation, level, ego)
	result= doQuery(mutation)
if result ~= nil then
  return format(result["_pageName"], result["Name"], result["Cost"], result["Type"], level, ego)
else
  return format(mutation, mutation, nil, "", level, ego)
end
end

function p.main() 
local mutation = ""
local ego = 16  --[default, is 0 modifier]
local level = 1
local isDefect = false
local isMental = false

frame=mw.getCurrentFrame()
if frame.args ~= nil and frame.args ~= '' then
	if frame.args[1] ~= nil and frame.args[1] ~= '' then
		mutation = mw.text.trim(frame:preprocess(frame.args[1]))
		level = tonumber(mw.text.trim(frame:preprocess(frame.args[2])))
    end
    if frame.args[3] ~= nil and frame.args[3] ~= '' then
		ego = tonumber(mw.text.trim(frame:preprocess(frame.args[3])))
	end
end
return p.calculate(mutation, level, ego)
end

function doQuery(mutation)
	-- TODO: edit QBE to remove P{{mutation ID to name}} so we can just query 
	-- the MutationID
    local query = {
		tables = { 'Mutations' },
		fields = {'Cost, Type, _pageName, MutationID, Name'},
		where = '(_pageNamespace="14" OR _pageNamespace="0") AND Name="' .. mutation..'"',
		limit= '1'
	}
-- Try to find a mutation with this name, if not, just return the name unlinked.
    result = util_cargo.queryAndCast(query)
    if not next(result) then
        return nil
    else return result[1]
    end
end

function formatBonus(modifier)
	c = ""
    if modifier > 0 then
       c = "00c420;\">+"
    elseif modifier < 0 then
       c= "d74200;\">"
    end
    return '<span style = \"color:#' .. c .. tostring(modifier) .. '</span>'
end

function formatResult(page, name, formattedLevel, modifier, showBonus)
  parentheses ='<b><span style="color:#b1c9c3;">&nbsp;(</span><span style="color:#' .. formattedLevel
  if showBonus then
        parentheses = parentheses .. formatBonus(modifier)
  end
  parentheses = parentheses .. '</span><span style="color:#b1c9c3;">)</span></b>'
-- formatting
local mutationString = mw.html.create('div')
mutationString
  :addClass('qud-mutation-entry')
:wikitext('[[' .. name.. '|'.. page .. parentheses .. ']]')
   return mutationString
end


function format(page, name, cost, type, level, ego)
    local isDefect = false
    local isMental = false
    if type ~= nil then
      isDefect = string.match(type,"defect") ~= nil
      isMental = string.match(type,"Mental") ~= nil
    end
    local modifier = math.floor((ego-16)/2)
    mw.log(modifier)
    local parentheses = ""
  if isDefect then
      formattedLevel ='a64a2e;">D'
  elseif (level) then
      formattedLevel = '77bfcf;">' .. level
  else
  	  formattedLevel = ('77bfcf;">no level') 
  end
  
  return formatResult(page, name, formattedLevel, modifier, isMental)
end

function p.test()
 return p.calculate("Teleportation", -2, 28)	
end
return p