Class: BggHotnessCLI::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/bgg-hotness-cli/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id, year, rank) ⇒ Game

Initialize with as much data as the list API gives for each game



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/bgg-hotness-cli/game.rb', line 6

def initialize(name, id, year, rank) 
  @name = name
  @id = id
  @year = year
  @rank = rank

  # Calculate page index of Page.all[]
  page_index = ((@rank.to_i - 1) / 10).floor
  # Put page into its game
  @page = BggHotnessCLI::Page.all[page_index]
  # Put game into its page
  BggHotnessCLI::Page.all[page_index].games << self
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def category
  @category
end

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def description
  @description
end

#designerObject

Returns the value of attribute designer.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def designer
  @designer
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/bgg-hotness-cli/game.rb', line 3

def id
  @id
end

#maxplayersObject

Returns the value of attribute maxplayers.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def maxplayers
  @maxplayers
end

#maxplaytimeObject

Returns the value of attribute maxplaytime.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def maxplaytime
  @maxplaytime
end

#mechanicObject

Returns the value of attribute mechanic.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def mechanic
  @mechanic
end

#minageObject

Returns the value of attribute minage.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def minage
  @minage
end

#minplayersObject

Returns the value of attribute minplayers.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def minplayers
  @minplayers
end

#minplaytimeObject

Returns the value of attribute minplaytime.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def minplaytime
  @minplaytime
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bgg-hotness-cli/game.rb', line 3

def name
  @name
end

#pageObject

Returns the value of attribute page.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def page
  @page
end

#publisherObject

Returns the value of attribute publisher.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def publisher
  @publisher
end

#rankObject (readonly)

Returns the value of attribute rank.



3
4
5
# File 'lib/bgg-hotness-cli/game.rb', line 3

def rank
  @rank
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/bgg-hotness-cli/game.rb', line 2

def url
  @url
end

#yearObject (readonly)

Returns the value of attribute year.



3
4
5
# File 'lib/bgg-hotness-cli/game.rb', line 3

def year
  @year
end

Instance Method Details

#details_inputObject



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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bgg-hotness-cli/game.rb', line 101

def details_input

  choices = [
    "Return to the list",
    "See full description",
    "See publisher(s) & designer(s)",
    "Open BGG page in your default browser"
  ]
  choices << "Return to game details" if @back_to_details
  choices << "Quit"

  # Set up prompt
  prompt = TTY::Prompt.new(active_color: :blue)

  # Set up greeting
  greeting = "Select an option:"

  # Capture input & display prompt
  @input = prompt.select(greeting, choices, cycle: true)

  # Parse user input
  if @input == choices[0] 
    # If they chose 1st option, return to the list
    @page.display_page
  elsif @input == choices[1]
    # If they choose 2nd option, print full description
    @back_to_details = true
    full_description
  elsif @input == choices[2]
    # If they choose 3rd option, print publisher and designer
    @back_to_details = true
    publisher_designer
  elsif @input == choices[3]
    # If they choose 4th option, open URL with launchy and reset details page
    puts
    puts "Attempting to open URL..."
    puts
    Launchy.open(@url)
    @input = @rank # Set input to rank so correct game is chosen
    display_details
  elsif @input == choices[4] && @back_to_details
    # If they choose back to game details, go back to game details
    @back_to_details = false
    display_details
  elsif @input == choices.last
    # If they quit, run "goodbye" method
    BggHotnessCLI::CLI.goodbye
  end
end

#display_detailsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bgg-hotness-cli/game.rb', line 37

def display_details

  # Variable for indentation string to make it easy to change
  @indent = "· " 
  
  # Print loading message. 
  # This is cleared by separator after load finishes,
  # and generally won't be on the screen for long.
  puts
  puts "Loading details..."
  puts

  # Check to see if details exist, and scrapes if needed
  get_details 
  @back_to_details = false
  
  header

  # 2-4 players • 60-90 minutes • ages 12+
  puts "#{@minplayers}#{@maxplayers} players • #{@minplaytime}#{@maxplaytime} minutes • ages #{@minage}+"

  # https://boardgamegeek.com/boardgame/ID
  puts "#{@url}"
  puts

  # Use wrap method to add indentation & word wrap
  puts "DESCRIPTION:"
  puts wrap("#{@description[0..140]}...",@indent) 
  puts

  # Use print_array method to print arrays (with wrap method)
  print_array("categories", "category", @category, @indent) 
  print_array("mechanics", "mechanic", @mechanic, @indent)

  # See what user wants to do next
  details_input
end

#full_descriptionObject

Displays full description



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bgg-hotness-cli/game.rb', line 76

def full_description
  header

  # Use wrap method to add indentation & word wrap
  puts "DESCRIPTION:"
  puts wrap("#{@description}",@indent) 
  puts 

  # See what user wants to do next
  details_input
end

#get_detailsObject

Get the details from the game’s details page via API



28
29
30
31
32
33
34
35
# File 'lib/bgg-hotness-cli/game.rb', line 28

def get_details 

  # If the description is nil, it needs to be scraped. 
  # Otherwise, all data should be in memory, so skip this.
  if @description.nil? 
    BggHotnessCLI::Scraper.new("https://boardgamegeek.com/xmlapi2/thing?id=#{@id}").get_details(self)
  end
end

#headerObject



20
21
22
23
24
25
# File 'lib/bgg-hotness-cli/game.rb', line 20

def header
  # Print header
  BggHotnessCLI::CLI.header
  puts 
  puts "#{@rank}. #{@name} (#{@year})"
end

#publisher_designerObject

Displays publisher(s) and designer(s)



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bgg-hotness-cli/game.rb', line 89

def publisher_designer
  header

  # Use print_array method to print arrays (with wrap method)
  print_array("publishers", "publisher", @publisher, @indent)
  print_array("designers", "designer", @designer, @indent)
  puts 

  # See what user wants to do next
  details_input
end