| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
quiz = {} |
| 10 |
q_dictionary = {} |
| 11 |
q_records_current = { } |
| 12 |
q_records_total = {} |
| 13 |
q_records_diff = {} |
| 14 |
|
| 15 |
|
| 16 |
local _q_question_counter = 0 |
| 17 |
|
| 18 |
local _q_hint_counter = 0 |
| 19 |
|
| 20 |
local _q_current_index = 0 |
| 21 |
|
| 22 |
local _q_hint = nil |
| 23 |
|
| 24 |
local _q_time_start = 0 |
| 25 |
|
| 26 |
local _q_streak = { username = nil, count = 0 } |
| 27 |
|
| 28 |
|
| 29 |
local _q_next_question_skip_first = false |
| 30 |
|
| 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 |
|
| 44 |
_q_question_counter = 0 |
| 45 |
_q_streak.username = nil |
| 46 |
_q_streak.count = 0 |
| 47 |
|
| 48 |
table.clear(q_dictionary) |
| 49 |
table.clear(q_records_current) |
| 50 |
table.clear(q_records_diff) |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
q_load_records() |
| 55 |
|
| 56 |
|
| 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 |
|
| 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 |
|
| 91 |
if (q_records_total) and next(q_records_total) then |
| 92 |
for i,t in pairs(q_records_current) do |
| 93 |
|
| 94 |
|
| 95 |
if (config.quiz_competitive_mode) then |
| 96 |
if q_records_total[i] and not (q_records_total[i].username == t.username) then |
| 97 |
|
| 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 |
|
| 102 |
if q_records_total[i].points <= 0 then |
| 103 |
q_records_total[i].points = 0 |
| 104 |
end |
| 105 |
end |
| 106 |
end |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
local idx = q_records_total_find(t.username) |
| 111 |
|
| 112 |
local tusername = q_records_total[idx].username |
| 113 |
q_records_diff[tusername] = t.points |
| 114 |
|
| 115 |
|
| 116 |
if q_records_total[idx].points == 0 then |
| 117 |
q_records_total[idx].points = t.points |
| 118 |
else |
| 119 |
|
| 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 |
|
| 128 |
table.sort(q_records_total, q_compare_desc) |
| 129 |
|
| 130 |
quiz_display_top_players() |
| 131 |
else |
| 132 |
|
| 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 |
|
| 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 |
|
| 152 |
local time_diff = os.clock() - _q_time_start |
| 153 |
|
| 154 |
local points, total = 0,0 |
| 155 |
|
| 156 |
points = 1+(#q.word - _q_hint_counter) - math.floor(time_diff / config.quiz_hint_delay) |
| 157 |
|
| 158 |
if (points <= 0) then points = 1 end |
| 159 |
|
| 160 |
|
| 161 |
local prev_streak = _q_streak |
| 162 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 216 |
function q_tick_next_question(options) |
| 217 |
|
| 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 |
|
| 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 |
|
| 231 |
_q_hint = q_hide_unswer(q.word) |
| 232 |
|
| 233 |
|
| 234 |
timer_del(options.id) |
| 235 |
|
| 236 |
_q_hint_counter = 0 |
| 237 |
|
| 238 |
timer_add("q_hint", config.quiz_hint_delay, q_tick_hint_unswer) |
| 239 |
|
| 240 |
q_unswered = false |
| 241 |
end |
| 242 |
|
| 243 |
|
| 244 |
function q_tick_hint_unswer(options) |
| 245 |
|
| 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 |
|
| 254 |
_q_hint = q_show_next_symbol(_q_hint, q.word) |
| 255 |
|
| 256 |
if not (_q_hint == q.word) then |
| 257 |
|
| 258 |
channel_send_message(config.quiz_channel, "Hint: ".._q_hint, message_type_info) |
| 259 |
else |
| 260 |
|
| 261 |
_q_hint_counter = 0 |
| 262 |
|
| 263 |
|
| 264 |
q_nounswer(nil) |
| 265 |
end |
| 266 |
|
| 267 |
end |
| 268 |
|
| 269 |
|
| 270 |
function q_nounswer(username) |
| 271 |
channel_send_message(config.quiz_channel, 'Nobody unswered. The unswer was: '.._q_hint, message_type_info) |
| 272 |
|
| 273 |
|
| 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 |
|