2014-05-26 16:17:51 +04:00
--[[
Copyright ( C ) 2014 HarpyWar ( harpywar @ gmail.com )
This file is a part of the PvPGN Project http : // pvpgn.pro
Licensed under the same terms as Lua itself .
] ] --
-- /quiz <start|stop|stats>
function command_quiz ( account , text )
if not config.quiz then
2014-07-26 16:39:14 +04:00
return 1
2014-05-26 16:17:51 +04:00
end
local args = split_command ( text , 2 )
if ( args [ 1 ] == " start " ) then
return q_command_start ( account , args [ 2 ] )
elseif ( args [ 1 ] == " stop " ) then
return q_command_stop ( account )
elseif ( args [ 1 ] == " stats " ) then
if not args [ 2 ] then
return q_command_toplist ( account )
else
return q_command_stats ( account , args [ 2 ] )
end
end
api.describe_command ( account.name , args [ 0 ] )
2014-07-26 16:39:14 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
-- Start quiz in current channel
function q_command_start ( account , filename )
2014-06-23 15:12:59 +04:00
if not account_is_operator_or_admin ( account.name ) then
2014-06-28 21:33:09 +04:00
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. " ) )
2014-07-26 16:39:14 +04:00
return - 1
2014-06-23 15:12:59 +04:00
end
2014-05-26 16:17:51 +04:00
local channel = api.channel_get_by_id ( account.channel_id )
if not channel then
2014-06-28 21:33:09 +04:00
api.message_send_text ( account.name , message_type_error , account.name , localize ( account.name , " This command can only be used inside a channel. " ) )
2014-07-26 16:39:14 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
2014-06-23 15:12:59 +04:00
2014-05-26 16:17:51 +04:00
if config.quiz_channel then
2014-06-28 21:33:09 +04:00
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 ) )
2014-07-26 16:39:14 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
-- check if file exists
if not filename or not file_exists ( q_directory ( ) .. " /questions/ " .. filename .. " .txt " ) then
2014-06-28 21:33:09 +04:00
api.message_send_text ( account.name , message_type_error , account.name , localize ( account.name , " Available Quiz dictionaries: " ) )
2014-05-26 16:17:51 +04:00
api.message_send_text ( account.name , message_type_error , account.name , " " .. config.quiz_filelist )
2014-07-26 16:39:14 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
quiz : start ( channel.name , filename )
2014-09-13 12:42:50 +04:00
2014-07-26 16:39:14 +04:00
return 0
2014-05-26 16:17:51 +04:00
end
-- Stop quiz
function q_command_stop ( account )
2014-06-23 15:12:59 +04:00
if not account_is_operator_or_admin ( account.name ) then
2014-06-28 21:33:09 +04:00
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. " ) )
2014-07-26 16:39:14 +04:00
return - 1
2014-06-23 15:12:59 +04:00
end
2014-05-26 16:17:51 +04:00
if not config.quiz_channel then
2014-06-28 21:33:09 +04:00
api.message_send_text ( account.name , message_type_error , account.name , localize ( account.name , " Quiz is not running. " ) )
2014-07-26 16:39:14 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
quiz : stop ( account.name )
2014-07-26 16:39:14 +04:00
return 0
2014-05-26 16:17:51 +04:00
end
-- Display Quiz Top players record
function q_command_toplist ( account )
-- load records (if it was not loaded yet)
if not q_load_records ( ) then
2014-09-13 12:42:50 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
2014-06-28 21:33:09 +04:00
local output = localize ( account.name , " Top {} Quiz records: " , config.quiz_users_in_top )
2014-05-26 16:17:51 +04:00
api.message_send_text ( account.name , message_type_info , account.name , output )
-- display TOP of total records
for i , t in pairs ( q_records_total ) do
if ( i > config.quiz_users_in_top ) then break end
2014-06-28 21:33:09 +04:00
local output = string.format ( " %d. %s [%d %s] " , i , t.username , t.points , localize ( account.name , " points " ) )
2014-05-26 16:17:51 +04:00
api.message_send_text ( account.name , message_type_info , account.name , output )
end
2014-07-26 16:39:14 +04:00
return 0
2014-05-26 16:17:51 +04:00
end
-- Display single player's record
function q_command_stats ( account , username )
-- load records (if it was not loaded yet)
if not q_load_records ( ) then
2014-09-13 12:42:50 +04:00
return - 1
2014-05-26 16:17:51 +04:00
end
local found = false
-- find user in records
for i , t in pairs ( q_records_total ) do
if string.upper ( t.username ) == string.upper ( username ) then
2014-06-28 21:33:09 +04:00
api.message_send_text ( account.name , message_type_info , account.name , localize ( account.name , " {}'s Quiz record: " , t.username ) )
2014-05-26 16:17:51 +04:00
2014-06-28 21:33:09 +04:00
local output = string.format ( " %d. %s [%d %s] " , i , t.username , t.points , localize ( account.name , " points " ) )
2014-05-26 16:17:51 +04:00
api.message_send_text ( account.name , message_type_info , account.name , output )
found = true
end
end
if not found then
2014-06-28 21:33:09 +04:00
api.message_send_text ( account.name , message_type_info , account.name , localize ( account.name , " {} has never played Quiz. " , username ) )
2014-05-26 16:17:51 +04:00
end
2014-07-26 16:39:14 +04:00
return 0
2014-05-26 16:17:51 +04:00
end