Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.37 KB | None | 0 0
  1. class User < Struct.new(:nickname)
  2. end
  3.  
  4. class Question < Struct.new(:asker_id, :title, :body)
  5. end
  6.  
  7. class Answer < Struct.new(:question_id, :responder_id, :body)
  8. end
  9.  
  10. class QuestionVote < Struct.new(:user_id, :question_id, :vote)
  11. end
  12.  
  13. class AnswerVote < Struct.new(:user_id, :answer_id, :vote)
  14. end
  15.  
  16. @users = { 1 => User.new("Warrior"),
  17.            2 => User.new("EtherealCereal"),
  18.            3 => User.new("AgentSmith"),
  19.            4 => User.new("WeaponX"),
  20.            5 => User.new("ManOnTheRun"),
  21.            6 => User.new("ButtersFromSouthPark"),
  22.            7 => User.new("Avenger") }
  23.  
  24. @questions = { 1 => Question.new(4, "Iterating Over an Array in Ruby", "How to do it?"),
  25.                2 => Question.new(7, "Substituting Variables Into Strings in Ruby", "How to do it?"),
  26.                3 => Question.new(1, "Generating Random Numbers in Ruby", "How to do it?"),
  27.                4 => Question.new(2, "Validating an Email Address in Ruby", "How to do it?"),
  28.                5 => Question.new(4, "Generating Prime Numbers in Ruby", "How to do it?"),
  29.                6 => Question.new(5, "Performing Date Arithmetic in Ruby", "How to do it?"),
  30.                7 => Question.new(1, "Removing Duplicate Elements from an Array in Ruby", "How to do it?"),
  31.                8 => Question.new(6, "Using Symbols as Hash Keys in Ruby", "How to do it?"),
  32.                9 => Question.new(4, "Writing an Infinite Loop in Ruby", "How to do it?") }
  33.  
  34. @answers = { 10 => Answer.new(1, 5, "Try this: your_array.each { |x| ... }"),
  35.              11 => Answer.new(2, 3, "Try this: \#{variable_to_interpolate}"),
  36.              12 => Answer.new(5, 1, "You need the mathn gem."),
  37.              14 => Answer.new(9, 7, "Try this: loop do ... end") }
  38.  
  39. @q_votes = [ QuestionVote.new(1, 1,  1),
  40.              QuestionVote.new(2, 1,  1),
  41.              QuestionVote.new(3, 1,  1),
  42.              QuestionVote.new(5, 1,  1),
  43.              QuestionVote.new(7, 1,  1),
  44.              QuestionVote.new(1, 2,  1),
  45.              QuestionVote.new(2, 2,  1),
  46.              QuestionVote.new(3, 2,  1),
  47.              QuestionVote.new(4, 2,  1),
  48.              QuestionVote.new(2, 3,  1),
  49.              QuestionVote.new(3, 3,  1),
  50.              QuestionVote.new(4, 3,  1),
  51.              QuestionVote.new(5, 3,  1),
  52.              QuestionVote.new(6, 3,  1),
  53.              QuestionVote.new(7, 3,  1),
  54.              QuestionVote.new(1, 9, -1),
  55.              QuestionVote.new(2, 9, -1),
  56.              QuestionVote.new(3, 9, -1),
  57.              QuestionVote.new(5, 9, -1),
  58.              QuestionVote.new(6, 9, -1),
  59.              QuestionVote.new(7, 9, -1) ]
  60.  
  61. @a_votes = [ AnswerVote.new(1, 10,  1),
  62.              AnswerVote.new(2, 10,  1),
  63.              AnswerVote.new(3, 10,  1),
  64.              AnswerVote.new(1, 11,  1),
  65.              AnswerVote.new(4, 11,  1),
  66.              AnswerVote.new(5, 11,  1),
  67.              AnswerVote.new(7, 11,  1),
  68.              AnswerVote.new(2, 12,  1),
  69.              AnswerVote.new(3, 12,  1),
  70.              AnswerVote.new(4, 12,  1),
  71.              AnswerVote.new(5, 12,  1),
  72.              AnswerVote.new(6, 12,  1),
  73.              AnswerVote.new(4, 14, -1),
  74.              AnswerVote.new(5, 14, -1) ]
  75.  
  76. def vote_question(uid,qid,which_way)
  77.   return if @questions[qid].asker_id == uid
  78.   ok_to_append_vote = true
  79.   @q_votes.each_with_index do |qv,i|
  80.     if qv.user_id == uid and qv.question_id == qid
  81.       @q_votes[i].vote = which_way
  82.       ok_to_append_vote = false
  83.     end
  84.   end
  85.   if ok_to_append_vote
  86.     @q_votes << QuestionVote.new(uid,qid,which_way)
  87.   end
  88. end
  89.  
  90. def vote_answer(uid,aid,which_way)
  91.   return if @answers[aid].responder_id == uid
  92.   ok_to_append_vote = true
  93.   @a_votes.each_with_index do |av,i|
  94.     if av.user_id == uid and av.answer_id == aid
  95.       @a_votes[i].vote = which_way
  96.       ok_to_append_vote = false
  97.     end
  98.   end
  99.   if ok_to_append_vote
  100.     @a_votes << AnswerVote.new(uid,aid,which_way)
  101.   end
  102. end
  103.  
  104. def content(navst)
  105.   c = []
  106.   if navst == :single_question
  107.     c = []
  108.     c << "Title:    #{@questions[@selected_qid].title}"
  109.     c << "Body:     #{@questions[@selected_qid].body}"
  110.     q_votes = @q_votes.select do |qvote|
  111.                          qvote.question_id == @selected_qid
  112.                        end.reduce(0) do |sum,qvote|
  113.                              sum + qvote.vote
  114.                            end
  115.     c << "Votes:    #{q_votes}"
  116.     q_author = @users[@questions[@selected_qid].asker_id].nickname
  117.     c << "Asked by: #{q_author}"
  118.     @answers.select { |k,v| v.question_id == @selected_qid }.each do |k,v|
  119.       c << ""
  120.       c << "#{k}) Answer:"
  121.       c << "#{v.body}"
  122.       a_author = @users[@answers[k].responder_id].nickname
  123.       a_votes = @a_votes.select do |avote|
  124.                            avote.answer_id == k
  125.                          end.reduce(0) do |sum,avote|
  126.                                sum + avote.vote
  127.                              end
  128.       c << "Votes: #{a_votes} (answer provided by #{a_author})"
  129.     end
  130.   end
  131.   c
  132. end
  133.  
  134. # questions.select { |k,v| v.title.include?("aria") }
  135.  
  136. @menu_options = { :main_menu       => ["1) Show all questions",
  137.                                        "2) Ask a question"],
  138.                   :single_question => ["1) Upvote the question   (not possible if you authored it)",
  139.                                        "2) Downvote the question (not possible if you authored it)",
  140.                                        "3) Provide an answer",
  141.                                        "4) Select an answer to upvote/downvote"] }
  142.  
  143. system("clear")
  144. @prompt_with = "Hello there! Choose a nickname:"
  145. puts @prompt_with
  146. @my_nickname = gets.chomp
  147. @my_user_id = @users.count + 1
  148. @users[@my_user_id] = User.new(@my_nickname)
  149. @banner_text = "Main menu"
  150. @navigation_state = :main_menu
  151. @selected_qid = 0
  152. @selected_aid = 0
  153. loop do
  154.   system("clear")
  155.   puts "Logged in as: #{@my_nickname}"
  156.   puts
  157.   puts @banner_text
  158.   puts
  159.   content(@navigation_state).each { |line| puts line }
  160.   puts
  161.   @menu_options[@navigation_state].each { |option| puts option }
  162.   puts
  163.   puts "m) Main menu" unless @navigation_state == :main_menu
  164.   puts "q) Quit"
  165.   puts
  166.   @prompt_with = "Select an option:"
  167.   puts @prompt_with
  168.   @keyboard_input = gets.chomp
  169.   if @navigation_state == :main_menu and @keyboard_input == "1"
  170.     @banner_text = "Showing all questions"
  171.     @navigation_state = :all_questions
  172.     @menu_options[@navigation_state] = @questions.map { |k,v| "#{k}) #{v.title}" }
  173.     next
  174.   end
  175.   if @navigation_state == :main_menu and @keyboard_input == "2"
  176.     system("clear")
  177.     @prompt_with = "Ask a question:"
  178.     puts @prompt_with
  179.     @keyboard_input = gets.chomp
  180.     @questions[@questions.count + 1] = Question.new(@my_user_id, @keyboard_input, "Any ideas?")
  181.     next
  182.   end
  183.   if @navigation_state == :all_questions
  184.     aux_int = @keyboard_input.to_i
  185.     if @questions.has_key?(aux_int)
  186.       @selected_qid = aux_int
  187.       @banner_text = "Showing single question (and its answers, if there are any)"
  188.       @navigation_state = :single_question
  189.       next
  190.     end
  191.   end
  192.   if @navigation_state == :single_question and @keyboard_input == "1"
  193.     vote_question(@my_user_id,@selected_qid,1)
  194.   end
  195.   if @navigation_state == :single_question and @keyboard_input == "2"
  196.     vote_question(@my_user_id,@selected_qid,-1)
  197.   end
  198.   if @navigation_state == :single_question and @keyboard_input == "3"
  199.     @prompt_with = "Provide an answer:"
  200.     puts @prompt_with
  201.     @keyboard_input = gets.chomp
  202.     @answers[@answers.count + 11] = Answer.new(@selected_qid, @my_user_id, @keyboard_input)
  203.     next
  204.   end
  205.   if @navigation_state == :single_question and @keyboard_input == "4"
  206.     @prompt_with = "Select an answer:"
  207.     puts @prompt_with
  208.     @keyboard_input = gets.chomp
  209.     aux_int = @keyboard_input.to_i
  210.     if @answers.has_key?(aux_int)
  211.       @selected_aid = aux_int
  212.       puts "1) Upvote the selected answer   (not possible if you authored it)"
  213.       puts "2) Downvote the selected answer (not possible if you authored it)"
  214.       @keyboard_input = gets.chomp
  215.       if @keyboard_input == "1"
  216.         vote_answer(@my_user_id,@selected_aid,1)
  217.       end
  218.       if @keyboard_input == "2"
  219.         vote_answer(@my_user_id,@selected_aid,-1)
  220.       end
  221.     end
  222.   end
  223.   if @keyboard_input == "m"
  224.     @banner_text = "Main menu"
  225.     @navigation_state = :main_menu
  226.     @selected_qid = 0
  227.     @selected_aid = 0
  228.     next
  229.   end
  230.   if @keyboard_input == "q"
  231.     exit
  232.   end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement