| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
function q_load_records() |
| 10 |
local filename = config.vardir() .. "quiz_records.txt" |
| 11 |
if not file_exists(filename) then |
| 12 |
DEBUG("Could not open file with Quiz records: " .. filename) |
| 13 |
return false |
| 14 |
end |
| 15 |
if not q_records_total or not next(q_records_total) then |
| 16 |
|
| 17 |
return file_load(filename, file_load_dictionary_callback, q_read_records_callback) |
| 18 |
end |
| 19 |
return true |
| 20 |
end |
| 21 |
function q_read_records_callback(a, b) |
| 22 |
table.insert(q_records_total, { username = a, points = b }) |
| 23 |
end |
| 24 |
|
| 25 |
|
| 26 |
function q_save_records() |
| 27 |
local filename = config.vardir() .. "quiz_records.txt" |
| 28 |
file_save2(filename, q_save_records_callback) |
| 29 |
end |
| 30 |
function q_save_records_callback(file) |
| 31 |
if (q_records_total) and next(q_records_total) then |
| 32 |
for i,t in pairs(q_records_total) do |
| 33 |
file:write(t.username .. " = " .. t.points) |
| 34 |
file:write("\n") |
| 35 |
end |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
function quiz_display_top_players() |
| 42 |
|
| 43 |
local output = "Top " .. config.quiz_users_in_top .. " Quiz players:" |
| 44 |
channel_send_message(config.quiz_channel, output, message_type_info) |
| 45 |
|
| 46 |
|
| 47 |
for i,t in pairs(q_records_total) do |
| 48 |
if (i > config.quiz_users_in_top) then break end |
| 49 |
|
| 50 |
local diff = "" |
| 51 |
if q_records_diff[t.username] then |
| 52 |
if q_records_diff[t.username] < 0 then |
| 53 |
diff = "(" .. q_records_diff[t.username] .. ")" |
| 54 |
elseif q_records_diff[t.username] > 0 then |
| 55 |
diff = "(+" .. q_records_diff[t.username] .. ")" |
| 56 |
end |
| 57 |
end |
| 58 |
|
| 59 |
local output = string.format(" %d. %s [%d points] %s", i, t.username, t.points, diff) |
| 60 |
channel_send_message(config.quiz_channel, output, message_type_info) |
| 61 |
end |
| 62 |
end |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
function q_records_current_find(username, preventnew) |
| 67 |
|
| 68 |
for i,t in pairs(q_records_current) do |
| 69 |
if (t.username == username) then return i end |
| 70 |
end |
| 71 |
if preventnew then return nil end |
| 72 |
|
| 73 |
|
| 74 |
table.insert(q_records_current, {username = username, points = 0}) |
| 75 |
return #q_records_current |
| 76 |
end |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
function q_records_total_find(username, prevent_new) |
| 81 |
for i,t in pairs(q_records_total) do |
| 82 |
if (t.username == username) then return i end |
| 83 |
end |
| 84 |
if prevent_new then return nil end |
| 85 |
|
| 86 |
|
| 87 |
table.insert(q_records_total, {username = username, points = 0}) |
| 88 |
return #q_records_total |
| 89 |
end |
| 90 |
|
| 91 |
|