Skip to content

Commit

Permalink
change return codes in Lua commands #47
Browse files Browse the repository at this point in the history
  • Loading branch information
HarpyWar committed Jul 26, 2014
1 parent 60ea380 commit a49a95a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lua/command/redirect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ function command_redirect(account, text)

if not args[1] or not args[2] then
api.describe_command(account.name, args[0])
return 1
return -1
end

-- get destination account
local dest = api.account_get_by_name(args[1])

if next(dest) == nil or dest.online == "false" then
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "User \"{}\" is offline", args[1]))
return 1
return -1
end

api.message_send_text(dest.name, message_type_info, dest.name, args[2])

return 1
return 0
end
6 changes: 3 additions & 3 deletions lua/command/w3motd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ local username = nil
function command_w3motd(account, text)
-- allow warcraft 3 client only
if not (account.clienttag == "W3XP" or account.clienttag == "WAR3") then
return 0
return 1
end

username = account.name
local data = file_load(config.motdw3file, w3motd_sendline_callback)

return 1
return 0
end

function w3motd_sendline_callback(line)
Expand Down
6 changes: 3 additions & 3 deletions lua/handle_command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local lua_command_table = {


-- Global function to handle commands
-- ("return 1" from a command will break next C++ code execution)
-- ("return 1" from a command will allow next C++ code execution)
function handle_command(account, text)
-- find command in table
for cg,cmdlist in pairs(lua_command_table) do
Expand All @@ -32,7 +32,7 @@ function handle_command(account, text)
-- check if command group is in account.commandgroups
if math_and(account.commandgroups, cg) == 0 then
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command is reserved for admins."))
return 1
return -1
end

-- FIXME: we can use _G[func] if func is a text but not a function,
Expand All @@ -42,7 +42,7 @@ function handle_command(account, text)
end
end
end
return 0
return 1
end


Expand Down
28 changes: 14 additions & 14 deletions lua/quiz/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
-- /quiz <start|stop|stats>
function command_quiz(account, text)
if not config.quiz then
return 0
return 1
end

local args = split_command(text, 2)
Expand All @@ -29,7 +29,7 @@ function command_quiz(account, text)
end

api.describe_command(account.name, args[0])
return 1
return -1
end


Expand All @@ -38,55 +38,55 @@ function q_command_start(account, filename)

if not account_is_operator_or_admin(account.name) then
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."))
return 1
return -1
end

local channel = api.channel_get_by_id(account.channel_id)
if not channel then
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "This command can only be used inside a channel."))
return 1
return -1
end

if config.quiz_channel then
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))
return 1
return -1
end

-- check if file exists
if not filename or not file_exists(q_directory() .. "/questions/" .. filename .. ".txt") then
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Available Quiz dictionaries: "))
api.message_send_text(account.name, message_type_error, account.name, " " .. config.quiz_filelist)
return 1
return -1
end

quiz:start(channel.name, filename)
return 1
return 0
end

-- Stop quiz
function q_command_stop(account)

if not account_is_operator_or_admin(account.name) then
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."))
return 1
return -1
end

if not config.quiz_channel then
api.message_send_text(account.name, message_type_error, account.name, localize(account.name, "Quiz is not running."))
return 1
return -1
end

quiz:stop(account.name)

return 1
return 0
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
return 0
return 1
end

local output = localize(account.name, "Top {} Quiz records:", config.quiz_users_in_top)
Expand All @@ -100,7 +100,7 @@ function q_command_toplist(account)
api.message_send_text(account.name, message_type_info, account.name, output)
end

return 1
return 0
end


Expand All @@ -109,7 +109,7 @@ function q_command_stats(account, username)

-- load records (if it was not loaded yet)
if not q_load_records() then
return 0
return 1
end

local found = false
Expand All @@ -129,5 +129,5 @@ function q_command_stats(account, username)
api.message_send_text(account.name, message_type_info, account.name, localize(account.name, "{} has never played Quiz.", username))
end

return 1
return 0
end
23 changes: 20 additions & 3 deletions src/bnetd/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ namespace pvpgn

extern int handle_command(t_connection * c, char const * text)
{
int result = 0;
t_command_table_row const *p;

if ((text[0] != '\0') && (conn_quota_exceeded(c, text)))
Expand All @@ -561,8 +562,14 @@ namespace pvpgn
}

#ifdef WITH_LUA
if (lua_handle_command(c, text) > 0)
return 0;
result = lua_handle_command(c, text);
// -1 = unsuccess, 0 = success, 1 = execute next c++ code
if (result == 0)
{
// TODO: log command
}
if (result < 1)
return result;
#endif

for (p = standard_command_table; p->command_string != NULL; p++)
Expand All @@ -579,7 +586,17 @@ namespace pvpgn
message_send_text(c, message_type_error, c, localize(c, "This command is reserved for admins."));
return 0;
}
if (p->command_handler != NULL) return ((p->command_handler)(c, text));
if (p->command_handler != NULL)
{
result = ((p->command_handler)(c, text));
// -1 = unsuccess, 0 = success
if (result == 0)
{
// TODO: log command
// TODO: modify all commands to return "0" only if success, and "-1" if not
}
return result;
}
}
}

Expand Down

0 comments on commit a49a95a

Please sign in to comment.