00001
00002
00003 #include "application_client.h"
00004 #include "commander_client.h"
00005 #include "log.h"
00006 #include "settings.h"
00007 #include "translator.h"
00008
00009 using namespace std;
00010
00011
00012 #define TEST_CMD(name, function) \
00013 if ( cmd == name ) \
00014 { \
00015 function(s); \
00016 return; \
00017 }
00018
00019 void CommanderClient::processCommand(const std::string &command, int playerID)
00020 {
00021
00022 if (command.size() > 0 && command[0] == '/')
00023 {
00024 command_to_server(command);
00025 return;
00026 }
00027
00028
00029
00030 stringstream s(command);
00031 string cmd;
00032
00033 LOGD << "Processing command " << command << endl;
00034
00035 s >> cmd;
00036
00037
00038 TEST_CMD("quit", command_quit);
00039 TEST_CMD("help", command_help);
00040 TEST_CMD("getvar", command_getvar);
00041 TEST_CMD("setvar", command_setvar);
00042 TEST_CMD("players", command_players);
00043
00044
00045 getAppClient().getConsole().addConsoleMessage( _("Unknown command") );
00046 }
00047
00048
00049 void CommanderClient::command_to_server(const std::string &args)
00050 {
00051
00052 }
00053
00054 void CommanderClient::command_quit(std::stringstream &args)
00055 {
00056 getAppClient().getGame().endFun();
00057 }
00058
00059 void CommanderClient::command_help(std::stringstream &args)
00060 {
00061 Console &c = getAppClient().getConsole();
00062
00063 c.addConsoleMessage( _("Help for BJS console") );
00064 c.addConsoleMessage( _(" commands: quit, help, getvar, setvar, players") );
00065 }
00066
00067 void CommanderClient::command_getvar(std::stringstream &args)
00068 {
00069 string varname;
00070 args >> varname;
00071
00072 if ( !g_settings.exist(varname.c_str()) )
00073 {
00074 getAppClient().getConsole().addConsoleMessage( _("Unknown variable") );
00075 return;
00076 }
00077
00078 getAppClient().getConsole().addConsoleMessage( g_settings.getString(varname.c_str()) );
00079 }
00080
00081 void CommanderClient::command_setvar(std::stringstream &args)
00082 {
00083 string varname, value;
00084 args >> varname >> value;
00085
00086 if ( !g_settings.exist(varname.c_str()) )
00087 {
00088 getAppClient().getConsole().addConsoleMessage( _("Unknown variable") );
00089 return;
00090 }
00091
00092 g_settings.set(varname, value);
00093 }
00094
00095 void CommanderClient::command_players(std::stringstream &args)
00096 {
00097 vector<int> &pl = getAppClient().getGame().getValidPlayersID();
00098 Console &c = getAppClient().getConsole();
00099
00100 stringstream msg;
00101 msg << _("Number of players: ") << pl.size();
00102 c.addConsoleMessage( msg.str() );
00103
00104 for (int i = 0; i < (int)pl.size(); ++i)
00105 {
00106 stringstream msg;
00107 Player &p = getAppClient().getGame().getPlayer(pl[i]);
00108 msg << p.getID() << " " << p.getName();
00109 c.addConsoleMessage(msg.str());
00110 }
00111 }
00112