Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User < Struct.new(:nickname)
- end
- class Question < Struct.new(:asker_id, :title, :body)
- end
- class Answer < Struct.new(:question_id, :responder_id, :body)
- end
- class QuestionVote < Struct.new(:user_id, :question_id, :vote)
- end
- class AnswerVote < Struct.new(:user_id, :answer_id, :vote)
- end
- @users = { 1 => User.new("Warrior"),
- 2 => User.new("EtherealCereal"),
- 3 => User.new("AgentSmith"),
- 4 => User.new("WeaponX"),
- 5 => User.new("ManOnTheRun"),
- 6 => User.new("ButtersFromSouthPark"),
- 7 => User.new("Avenger") }
- @questions = { 1 => Question.new(4, "Iterating Over an Array in Ruby", "How to do it?"),
- 2 => Question.new(7, "Substituting Variables Into Strings in Ruby", "How to do it?"),
- 3 => Question.new(1, "Generating Random Numbers in Ruby", "How to do it?"),
- 4 => Question.new(2, "Validating an Email Address in Ruby", "How to do it?"),
- 5 => Question.new(4, "Generating Prime Numbers in Ruby", "How to do it?"),
- 6 => Question.new(5, "Performing Date Arithmetic in Ruby", "How to do it?"),
- 7 => Question.new(1, "Removing Duplicate Elements from an Array in Ruby", "How to do it?"),
- 8 => Question.new(6, "Using Symbols as Hash Keys in Ruby", "How to do it?"),
- 9 => Question.new(4, "Writing an Infinite Loop in Ruby", "How to do it?") }
- @answers = { 10 => Answer.new(1, 5, "Try this: your_array.each { |x| ... }"),
- 11 => Answer.new(2, 3, "Try this: \#{variable_to_interpolate}"),
- 12 => Answer.new(5, 1, "You need the mathn gem."),
- 14 => Answer.new(9, 7, "Try this: loop do ... end") }
- @q_votes = [ QuestionVote.new(1, 1, 1),
- QuestionVote.new(2, 1, 1),
- QuestionVote.new(3, 1, 1),
- QuestionVote.new(5, 1, 1),
- QuestionVote.new(7, 1, 1),
- QuestionVote.new(1, 2, 1),
- QuestionVote.new(2, 2, 1),
- QuestionVote.new(3, 2, 1),
- QuestionVote.new(4, 2, 1),
- QuestionVote.new(2, 3, 1),
- QuestionVote.new(3, 3, 1),
- QuestionVote.new(4, 3, 1),
- QuestionVote.new(5, 3, 1),
- QuestionVote.new(6, 3, 1),
- QuestionVote.new(7, 3, 1),
- QuestionVote.new(1, 9, -1),
- QuestionVote.new(2, 9, -1),
- QuestionVote.new(3, 9, -1),
- QuestionVote.new(5, 9, -1),
- QuestionVote.new(6, 9, -1),
- QuestionVote.new(7, 9, -1) ]
- @a_votes = [ AnswerVote.new(1, 10, 1),
- AnswerVote.new(2, 10, 1),
- AnswerVote.new(3, 10, 1),
- AnswerVote.new(1, 11, 1),
- AnswerVote.new(4, 11, 1),
- AnswerVote.new(5, 11, 1),
- AnswerVote.new(7, 11, 1),
- AnswerVote.new(2, 12, 1),
- AnswerVote.new(3, 12, 1),
- AnswerVote.new(4, 12, 1),
- AnswerVote.new(5, 12, 1),
- AnswerVote.new(6, 12, 1),
- AnswerVote.new(4, 14, -1),
- AnswerVote.new(5, 14, -1) ]
- def vote_question(uid,qid,which_way)
- return if @questions[qid].asker_id == uid
- ok_to_append_vote = true
- @q_votes.each_with_index do |qv,i|
- if qv.user_id == uid and qv.question_id == qid
- @q_votes[i].vote = which_way
- ok_to_append_vote = false
- end
- end
- if ok_to_append_vote
- @q_votes << QuestionVote.new(uid,qid,which_way)
- end
- end
- def vote_answer(uid,aid,which_way)
- return if @answers[aid].responder_id == uid
- ok_to_append_vote = true
- @a_votes.each_with_index do |av,i|
- if av.user_id == uid and av.answer_id == aid
- @a_votes[i].vote = which_way
- ok_to_append_vote = false
- end
- end
- if ok_to_append_vote
- @a_votes << AnswerVote.new(uid,aid,which_way)
- end
- end
- def content(navst)
- c = []
- if navst == :single_question
- c = []
- c << "Title: #{@questions[@selected_qid].title}"
- c << "Body: #{@questions[@selected_qid].body}"
- q_votes = @q_votes.select do |qvote|
- qvote.question_id == @selected_qid
- end.reduce(0) do |sum,qvote|
- sum + qvote.vote
- end
- c << "Votes: #{q_votes}"
- q_author = @users[@questions[@selected_qid].asker_id].nickname
- c << "Asked by: #{q_author}"
- @answers.select { |k,v| v.question_id == @selected_qid }.each do |k,v|
- c << ""
- c << "#{k}) Answer:"
- c << "#{v.body}"
- a_author = @users[@answers[k].responder_id].nickname
- a_votes = @a_votes.select do |avote|
- avote.answer_id == k
- end.reduce(0) do |sum,avote|
- sum + avote.vote
- end
- c << "Votes: #{a_votes} (answer provided by #{a_author})"
- end
- end
- c
- end
- # questions.select { |k,v| v.title.include?("aria") }
- @menu_options = { :main_menu => ["1) Show all questions",
- "2) Ask a question"],
- :single_question => ["1) Upvote the question (not possible if you authored it)",
- "2) Downvote the question (not possible if you authored it)",
- "3) Provide an answer",
- "4) Select an answer to upvote/downvote"] }
- system("clear")
- @prompt_with = "Hello there! Choose a nickname:"
- puts @prompt_with
- @my_nickname = gets.chomp
- @my_user_id = @users.count + 1
- @users[@my_user_id] = User.new(@my_nickname)
- @banner_text = "Main menu"
- @navigation_state = :main_menu
- @selected_qid = 0
- @selected_aid = 0
- loop do
- system("clear")
- puts "Logged in as: #{@my_nickname}"
- puts
- puts @banner_text
- puts
- content(@navigation_state).each { |line| puts line }
- puts
- @menu_options[@navigation_state].each { |option| puts option }
- puts
- puts "m) Main menu" unless @navigation_state == :main_menu
- puts "q) Quit"
- puts
- @prompt_with = "Select an option:"
- puts @prompt_with
- @keyboard_input = gets.chomp
- if @navigation_state == :main_menu and @keyboard_input == "1"
- @banner_text = "Showing all questions"
- @navigation_state = :all_questions
- @menu_options[@navigation_state] = @questions.map { |k,v| "#{k}) #{v.title}" }
- next
- end
- if @navigation_state == :main_menu and @keyboard_input == "2"
- system("clear")
- @prompt_with = "Ask a question:"
- puts @prompt_with
- @keyboard_input = gets.chomp
- @questions[@questions.count + 1] = Question.new(@my_user_id, @keyboard_input, "Any ideas?")
- next
- end
- if @navigation_state == :all_questions
- aux_int = @keyboard_input.to_i
- if @questions.has_key?(aux_int)
- @selected_qid = aux_int
- @banner_text = "Showing single question (and its answers, if there are any)"
- @navigation_state = :single_question
- next
- end
- end
- if @navigation_state == :single_question and @keyboard_input == "1"
- vote_question(@my_user_id,@selected_qid,1)
- end
- if @navigation_state == :single_question and @keyboard_input == "2"
- vote_question(@my_user_id,@selected_qid,-1)
- end
- if @navigation_state == :single_question and @keyboard_input == "3"
- @prompt_with = "Provide an answer:"
- puts @prompt_with
- @keyboard_input = gets.chomp
- @answers[@answers.count + 11] = Answer.new(@selected_qid, @my_user_id, @keyboard_input)
- next
- end
- if @navigation_state == :single_question and @keyboard_input == "4"
- @prompt_with = "Select an answer:"
- puts @prompt_with
- @keyboard_input = gets.chomp
- aux_int = @keyboard_input.to_i
- if @answers.has_key?(aux_int)
- @selected_aid = aux_int
- puts "1) Upvote the selected answer (not possible if you authored it)"
- puts "2) Downvote the selected answer (not possible if you authored it)"
- @keyboard_input = gets.chomp
- if @keyboard_input == "1"
- vote_answer(@my_user_id,@selected_aid,1)
- end
- if @keyboard_input == "2"
- vote_answer(@my_user_id,@selected_aid,-1)
- end
- end
- end
- if @keyboard_input == "m"
- @banner_text = "Main menu"
- @navigation_state = :main_menu
- @selected_qid = 0
- @selected_aid = 0
- next
- end
- if @keyboard_input == "q"
- exit
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement