Sha256: ab483bc94ff031d3c70ec8e92ff19aaf2a09dd967e2ebcb9586cb193f15d10c6

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

# -*- encoding: utf-8 -*-
require 'cinch'
require 'time-lord'
require 'cinch/toolbox'
require 'cinch/cooldown'

module Cinch
  module Plugins
    # Cinch Cooldown for PAX countdowns
    class PaxTimer
      include Cinch::Plugin

      self.help = 'Use .pax for the next pax or .east, .west, .south or ' \
                  '.aus for the time to a specific pax.'

      match(/pax\z/, method: :next_pax)

      def next_pax(m)
        m.reply get_next_pax
      end

      PAXES.map { |p| p[:type] }.each do |pax|
        match(/(#{pax}|pax#{pax})\z/, method: "next_#{pax}")

        define_method "next_#{pax}" do |m|
          debug "#{pax}"
          m.reply get_next_pax(pax.to_s)
        end
      end

      private

      def get_next_pax(type = nil)
        @pax = get_next_pax_for(type)

        return 'I don\'t have info for the next one of those PAXes' if @pax.nil?

        message = ["#{@pax[:name]} is"]
        message << 'approximately' if @pax[:estimated]

        # Uncomment this once we can specify granularity in Time Lord.
        # message << TimeLord::Period.new(@pax[:date], Time.now).to_words
        message << Cinch::Toolbox.time_format(@pax[:date] - Time.now, [:days])
        message << 'from now'

        message << ' (No official date, yet)' if @pax[:estimated]
        message.join(' ')
      end

      def get_next_pax_for(type)
        paxes = PAXES.dup

        paxes.delete_if { |pax| pax[:date] - Time.now < 0 }
        paxes.delete_if { |pax| pax[:type] != type } unless type.nil?
        paxes.sort! { |a, b| b[:date] <=> a[:date] }

        paxes.last
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cinch-pax-timer-1.0.20 lib/cinch/plugins/pax-timer.rb
cinch-pax-timer-1.0.18 lib/cinch/plugins/pax-timer.rb
cinch-pax-timer-1.0.16 lib/cinch/plugins/pax-timer.rb
cinch-pax-timer-1.0.15 lib/cinch/plugins/pax-timer.rb
cinch-pax-timer-1.0.14 lib/cinch/plugins/pax-timer.rb
cinch-pax-timer-1.0.13 lib/cinch/plugins/pax-timer.rb