Module:Rotations

From Taskman Wiki
Jump to navigation Jump to search

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

local p = {}

local seconds_in_day = 24 * 60 * 60

function p.simple_rotation(interval, rotation_count, offset)
  local days_after_utc = math.floor(os.time() / seconds_in_day)
  local days_into_period = (days_after_utc + offset) % (interval * rotation_count)

  local rotation = math.floor(days_into_period / interval) + 1
  local days_until_next_rotation = interval - days_into_period % interval

  return rotation, days_until_next_rotation
end

function p.on_off(on_time, total_time, offset, unit_seconds)
  local units_after_utc = math.floor(os.time() / unit_seconds)
  local units_into_start = (units_after_utc + offset) % total_time

  local on = units_into_start < on_time

  local units_until_change
  if on then
    units_until_change = on_time - units_into_start
  else
    units_until_change = total_time - units_into_start
  end

  return on, units_until_change
end

function p.plural(word, n, plural)
  if n == 1 then
    return word
  else
    return plural or (word .. 's')
  end
end

function p.castaways(frame)
  local args = frame:getParent().args
  
  local bottle = args.bottle
  if (bottle == nil) then
    bottle = 1
  end
  
  local offset = 7
  
  local starttext = ""
  
  if (bottle == "3") then
    offset = 6
  end
  if (bottle == "5") then
    offset = 5
  end
  if (bottle == "6") then
    offset = 3
  end
  if (bottle == "7") then
    offset = 8
  end
  if (bottle == "9") then
    offset = 9
  end
  if (bottle == "10") then
    offset = 5
  end
    
  local on, change_time = p.on_off(1, bottle, offset, seconds_in_day)

  local text
  if on then
    text = "<b>Available now</b>"
  else
    local last = bottle - change_time

    local date_format = "%e %B"
    local last_date = os.date(date_format, os.time() - last * seconds_in_day)
    local next_date = os.date(date_format, os.time() + change_time * seconds_in_day)

    text = "Available in " .. change_time .. " " .. p.plural('day', change_time)
  end

  return text
end

return p