GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/tmwa/charserverrecv.cpp Lines: 1 132 0.8 %
Date: 2020-06-04 Branches: 0 62 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "net/tmwa/charserverrecv.h"
24
25
#include "client.h"
26
#include "configuration.h"
27
#include "settings.h"
28
29
#include "gui/windows/charcreatedialog.h"
30
#include "gui/windows/okdialog.h"
31
32
#include "gui/widgets/createwidget.h"
33
34
#include "net/character.h"
35
#include "net/charserverhandler.h"
36
#include "net/messagein.h"
37
#include "net/serverfeatures.h"
38
39
#include "net/ea/token.h"
40
41
#include "net/tmwa/gamehandler.h"
42
#include "net/tmwa/loginhandler.h"
43
#include "net/tmwa/network.h"
44
#include "net/tmwa/sprite.h"
45
46
#include "resources/iteminfo.h"
47
48
#include "resources/db/itemdb.h"
49
50
#include "utils/dtor.h"
51
#include "utils/gettext.h"
52
53
#include "debug.h"
54
55
namespace TmwAthena
56
{
57
58
extern ServerInfo mapServer;
59
60
extern ServerInfo charServer;
61
62
void CharServerRecv::readPlayerData(Net::MessageIn &msg,
63
                                    Net::Character *const character)
64
{
65
    if (character == nullptr)
66
        return;
67
68
    const Token &token =
69
        static_cast<LoginHandler*>(loginHandler)->getToken();
70
71
    LocalPlayer *const tempPlayer = new LocalPlayer(
72
        msg.readBeingId("account id"), BeingTypeId_zero);
73
74
    PlayerInfoBackend &data = character->data;
75
    data.mAttributes[Attributes::PLAYER_EXP] = msg.readInt32("base exp");
76
    data.mAttributes[Attributes::MONEY] = msg.readInt32("money");
77
    data.mAttributes[Attributes::PLAYER_JOB_EXP] = msg.readInt32("job exp");
78
    data.mAttributes[Attributes::PLAYER_JOB_LEVEL] =
79
        msg.readInt32("job level");
80
81
    const int shoes = msg.readInt16("shoes");
82
    const int gloves = msg.readInt16("gloves");
83
    const int cape = msg.readInt16("cape");
84
    const int misc1 = msg.readInt16("misc1");
85
86
    msg.readInt32("option");
87
    tempPlayer->setKarma(msg.readInt32("karma"));
88
    tempPlayer->setManner(msg.readInt32("manner"));
89
    msg.readInt16("character points left");
90
91
    data.mAttributes[Attributes::PLAYER_HP] = msg.readInt16("hp");
92
    data.mAttributes[Attributes::PLAYER_MAX_HP] = msg.readInt16("max hp");
93
    data.mAttributes[Attributes::PLAYER_MP] = msg.readInt16("mp");
94
    data.mAttributes[Attributes::PLAYER_MAX_MP] = msg.readInt16("max mp");
95
96
    msg.readInt16("speed");
97
    const uint16_t race = msg.readInt16("class");
98
    const uint8_t hairStyle = msg.readUInt8("hair style");
99
    const uint16_t look = msg.readUInt8("look");
100
    tempPlayer->setSubtype(fromInt(race, BeingTypeId), look);
101
    const uint16_t weapon = msg.readInt16("weapon");
102
    tempPlayer->setSpriteId(SPRITE_BODY,
103
        weapon);
104
    tempPlayer->setWeaponId(weapon);
105
106
    data.mAttributes[Attributes::PLAYER_BASE_LEVEL] = msg.readInt16("level");
107
108
    msg.readInt16("skill point");
109
    const int bottomClothes = msg.readInt16("bottom clothes");
110
    const int shield = msg.readInt16("shield");
111
112
    const int hat = msg.readInt16("hat");
113
    const int topClothes = msg.readInt16("top clothes");
114
115
    const ItemColor hairColor = fromInt(
116
        msg.readUInt8("hair color"), ItemColor);
117
    msg.readUInt8("unused");
118
    if (hairStyle == 0)
119
    {
120
        tempPlayer->unSetSprite(SPRITE_HAIR_COLOR);
121
    }
122
    else
123
    {
124
        tempPlayer->setSpriteColor(SPRITE_HAIR_COLOR,
125
            hairStyle * -1,
126
            ItemDB::get(-hairStyle).getDyeColorsString(hairColor));
127
    }
128
    tempPlayer->setHairColor(hairColor);
129
130
    const int misc2 = msg.readInt16("misc2");
131
    tempPlayer->setName(msg.readString(24, "name"));
132
133
    character->dummy = tempPlayer;
134
135
    character->data.mStats[Attributes::PLAYER_STR].base = msg.readUInt8("str");
136
    character->data.mStats[Attributes::PLAYER_AGI].base = msg.readUInt8("agi");
137
    character->data.mStats[Attributes::PLAYER_VIT].base = msg.readUInt8("vit");
138
    character->data.mStats[Attributes::PLAYER_INT].base = msg.readUInt8("int");
139
    character->data.mStats[Attributes::PLAYER_DEX].base = msg.readUInt8("dex");
140
    character->data.mStats[Attributes::PLAYER_LUK].base = msg.readUInt8("luk");
141
142
    tempPlayer->setSpriteId(SPRITE_HAIR,
143
        shoes);
144
    tempPlayer->setSpriteId(SPRITE_SHOES,
145
        gloves);
146
    tempPlayer->setSpriteId(SPRITE_SHIELD,
147
        cape);
148
    tempPlayer->setSpriteId(SPRITE_HEAD_TOP,
149
        misc1);
150
    tempPlayer->setSpriteId(SPRITE_WEAPON,
151
        bottomClothes);
152
    tempPlayer->setSpriteId(SPRITE_FLOOR,
153
        shield);
154
    tempPlayer->setSpriteId(SPRITE_CLOTHES_COLOR,
155
        hat);
156
    tempPlayer->setSpriteId(SPRITE_HEAD_BOTTOM,
157
        topClothes);
158
    tempPlayer->setSpriteId(SPRITE_HEAD_MID,
159
        misc2);
160
161
    character->slot = msg.readUInt8("slot");
162
    const uint8_t sex = CAST_U8(msg.readUInt8("gender"));
163
    if (serverFeatures->haveCreateCharGender())
164
        tempPlayer->setGender(Being::intToGender(sex));
165
    else
166
        tempPlayer->setGender(token.sex);
167
}
168
169
void CharServerRecv::processCharLogin(Net::MessageIn &msg)
170
{
171
    BLOCK_START("CharServerRecv::processCharLogin")
172
173
    msg.readInt16("len");
174
    const int slots = msg.readInt16("slots");
175
    if (slots > 0 && slots < 30)
176
        loginData.characterSlots = CAST_U16(slots);
177
178
    msg.skip(18, "unused");
179
180
    delete_all(Net::CharServerHandler::mCharacters);
181
    Net::CharServerHandler::mCharacters.clear();
182
183
    // Derive number of characters from message length
184
    const int count = (msg.getLength() - 24) / 106;
185
186
    for (int i = 0; i < count; ++i)
187
    {
188
        Net::Character *const character = new Net::Character;
189
        readPlayerData(msg, character);
190
        Net::CharServerHandler::mCharacters.push_back(character);
191
        if (character->dummy != nullptr)
192
        {
193
            logger->log("CharServer: Player: %s (%d)",
194
                character->dummy->getName().c_str(), character->slot);
195
        }
196
    }
197
198
    client->setState(State::CHAR_SELECT);
199
    BLOCK_END("CharServerRecv::processCharLogin")
200
}
201
202
void CharServerRecv::processCharMapInfo(Net::MessageIn &restrict msg)
203
{
204
    Network *const network = Network::mInstance;
205
    ServerInfo &server = mapServer;
206
    BLOCK_START("CharServerRecv::processCharMapInfo")
207
    PlayerInfo::setCharId(msg.readInt32("char id?"));
208
    GameHandler::setMap(msg.readString(16, "map name"));
209
    if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
210
    {
211
        msg.readInt32("ip address");
212
        server.hostname = settings.serverName;
213
    }
214
    else
215
    {
216
        server.hostname = ipToString(msg.readInt32("ip address"));
217
    }
218
    server.port = msg.readInt16("port");
219
    server.althostname = charServer.althostname;
220
221
    // Prevent the selected local player from being deleted
222
    localPlayer = Net::CharServerHandler::mSelectedCharacter->dummy;
223
    PlayerInfo::setBackend(Net::CharServerHandler::mSelectedCharacter->data);
224
225
    Net::CharServerHandler::mSelectedCharacter->dummy = nullptr;
226
227
    charServerHandler->clear();
228
    Net::CharServerHandler::updateCharSelectDialog();
229
230
    if (network != nullptr)
231
        network->disconnect();
232
    client->setState(State::CONNECT_GAME);
233
    BLOCK_END("CharServerRecv::processCharMapInfo")
234
}
235
236
void CharServerRecv::processChangeMapServer(Net::MessageIn &msg)
237
{
238
    Network *const network = Network::mInstance;
239
    ServerInfo &server = mapServer;
240
    BLOCK_START("CharServerRecv::processChangeMapServer")
241
    if (network == nullptr)
242
    {
243
        BLOCK_END("CharServerRecv::processChangeMapServer")
244
        return;
245
    }
246
    GameHandler::setMap(msg.readString(16, "map name"));
247
    const int x = msg.readInt16("x");
248
    const int y = msg.readInt16("y");
249
    if (config.getBoolValue("usePersistentIP") || settings.persistentIp)
250
    {
251
        msg.readInt32("ip address");
252
        server.hostname = settings.serverName;
253
    }
254
    else
255
    {
256
        server.hostname = ipToString(msg.readInt32("ip address"));
257
    }
258
    server.port = msg.readInt16("port");
259
260
    network->disconnect();
261
    client->setState(State::CHANGE_MAP);
262
    if (localPlayer != nullptr)
263
    {
264
        localPlayer->setTileCoords(x, y);
265
        localPlayer->setMap(nullptr);
266
    }
267
    BLOCK_END("CharServerRecv::processChangeMapServer")
268
}
269
270
void CharServerRecv::processCharCreate(Net::MessageIn &msg)
271
{
272
    BLOCK_START("CharServerRecv::processCharCreate")
273
    Net::Character *const character = new Net::Character;
274
    readPlayerData(msg, character);
275
    Net::CharServerHandler::mCharacters.push_back(character);
276
277
    Net::CharServerHandler::updateCharSelectDialog();
278
279
    // Close the character create dialog
280
    Net::CharServerHandler::mCharCreateDialog->scheduleDelete();
281
    Net::CharServerHandler::mCharCreateDialog = nullptr;
282
    BLOCK_END("CharServerRecv::processCharCreate")
283
}
284
285
void CharServerRecv::processCharDeleteFailed(Net::MessageIn &msg)
286
{
287
    BLOCK_START("CharServerRecv::processCharDeleteFailed")
288
    Net::CharServerHandler::unlockCharSelectDialog();
289
    msg.readUInt8("error");
290
    CREATEWIDGET(OkDialog,
291
        // TRANSLATORS: error header
292
        _("Error"),
293
        // TRANSLATORS: error message
294
        _("Failed to delete character."),
295
        // TRANSLATORS: ok dialog button
296
        _("OK"),
297
        DialogType::ERROR,
298
        Modal_true,
299
        ShowCenter_true,
300
        nullptr,
301
        260);
302
    BLOCK_END("CharServerRecv::processCharDeleteFailed")
303
}
304
305
2
}  // namespace TmwAthena