From a49a95a44866dc45cb44c57eddb5cdcb30234f78 Mon Sep 17 00:00:00 2001 From: HarpyWar Date: Sat, 26 Jul 2014 16:39:14 +0400 Subject: [PATCH] change return codes in Lua commands https://github.com/HarpyWar/pvpgn/issues/47 --- lua/command/redirect.lua | 6 +++--- lua/command/w3motd.lua | 6 +++--- lua/handle_command.lua | 6 +++--- lua/quiz/command.lua | 28 ++++++++++++++-------------- src/bnetd/command.cpp | 23 ++++++++++++++++++++--- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lua/command/redirect.lua b/lua/command/redirect.lua index c6ac1f84e..7b692f83a 100644 --- a/lua/command/redirect.lua +++ b/lua/command/redirect.lua @@ -15,7 +15,7 @@ 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 @@ -23,10 +23,10 @@ function command_redirect(account, text) 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 diff --git a/lua/command/w3motd.lua b/lua/command/w3motd.lua index 5c47373a3..dad528e9f 100644 --- a/lua/command/w3motd.lua +++ b/lua/command/w3motd.lua @@ -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) diff --git a/lua/handle_command.lua b/lua/handle_command.lua index 43aae10e5..2a879f6d9 100644 --- a/lua/handle_command.lua +++ b/lua/handle_command.lua @@ -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 @@ -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, @@ -42,7 +42,7 @@ function handle_command(account, text) end end end - return 0 + return 1 end diff --git a/lua/quiz/command.lua b/lua/quiz/command.lua index eacb8710a..cf3745913 100644 --- a/lua/quiz/command.lua +++ b/lua/quiz/command.lua @@ -9,7 +9,7 @@ -- /quiz function command_quiz(account, text) if not config.quiz then - return 0 + return 1 end local args = split_command(text, 2) @@ -29,7 +29,7 @@ function command_quiz(account, text) end api.describe_command(account.name, args[0]) - return 1 + return -1 end @@ -38,29 +38,29 @@ 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 @@ -68,17 +68,17 @@ 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 @@ -86,7 +86,7 @@ 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) @@ -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 @@ -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 @@ -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 diff --git a/src/bnetd/command.cpp b/src/bnetd/command.cpp index 96cd25802..db6e32206 100644 --- a/src/bnetd/command.cpp +++ b/src/bnetd/command.cpp @@ -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))) @@ -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++) @@ -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; + } } }