00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "net/tmwserv/charserverhandler.h"
00023
00024 #include "net/tmwserv/connection.h"
00025 #include "net/tmwserv/protocol.h"
00026
00027 #include "net/tmwserv/accountserver/accountserver.h"
00028 #include "net/tmwserv/accountserver/account.h"
00029
00030 #include "net/messagein.h"
00031
00032 #include "game.h"
00033 #include "localplayer.h"
00034 #include "log.h"
00035 #include "logindata.h"
00036 #include "main.h"
00037
00038 #include "gui/charcreatedialog.h"
00039 #include "gui/ok_dialog.h"
00040
00041 #include "utils/gettext.h"
00042
00043 extern Net::Connection *gameServerConnection;
00044 extern Net::Connection *chatServerConnection;
00045
00046 Net::CharHandler *charHandler;
00047
00048 namespace TmwServ {
00049
00050 CharServerHandler::CharServerHandler():
00051 mCharCreateDialog(0)
00052 {
00053 static const Uint16 _messages[] = {
00054 APMSG_CHAR_CREATE_RESPONSE,
00055 APMSG_CHAR_DELETE_RESPONSE,
00056 APMSG_CHAR_INFO,
00057 APMSG_CHAR_SELECT_RESPONSE,
00058 0
00059 };
00060 handledMessages = _messages;
00061 charHandler = this;
00062 }
00063
00064 void CharServerHandler::handleMessage(MessageIn &msg)
00065 {
00066 int slot;
00067 LocalPlayer *tempPlayer;
00068
00069 switch (msg.getId())
00070 {
00071 case APMSG_CHAR_CREATE_RESPONSE:
00072 handleCharCreateResponse(msg);
00073 break;
00074
00075 case APMSG_CHAR_DELETE_RESPONSE:
00076 {
00077 int errMsg = msg.readInt8();
00078
00079 if (errMsg == ERRMSG_OK)
00080 {
00081 delete mCharInfo->getEntry();
00082 mCharInfo->setEntry(0);
00083 mCharInfo->unlock();
00084 new OkDialog("Info", "Player deleted");
00085 }
00086
00087 else
00088 {
00089 std::string message = "";
00090 switch (errMsg)
00091 {
00092 case ERRMSG_NO_LOGIN:
00093 message = "Not logged in";
00094 break;
00095 case ERRMSG_INVALID_ARGUMENT:
00096 message = "Selection out of range";
00097 break;
00098 default:
00099 message = "Unknown error";
00100 }
00101 mCharInfo->unlock();
00102 new OkDialog("Error", message);
00103 }
00104 }
00105 break;
00106
00107 case APMSG_CHAR_INFO:
00108 tempPlayer = readPlayerData(msg, slot);
00109 mCharInfo->unlock();
00110 mCharInfo->select(slot);
00111 mCharInfo->setEntry(tempPlayer);
00112
00113
00114 if (mCharCreateDialog)
00115 {
00116 mCharCreateDialog->scheduleDelete();
00117 mCharCreateDialog = 0;
00118 }
00119 break;
00120
00121 case APMSG_CHAR_SELECT_RESPONSE:
00122 handleCharSelectResponse(msg);
00123 break;
00124 }
00125 }
00126
00127 void CharServerHandler::handleCharCreateResponse(MessageIn &msg)
00128 {
00129 int errMsg = msg.readInt8();
00130
00131
00132 if (errMsg != ERRMSG_OK)
00133 {
00134 std::string message = "";
00135 switch (errMsg)
00136 {
00137 case ERRMSG_NO_LOGIN:
00138 message = "Not logged in";
00139 break;
00140 case CREATE_TOO_MUCH_CHARACTERS:
00141 message = "No empty slot";
00142 break;
00143 case ERRMSG_INVALID_ARGUMENT:
00144 message = "Invalid name";
00145 break;
00146 case CREATE_EXISTS_NAME:
00147 message = "Character's name already exists";
00148 break;
00149 case CREATE_INVALID_HAIRSTYLE:
00150 message = "Invalid hairstyle";
00151 break;
00152 case CREATE_INVALID_HAIRCOLOR:
00153 message = "Invalid hair color";
00154 break;
00155 case CREATE_INVALID_GENDER:
00156 message = "Invalid gender";
00157 break;
00158 case CREATE_RAW_STATS_TOO_HIGH:
00159 message = "Character's stats are too high";
00160 break;
00161 case CREATE_RAW_STATS_TOO_LOW:
00162 message = "Character's stats are too low";
00163 break;
00164 case CREATE_RAW_STATS_EQUAL_TO_ZERO:
00165 message = "One stat is zero";
00166 break;
00167 default:
00168 message = "Unknown error";
00169 break;
00170 }
00171 new OkDialog("Error", message);
00172 }
00173
00174 if (mCharCreateDialog)
00175 mCharCreateDialog->unlock();
00176 }
00177
00178 void CharServerHandler::handleCharSelectResponse(MessageIn &msg)
00179 {
00180 int errMsg = msg.readInt8();
00181
00182 if (errMsg == ERRMSG_OK)
00183 {
00184 token = msg.readString(32);
00185 std::string gameServer = msg.readString();
00186 unsigned short gameServerPort = msg.readInt16();
00187 std::string chatServer = msg.readString();
00188 unsigned short chatServerPort = msg.readInt16();
00189
00190 logger->log("Game server: %s:%d", gameServer.c_str(), gameServerPort);
00191 logger->log("Chat server: %s:%d", chatServer.c_str(), chatServerPort);
00192
00193 gameServerConnection->connect(gameServer, gameServerPort);
00194 chatServerConnection->connect(chatServer, chatServerPort);
00195
00196
00197 player_node = mCharInfo->getEntry();
00198 int slot = mCharInfo->getPos();
00199 mCharInfo->unlock();
00200 mCharInfo->select(0);
00201
00202 do {
00203 LocalPlayer *tmp = mCharInfo->getEntry();
00204 if (tmp != player_node)
00205 {
00206 delete tmp;
00207 mCharInfo->setEntry(0);
00208 }
00209 mCharInfo->next();
00210 } while (mCharInfo->getPos());
00211 mCharInfo->select(slot);
00212
00213 mCharInfo->clear();
00214
00215 state = STATE_CONNECT_GAME;
00216 }
00217 else if(errMsg == ERRMSG_FAILURE)
00218 {
00219 errorMessage = _("No gameservers are available.");
00220 mCharInfo->clear();
00221 state = STATE_ERROR;
00222 }
00223 }
00224
00225 LocalPlayer* CharServerHandler::readPlayerData(MessageIn &msg, int &slot)
00226 {
00227 LocalPlayer *tempPlayer = new LocalPlayer;
00228 slot = msg.readInt8();
00229 tempPlayer->setName(msg.readString());
00230 tempPlayer->setGender(msg.readInt8() == GENDER_MALE ? GENDER_MALE : GENDER_FEMALE);
00231 int hs = msg.readInt8(), hc = msg.readInt8();
00232 tempPlayer->setHairStyle(hs, hc);
00233 tempPlayer->setLevel(msg.readInt16());
00234 tempPlayer->setCharacterPoints(msg.readInt16());
00235 tempPlayer->setCorrectionPoints(msg.readInt16());
00236 tempPlayer->setMoney(msg.readInt32());
00237
00238 for (int i = 0; i < 7; i++)
00239 {
00240 tempPlayer->setAttributeBase(i, msg.readInt8());
00241 }
00242
00243 return tempPlayer;
00244 }
00245
00246 void CharServerHandler::setCharCreateDialog(CharCreateDialog *window)
00247 {
00248 mCharCreateDialog = window;
00249
00250 if (!mCharCreateDialog) return;
00251
00252 std::vector<std::string> attributes;
00253 attributes.push_back(_("Strength:"));
00254 attributes.push_back(_("Agility:"));
00255 attributes.push_back(_("Dexterity:"));
00256 attributes.push_back(_("Vitality:"));
00257 attributes.push_back(_("Intelligence:"));
00258 attributes.push_back(_("Willpower:"));
00259
00260 mCharCreateDialog->setAttributes(attributes, 60, 1, 20);
00261 }
00262
00263 void CharServerHandler::chooseCharacter(int slot, LocalPlayer* character)
00264 {
00265 Net::AccountServer::Account::selectCharacter(slot);
00266 }
00267
00268 void CharServerHandler::newCharacter(const std::string &name, int slot, bool gender,
00269 int hairstyle, int hairColor, std::vector<int> stats)
00270 {
00271 Net::AccountServer::Account::createCharacter(name, hairstyle, hairColor,
00272 gender,
00273 stats[0],
00274 stats[1],
00275 stats[2],
00276 stats[3],
00277 stats[4],
00278 stats[5]
00279 );
00280 }
00281
00282 void CharServerHandler::deleteCharacter(int slot, LocalPlayer* character)
00283 {
00284 Net::AccountServer::Account::deleteCharacter(slot);
00285 }
00286
00287 }