-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.rb
More file actions
304 lines (261 loc) · 9.74 KB
/
program.rb
File metadata and controls
304 lines (261 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
63
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
class PureTextOverflow
class User < Struct.new(:nickname)
@@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") }
def self.find_by_id(u_id)
@@users[u_id]
end
def self.create(u_nickname)
u_id = @@users.count + 1
@@users[u_id] = User.new(u_nickname)
u_id
end
end
class Question < Struct.new(:asker_id, :title, :body)
@@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?") }
def self.find_by_id(q_id)
@@questions[q_id]
end
def self.create(u_id,q_text)
@@questions[@@questions.count + 1] = Question.new(u_id, q_text, "Any ideas?")
end
def self.all
@@questions.map { |k,v| "#{k}) #{v.title}" }
end
def self.exists?(q_id)
@@questions.has_key?(q_id)
end
end
class Answer < Struct.new(:question_id, :responder_id, :body)
@@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") }
def self.find_by_id(a_id)
@@answers[a_id]
end
def self.find_by_question_id(q_id)
@@answers.select { |k,v| v.question_id == q_id }
end
def self.create(q_id,user_id,a_text)
@@answers[@@answers.count + 11] = Answer.new(q_id, user_id, a_text)
end
def self.exists?(a_id)
@@answers.has_key?(a_id)
end
end
class QuestionVote < Struct.new(:user_id, :question_id, :vote)
@@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) ]
def self.upvote(u_id,q_id)
vote(u_id, q_id, 1)
end
def self.downvote(u_id,q_id)
vote(u_id, q_id, -1)
end
def self.popularity(q_id)
pop = @@q_votes.select do |qvote|
qvote.question_id == q_id
end.reduce(0) do |sum,qvote|
sum + qvote.vote
end
end
private
def self.vote(u_id,q_id,which_way)
return if Question.find_by_id(q_id).asker_id == u_id
v = @@q_votes.find do |v|
v.user_id == u_id and v.question_id == q_id
end
if v
v.vote = which_way
else
@@q_votes << QuestionVote.new(u_id, q_id, which_way)
end
end
end
class AnswerVote < Struct.new(:user_id, :answer_id, :vote)
@@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 self.upvote(u_id,a_id)
vote(u_id, a_id, 1)
end
def self.downvote(u_id,a_id)
vote(u_id, a_id, -1)
end
def self.popularity(a_id)
pop = @@a_votes.select do |avote|
avote.answer_id == a_id
end.reduce(0) do |sum,avote|
sum + avote.vote
end
end
private
def self.vote(u_id,a_id,which_way)
return if Answer.find_by_id(a_id).responder_id == u_id
v = @@a_votes.find do |v|
v.user_id == u_id and v.answer_id == a_id
end
if v
v.vote = which_way
else
@@a_votes << AnswerVote.new(u_id, a_id, which_way)
end
end
end
def self.content_for_single_question(q_id)
c = []
c << "Title: #{Question.find_by_id(q_id).title}"
c << "Body: #{Question.find_by_id(q_id).body}"
c << "Votes: #{QuestionVote.popularity(q_id)}"
q_author = User.find_by_id(Question.find_by_id(q_id).asker_id).nickname
c << "Asked by: #{q_author}"
Answer.find_by_question_id(q_id).each do |k,v|
c << ""
c << "#{k}) Answer:"
c << "#{v.body}"
a_author = User.find_by_id(Answer.find_by_id(k).responder_id).nickname
c << "Votes: #{AnswerVote.popularity(k)} (answer provided by #{a_author})"
end
c
end
@@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"] }
def self.get_user_input(prompt)
puts prompt
gets.chomp
end
def self.start
system("clear")
my_nickname = get_user_input("Hello there! Choose a nickname:")
my_user_id = User.create(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
if navigation_state == :single_question
puts
content_for_single_question(selected_qid).each { |line| puts line }
end
puts
@@menu_options[navigation_state].each { |option| puts option }
puts
puts "m) Main menu" unless navigation_state == :main_menu
puts "q) Quit"
puts
keyboard_input = get_user_input("Select an option:")
if navigation_state == :main_menu and keyboard_input == "1"
banner_text = "Showing all questions"
navigation_state = :all_questions
@@menu_options[navigation_state] = Question.all
next
end
if navigation_state == :main_menu and keyboard_input == "2"
system("clear")
keyboard_input = get_user_input("Ask a question:")
Question.create(my_user_id, keyboard_input)
next
end
if navigation_state == :all_questions
aux_int = keyboard_input.to_i
if Question.exists?(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"
QuestionVote.upvote(my_user_id, selected_qid)
end
if navigation_state == :single_question and keyboard_input == "2"
QuestionVote.downvote(my_user_id, selected_qid)
end
if navigation_state == :single_question and keyboard_input == "3"
keyboard_input = get_user_input("Provide an answer:")
Answer.create(selected_qid, my_user_id, keyboard_input)
next
end
if navigation_state == :single_question and keyboard_input == "4"
keyboard_input = get_user_input("Select an answer:")
aux_int = keyboard_input.to_i
if Answer.exists?(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"
AnswerVote.upvote(my_user_id, selected_aid)
end
if keyboard_input == "2"
AnswerVote.downvote(my_user_id, selected_aid)
end
end
next
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
end
end
PureTextOverflow.start