Module:Qud time

From Caves of Qud Wiki
Jump to navigation Jump to search

local p = {}
local text_util = require('Module:Text Utility')

function p.minutesToTime(minute)
  if (minute >= 0 and minute < 26) then
        return "Beetle Moon Zenith"
      elseif (minute < 151) then
        return "Waning Beetle Moon"
      elseif (minute < 301) then
        return "Shallows" 
      elseif (minute < 451) then
        return "Harvest Dawn"
      elseif (minute < 576) then
        return "Waxing Salt Sun"
      elseif (minute < 626) then
        return "High Salt Sun"
      elseif (minute < 751) then
        return "Waning Salt Sun"
      elseif (minute < 901) then
        return "Hindsun"
      elseif (minute < 1051) then
        return "Jeweled Dusk"
      elseif (minute < 1176) then
        return "Waxing Beetle Moon"
      end
      return "Beetle Moon Zenith" 
end

function p.isDay(minute)
  if minute >= 325 and minute < 1000 then
    return true
  else
    return false
  end
end

function p.dayEnding(num)
  local a = num%10
  local ending = {
    [1] = 'st',
    [2] = 'nd',
    [3] = 'rd',
    [4] = 'th',
    [5] = 'th',
    [6] = 'th',
    [7] = 'th',
    [8] = 'th',
    [9] = 'th',
    [0] = 'th'
  }
  return ending[a] or ''
end

function p.qudifyTime(year, days, hour, mins)
  frame = mw.getCurrentFrame()
  local qudmonth = 'Uru Ux'
  local quddayint = 0
  local qudperiodint = math.floor((hour + (mins/60)) * 50 )
  local qudsegment = p.minutesToTime(qudperiodint)
  local morningorevening = 'evening'
  if p.isDay(qudperiodint) == true then
    morningorevening = 'morning'
  end
  
  if days < 30 then
    qudmonth = 'Nivvun Ut'
    quddayint = days
  elseif days < 60 then
    qudmonth = 'Iyur Ut'
    quddayint = days - 29
  elseif days < 90 then
    qudmonth = 'Simmun Ut'
    quddayint = days - 59
  elseif days < 120 then
    qudmonth = 'Tuum Ut'
    quddayint = days - 89
  elseif days < 150 then
    qudmonth = 'Ubu Ut'
    quddayint = days - 119
  elseif days < 180 then
    qudmonth = 'Uulu Ut'
    quddayint = days - 149
  elseif days < 185 then
    qudmonth = 'Ut yara Ux'
    quddayint = days - 179
  elseif days < 215 then
    qudmonth = 'Tishru i Ux'
    quddayint = days - 184
  elseif days < 245 then
    qudmonth = 'Tishru ii Ux'
    quddayint = days - 214
  elseif days < 275 then
    qudmonth = 'Kisu Ux'
    quddayint = days - 244
  elseif days < 305 then
    qudmonth = 'Tebet Ux'
    quddayint = days - 274
  elseif days < 335 then
    qudmonth = 'Shwut Ux'
    quddayint = days - 304
  else
    quddayint = days - 334
  end
  local qudday = tostring(quddayint) .. p.dayEnding(quddayint)
  return {morningorevening, qudsegment, qudday, qudmonth}
end

function p.getTime(frame)
  local timeArgNames= {'year', 'days', 'hour', 'mins'}
  frame = mw.getCurrentFrame()
  local argslist =  text_util.splitArgs(frame.args[1], timeArgNames, '-')
  local qudtime = p.qudifyTime(tonumber(argslist['year']), tonumber(argslist['days']), tonumber(argslist['hour']), tonumber(argslist['mins']))
  local qudstring = frame:expandTemplate{ title = 'Qud time/format', args = { morningorevening=qudtime[1], qudsegment=qudtime[2], qudday=qudtime[3], qudmonth=qudtime[4]} }
  return qudstring
end

function p.getTimePlain(frame)
 -- return time without the dialogue box.
  local timeArgNames= {'year', 'days', 'hour', 'mins'}
  frame = mw.getCurrentFrame()
  local argslist =  text_util.splitArgs(frame.args[1], timeArgNames, '-')
  local qudtime = p.qudifyTime(tonumber(argslist['year']), tonumber(argslist['days']), tonumber(argslist['hour']), tonumber(argslist['mins']))
  local qudstring = frame:expandTemplate{ title = 'Qud time/format plain', args = { qudsegment=qudtime[2], qudday=qudtime[3], qudmonth=qudtime[4]} }
  return qudstring
end

function p.test(frame)
  return p.qudifyTime(2019,318,22,47 )
end

return p