Debian Codegraph @

lua/quiz/records.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 -- Load total records from a text file into the table
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 -- fill records table
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 -- Save total records from the table into a text file
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 -- Print Top X players records
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 -- display TOP of total records
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] .. ")" -- minus points
54 elseif q_records_diff[t.username] > 0 then
55 diff = "(+" .. q_records_diff[t.username] .. ")" -- plus points
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 -- find username and return table index
65 -- preventnew - do not add user in table if it is not found
66 function q_records_current_find(username, preventnew)
67 -- find username
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 -- if not found then insert
74 table.insert(q_records_current, {username = username, points = 0})
75 return #q_records_current
76 end
77
78 -- find username and return table index
79 -- prevent_new = do not add user in table if it is not found
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 -- if not found then insert
87 table.insert(q_records_total, {username = username, points = 0})
88 return #q_records_total
89 end
90
91