Class: Quiz::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/quiz/cli.rb

Constant Summary collapse

@@points =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



154
155
156
# File 'lib/quiz/cli.rb', line 154

def self.clear
    @@points.clear
end

.pointsObject



151
152
153
# File 'lib/quiz/cli.rb', line 151

def self.points
    @@points
end

Instance Method Details

#all_question(team, year) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/quiz/cli.rb', line 99

def all_question(team, year)
    ["Who won the world cup in #{year}?",
     "Who was the host in #{year} world cup",
     "#{team} won the World cup final in #{year}. Who was the runner-up?",
     "How many world cup #{team} has won?", 
     "Who won the champion leage in #{year}?",
     "#{team} won the Champion leage final in #{year}. Who was the runner-up?",
     "Who hosted champion league final in #{year}?",
     "Who won the balon d'Or in #{year}?",
     "How many balon de'or #{team} has won?",
     "How many Champion league #{team} has won?"         
    ]
end

#callObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/quiz/cli.rb', line 4

def call
    puts "Welcome to football soccer quiz." 
    champion_league_url = "https://en.wikipedia.org/wiki/List_of_European_Cup_and_UEFA_Champions_League_finals"
    champion_league_file = Quiz::Scraper.champion_league(champion_league_url,"tbody")
    Quiz::CHAMPIONLEAGUE.champion_league_files(champion_league_file)
    champion_league = Quiz::CHAMPIONLEAGUE.all

    world_cup_url = "https://www.foxsports.com/soccer/fifa-world-cup/history"
    world_cup = Quiz::Scraper.world_cup(world_cup_url,"tbody")
    Quiz::Worldcup.world_cup_file(world_cup)
    world_cup = Quiz::Worldcup.all


    balon_d_or_url = "https://www.goal.com/es/noticias/todos-los-ganadores-del-balon-de-oro/wn19xivn1eh91t0jrslbzz5kq"
    players = Quiz::Scraper.balon_d_or_players(balon_d_or_url,".tableizer-table")
    Quiz::BALONDOR.balon_d_or(players)
    balon_d_or = Quiz::BALONDOR.all
     
    start_quiz(balon_d_or,champion_league,world_cup) 
end

#multiple_choice(answer, winners) ⇒ Object



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
# File 'lib/quiz/cli.rb', line 37

def multiple_choice(answer,winners)
    multiple_choice = []
    multiple_choice << answer
    until multiple_choice.size == 4  
        multiple_choice << winners.sample
        multiple_choice = multiple_choice.uniq
    end
    multiple_choice = multiple_choice.shuffle
    multiple_choice.each_with_index{|item,index| puts "#{index + 1}: #{item}"}
    input = ""
    loop do
        input = gets  
        if !(1..4).include?(input.to_i)
             puts "Invalid choice, enter a number from 1 to 4"
        else break    
        end         
    end
    if multiple_choice.index(answer) == input.to_i - 1 
        @@points << 1
        puts "Right"
    else
        puts "Wrong. Right answer is #{multiple_choice.index(answer) + 1}: #{answer}." 
    end
    puts "Press enter for next question"
    gets
end

#question_processor(balon_d_or, champion_leage, world_cup) ⇒ Object



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/quiz/cli.rb', line 114

def question_processor(balon_d_or,champion_leage,world_cup)
last_20_champions = []
counter = 44
while counter < champion_leage.size
   last_20_champions << champion_leage[counter]
   counter += 1
end 
#    question_counter = 0
#     until question_counter == 9
#         if question_counter < 4
#             question_selector(world_cup,question_counter)
#         elsif question_counter > 4 && question_counter < 7
#             question_selector(last_20_champions,question_counter)
#         elsif question_counter > 6 && question_counter < 9 
#         question_selector(balon_d_or,question_counter)
#         else question_selector(champion_leage,question_counter) 
#         question_counter += 1
#  end
        question_selector(world_cup,0)
        question_selector(world_cup,1)
        question_selector(world_cup,2)
        question_selector(world_cup,3) 
        question_selector(last_20_champions,4) 
        question_selector(last_20_champions,5)
        question_selector(last_20_champions,6)
        question_selector(balon_d_or,7)                
        question_selector(balon_d_or,8)              
        question_selector(champion_leage,9)  
         points = 0
        @@points.each{|i|points += i }
        if points > 5
        puts "Congratulation you score #{points} of 10 points."
        else puts "Sorry you failed. you score #{points} of 10 points."
        end
        Quiz::CLI.clear
end

#question_selector(objects, question) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/quiz/cli.rb', line 64

def question_selector(objects,question) 
  years = objects.map{|item| item.year} 
  winners = objects.map{|item|item.winner}
  hosts = objects.map{|item|item.host}
  runner_ups = objects.map{|item|item.runner_up}
  random_year = years.sample
  random_winner = winners.sample
  answer = objects.select{|item|item.year == random_year}
  titles_amount = objects.select{|item|item.winner == random_winner}.size
  winner = answer[0].winner
  host = answer[0].host
  runner_up = answer[0].runner_up
  
  if all_question("none",random_year)[question].include?("host")
      puts all_question(host,random_year)[question]
      multiple_choice(host,hosts)
  elsif all_question("none",random_year)[question].include?("runner-up")
      runner_ups = runner_ups.reject{|item| item == winner}
      puts all_question(winner,random_year)[question]
      multiple_choice(runner_up,runner_ups)
  elsif all_question("none",random_year)[question].include?("How many")
      puts all_question(random_winner,'none')[question]
      obtions = []
      counter = 1
      while obtions.size < 13
          obtions << counter
          counter += 1
       end
    multiple_choice(titles_amount,obtions)
  else 
      puts all_question(winner,random_year)[question]
      multiple_choice(winner,winners)
  end
end

#start_quiz(balon_d_or, champion_league, world_cup) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quiz/cli.rb', line 25

def start_quiz(balon_d_or,champion_league,world_cup)
    puts "Take this 10 question quiz to find out how much you know about soccer."
    puts "You pass the test if you score 6 or more points."
    puts "To take the quiz press enter, to exit press 1 and enter."
    input = gets
    while input.to_i != 1
        question_processor(balon_d_or,champion_league,world_cup)
        puts "To take the quiz one more time press enter. to exit press 1 and enter."
        input = gets   
    end
end