Class: Adventure::Calendar

Inherits:
Object
  • Object
show all
Defined in:
lib/adventure/calendar.rb

Overview

Represents the world's time system.

Constant Summary collapse

DEFAULT_WEEKDAYS =
['Firstday', 'Secondday', 'Thirdday', 'Fourthday', 'Fifthday', 'Sixthday', 'Seventhday', 'Eighthday', 'Ninthday', 'Tenthday'].freeze
DEFAULT_MONTHS =
{
  'Month One' => 28,
  'Month Two' => 28,
  'Month Three' => 28,
  'Month Four' => 28,
  'Month Five' => 28,
  'Month Six' => 28,
  'Month Seven' => 28,
  'Month Eight' => 28,
  'Month Nine' => 28,
  'Month Ten' => 28
}.freeze
DEFAULT_EPOCHS =
{
  name: '',
  abbreviation: '',
  days: nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_epoch, current_year, current_month, current_day, **opts) ⇒ Calendar

Create a new instance of Calendar.

Parameters:

  • current_epoch (Integer)

    The current epoch, as the index of the epochs Hash.

  • current_year (Integer)

    The number of the current year.

  • current_month (Integer)

    The current month, as the index of the months Array.

  • current_day (Integer)

    The number of the current day.

  • opts (Hash)

    A list of optional parameters.

Options Hash (**opts):

  • :hours_per_day (Integer)

    How many hours a day have.

  • :weekdays (Array<String>)

    The list of weekdays' names.

  • :months (Hash)

    The list of months's names, and days in each month.

  • :epochs (Hash)

    The list of epochs' names, abbreviation, and respective day count (or nil, if the current epoch).

  • :holidays (Array<Hash>)

    The list of holidays with date and name.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/adventure/calendar.rb', line 48

def initialize(current_epoch, current_year, current_month, current_day, **opts)
  if opts.key?(:preset)
    case opts[:preset].strip.downcase
    when 'gregorian' then gregorian
    when 'harptos' then harptos
    else
      raise StandardError, 'No such preset calendar.'
    end
  else
    @hours_per_day = opts.key?(:hours_per_day) ? opts[:hours_per_day].to_i : 24
    @weekdays = opts.key?(:weekdays) ? opts[:weekdays].to_a : DEFAULT_WEEKDAYS
    @months = opts.key?(:months) ? opts[:months].to_h : DEFAULT_MONTHS
    @epochs = opts.key?(:epochs) ? opts[:epochs].to_h : DEFAULT_EPOCHS
    @holidays = opts.key?(:holidays) ? opts[:holidays].to_a : []
  end
  @today_index = parse_calendar(current_epoch, current_year, current_month, current_day)
end

Instance Attribute Details

#calendarObject (readonly)

An Array with each day since year 1 as a Hash with day, month, year, and weekday.



34
35
36
# File 'lib/adventure/calendar.rb', line 34

def calendar
  @calendar
end

#epochsObject (readonly)

The list of epochs' names and abbreviation.



32
33
34
# File 'lib/adventure/calendar.rb', line 32

def epochs
  @epochs
end

#hours_per_dayObject (readonly)

How many hours a day have.



26
27
28
# File 'lib/adventure/calendar.rb', line 26

def hours_per_day
  @hours_per_day
end

#monthsObject (readonly)

The list of months's names.



30
31
32
# File 'lib/adventure/calendar.rb', line 30

def months
  @months
end

#weekdaysObject (readonly)

The list of weekdays' names.



28
29
30
# File 'lib/adventure/calendar.rb', line 28

def weekdays
  @weekdays
end

Instance Method Details

#days_per_weekInteger

Return how many days a week have.

Returns:

  • (Integer)

    The number of days in a week.



83
84
85
# File 'lib/adventure/calendar.rb', line 83

def days_per_week
  @weekdays.length
end

#gregorianObject (private)

Set calendar variables as Gregorian.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/adventure/calendar.rb', line 96

def gregorian
  @hours_per_day = 24
  @weekdays = [
    'Sunday',
    'Monday',
    'Tuesday',
    'Wednesday',
    'Thursday',
    'Friday',
    'Saturday'
  ]
  @months = {
    'January' => 31,
    'February' => 28,
    'March' => 31,
    'April' => 30,
    'May' => 31,
    'June' => 30,
    'July' => 31,
    'August' => 31,
    'September' => 30,
    'October' => 31,
    'November' => 30,
    'December' => 31
  }
  @epochs = {
    -1 => {
      name: 'Before Common Era',
      abbreviation: 'BCE',
      days: 4000*365.25
    },
    0  => {
      name: 'Common Era',
      abbreviation: 'CE',
      days: nil
    }
  }
  @holidays = []
end

#parse_calendar(current_epoch, current_year, current_month, current_day) ⇒ Object (private)



89
90
91
92
93
# File 'lib/adventure/calendar.rb', line 89

def parse_calendar(current_epoch, current_year, current_month, current_day)
  @epochs.each_with_index do |epoch, epoch_index|
    #
  end
end

#to_iInteger

Return an Integer as the current date's index in the calendar.

Returns:

  • (Integer)

    Current date's index.



76
77
78
# File 'lib/adventure/calendar.rb', line 76

def to_i
  @today_index
end

#to_sString

Return a String with the player's name, gender, species, and total level.

Returns:

  • (String)

    A String with the player's name, gender, species, and total level.



69
70
71
# File 'lib/adventure/calendar.rb', line 69

def to_s
  "#{@name}, #{@gender.downcase} #{@species.downcase}, level #{@level}"
end