summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRoderic Morris <roderic@ccs.neu.edu>2008-11-01 22:20:13 +0000
committerRoderic Morris <roderic@ccs.neu.edu>2008-11-01 22:20:13 +0000
commitb714a1ff7e3616233b7232ef05f585e8806b3254 (patch)
tree2d78ba3f648b4737b001a47009f07c7239383d86 /src
parent7cdecda38dc75dd855a45af6faad14d1c1e4e72f (diff)
downloadmanaserv-b714a1ff7e3616233b7232ef05f585e8806b3254.tar.gz
manaserv-b714a1ff7e3616233b7232ef05f585e8806b3254.tar.bz2
manaserv-b714a1ff7e3616233b7232ef05f585e8806b3254.tar.xz
manaserv-b714a1ff7e3616233b7232ef05f585e8806b3254.zip
fixes for commandhandler
Diffstat (limited to 'src')
-rw-r--r--src/game-server/commandhandler.cpp312
-rw-r--r--src/game-server/commandhandler.hpp46
-rw-r--r--src/game-server/gamehandler.cpp4
3 files changed, 165 insertions, 197 deletions
diff --git a/src/game-server/commandhandler.cpp b/src/game-server/commandhandler.cpp
index a5be113a..8430a6ac 100644
--- a/src/game-server/commandhandler.cpp
+++ b/src/game-server/commandhandler.cpp
@@ -21,6 +21,7 @@
* $Id$
*/
+#include "defines.h"
#include "commandhandler.hpp"
#include "accountconnection.hpp"
#include "character.hpp"
@@ -37,103 +38,69 @@
#include <sstream>
-void CommandHandler::handleCommand(Character *player,
- const std::string &command)
+
+
+static void say(const std::string error, Character *player)
{
- // check character permissions
- // finer tuning for checking per command can be done
- // in the handle function for that command
- if (player->getAccountLevel() != AL_GM)
+ GameState::sayTo(player, NULL, error);
+}
+
+static std::string getArgument(std::string &args)
+{
+ std::string argument = "";
+ std::string::size_type pos = args.find(' ');
+ if (pos != std::string::npos)
{
- return;
+ argument = args.substr(0, pos-1);
+ args = args.substr(pos+1);
}
- // get command type, and arguments
- // remove first character (the @)
- std::string::size_type pos = command.find(' ');
- std::string type(command, 1, pos);
- std::string args(command, pos == std::string::npos ? command.size() : pos + 1);
+ return argument;
+}
- // handle the command
- if (type == "help")
- {
- handleHelp(player, args);
- }
- else if (type == "warp")
- {
- handleWarp(player, args);
- }
- else if (type == "item")
- {
- handleItem(player, args);
- }
- else if (type == "drop")
- {
- handleDrop(player, args);
- }
- else if (type == "money")
- {
- handleMoney(player, args);
- }
- else if (type == "spawn")
- {
- handleSpawn(player, args);
- }
- else if (type == "goto")
- {
- handleGoto(player, args);
- }
- else if (type == "recall")
- {
- handleRecall(player, args);
- }
- else if (type == "reload")
- {
- handleReload(player);
- }
- else if (type == "ban")
- {
- handleBan(player, args);
- }
- else if (type == "level")
+static Character* getPlayer(const std::string &player)
+{
+ // get character, via the client, as they may be
+ // on a different game server
+ GameClient *client = gameHandler->getClientByNameSlow(player);
+ if (!client)
{
- handleLevel(player, args);
+ return NULL;
}
- else if (type == "attribute")
+
+ if (client->status != CLIENT_CONNECTED)
{
- handleAttribute(player, args);
+ return NULL;
}
+
+ return client->character;
}
-void CommandHandler::handleHelp(Character *player, std::string &args)
+static void handleHelp(Character *player, std::string &args)
{
if (args == "")
{
- std::stringstream output;
-
if (player->getAccountLevel() >= AL_GM)
{
- // display GM commands
- output << "@help" << std::endl;
- output << "@warp" << std::endl;
- output << "@item" << std::endl;
- output << "@drop" << std::endl;
- output << "@money" << std::endl;
- output << "@spawn" << std::endl;
- output << "@goto" << std::endl;
- output << "@recall" << std::endl;
- output << "@ban" << std::endl;
- output << "@attribute" << std::endl;
+ say("Game Master Commands:", player);
+ say("@help [command]", player);
+ say("@warp <character> <map> <x> <y>", player);
+ say("@item", player);
+ say("@drop", player);
+ say("@money", player);
+ say("@spawn <monster id> <number>", player);
+ say("@goto", player);
+ say("@recall", player);
+ say("@ban", player);
+ say("@attribute", player);
}
if (player->getAccountLevel() == AL_ADMIN)
{
- // display Admin commands
- output << "@reload" << std::endl;
- output << "@level" << std::endl;
+ say("Administrator Commands", player);
+ say("@reload", player);
+ say("@level", player);
}
-
- GameState::sayTo(player, NULL, output.str());
}
else
{
@@ -141,7 +108,7 @@ void CommandHandler::handleHelp(Character *player, std::string &args)
}
}
-void CommandHandler::handleWarp(Character *player, std::string &args)
+static void handleWarp(Character *player, std::string &args)
{
std::stringstream str;
int x, y;
@@ -157,7 +124,7 @@ void CommandHandler::handleWarp(Character *player, std::string &args)
// if any of them are empty strings, no argument was given
if (character == "" || mapstr == "" || xstr == "" || ystr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
@@ -172,7 +139,7 @@ void CommandHandler::handleWarp(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character, or they are offline", player);
+ say("Invalid character, or they are offline", player);
return;
}
}
@@ -188,7 +155,7 @@ void CommandHandler::handleWarp(Character *player, std::string &args)
int id;
if (!utils::isNumeric(mapstr))
{
- errorMsg("Invalid map", player);
+ say("Invalid map", player);
return;
}
@@ -200,20 +167,20 @@ void CommandHandler::handleWarp(Character *player, std::string &args)
map = MapManager::getMap(id);
if (!map)
{
- errorMsg("Invalid map", player);
+ say("Invalid map", player);
return;
}
}
if (!utils::isNumeric(xstr))
{
- errorMsg("Invalid x", player);
+ say("Invalid x", player);
return;
}
if (!utils::isNumeric(ystr))
{
- errorMsg("Invalid y", player);
+ say("Invalid y", player);
return;
}
@@ -229,7 +196,7 @@ void CommandHandler::handleWarp(Character *player, std::string &args)
GameState::warp(other, map, x, y);
}
-void CommandHandler::handleItem(Character *player, std::string &args)
+static void handleItem(Character *player, std::string &args)
{
Character *other;
ItemClass *ic;
@@ -245,7 +212,7 @@ void CommandHandler::handleItem(Character *player, std::string &args)
// check all arguments are there
if (character == "" || itemclass == "" || valuestr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
@@ -260,7 +227,7 @@ void CommandHandler::handleItem(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character or they are offline", player);
+ say("Invalid character or they are offline", player);
return;
}
}
@@ -268,7 +235,7 @@ void CommandHandler::handleItem(Character *player, std::string &args)
// check we have a valid item
if (!utils::isNumeric(itemclass))
{
- errorMsg("Invalid item", player);
+ say("Invalid item", player);
return;
}
@@ -282,13 +249,13 @@ void CommandHandler::handleItem(Character *player, std::string &args)
if (!ic)
{
- errorMsg("Invalid item", player);
+ say("Invalid item", player);
return;
}
if (!utils::isNumeric(valuestr))
{
- errorMsg("Invalid value", player);
+ say("Invalid value", player);
return;
}
@@ -298,7 +265,7 @@ void CommandHandler::handleItem(Character *player, std::string &args)
if (value < 0)
{
- errorMsg("Invalid amount", player);
+ say("Invalid amount", player);
return;
}
@@ -306,7 +273,7 @@ void CommandHandler::handleItem(Character *player, std::string &args)
Inventory(other).insert(ic->getDatabaseID(), value);
}
-void CommandHandler::handleDrop(Character *player, std::string &args)
+static void handleDrop(Character *player, std::string &args)
{
ItemClass *ic;
int value, id;
@@ -319,14 +286,14 @@ void CommandHandler::handleDrop(Character *player, std::string &args)
// check all arguments are there
if (itemclass == "" || valuestr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
// check that itemclass id and value are really integers
if (!utils::isNumeric(itemclass) || !utils::isNumeric(valuestr))
{
- errorMsg("Invalid arguments passed.", player);
+ say("Invalid arguments passed.", player);
return;
}
@@ -339,7 +306,7 @@ void CommandHandler::handleDrop(Character *player, std::string &args)
ic = ItemManager::getItem(id);
if (!ic)
{
- errorMsg("Invalid item", player);
+ say("Invalid item", player);
return;
}
@@ -350,7 +317,7 @@ void CommandHandler::handleDrop(Character *player, std::string &args)
if (value < 0)
{
- errorMsg("Invalid amount", player);
+ say("Invalid amount", player);
return;
}
@@ -361,7 +328,7 @@ void CommandHandler::handleDrop(Character *player, std::string &args)
GameState::insertSafe(item);
}
-void CommandHandler::handleMoney(Character *player, std::string &args)
+static void handleMoney(Character *player, std::string &args)
{
Character *other;
int value;
@@ -374,7 +341,7 @@ void CommandHandler::handleMoney(Character *player, std::string &args)
// check all arguments are there
if (character == "" || valuestr == "")
{
- errorMsg("Invalid number of arguments given", player);
+ say("Invalid number of arguments given", player);
return;
}
@@ -389,7 +356,7 @@ void CommandHandler::handleMoney(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character or they are offline", player);
+ say("Invalid character or they are offline", player);
return;
}
}
@@ -397,7 +364,7 @@ void CommandHandler::handleMoney(Character *player, std::string &args)
// check value is an integer
if (!utils::isNumeric(valuestr))
{
- errorMsg("Invalid argument", player);
+ say("Invalid argument", player);
return;
}
@@ -410,7 +377,7 @@ void CommandHandler::handleMoney(Character *player, std::string &args)
Inventory(other).changeMoney(value);
}
-void CommandHandler::handleSpawn(Character *player, std::string &args)
+static void handleSpawn(Character *player, std::string &args)
{
MonsterClass *mc;
MapComposite *map = player->getMap();
@@ -425,14 +392,14 @@ void CommandHandler::handleSpawn(Character *player, std::string &args)
// check all arguments are there
if (monsterclass == "" || valuestr == "")
{
- errorMsg("Invalid amount of arguments given.", player);
+ say("Invalid amount of arguments given.", player);
return;
}
// check they are really numbers
if (!utils::isNumeric(monsterclass) || !utils::isNumeric(valuestr))
{
- errorMsg("Invalid arguments", player);
+ say("Invalid arguments", player);
return;
}
@@ -445,7 +412,7 @@ void CommandHandler::handleSpawn(Character *player, std::string &args)
mc = MonsterManager::getMonster(id);
if (!mc)
{
- errorMsg("Invalid monster", player);
+ say("Invalid monster", player);
return;
}
@@ -456,7 +423,7 @@ void CommandHandler::handleSpawn(Character *player, std::string &args)
if (value < 0)
{
- errorMsg("Invalid amount", player);
+ say("Invalid amount", player);
return;
}
@@ -475,7 +442,7 @@ void CommandHandler::handleSpawn(Character *player, std::string &args)
}
}
-void CommandHandler::handleGoto(Character *player, std::string &args)
+static void handleGoto(Character *player, std::string &args)
{
Character *other;
@@ -485,7 +452,7 @@ void CommandHandler::handleGoto(Character *player, std::string &args)
// check all arguments are there
if (character == "")
{
- errorMsg("Invalid amount of arguments given.", player);
+ say("Invalid amount of arguments given.", player);
return;
}
@@ -493,7 +460,7 @@ void CommandHandler::handleGoto(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character, or they are offline.", player);
+ say("Invalid character, or they are offline.", player);
return;
}
@@ -503,7 +470,7 @@ void CommandHandler::handleGoto(Character *player, std::string &args)
GameState::warp(player, map, pos.x, pos.y);
}
-void CommandHandler::handleRecall(Character *player, std::string &args)
+static void handleRecall(Character *player, std::string &args)
{
Character *other;
@@ -513,7 +480,7 @@ void CommandHandler::handleRecall(Character *player, std::string &args)
// check all arguments are there
if (character == "")
{
- errorMsg("Invalid amount of arguments given.", player);
+ say("Invalid amount of arguments given.", player);
return;
}
@@ -521,7 +488,7 @@ void CommandHandler::handleRecall(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character, or they are offline.", player);
+ say("Invalid character, or they are offline.", player);
return;
}
@@ -531,7 +498,7 @@ void CommandHandler::handleRecall(Character *player, std::string &args)
GameState::warp(other, map, pos.x, pos.y);
}
-void CommandHandler::handleReload(Character *player)
+static void handleReload(Character *player)
{
// check for valid permissions
// reload the items and monsters
@@ -542,11 +509,11 @@ void CommandHandler::handleReload(Character *player)
}
else
{
- errorMsg("Invalid permissions.", player);
+ say("Invalid permissions.", player);
}
}
-void CommandHandler::handleBan(Character *player, std::string &args)
+static void handleBan(Character *player, std::string &args)
{
Character *other;
int length;
@@ -559,7 +526,7 @@ void CommandHandler::handleBan(Character *player, std::string &args)
// check all arguments are there
if (character == "" || valuestr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
@@ -567,14 +534,14 @@ void CommandHandler::handleBan(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character", player);
+ say("Invalid character", player);
return;
}
// check the length is really an integer
if (!utils::isNumeric(valuestr))
{
- errorMsg("Invalid argument", player);
+ say("Invalid argument", player);
return;
}
@@ -585,7 +552,7 @@ void CommandHandler::handleBan(Character *player, std::string &args)
if (length < 0)
{
- errorMsg("Invalid length", player);
+ say("Invalid length", player);
return;
}
@@ -593,7 +560,7 @@ void CommandHandler::handleBan(Character *player, std::string &args)
accountHandler->banCharacter(other, length);
}
-void CommandHandler::handleLevel(Character *player, std::string &args)
+static void handleLevel(Character *player, std::string &args)
{
Character *other;
int level;
@@ -606,7 +573,7 @@ void CommandHandler::handleLevel(Character *player, std::string &args)
// check all arguments are there
if (character == "" || valuestr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
@@ -621,7 +588,7 @@ void CommandHandler::handleLevel(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character", player);
+ say("Invalid character", player);
return;
}
}
@@ -629,7 +596,7 @@ void CommandHandler::handleLevel(Character *player, std::string &args)
// check the amount is really an integer
if (!utils::isNumeric(valuestr))
{
- errorMsg("Invalid argument", player);
+ say("Invalid argument", player);
return;
}
@@ -640,7 +607,7 @@ void CommandHandler::handleLevel(Character *player, std::string &args)
if (level < 0)
{
- errorMsg("Invalid level", player);
+ say("Invalid level", player);
return;
}
@@ -648,7 +615,7 @@ void CommandHandler::handleLevel(Character *player, std::string &args)
accountHandler->changeAccountLevel(other, level);
}
-void CommandHandler::handleAttribute(Character *player, std::string &args)
+static void handleAttribute(Character *player, std::string &args)
{
Character *other;
int attr, value;
@@ -662,7 +629,7 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
// check all arguments are there
if (character == "" || valuestr == "" || attrstr == "")
{
- errorMsg("Invalid number of arguments given.", player);
+ say("Invalid number of arguments given.", player);
return;
}
@@ -677,7 +644,7 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
other = getPlayer(character);
if (!other)
{
- errorMsg("Invalid character", player);
+ say("Invalid character", player);
return;
}
}
@@ -685,7 +652,7 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
// check they are really integers
if (!utils::isNumeric(valuestr) || !utils::isNumeric(attrstr))
{
- errorMsg("Invalid argument", player);
+ say("Invalid argument", player);
return;
}
@@ -696,7 +663,7 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
if (attr < 0)
{
- errorMsg("Invalid Attribute", player);
+ say("Invalid Attribute", player);
return;
}
@@ -707,7 +674,7 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
if (value < 0)
{
- errorMsg("Invalid amount", player);
+ say("Invalid amount", player);
return;
}
@@ -715,39 +682,74 @@ void CommandHandler::handleAttribute(Character *player, std::string &args)
other->setAttribute(attr, value);
}
-void CommandHandler::errorMsg(const std::string error, Character *player)
+void CommandHandler::handleCommand(Character *player,
+ const std::string &command)
{
- // output an error
- GameState::sayTo(player, NULL, error);
-}
+ // check character permissions
+ // finer tuning for checking per command can be done
+ // in the handle function for that command
+ //if (player->getAccountLevel() >= AL_GM)
+ //{
+ // return;
+ //}
-std::string CommandHandler::getArgument(std::string &args)
-{
- std::string argument = "";
- std::string::size_type pos = args.find(' ');
- if (pos != std::string::npos)
+ // get command type, and arguments
+ // remove first character (the @)
+ std::string::size_type pos = command.find(' ');
+ std::string type(command, 1, pos == std::string::npos ? pos : pos - 1);
+ std::string args(command, pos == std::string::npos ? command.size() : pos + 1);
+
+ // handle the command
+ if (type == "help")
{
- argument = args.substr(0, pos-1);
- args = args.substr(pos+1);
+ handleHelp(player, args);
}
-
- return argument;
-}
-
-Character* CommandHandler::getPlayer(const std::string &player)
-{
- // get character, via the client, as they may be
- // on a different game server
- GameClient *client = gameHandler->getClientByNameSlow(player);
- if (!client)
+ else if (type == "warp")
{
- return NULL;
+ handleWarp(player, args);
}
-
- if (client->status != CLIENT_CONNECTED)
+ else if (type == "item")
{
- return NULL;
+ handleItem(player, args);
+ }
+ else if (type == "drop")
+ {
+ handleDrop(player, args);
+ }
+ else if (type == "money")
+ {
+ handleMoney(player, args);
+ }
+ else if (type == "spawn")
+ {
+ handleSpawn(player, args);
+ }
+ else if (type == "goto")
+ {
+ handleGoto(player, args);
+ }
+ else if (type == "recall")
+ {
+ handleRecall(player, args);
+ }
+ else if (type == "reload")
+ {
+ handleReload(player);
+ }
+ else if (type == "ban")
+ {
+ handleBan(player, args);
+ }
+ else if (type == "level")
+ {
+ handleLevel(player, args);
+ }
+ else if (type == "attribute")
+ {
+ handleAttribute(player, args);
+ }
+ else
+ {
+ say("command not found", player);
}
-
- return client->character;
}
diff --git a/src/game-server/commandhandler.hpp b/src/game-server/commandhandler.hpp
index 04df9019..4226dcda 100644
--- a/src/game-server/commandhandler.hpp
+++ b/src/game-server/commandhandler.hpp
@@ -28,44 +28,12 @@
class Character;
-/**
- * A class to parse and handle user commands
- */
-class CommandHandler
+namespace CommandHandler
{
- public:
- /**
- * Constructor
- */
- CommandHandler() {}
-
- /**
- * Destructor
- */
- ~CommandHandler() {}
-
- /**
- * Parse and handle the given command.
- */
- void handleCommand(Character *player, const std::string &command);
-
- private:
- void handleHelp(Character *player, std::string &args);
- void handleWarp(Character *player, std::string &args);
- void handleItem(Character *player, std::string &args);
- void handleDrop(Character *player, std::string &args);
- void handleMoney(Character *player, std::string &args);
- void handleSpawn(Character *player, std::string &args);
- void handleGoto(Character *player, std::string &args);
- void handleRecall(Character *player, std::string &args);
- void handleReload(Character *player);
- void handleBan(Character *player, std::string &args);
- void handleLevel(Character *player, std::string &args);
- void handleAttribute(Character *player, std::string &args);
-
- void errorMsg(const std::string error, Character *player);
- std::string getArgument(std::string &args);
- Character* getPlayer(const std::string &player);
-};
+ /**
+ * Parse and handle the given command.
+ */
+ void handleCommand(Character *player, const std::string &command);
+}
-#endif //_TMW_COMMANDHANDLER_H
+#endif //_TMW_SERVER_COMMANDHANDLER_H
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index 005c19c3..701c759a 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -193,9 +193,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
if (say[0] == '@')
{
- CommandHandler *commandHandler = new CommandHandler();
- commandHandler->handleCommand(computer.character, say);
- delete commandHandler;
+ CommandHandler::handleCommand(computer.character, say);
break;
}
GameState::sayAround(computer.character, say);