Debian Codegraph @

lua/quiz/quiz.lua

raw symbols SCIP
1 --[[
2 Copyright (C) 2014 HarpyWar ([email protected])
3
4 This file is a part of the PvPGN Project http://pvpgn.pro
5 Licensed under the same terms as Lua itself.
6 ]]--
7
8
9 quiz = {} -- object
10 q_dictionary = {} -- dictionary word=question
11 q_records_current = { } -- table with current player records
12 q_records_total = {} -- table with total player records
13 q_records_diff = {} -- table with current diffs
14
15 -- counter of questions
16 local _q_question_counter = 0
17 -- count of current question's hints
18 local _q_hint_counter = 0
19 -- count of current question's hints
20 local _q_current_index = 0
21 -- current hint
22 local _q_hint = nil
23 -- time when question was shown
24 local _q_time_start = 0
25 -- last user streaks
26 local _q_streak = { username = nil, count = 0 }
27
28 -- delay flag for next_question timer
29 local _q_next_question_skip_first = false
30 -- is question unswered?
31 local q_unswered = true
32
33 function quiz:start(channelname, quizname)
34 local filename = q_directory() .. "/questions/" .. string.lower(quizname) .. ".txt"
35
36 if not file_exists(filename) then
37 channel_send_message(config.quiz_channel, filename, message_type_error)
38 return false
39 end
40
41 config.quiz_channel = channelname
42
43 -- reset
44 _q_question_counter = 0
45 _q_streak.username = nil
46 _q_streak.count = 0
47 -- clear tables
48 table.clear(q_dictionary)
49 table.clear(q_records_current)
50 table.clear(q_records_diff)
51 -- table.clear(q_records_total) -- do not clear it, it should be cached
52
53 -- load records (if it was not loaded yet)
54 q_load_records()
55
56 -- fill dictionary from file
57 if file_load(filename, file_load_dictionary_callback, q_read_dictionary_callback) then
58 channel_send_message(config.quiz_channel, string.format("Quiz \"%s\" started!", quizname), message_type_error)
59 q_next_question()
60 end
61 end
62 function q_read_dictionary_callback(a, b)
63 table.insert(q_dictionary, { question = a, word = b })
64 end
65
66
67 function quiz:stop(username)
68
69 timer_del("q_hint")
70 timer_del("q_question")
71
72 local output = nil
73 if username then
74 output = "Quiz stopped by "..username
75 else
76 output = "Quiz finished!"
77 end
78 channel_send_message(config.quiz_channel, output, message_type_error)
79
80 -- display current records
81 if (q_records_current) and next(q_records_current) then
82 table.sort(q_records_current, q_compare_desc)
83
84 channel_send_message(config.quiz_channel, "Records of this game:", message_type_info)
85 for i,t in pairs(q_records_current) do
86 channel_send_message(config.quiz_channel, string.format(" %d. %s [%d points]", i, t.username, t.points), message_type_info)
87 end
88 end
89
90 -- merge current with total records
91 if (q_records_total) and next(q_records_total) then
92 for i,t in pairs(q_records_current) do
93
94 -- steal points from players in total table accordingly with half points of current table players
95 if (config.quiz_competitive_mode) then
96 if q_records_total[i] and not (q_records_total[i].username == t.username) then
97 -- remove half points
98 local points = math.floor(t.points / 2)
99 q_records_diff[q_records_total[i].username] = points * -1
100 q_records_total[i].points = q_records_total[i].points - points
101 -- avoid negative value
102 if q_records_total[i].points <= 0 then
103 q_records_total[i].points = 0
104 end
105 end
106 end
107
108 -- add non exist users from current to total table
109 -- and increase total users points
110 local idx = q_records_total_find(t.username)
111 -- set diff as winned points
112 local tusername = q_records_total[idx].username
113 q_records_diff[tusername] = t.points
114
115 -- if user not found (new user with 0 points)
116 if q_records_total[idx].points == 0 then
117 q_records_total[idx].points = t.points
118 else
119 -- update diff if it was changed to negative
120 if tonumber(q_records_diff[tusername]) < 0 then
121 q_records_diff[tusername] = q_records_diff[tusername] + t.points
122 end
123 q_records_total[idx].points = q_records_total[idx].points + t.points
124 end
125 end
126
127 -- sort by points
128 table.sort(q_records_total, q_compare_desc)
129
130 quiz_display_top_players()
131 else
132 -- if quiz_records.txt is empty then save first records
133 q_records_total = q_records_current
134 end
135 q_save_records()
136
137 config.quiz_channel = nil
138 end
139
140
141
142
143
144 -- handle channel message
145 function quiz_handle_message(username, text)
146 if q_unswered then return end
147
148 local q = q_dictionary[_q_current_index]
149
150 if string.upper(q.word) == string.upper(text) then
151 -- time from question to unswer
152 local time_diff = os.clock() - _q_time_start
153
154 local points, total = 0,0
155 -- calc points
156 points = 1+(#q.word - _q_hint_counter) - math.floor(time_diff / config.quiz_hint_delay)
157 -- avoid negative value
158 if (points <= 0) then points = 1 end
159
160 -- remember previous streak
161 local prev_streak = _q_streak
162 -- increase streaks
163 if (_q_streak.username == username) then
164 _q_streak.count = _q_streak.count + 1
165 else
166 _q_streak.username = username
167 _q_streak.count = 0
168 end
169
170 local bonus = ""
171 -- add bonus points for streaks
172 if (_q_streak.count > 0) then
173 points = points + _q_streak.count
174 bonus = string.format(" +%s streak bonus", _q_streak.count)
175 end
176
177 local idx = q_records_current_find(username)
178 total = q_records_current[idx].points + points
179 q_records_current[idx].points = total
180
181 -- lose half points for previous user
182 if config.quiz_competitive_mode and not (prev_streak.username == username) then
183 local lose_points = math.floor(total / 2)
184 local idx = q_records_current_find(prev_streak.username)
185 q_records_current[idx].points = q_records_current[idx].points - lose_points
186 -- avoid negative value
187 if q_records_current[idx].points <= 0 then
188 q_records_current[idx].points = 0
189 end
190 end
191
192 channel_send_message(config.quiz_channel, string.format("%s is correct! The unswer is: %s (+%d points%s, %d total) [%d sec]", username, q.word, (points-_q_streak.count), bonus, total, time_diff), message_type_info)
193
194 q_next_question()
195 end
196
197 end
198
199 -- go to next question
200 function q_next_question()
201 q_unswered = true
202
203 _q_question_counter = _q_question_counter + 1
204 if (_q_question_counter > config.quiz_max_questions) then
205 quiz:stop()
206 return 0
207 end
208
209 timer_del("q_hint")
210
211 _q_next_question_skip_first = false
212 timer_add("q_question", config.quiz_question_delay, q_tick_next_question)
213 end
214
215 -- write random question
216 function q_tick_next_question(options)
217 -- skip first tick
218 if not _q_next_question_skip_first then
219 _q_next_question_skip_first = true
220 return 0
221 end
222 _q_time_start = os.clock()
223 _q_current_index = math.random(#q_dictionary)
224
225 local q = q_dictionary[_q_current_index]
226 -- send question
227 channel_send_message(config.quiz_channel, "--------------------------------------------------------", message_type_info)
228 channel_send_message(config.quiz_channel, string.format(" %s (%d letters)", q.question, #q.word), message_type_info)
229
230 -- hide hint
231 _q_hint = q_hide_unswer(q.word)
232
233 -- stop this timer
234 timer_del(options.id)
235
236 _q_hint_counter = 0
237 -- start timer to hint unswer
238 timer_add("q_hint", config.quiz_hint_delay, q_tick_hint_unswer)
239
240 q_unswered = false
241 end
242
243 -- write hint for unswer
244 function q_tick_hint_unswer(options)
245 -- skip first tick
246 if _q_hint_counter == 0 then
247 _q_hint_counter = _q_hint_counter + 1
248 return 0
249 end
250 _q_hint_counter = _q_hint_counter + 1
251
252 local q = q_dictionary[_q_current_index]
253 -- update hint
254 _q_hint = q_show_next_symbol(_q_hint, q.word)
255
256 if not (_q_hint == q.word) then
257 -- show hint
258 channel_send_message(config.quiz_channel, "Hint: ".._q_hint, message_type_info)
259 else
260 -- reset counter
261 _q_hint_counter = 0
262
263 -- show unswer
264 q_nounswer(nil)
265 end
266
267 end
268
269 -- nobody unswered
270 function q_nounswer(username)
271 channel_send_message(config.quiz_channel, 'Nobody unswered. The unswer was: '.._q_hint, message_type_info)
272
273 -- decrease streaks
274 if (_q_streak.count > 0) then
275 _q_streak.count = _q_streak.count - 1
276 end
277
278 q_next_question()
279 end
280
281