gemillionaire 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a0d866de6a4bcd45b10008c1683ec9ae7a323684a7046099aa009e25e4a1bf3
4
- data.tar.gz: 44e42eb68cbfca9657e09bbc8095489fb19a3cad3220da73cd4d095e847c3fb9
3
+ metadata.gz: b7a56b2de74afac31e283228b91af07cabca44318cc81b630db09d3baef31a05
4
+ data.tar.gz: b3716f769696d89ffbaa73e387cbe117dcd29b701fd3f39bdf61500f309d8a3e
5
5
  SHA512:
6
- metadata.gz: 828d8d95ec1f791ae665cac6584fd5fb2e2314ccf6938e872e30e4a438239fd3374a510e5f4b7dd65332946f422e09f8a89bf4b52bbd96335aeec68a778a6702
7
- data.tar.gz: f8b5409ffc59032630ca3c69be1582d0880b0185d4055916379b2c901299c0c67da8d57b813eb610a3b432e5edccf161ab2dabf6fcbb85aef59614a1718446fc
6
+ metadata.gz: 989f12341762af136051061c7f163149ad76505cbf7ff5a09a2a29e6bf518a35b4b70294e7d258e5ee638a7000e9b36bf018e7852db08c0751e3a52b51807f5e
7
+ data.tar.gz: f1f28e1160363c58b5f03682a9093c33a023b2b60911ec2f2936072801f5d26636870a56d9429fb322c56163ad399ecbbeb39b9fdfe2a1389230d3c105132d95
data/lib/gemillionaire.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'gemillionaire/app'
3
+ require "#{__dir__}/gemillionaire/app"
4
4
 
5
5
  app = App.new
6
6
  app.menu
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require 'csv'
4
5
  require 'tty-prompt'
5
6
  require 'colorize'
6
7
 
@@ -13,9 +14,10 @@ class App
13
14
  attr_accessor :questions, :statistics, :prizes, :main_menu
14
15
 
15
16
  def initialize
17
+ @args = ARGV.clone
16
18
  check_dependencies
17
- @questions = JSON.parse(File.read('lib/gemillionaire/questions.json'))
18
- @statistics = JSON.parse(File.read('lib/gemillionaire/hiscores.json'))
19
+ @questions = JSON.parse(File.read("#{__dir__}/questions.json"))
20
+ @statistics = JSON.parse(File.read("#{__dir__}/hiscores.json"))
19
21
  @prizes = ['0', '500', '1,000', '2,000', '3,000', '5,000', '7,500', '10,000', '12,500', '15,000',
20
22
  '25,000', '50,000', '100,000', '250,000', '500,000', '1,000,000'].freeze
21
23
  @main_menu = [
@@ -30,12 +32,12 @@ class App
30
32
  game = Game.new
31
33
  game.start
32
34
  end
33
-
35
+
34
36
  def check_dependencies
35
- missing_file = 'app.rb' unless File.exist?('lib/gemillionaire/app.rb')
36
- missing_file = 'game.rb' unless File.exist?('lib/gemillionaire/game.rb')
37
- missing_file = 'hiscores.json' unless File.exist?('lib/gemillionaire/hiscores.json')
38
- missing_file = 'questions.json' unless File.exist?('lib/gemillionaire/questions.json')
37
+ missing_file = 'app.rb' unless File.exist?("#{__dir__}/app.rb")
38
+ missing_file = 'game.rb' unless File.exist?("#{__dir__}/game.rb")
39
+ missing_file = 'hiscores.json' unless File.exist?("#{__dir__}/hiscores.json")
40
+ missing_file = 'questions.json' unless File.exist?("#{__dir__}/questions.json")
39
41
  return unless missing_file
40
42
 
41
43
  puts 'Missing File Error'.bold.red
@@ -49,6 +51,10 @@ class App
49
51
  loop do
50
52
  system('clear')
51
53
  display_intro
54
+ if @args.include?('newgame')
55
+ @args = []
56
+ new_game
57
+ end
52
58
  prompt = prompt_instance
53
59
  puts ('─' * 50).yellow
54
60
  prompt.select("For best experience, please maximise your terminal.\n".bold, @main_menu)
@@ -79,7 +85,7 @@ class App
79
85
  end
80
86
 
81
87
  def run_hiscores
82
- @statistics = JSON.parse(File.read('lib/gemillionaire/hiscores.json'))
88
+ @statistics = JSON.parse(File.read("#{__dir__}/hiscores.json"))
83
89
  games_played = @statistics['games_played'].to_i
84
90
  total_winnings = @statistics['total_winnings'].to_i
85
91
  average_earnings = games_played.zero? ? 0 : total_winnings / games_played
@@ -24,25 +24,43 @@ class Game < App
24
24
  current_q = @q_sample[@score]
25
25
  score_table
26
26
  puts "\nQuestion #{@score + 1}: #{current_q['question']}\n".bold
27
- display_ata if @lifelines[:ata][:status] == 'active'
28
- display_paf if @lifelines[:paf][:status] == 'active'
27
+ display_lifelines
29
28
  prompt = prompt_instance
30
29
  prompt.select('Select an option:'.bold, gen_menu(current_q), per_page: 8)
31
30
  end
32
31
 
32
+ def display_lifelines
33
+ display_ask_the_audience if @lifelines[:ask][:status] == 'active'
34
+ display_phone_a_friend if @lifelines[:phone][:status] == 'active'
35
+ end
36
+
37
+ def display_ask_the_audience
38
+ puts 'Ask The Audience'.center(40, ' ')
39
+ @keys.each do |k, v|
40
+ print "#{k} - #{@lifelines[:ask][:value][v]}%".center(10, ' ')
41
+ end
42
+ puts "\n\n"
43
+ end
44
+
45
+ def display_phone_a_friend
46
+ puts 'Phone A Friend'.center(40, ' ')
47
+ puts "\'I think it's #{@lifelines[:phone][:value]}.\'".center(40, ' ')
48
+ puts
49
+ end
50
+
33
51
  def gen_menu(current_q)
34
- answer_k = current_q['answer']
35
- answer_v = current_q[answer_k]
52
+ answer_key = current_q['answer']
53
+ answer_value = current_q[answer_key]
36
54
  choices = %w[A B C D].map do |k|
37
- { name: "#{k} - #{current_q[k]}", value: -> { check_answer(answer_k, answer_v, k) } }
55
+ { name: "#{k} - #{current_q[k]}", value: -> { check_answer(answer_key, answer_value, k) } }
38
56
  end
39
- disable_answers(choices) if @lifelines[:ff][:status] == 'active'
40
- add_lifelines(choices, answer_k)
57
+ disable_answers(choices) if @lifelines[:fifty][:status] == 'active'
58
+ add_lifelines(choices, answer_key)
41
59
  end
42
60
 
43
61
  def disable_answers(choices)
44
62
  @keys.each_value do |v|
45
- if @lifelines[:ff][:value].include?(v)
63
+ if @lifelines[:fifty][:value].include?(v)
46
64
  choices[v][:name] = choices[v][:name].red
47
65
  choices[v][:disabled] = '- (50/50)'.red
48
66
  end
@@ -50,16 +68,16 @@ class Game < App
50
68
  choices
51
69
  end
52
70
 
53
- def add_lifelines(choices, answer_k)
71
+ def add_lifelines(choices, answer_key)
54
72
  choices << { name: "Walk away with #{@prizes[@score].yellow.bold} 💎", value: -> { confirm_walk_away } }
55
- choices << { name: ' ½ - 50/50', value: -> { fifty_fifty(answer_k) } }
56
- choices << { name: '🗨 - Ask The Audience', value: -> { ask_the_audience(answer_k) } }
57
- choices << { name: '📱 - Phone A Friend', value: -> { phone_a_friend(answer_k) } }
73
+ choices << { name: ' ½ - 50/50', value: -> { fifty_fifty(answer_key) } }
74
+ choices << { name: '🗨 - Ask The Audience', value: -> { ask_the_audience(answer_key) } }
75
+ choices << { name: '📱 - Phone A Friend', value: -> { phone_a_friend(answer_key) } }
58
76
  disable_lifelines(choices)
59
77
  end
60
78
 
61
79
  def lifeline_statuses
62
- [@lifelines[:ff][:status], @lifelines[:ata][:status], @lifelines[:paf][:status]]
80
+ [@lifelines[:fifty][:status], @lifelines[:ask][:status], @lifelines[:phone][:status]]
63
81
  end
64
82
 
65
83
  def disable_lifelines(choices)
@@ -72,22 +90,8 @@ class Game < App
72
90
  choices
73
91
  end
74
92
 
75
- def display_ata
76
- puts 'Ask The Audience'.center(40, ' ')
77
- @keys.each do |k, v|
78
- print "#{k} - #{@lifelines[:ata][:value][v]}%".center(10, ' ')
79
- end
80
- puts "\n\n"
81
- end
82
-
83
- def display_paf
84
- puts 'Phone A Friend'.center(40, ' ')
85
- puts "\'I think it's #{@lifelines[:paf][:value]}.\'".center(40, ' ')
86
- puts
87
- end
88
-
89
- def check_answer(answer_k, answer_v, input)
90
- return you_lose(answer_k, answer_v) unless input == answer_k
93
+ def check_answer(answer_key, answer_value, input)
94
+ return you_lose(answer_key, answer_value) unless input == answer_key
91
95
 
92
96
  @score += 1
93
97
  reset_lifelines
@@ -95,9 +99,9 @@ class Game < App
95
99
  end
96
100
 
97
101
  def reset_lifelines
98
- @lifelines[:ff][:status] = 'used' if @lifelines[:ff][:status] == 'active'
99
- @lifelines[:ata][:status] = 'used' if @lifelines[:ata][:status] == 'active'
100
- @lifelines[:paf][:status] = 'used' if @lifelines[:paf][:status] == 'active'
102
+ @lifelines[:fifty][:status] = 'used' if @lifelines[:fifty][:status] == 'active'
103
+ @lifelines[:ask][:status] = 'used' if @lifelines[:ask][:status] == 'active'
104
+ @lifelines[:phone][:status] = 'used' if @lifelines[:phone][:status] == 'active'
101
105
  end
102
106
 
103
107
  def you_win
@@ -108,8 +112,8 @@ class Game < App
108
112
  update_stats(1_000_000)
109
113
  end
110
114
 
111
- def you_lose(answer_k, answer_v)
112
- puts "Incorrect! The correct answer was #{answer_k} - #{answer_v}."
115
+ def you_lose(answer_key, answer_value)
116
+ puts "Incorrect! The correct answer was #{answer_key} - #{answer_value}."
113
117
  @game_over = true
114
118
  prize = @prizes[@score / 5 * 5] # removes remainder and returns nearest safe point at 0, 5 or 10
115
119
  puts "You won #{prize} 💎. Better luck next time!"
@@ -120,7 +124,7 @@ class Game < App
120
124
  @statistics['games_played'] = @statistics['games_played'].to_i + 1
121
125
  @statistics['total_winnings'] = @statistics['total_winnings'].to_i + prize
122
126
  @statistics['hiscore'] = prize if prize > @statistics['hiscore'].to_i
123
- File.write('lib/gemillionaire/hiscores.json', JSON.dump(@statistics))
127
+ File.write("#{__dir__}/hiscores.json", JSON.dump(@statistics))
124
128
  any_key
125
129
  end
126
130
 
@@ -149,9 +153,9 @@ class Game < App
149
153
  end
150
154
 
151
155
  def fifty_fifty(answer)
152
- @lifelines[:ff][:status] = 'active'
153
- @lifelines[:ff][:value].slice!(@keys[answer])
154
- @lifelines[:ff][:value].slice!(rand(2))
156
+ @lifelines[:fifty][:status] = 'active'
157
+ @lifelines[:fifty][:value].slice!(@keys[answer])
158
+ @lifelines[:fifty][:value].slice!(rand(2))
155
159
  end
156
160
 
157
161
  def rng_graph
@@ -167,15 +171,15 @@ class Game < App
167
171
  end
168
172
 
169
173
  def ask_the_audience(answer)
170
- @lifelines[:ata][:status] = 'active'
174
+ @lifelines[:ask][:status] = 'active'
171
175
  return if rand(2).zero?
172
176
 
173
177
  # increase correct answer percentage by 30% and reduce others by 10%
174
- @lifelines[:ata][:value].map!.with_index { |n, index| n + (index == @keys[answer] ? 30 : -10) }
178
+ @lifelines[:ask][:value].map!.with_index { |n, index| n + (index == @keys[answer] ? 30 : -10) }
175
179
  end
176
180
 
177
181
  def phone_a_friend(answer)
178
- @lifelines[:paf][:status] = 'active'
179
- @lifelines[:paf][:value] = rand(2).zero? ? %w[A B C D].sample : answer
182
+ @lifelines[:phone][:status] = 'active'
183
+ @lifelines[:phone][:value] = rand(2).zero? ? %w[A B C D].sample : answer
180
184
  end
181
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemillionaire
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Sterling
@@ -16,11 +16,11 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
- - lib/gemillionaire.rb
20
- - lib/gemillionaire/app.rb
21
- - lib/gemillionaire/game.rb
22
- - lib/gemillionaire/hiscores.json
23
- - lib/gemillionaire/questions.json
19
+ - "./lib/gemillionaire.rb"
20
+ - "./lib/gemillionaire/app.rb"
21
+ - "./lib/gemillionaire/game.rb"
22
+ - "./lib/gemillionaire/hiscores.json"
23
+ - "./lib/gemillionaire/questions.json"
24
24
  homepage: https://rubygems.org/gems/gemillionaire
25
25
  licenses:
26
26
  - MIT