Debian Codegraph @

lua/quiz/command.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 <start|stop|stats>
10 function command_quiz(account, text)
11 if not config.quiz then
12 return 1
13 end
14
15 local args = split_command(text, 2)
16
17 if (args[1] == "start") then
18 return q_command_start(account, args[2])
19
20 elseif (args[1] == "stop") then
21 return q_command_stop(account)
22
23 elseif (args[1] == "stats") then
24 if not args[2] then
25 return q_command_toplist(account)
26 else
27 return q_command_stats(account, args[2])
28 end
29 end
30
31 api.describe_command(account.name, args[0])
32 return -1
33 end
34
35
36 -- Start quiz in current channel
37 function q_command_start(account, filename)
38
39 if not account_is_operator_or_admin(account.name) then
40 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "You must be at least a Channel Operator to use this command."))
41 return -1
42 end
43
44 local channel = api.channel_get_by_id(account.channel_id)
45 if not channel then
46 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command can only be used inside a channel."))
47 return -1
48 end
49
50 if config.quiz_channel then
51 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Quiz has already ran in channel \"{}\". Use /quiz stop to force finish.", config.quiz_channel))
52 return -1
53 end
54
55 -- check if file exists
56 if not filename or not file_exists(q_directory() .. "/questions/" .. filename .. ".txt") then
57 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Available Quiz dictionaries: "))
58 api.message_send_text(account.name, message_type_error, account.name, " " .. config.quiz_filelist)
59 return -1
60 end
61
62 quiz:start(channel.name, filename)
63
64 return 0
65 end
66
67 -- Stop quiz
68 function q_command_stop(account)
69
70 if not account_is_operator_or_admin(account.name) then
71 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "You must be at least a Channel Operator to use this command."))
72 return -1
73 end
74
75 if not config.quiz_channel then
76 api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Quiz is not running."))
77 return -1
78 end
79
80 quiz:stop(account.name)
81
82 return 0
83 end
84
85 -- Display Quiz Top players record
86 function q_command_toplist(account)
87
88 -- load records (if it was not loaded yet)
89 if not q_load_records() then
90 return -1
91 end
92
93 local output = localize(account.name, "Top {} Quiz records:", config.quiz_users_in_top)
94 api.message_send_text(account.name, message_type_info, account.name, output)
95
96 -- display TOP of total records
97 for i,t in pairs(q_records_total) do
98 if (i > config.quiz_users_in_top) then break end
99
100 local output = string.format(" %d. %s [%d %s]", i, t.username, t.points, localize(account.name, "points"))
101 api.message_send_text(account.name, message_type_info, account.name, output)
102 end
103
104 return 0
105 end
106
107
108 -- Display single player's record
109 function q_command_stats(account, username)
110
111 -- load records (if it was not loaded yet)
112 if not q_load_records() then
113 return -1
114 end
115
116 local found = false
117 -- find user in records
118 for i,t in pairs(q_records_total) do
119 if string.upper(t.username) == string.upper(username) then
120 api.message_send_text(account.name, message_type_info, account.name, localize(account.name, "{}'s Quiz record:", t.username))
121
122 local output = string.format(" %d. %s [%d %s]", i, t.username, t.points, localize(account.name, "points"))
123 api.message_send_text(account.name, message_type_info, account.name, output)
124
125 found = true
126 end
127 end
128
129 if not found then
130 api.message_send_text(account.name, message_type_info, account.name, localize(account.name, "{} has never played Quiz.", username))
131 end
132
133 return 0
134 end