Advertisement
Guest User

PureTextOverflow.rb

a guest
Nov 5th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.03 KB | None | 0 0
  1. class PureTextOverflow
  2.   class User < Struct.new(:nickname)
  3.     @@users = { 1 => User.new("Warrior"),
  4.                 2 => User.new("EtherealCereal"),
  5.                 3 => User.new("AgentSmith"),
  6.                 4 => User.new("WeaponX"),
  7.                 5 => User.new("ManOnTheRun"),
  8.                 6 => User.new("ButtersFromSouthPark"),
  9.                 7 => User.new("Avenger") }
  10.  
  11.     def self.find_by_id(u_id)
  12.       @@users[u_id]
  13.     end
  14.  
  15.     def self.create(u_nickname)
  16.       u_id = @@users.count + 1
  17.       @@users[u_id] = User.new(u_nickname)
  18.       u_id
  19.     end
  20.   end
  21.  
  22.   class Question < Struct.new(:asker_id, :title, :body)
  23.     @@questions = { 1 => Question.new(4, "Iterating Over an Array in Ruby", "How to do it?"),
  24.                     2 => Question.new(7, "Substituting Variables Into Strings in Ruby", "How to do it?"),
  25.                     3 => Question.new(1, "Generating Random Numbers in Ruby", "How to do it?"),
  26.                     4 => Question.new(2, "Validating an Email Address in Ruby", "How to do it?"),
  27.                     5 => Question.new(4, "Generating Prime Numbers in Ruby", "How to do it?"),
  28.                     6 => Question.new(5, "Performing Date Arithmetic in Ruby", "How to do it?"),
  29.                     7 => Question.new(1, "Removing Duplicate Elements from an Array in Ruby", "How to do it?"),
  30.                     8 => Question.new(6, "Using Symbols as Hash Keys in Ruby", "How to do it?"),
  31.                     9 => Question.new(4, "Writing an Infinite Loop in Ruby", "How to do it?") }
  32.  
  33.     def self.find_by_id(q_id)
  34.       @@questions[q_id]
  35.     end
  36.  
  37.     def self.create(u_id,q_text)
  38.       @@questions[@@questions.count + 1] = Question.new(u_id, q_text, "Any ideas?")
  39.     end
  40.  
  41.     def self.all
  42.       @@questions.map { |k,v| "#{k}) #{v.title}" }
  43.     end
  44.  
  45.     def self.exists?(q_id)
  46.       @@questions.has_key?(q_id)
  47.     end
  48.   end
  49.  
  50.   class Answer < Struct.new(:question_id, :responder_id, :body)
  51.     @@answers = { 10 => Answer.new(1, 5, "Try this: your_array.each { |x| ... }"),
  52.                   11 => Answer.new(2, 3, "Try this: \#{variable_to_interpolate}"),
  53.                   12 => Answer.new(5, 1, "You need the mathn gem."),
  54.                   14 => Answer.new(9, 7, "Try this: loop do ... end") }
  55.  
  56.     def self.find_by_id(a_id)
  57.       @@answers[a_id]
  58.     end
  59.  
  60.     def self.find_by_question_id(q_id)
  61.       @@answers.select { |k,v| v.question_id == q_id }
  62.     end
  63.  
  64.     def self.create(q_id,user_id,a_text)
  65.       @@answers[@@answers.count + 11] = Answer.new(q_id, user_id, a_text)
  66.     end
  67.  
  68.     def self.exists?(a_id)
  69.       @@answers.has_key?(a_id)
  70.     end
  71.   end
  72.  
  73.   class QuestionVote < Struct.new(:user_id, :question_id, :vote)
  74.     @@q_votes = [ QuestionVote.new(1, 1,  1),
  75.                   QuestionVote.new(2, 1,  1),
  76.                   QuestionVote.new(3, 1,  1),
  77.                   QuestionVote.new(5, 1,  1),
  78.                   QuestionVote.new(7, 1,  1),
  79.                   QuestionVote.new(1, 2,  1),
  80.                   QuestionVote.new(2, 2,  1),
  81.                   QuestionVote.new(3, 2,  1),
  82.                   QuestionVote.new(4, 2,  1),
  83.                   QuestionVote.new(2, 3,  1),
  84.                   QuestionVote.new(3, 3,  1),
  85.                   QuestionVote.new(4, 3,  1),
  86.                   QuestionVote.new(5, 3,  1),
  87.                   QuestionVote.new(6, 3,  1),
  88.                   QuestionVote.new(7, 3,  1),
  89.                   QuestionVote.new(1, 9, -1),
  90.                   QuestionVote.new(2, 9, -1),
  91.                   QuestionVote.new(3, 9, -1),
  92.                   QuestionVote.new(5, 9, -1),
  93.                   QuestionVote.new(6, 9, -1),
  94.                   QuestionVote.new(7, 9, -1) ]
  95.  
  96.     def self.upvote(u_id,q_id)
  97.       vote(u_id, q_id, 1)
  98.     end
  99.  
  100.     def self.downvote(u_id,q_id)
  101.       vote(u_id, q_id, -1)
  102.     end
  103.  
  104.     def self.popularity(q_id)
  105.       pop = @@q_votes.select do |qvote|
  106.                         qvote.question_id == q_id
  107.                       end.reduce(0) do |sum,qvote|
  108.                             sum + qvote.vote
  109.                           end
  110.     end
  111.  
  112.     private
  113.  
  114.     def self.vote(u_id,q_id,which_way)
  115.       return if Question.find_by_id(q_id).asker_id == u_id
  116.  
  117.       v = @@q_votes.find do |v|
  118.         v.user_id == u_id and v.question_id == q_id
  119.       end
  120.  
  121.       if v
  122.         v.vote = which_way
  123.       else
  124.         @@q_votes << QuestionVote.new(u_id, q_id, which_way)
  125.       end
  126.     end
  127.   end
  128.  
  129.   class AnswerVote < Struct.new(:user_id, :answer_id, :vote)
  130.     @@a_votes = [ AnswerVote.new(1, 10,  1),
  131.                   AnswerVote.new(2, 10,  1),
  132.                   AnswerVote.new(3, 10,  1),
  133.                   AnswerVote.new(1, 11,  1),
  134.                   AnswerVote.new(4, 11,  1),
  135.                   AnswerVote.new(5, 11,  1),
  136.                   AnswerVote.new(7, 11,  1),
  137.                   AnswerVote.new(2, 12,  1),
  138.                   AnswerVote.new(3, 12,  1),
  139.                   AnswerVote.new(4, 12,  1),
  140.                   AnswerVote.new(5, 12,  1),
  141.                   AnswerVote.new(6, 12,  1),
  142.                   AnswerVote.new(4, 14, -1),
  143.                   AnswerVote.new(5, 14, -1) ]
  144.  
  145.     def self.upvote(u_id,a_id)
  146.       vote(u_id, a_id, 1)
  147.     end
  148.  
  149.     def self.downvote(u_id,a_id)
  150.       vote(u_id, a_id, -1)
  151.     end
  152.  
  153.     def self.popularity(a_id)
  154.       pop = @@a_votes.select do |avote|
  155.                         avote.answer_id == a_id
  156.                       end.reduce(0) do |sum,avote|
  157.                             sum + avote.vote
  158.                           end
  159.     end
  160.  
  161.     private
  162.  
  163.     def self.vote(u_id,a_id,which_way)
  164.       return if Answer.find_by_id(a_id).responder_id == u_id
  165.  
  166.       v = @@a_votes.find do |v|
  167.         v.user_id == u_id and v.answer_id == a_id
  168.       end
  169.  
  170.       if v
  171.         v.vote = which_way
  172.       else
  173.         @@a_votes << AnswerVote.new(u_id, a_id, which_way)
  174.       end
  175.     end
  176.   end
  177.  
  178.   def self.content_for_single_question(q_id)
  179.     c = []
  180.     c << "Title:    #{Question.find_by_id(q_id).title}"
  181.     c << "Body:     #{Question.find_by_id(q_id).body}"
  182.     c << "Votes:    #{QuestionVote.popularity(q_id)}"
  183.     q_author = User.find_by_id(Question.find_by_id(q_id).asker_id).nickname
  184.     c << "Asked by: #{q_author}"
  185.     Answer.find_by_question_id(q_id).each do |k,v|
  186.       c << ""
  187.       c << "#{k}) Answer:"
  188.       c << "#{v.body}"
  189.       a_author = User.find_by_id(Answer.find_by_id(k).responder_id).nickname
  190.       c << "Votes: #{AnswerVote.popularity(k)} (answer provided by #{a_author})"
  191.     end
  192.     c
  193.   end
  194.  
  195.   @@menu_options = { :main_menu       => ["1) Show all questions",
  196.                                           "2) Ask a question"],
  197.                      :single_question => ["1) Upvote the question   (not possible if you authored it)",
  198.                                           "2) Downvote the question (not possible if you authored it)",
  199.                                           "3) Provide an answer",
  200.                                           "4) Select an answer to upvote/downvote"] }
  201.  
  202.   def self.get_user_input(prompt)
  203.     puts prompt
  204.     gets.chomp
  205.   end
  206.  
  207.   def self.start
  208.     system("clear")
  209.     my_nickname = get_user_input("Hello there! Choose a nickname:")
  210.     my_user_id = User.create(my_nickname)
  211.     banner_text = "Main menu"
  212.     navigation_state = :main_menu
  213.     selected_qid = 0
  214.     selected_aid = 0
  215.     loop do
  216.       system("clear")
  217.       puts "Logged in as: #{my_nickname}"
  218.       puts
  219.       puts banner_text
  220.       if navigation_state == :single_question
  221.         puts
  222.         content_for_single_question(selected_qid).each { |line| puts line }
  223.       end
  224.       puts
  225.       @@menu_options[navigation_state].each { |option| puts option }
  226.       puts
  227.       puts "m) Main menu" unless navigation_state == :main_menu
  228.       puts "q) Quit"
  229.       puts
  230.       keyboard_input = get_user_input("Select an option:")
  231.  
  232.       if navigation_state == :main_menu and keyboard_input == "1"
  233.         banner_text = "Showing all questions"
  234.         navigation_state = :all_questions
  235.         @@menu_options[navigation_state] = Question.all
  236.         next
  237.       end
  238.  
  239.       if navigation_state == :main_menu and keyboard_input == "2"
  240.         system("clear")
  241.         keyboard_input = get_user_input("Ask a question:")
  242.         Question.create(my_user_id, keyboard_input)
  243.         next
  244.       end
  245.  
  246.       if navigation_state == :all_questions
  247.         aux_int = keyboard_input.to_i
  248.         if Question.exists?(aux_int)
  249.           selected_qid = aux_int
  250.           banner_text = "Showing single question (and its answers, if there are any)"
  251.           navigation_state = :single_question
  252.           next
  253.         end
  254.       end
  255.  
  256.       if navigation_state == :single_question and keyboard_input == "1"
  257.         QuestionVote.upvote(my_user_id, selected_qid)
  258.       end
  259.  
  260.       if navigation_state == :single_question and keyboard_input == "2"
  261.         QuestionVote.downvote(my_user_id, selected_qid)
  262.       end
  263.  
  264.       if navigation_state == :single_question and keyboard_input == "3"
  265.         keyboard_input = get_user_input("Provide an answer:")
  266.         Answer.create(selected_qid, my_user_id, keyboard_input)
  267.         next
  268.       end
  269.  
  270.       if navigation_state == :single_question and keyboard_input == "4"
  271.         keyboard_input = get_user_input("Select an answer:")
  272.         aux_int = keyboard_input.to_i
  273.         if Answer.exists?(aux_int)
  274.           selected_aid = aux_int
  275.           puts "1) Upvote the selected answer   (not possible if you authored it)"
  276.           puts "2) Downvote the selected answer (not possible if you authored it)"
  277.           keyboard_input = gets.chomp
  278.           if keyboard_input == "1"
  279.             AnswerVote.upvote(my_user_id, selected_aid)
  280.           end
  281.           if keyboard_input == "2"
  282.             AnswerVote.downvote(my_user_id, selected_aid)
  283.           end
  284.         end
  285.         next
  286.       end
  287.  
  288.       if keyboard_input == "m"
  289.         banner_text = "Main menu"
  290.         navigation_state = :main_menu
  291.         selected_qid = 0
  292.         selected_aid = 0
  293.         next
  294.       end
  295.  
  296.       if keyboard_input == "q"
  297.         exit
  298.       end
  299.     end
  300.   end
  301. end
  302.  
  303. PureTextOverflow.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement