1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
/*
* The Mana World Server
* Copyright 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
* The Mana World is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or any later version.
*
* The Mana World is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with The Mana World; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id$
*/
#include <iostream>
#include <enet/enet.h>
#include "defines.h"
#include "messageout.h"
#include "messagein.h"
#include "packet.h"
#if defined WIN32
#include "../testclient_private.h"
#define PACKAGE_VERSION PRODUCT_VERSION
#endif
void parsePacket(char *data, int recvLength);
ENetHost *client;
ENetAddress addressAccount, addressGame;
ENetPeer *peerAccount, *peerGame;
std::string token;
bool connected = false;
int main(int argc, char *argv[])
{
#if (defined __USE_UNIX98 || defined __FreeBSD__)
#include "../config.h"
#endif
std::cout << "The Mana World Test Client v" << PACKAGE_VERSION << std::endl;
if (enet_initialize () != 0)
{
printf("An error occurred while initializing ENet.\n");
return EXIT_FAILURE;
}
atexit(enet_deinitialize);
client = enet_host_create(NULL /* create a client host */,
3 /* allows 3 outgoing connection */,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
if (client == NULL)
{
printf("An error occurred while trying to create an ENet client host.\n");
exit (EXIT_FAILURE);
}
/* Connect to localhost:9601. */
enet_address_set_host(&addressAccount, "localhost");
addressAccount.port = 9601;
/* Initiate the connection, allocating one channel. */
peerAccount = enet_host_connect(client, &addressAccount, 1);
if (peerAccount == NULL)
{
printf("No available peers for initiating an ENet connection.\n");
exit (EXIT_FAILURE);
}
ENetEvent event;
bool exit = true;
char line[256] = "";
printf("Starting client...\n");
/* Wait up to 1000 milliseconds for an event. */
do {
if (connected) {
int answer = -1;
std::cout << std::endl;
std::cout << "0) Quit 9) Character selection" << std::endl;
std::cout << "1) Register 10) Delete Character" << std::endl;
std::cout << "2) Unregister 11) List Characters" << std::endl;
std::cout << "3) Login 12) Move Character" << std::endl;
std::cout << "4) Logout 13) Say around" << std::endl;
std::cout << "5) Change Password 14) Equip Item" << std::endl;
std::cout << "6) Change Email 15) Ruby Expression" << std::endl;
std::cout << "7) Get Email 16) Enter world" << std::endl;
std::cout << "8) Create character 17) Enter world (GS)" << std::endl;
std::cout << "Choose your option: ";
std::cin >> answer;
std::cin.getline(line, 256); // skip the remaining of the line
MessageOut msg;
int msgDestination = 0; // account server
switch (answer) {
case 0:
// Disconnection
if (connected) {
enet_peer_disconnect(&client->peers[0], 0);
}
exit = true;
break;
case 1:
// Register
msg.writeShort(PAMSG_REGISTER);
// We send the client version
msg.writeString(PACKAGE_VERSION);
std::cout << "Account name: ";
std::cin >> line;
msg.writeString(line);
std::cout << "Password: ";
std::cin >> line;
msg.writeString(line);
std::cout << "Email address: ";
std::cin >> line;
msg.writeString(line);
break;
case 2:
// Unregister (deleting an account)
msg.writeShort(PAMSG_UNREGISTER);
std::cout << "Account name: ";
std::cin >> line;
msg.writeString(line);
std::cout << "Password: ";
std::cin >> line;
msg.writeString(line);
break;
case 3:
// Login
msg.writeShort(PAMSG_LOGIN);
// We send the client version
msg.writeString(PACKAGE_VERSION);
std::cout << "Account name: ";
std::cin >> line;
msg.writeString(line);
std::cout << "Password: ";
std::cin >> line;
msg.writeString(line);
break;
case 4:
// Logout
msg.writeShort(PAMSG_LOGOUT);
std::cout << "Logout" << std::endl;
break;
case 5:
{
// Change Password
msg.writeShort(PAMSG_PASSWORD_CHANGE);
std::cout << "Old Password: ";
std::cin >> line;
msg.writeString(line);
std::cout << "New Password: ";
std::cin >> line;
msg.writeString(line);
std::string line2;
std::cout << "Retype new Password: ";
std::cin >> line2;
if (line != line2)
{
std::cout << "Error: Password mismatch." << std::endl;
goto process_enet;
}
} break;
case 6:
// Change Email
msg.writeShort(PAMSG_EMAIL_CHANGE);
std::cout << "New Email: ";
std::cin >> line;
msg.writeString(line);
break;
case 7:
// Get current Account's Email value
msg.writeShort(PAMSG_EMAIL_GET);
break;
case 8:
{
// Create character
msg.writeShort(PAMSG_CHAR_CREATE);
std::cout << "Name: ";
std::cin >> line;
msg.writeString(line);
std::cout << "Hair Style ID (0 - " << MAX_HAIRSTYLE_VALUE << "): ";
std::cin >> line;
msg.writeByte(atoi(line));
std::cout << "Hair Color ID (0 - " << MAX_HAIRCOLOR_VALUE << "): ";
std::cin >> line;
msg.writeByte(atoi(line));
std::cout << "Gender ID (0 - " << MAX_GENDER_VALUE << "): ";
std::cin >> line;
msg.writeByte(atoi(line));
std::cout << "Strength: ";
std::cin >> line;
msg.writeShort(atoi(line));
std::cout << "Agility: ";
std::cin >> line;
msg.writeShort(atoi(line));
std::cout << "Vitality: ";
std::cin >> line;
msg.writeShort(atoi(line));
std::cout << "Intelligence: ";
std::cin >> line;
msg.writeShort(atoi(line));
std::cout << "Dexterity: ";
std::cin >> line;
msg.writeShort(atoi(line));
std::cout << "Luck: ";
std::cin >> line;
msg.writeShort(atoi(line));
} break;
case 9:
{
// Select character
msg.writeShort(PAMSG_CHAR_SELECT);
std::cout << "Character ID: ";
std::cin >> line;
msg.writeByte(atoi(line));
} break;
case 10:
{
// Delete character
msg.writeShort(PAMSG_CHAR_DELETE);
std::cout << "Character ID: ";
std::cin >> line;
msg.writeByte(atoi(line));
} break;
case 11:
{
// List characters
msg.writeShort(PAMSG_CHAR_LIST);
} break;
case 12:
{
// Move character
long x, y;
std::cout << "X: ";
std::cin >> x;
std::cout << "Y: ";
std::cin >> y;
msg.writeShort(PGMSG_WALK);
msg.writeLong(x);
msg.writeLong(y);
msgDestination = 1;
} break;
case 13:
{
// Chat
msg.writeShort(PGMSG_SAY);
std::cout << "Say: ";
std::cin.getline(line, 256);
line[255] = '\0';
msg.writeString(line);
msgDestination = 1;
} break;
case 14:
{
// Equip
unsigned int itemId;
unsigned int slot;
std::cout << "Item ID: ";
std::cin >> itemId;
std::cout << "Slot: ";
std::cin >> slot;
msg.writeShort(PGMSG_EQUIP);
msg.writeLong(itemId);
msg.writeByte(slot);
msgDestination = 1;
} break;
case 15:
{
std::cout << "Expr: ";
std::cin >> line;
msg.writeShort(0x800);
msg.writeString(line);
} break;
case 16:
{
msg.writeShort(PAMSG_ENTER_WORLD);
} break;
case 17:
{
msg.writeShort(PGMSG_CONNECT);
msg.writeString(token, 32);
msgDestination = 1;
} break;
default:
goto process_enet;
} // end switch
// Send prepared message
if (!exit && connected) {
ENetPacket *packet = enet_packet_create(
msg.getData(),
msg.getDataSize() + 1,
ENET_PACKET_FLAG_RELIABLE);
// Send the packet to the peer over channel id 0.
enet_peer_send(msgDestination == 0 ? peerAccount : peerGame, 0, packet);
} // end if
} // end if
process_enet:
while (enet_host_service(client, &event, 1000)) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
printf("Connected to server\n");
connected = true;
exit = false;
break;
case ENET_EVENT_TYPE_RECEIVE:
std::cout << "A packet of length "
<< event.packet->dataLength
<< " was received from "
<< event.peer->address.host
<< std::endl;
parsePacket((char *)event.packet->data,
event.packet->dataLength);
// Clean up the packet now that we're done using it.
enet_packet_destroy(event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
printf("Disconnected.\n");
connected = false;
exit = true;
break;
default:
//printf("Unhandled enet event.\n");
break;
} // end switch
} // end while
} while (!exit);
if (connected) {
// The disconnection attempt didn't succeed yet. Force disconnection.
enet_peer_reset(&client->peers[0]);
}
enet_host_destroy(client);
return 0;
}
void parsePacket(char *data, int recvLength) {
// Response handling
// Transforming it into a MessageIn object
if (recvLength >= 2) {
Packet *packet = new Packet(data, recvLength);
MessageIn msg(packet); // (MessageIn frees packet)
switch (msg.getId()) {
case APMSG_REGISTER_RESPONSE:
// Register
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Account registered." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Account registering: Invalid username, password, or email." << std::endl;
break;
case REGISTER_INVALID_VERSION:
std::cout << "Account registering: Invalid version." << std::endl;
break;
case REGISTER_EXISTS_USERNAME:
std::cout << "Account registering: Username already exists." << std::endl;
break;
case REGISTER_EXISTS_EMAIL:
std::cout << "Account registering: Email already exists." << std::endl;
break;
default:
std::cout << "Account registering: Unknown error." << std::endl;
break;
}
break;
case APMSG_UNREGISTER_RESPONSE:
// Register
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Account unregistered." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Account unregistering: Invalid username or password." << std::endl;
break;
default:
std::cout << "Account unregistering: Unknown error." << std::endl;
break;
}
break;
case APMSG_LOGIN_RESPONSE:
// Register
switch (msg.readByte()) {
case ERRMSG_OK:
unsigned char charNumber;
charNumber = msg.readByte();
std::cout << "Account has " << int(charNumber) << " characters." << std::endl;
for (unsigned int i = 0; i < charNumber; i++) {
if (i >0) std::cout << ", ";
std::cout << msg.readString();
}
std::cout << "." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Login: Invalid username or password." << std::endl;
break;
case LOGIN_INVALID_VERSION:
std::cout << "Login: Invalid Version." << std::endl;
break;
case ERRMSG_FAILURE:
std::cout << "Login: Already logged with another account." << std::endl;
break;
default:
std::cout << "Login: Unknown error." << std::endl;
break;
}
break;
case APMSG_LOGOUT_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Logout..." << std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Logout: Unsuccessful." << std::endl;
break;
default:
std::cout << "Logout: Unknown error." << std::endl;
}
} break;
case APMSG_PASSWORD_CHANGE_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Password correctly changed." << std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Password change: Not logged in." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Password change: New password is invalid." << std::endl;
break;
case ERRMSG_FAILURE:
std::cout << "Password change: Old password is invalid." << std::endl;
break;
default:
std::cout << "Password change: Unknown error." << std::endl;
break;
}
} break;
case APMSG_EMAIL_CHANGE_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Email correctly changed." << std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Email change: Not logged in." << std::endl;
break;
case EMAILCHG_EXISTS_EMAIL:
std::cout << "Email change: Email already exists." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Email change: New Email is invalid." << std::endl;
break;
default:
std::cout << "Email change: Unknown error." << std::endl;
break;
}
} break;
case APMSG_EMAIL_GET_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Current Email: " << msg.readString() << std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Get Email: Not logged in." << std::endl;
break;
default:
std::cout << "Get Email: Unknown error." << std::endl;
break;
}
} break;
case APMSG_CHAR_CREATE_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Character Created successfully." << std::endl;
break;
case CREATE_EXISTS_NAME:
std::cout << "Character Creation: Character's name already exists."
<< std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Character Creation: Not logged in." << std::endl;
break;
case CREATE_TOO_MUCH_CHARACTERS:
std::cout << "Character Creation: Too much characters." << std::endl;
break;
case CREATE_INVALID_HAIRSTYLE:
std::cout << "Character Creation: Invalid Hair Style Value." << std::endl;
break;
case CREATE_INVALID_HAIRCOLOR:
std::cout << "Character Creation: Invalid Hair Color Value." << std::endl;
break;
case CREATE_INVALID_GENDER:
std::cout << "Character Creation: Invalid Gender Value." << std::endl;
break;
case CREATE_RAW_STATS_EQUAL_TO_ZERO:
std::cout << "Character Creation: a Statistic is equal to zero." << std::endl;
break;
case CREATE_RAW_STATS_INVALID_DIFF:
std::cout << "Character Creation: Statistics disproportionned." << std::endl;
break;
case CREATE_RAW_STATS_TOO_HIGH:
std::cout << "Character Creation: Statistics too high for level 1." << std::endl;
break;
case CREATE_RAW_STATS_TOO_LOW:
std::cout << "Character Creation: Statistics too low for level 1." << std::endl;
break;
default:
std::cout << "Character Creation: Unknown error." << std::endl;
break;
}
} break;
case APMSG_CHAR_DELETE_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
std::cout << "Character deleted." << std::endl;
break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Character Deletion: Character's ID doesn't exist."
<< std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Character Deletion: Not logged in." << std::endl;
break;
default:
std::cout << "Character Deletion: Unknown error." << std::endl;
break;
}
} break;
case APMSG_CHAR_SELECT_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
{
std::cout << "Character selected successfully.";
std::cout << std::endl;
std::cout << "Current Map: ";
std::cout << msg.readString() << " (X:";
std::cout << (int)msg.readShort() << ", Y:";
std::cout << (int)msg.readShort() << ")" << std::endl;
} break;
case ERRMSG_INVALID_ARGUMENT:
std::cout << "Character Selection: invalid ID."
<< std::endl;
break;
case ERRMSG_NO_LOGIN:
std::cout << "Character Selection: Not logged in." << std::endl;
break;
default:
std::cout << "Character Selection: Unknown error." << std::endl;
break;
}
} break;
case APMSG_CHAR_LIST_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
{
unsigned char charNumber;
charNumber = msg.readByte();
std::cout << "Character List:" << std::endl
<< "---------------" << std::endl;
std::cout << int(charNumber) << " character(s) in the account."
<< std::endl;
char charID;
for (unsigned int i = 0; i < charNumber; i++) {
charID = msg.readByte();
std::cout << int(charID) << ". "
<< msg.readString() << ":" << std::endl;
std::cout << "Gender: " << int(msg.readByte()) << ", ";
std::cout << "Hair Style: " << int(msg.readByte()) << ", ";
std::cout << "Hair Color: " << int(msg.readByte()) << ", "
<< std::endl;
std::cout << "Level: " << int(msg.readByte()) << ", ";
std::cout << "Money: " << int(msg.readShort()) << ", "
<< std::endl;
std::cout << "Strength: " << int(msg.readShort()) << ", ";
std::cout << "Agility: " << int(msg.readShort()) << ", ";
std::cout << "Vitality: " << int(msg.readShort()) << ", "
<< std::endl;
std::cout << "Intelligence: " << int(msg.readShort()) << ", ";
std::cout << "Dexterity: " << int(msg.readShort()) << ", ";
std::cout << "Luck: " << int(msg.readShort()) << ". "
<< std::endl;
std::cout << "Current Map: " << msg.readString() << " (X:";
std::cout << int(msg.readShort()) << ", Y:" << int(msg.readShort()) << ")."
<< std::endl << std::endl;
}
} break;
default:
std::cout << "Character List: Unknown error." << std::endl;
break;
}
} break;
case GPMSG_SAY:
{
std::string who = msg.readString();
std::cout << who << " says around:" << std::endl
<< msg.readString() << std::endl;
} break;
case GPMSG_BEING_LEAVE:
{
switch (msg.readByte()) {
case OBJECT_PLAYER:
std::cout << "Player " << msg.readLong() << " left map." << std::endl;
break;
default:
std::cout << "Unknown being left map." << std::endl;
}
} break;
case GPMSG_BEING_ENTER:
{
switch (msg.readByte()) {
case OBJECT_PLAYER:
std::cout << "Player " << msg.readLong() << " entered map" << std::endl;
std::cout << " name: " << msg.readString() << std::endl;
std::cout << " hair style: " << (int)msg.readByte() << std::endl;
std::cout << " hair color: " << (int)msg.readByte() << std::endl;
std::cout << " gender: " << (int)msg.readByte() << std::endl;
break;
default:
std::cout << "Unknown being entered map." << std::endl;
}
} break;
case APMSG_ENTER_WORLD_RESPONSE:
{
switch (msg.readByte()) {
case ERRMSG_OK:
{
std::string server = msg.readString();
enet_address_set_host(&addressGame, server.c_str());
addressGame.port = msg.readShort();
peerGame = enet_host_connect(client, &addressGame, 1);
token = msg.readString(32);
connected = false;
std::cout << "Connecting to " << server << ':' << addressGame.port << std::endl;
} break;
case ERRMSG_NO_LOGIN:
std::cout << "Enter world: Not logged in." << std::endl;
break;
case ERRMSG_NO_CHARACTER_SELECTED:
std::cout << "Enter world: No character selected." << std::endl;
break;
default:
std::cout << "Enter world: Unknown error." << std::endl;
break;
}
} break;
case XXMSG_INVALID:
std::cout << "The server does not understand our message." << std::endl;
break;
default:
std::cout << "Unknown message received. Id: " << msg.getId() << "." << std::endl;
break;
} // end switch MessageId
} // end if recLength > 2 (MessageLength > 2)
}
|