This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMOnlineRoom.cpp
827 lines (725 loc) · 20.9 KB
/
SMOnlineRoom.cpp
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#include "MercuryString.h"
#include "SMOnlineRoom.h"
#include "SMOnlineGameRoom.h"
#include "SimpleLog.h"
#include "SQLfunctions.h"
#include "SMOnlineServer.h"
#include "Crash/crashDefines.h"
#include "MercuryINI.h"
SMOnlineRoom::SMOnlineRoom(const MString &name, const MString &description, bool allowgamerooms)
: m_roomType(ROOMChat), m_allowgamerooms(allowgamerooms), m_state(RS_OPEN), m_maxPlayers(16),
m_title(name), m_description(description)
{
LoadFromIni();
}
SMOnlineRoom::~SMOnlineRoom()
{
while (m_clients.size() > 0)
RemoveClient(m_clients.size()-1);
while (m_joinrooms.size() > 0)
UnjoinToRoom(m_joinrooms[m_joinrooms.size()-1]->m_title);
}
void SMOnlineRoom::LoadFromIni()
{
m_commonColor = PREFSMAN->GetValueS("ChatOptions", "CommonColorCode", "|c0", 1);
m_baseColor = PREFSMAN->GetValueS("ChatOptions", "Color", "FFFFFF", 1);
m_roomNameLength = PREFSMAN->GetValueI("ServerOptions", "RoomNameLength", 15, 1);
m_roomKickTime = PREFSMAN->GetValueI("ServerOptions", "RoomKickTime", 60, 1);
}
bool SMOnlineRoom::AddClient(SMOnlineClient *client, const MString& pw, bool ignorePass)
{
//IN_GAME is the only state that we are not allowed to add clients
if (m_state == RS_INGAME)
return false;
//Check for self
for (unsigned int i = 0; i < m_clients.size(); ++i)
if (m_clients[i] == client)
return false;
if ( CheckForBan(client) || CheckForKick(client) )
return false;
//ignorePass must be false and m_password must not be empty
if ( !ignorePass && !m_passwd.empty())
if (pw != m_passwd)
return false;
int cnum = m_clients.size();
m_clients.push_back(client);
client->SetInRoom(true);
SendRoomTitle(cnum);
SendRoomList(cnum);
PopulatePlayersList();
SendPlayersList();
return true;
}
void SMOnlineRoom::Update() {
UpdateClients();
}
void SMOnlineRoom::UpdateClients() {
int length = 0;
for (unsigned int x = 0; x < m_clients.size(); ++x)
{
if (!m_clients[x])
{
LOG->Write("Error: Trying to update NULL client");
RemoveClient(x);
continue;
}
try
{
do
{
//As long as we keep getting data from the socket, keep processing it
length = m_clients[x]->Update(m_packet);
if (length > 0)
ParseData(m_packet, x);
//Check for NULL incase the client switched rooms
} while ((length > 0) && (x < m_clients.size()) && (m_clients[x]));
}
catch (ClientError&)
{
RemoveClient(x);
}
}
}
void SMOnlineRoom::RemoveClient(const unsigned int index)
{
if (index >= m_clients.size())
return;
if (m_clients[index])
{
m_clients[index]->SetInRoom(false);
m_clients[index] = NULL;
}
m_clients.erase(m_clients.begin()+index);
// LOG->Write("Removed client from room");
PopulatePlayersList();
SendPlayersList();
}
void SMOnlineRoom::JoinToRoom(SMOnlineRoom *room) {
//join other room to this room.
m_joinrooms.push_back(room);
//join this room to the other room
room->m_joinrooms.push_back(this);
//Let users in both rooms know of the added rooms
SendRoomList();
room->SendRoomList();
}
void SMOnlineRoom::UnjoinToRoom(const MString &roomname) {
SMOnlineRoom* tmproom;
for (unsigned int x = 0; x < m_joinrooms.size(); ++x)
if (m_joinrooms[x]->GetTitle() == roomname)
{
tmproom = m_joinrooms[x];
m_joinrooms[x] = NULL;
m_joinrooms.erase(m_joinrooms.begin()+x);
tmproom->UnjoinToRoom(m_title); //Remove the other room's link to this room
break; //end the search
}
SendRoomList();
}
void SMOnlineRoom::AnalizeChat(unsigned int clientNum, PacketFunctions& Packet)
{
MString message = Packet.ReadNT();
ChatCommandPack ccp = ParseCommands(message, clientNum);
switch(ccp.cmd)
{
case NONE:
RelayChat(clientNum, message);
return;
case ANNOUNCE:
{
PacketFunctions reply;
reply.ClearPacket();
reply.Write1(NSCSU+NSServerOffset);
reply.WriteNT(ccp.data.front());
SERVER->SendToAll(reply);
}
return;
case KICK:
{
const MString& name = ccp.data.front();
for (unsigned int x = 0; x < m_clients.size(); ++x)
for (unsigned int y = 0; y < m_clients[x]->GetNumPlayers(); ++y)
if (m_clients[x]->GetPlayer(y)->GetName() == name)
{
LOG->Write("Kicking " + name);
m_kicked[ccp.data.front()] = KickClient(name);
ChangeRoom(1, m_joinrooms[0]->GetTitle(), x, "", true);
return;
}
}
break;
case BAN:
{
const MString& name = ccp.data.front();
for (unsigned int x = 0; x < m_clients.size(); ++x)
for (unsigned int y = 0; y < m_clients[x]->GetNumPlayers(); ++y)
if (m_clients[x]->GetPlayer(y)->GetName() == name)
{
LOG->Write("Banning " + name);
m_banned[name] = name;
ChangeRoom(1, m_joinrooms[0]->GetTitle(), x, "", true);
return;
}
}
break;
case PM:
{
MString user = ccp.data.front();
ccp.data.pop();
MString message = ccp.data.front();
MString tmp;
MString success = "PM Failed!";
if ((user.length() > 0) && (message.length() > 0))
{
tmp = "PM from ";
unsigned int numPlayers = m_clients[m_cNum]->GetNumPlayers();
for (unsigned int x = 0; x < numPlayers; ++x)
{
tmp += m_clients[m_cNum]->GetPlayerName(x);
if ((numPlayers > 1) && (x < (numPlayers - 1)))
tmp += "&";
}
tmp += ": " + message;
if (SERVER->MsgPlayer(user, tmp))
success = "PM Success!";
}
PacketFunctions result;
result.ClearPacket();
result.Write1(NSCCM + NSServerOffset);
result.WriteNT(success);
m_clients[m_cNum]->SendData(result);
}
break;
case DROP:
{
SMOnlineClient* tmp = SERVER->GetPlayerClient(ccp.data.front());
if (tmp != NULL)
tmp->connection.close();
}
break;
// case FORCESTART:
// break;
default:
break;
};
}
MString Detranslit( const MString & inputText )
{
MString sEndChat;
const char* chatStr = inputText.c_str();
for ( unsigned int i = 0; i < inputText.length(); i++ )
{
if ( (unsigned char )( chatStr[i] ) >= (unsigned char )( 0xE0 ) )
i+=2;
else if ( (unsigned char )( chatStr[i] ) >= (unsigned char )( 0xC0 ) )
i+=1;
else
sEndChat += chatStr[i];
}
return sEndChat;
}
void SMOnlineRoom::RelayChat(unsigned int clientNum, const MString &chat) {
//First, Remove any unicode in the chat message and load it
//into a temp message.
MString sEndChat;
sEndChat = Detranslit( chat );
unsigned int numPlayers = m_clients[clientNum]->GetNumPlayers();
m_reply.ClearPacket();
//For all clients who support colored text:
MString message = m_commonColor +
m_clients[clientNum]->GetColor();
for (unsigned int x = 0; x < numPlayers; ++x) {
message += m_clients[clientNum]->GetPlayerName(x);
if ((numPlayers > 1) && (x < (numPlayers - 1)))
message += "&";
}
message += ":" + m_commonColor + m_baseColor + " ";
message += sEndChat;
m_reply.ClearPacket();
m_reply.Write1(NSCCM + NSServerOffset);
m_reply.WriteNT(message);
SendToAllClients( m_reply, 2, 2 );
//For all clients who do not support colored text:
message = "";
for (unsigned int x = 0; x < numPlayers; ++x) {
message += m_clients[clientNum]->GetPlayerName(x);
if ((numPlayers > 1) && (x < (numPlayers - 1)))
message += "&";
}
message += ": " + sEndChat;// + MString( itoa( m_clients[clientNum]->GetVersion(), " ",10 ));
CHATLOG->QuietWrite(message);
m_reply.ClearPacket();
m_reply.Write1(NSCCM + NSServerOffset);
m_reply.WriteNT(message);
SendToAllClients( m_reply, 1, 1 );
}
void SMOnlineRoom::ParseData(PacketFunctions& Packet, int clientnum)
{
/* Try not to do any Packet Parsing here here because a lot of parsing
was done in the Client. Parsing again would be a waste.
Packet has been unaltered previous to this point.*/
int command = Packet.Read1();
switch (command)
{
case NSCPing:
// No Operation
// SendValue(NSServerOffset + NSCPingR, clientNum);
break;
case NSCPingR:
// No Operation response
break;
case NSCHello:
// Hello
break;
case NSCSU:
// Style Update
break;
case NSCCM:
// Chat message
AnalizeChat(clientnum, Packet);
break;
case NSCSMS:
//Room Status Change
SendRoomTitle(clientnum);
SendRoomList(clientnum);
break;
case NSSMONL:
SMOnlineParse(Packet, clientnum);
break;
default:
break;
}
}
void SMOnlineRoom::SMOnlineParse(PacketFunctions &Packet, int clientnum) {
int command = Packet.Read1();
switch (command) {
case 0:
//Login
break;
case 1:
//Change Room
{
int command = Packet.Read1();
MString title = Packet.ReadNT();
MString pw = Packet.ReadNT();
ChangeRoom(command, title, clientnum, pw);
}
break;
case 2:
//Create new room
if (m_allowgamerooms) {
CreateRoom(Packet);
ChangeRoom(1, m_joinrooms[m_joinrooms.size()-1]->GetTitle(), clientnum, "", true);
}
break;
case 3:
//Additional room info
GenAdditionalInfo(Packet.ReadNT(), clientnum);
break;
default:
LOG->Write(ssprintf("Invalid SMOnline command:%d", command));
break;
}
}
void SMOnlineRoom::WriteRoomListPacket(PacketFunctions &Packet) {
unsigned int numRooms = m_joinrooms.size();
Packet.ClearPacket();
Packet.Write1(NSSMONL + NSServerOffset);
Packet.Write1(1); //Room command
Packet.Write1(1); //Type (List Rooms)
Packet.Write1(numRooms);
for (unsigned int x = 0; x < numRooms; ++x) {
if (m_joinrooms[x] != NULL) {
Packet.WriteNT(m_joinrooms[x]->GetTitle());
Packet.WriteNT(m_joinrooms[x]->GetDescription());
}
}
//state
for (unsigned int x = 0; x < numRooms; ++x) {
if (m_joinrooms[x] != NULL)
{
//TO MAKE CHARLES HAPPY IF PASSWORDED USE 1 (UNSUSED)
if (!m_joinrooms[x]->m_passwd.empty() && (m_joinrooms[x]->m_state == RS_OPEN))
Packet.Write1(RS_UNUSED);
else
Packet.Write1(m_joinrooms[x]->m_state);
}
}
//Flags
char flags;
for (unsigned int x = 0; x < numRooms; ++x) {
if (m_joinrooms[x] != NULL)
{
flags = 0;
//bit 1 passworded
if (!m_joinrooms[x]->m_passwd.empty())
flags += 1;
Packet.Write1(flags);
}
}
}
void SMOnlineRoom::SendRoomList(int clientnum) {
WriteRoomListPacket(m_reply);
SendDataToClient(clientnum, m_reply);
}
void SMOnlineRoom::SendRoomList() {
WriteRoomListPacket(m_reply);
SendToAllClients(m_reply);
}
//Note: VersionRestriction = -1 means send to all clients, regardless.
// Otherwise, it indicates which version to send it to.
// VersionDirection of 0 indicates the version must match up perfectly
// 1: Must be less than or eqal to prescribed version
// 2: Must be greater than or equal to prescribed version
void SMOnlineRoom::SendToAllClients(const PacketFunctions& Packet, int VersionRestriction, int VersionDirection )
{
unsigned int size = m_clients.size();
for (unsigned int x = 0; x < size; ++x)
{
if ( ( VersionRestriction == -1 ) ||
( ( VersionDirection == 0 ) && ( VersionRestriction == m_clients[x]->GetVersion() ) ) ||
( ( VersionDirection == 1 ) && ( VersionRestriction >= m_clients[x]->GetVersion() ) ) ||
( ( VersionDirection == 2 ) && ( VersionRestriction <= m_clients[x]->GetVersion() ) ) )
SendDataToClient(x, Packet);
}
}
void SMOnlineRoom::SendRoomTitle(int clientnum) {
m_reply.ClearPacket();
m_reply.Write1(NSSMONL + NSServerOffset);
m_reply.Write1(1); //Room Update command
m_reply.Write1(0); //Title Update
m_reply.WriteNT(m_title);
m_reply.WriteNT(m_description);
m_reply.Write1(m_roomType);
SendDataToClient(clientnum, m_reply);
}
void SMOnlineRoom::ChangeRoom(int command, const MString& title, int clientnum, const MString& pw, bool ignorePass) {
SMOnlineClient *tmpclient;
if (command == 1) {
for (unsigned int x = 0;x < m_joinrooms.size(); ++x) {
if (m_joinrooms[x]->GetTitle() == title)
{
//We don't have to handle any state logic here,
//AddClient handles all of it.
tmpclient = m_clients[clientnum];
RemoveClient(clientnum);
//If we fail add the client back to self
if (!m_joinrooms[x]->AddClient(tmpclient, pw, ignorePass))
AddClient(tmpclient, "", true);
}
}
}
}
void SMOnlineRoom::SendPlayersList() {
unsigned int numplayers = m_PlayersList.size();
m_reply.ClearPacket();
m_reply.Write1(NSCUUL + NSServerOffset);
m_reply.Write1(numplayers);
m_reply.Write1(numplayers);
for (unsigned int x = 0; x < numplayers; ++x) {
m_reply.Write1(m_PlayersList[x]->ScreenPosition());
m_reply.WriteNT(m_PlayersList[x]->GetName());
}
SendToAllClients(m_reply);
}
unsigned int SMOnlineRoom::GetTotalPlayers() {
unsigned int players = 0;
for (unsigned int x = 0; x < m_clients.size(); ++x) {
players += m_clients[x]->GetNumPlayers();
}
return players;
}
bool SMOnlineRoom::CanDrop()
{
//don't drop rooms if we are SMLAN, we only have 1 room
if ( SERVER->IsSMLAN() )
return false;
//Only allow removal if we are a game room and have no clients
if ((m_roomType == ROOMGame) && m_clients.empty())
return true;
return false;
}
void SMOnlineRoom::PopulatePlayersList() {
unsigned int numClients = m_clients.size();
unsigned int numPlayers;
ClearPlayerList();
for (unsigned int x = 0; x < numClients; x++) {
numPlayers = m_clients[x]->GetNumPlayers();
for (unsigned int y = 0; y < numPlayers; y++) {
m_PlayersList.push_back(m_clients[x]->GetPlayer(y));
}
}
}
void SMOnlineRoom::ClearPlayerList() {
unsigned int size = m_PlayersList.size();
for (unsigned int x = 0; x < size; x++)
m_PlayersList[x] = NULL;
m_PlayersList.clear();
}
void SMOnlineRoom::CreateRoom(PacketFunctions& Packet) {
bool origional_name = true;
SMOnlineRoom* tmp = NULL;
int type = Packet.Read1();
MString title = Packet.ReadNT();
MString sub = Packet.ReadNT();
MString passwd = Packet.ReadNT();
if (!title.empty())
{
if (title.size() > m_roomNameLength)
title = title.substr(0, m_roomNameLength);
for (unsigned int x = 0; (x < m_joinrooms.size()) && origional_name; ++x)
if (title == m_joinrooms[x]->m_title)
origional_name = false;
if (origional_name) {
if (type) {
tmp = new SMOnlineGameRoom(title, sub);
} else {
tmp = new SMOnlineRoom(title, sub);
}
if (tmp) {
LOG->Write("Created room");
if (!passwd.empty())
tmp->m_passwd = passwd;
SERVER->GetRooms().push_back(tmp);
JoinToRoom(tmp);
SendRoomList();
}
}
}
}
void SMOnlineRoom::ChatToClient(const MString& message, unsigned int clientnum) {
m_reply.ClearPacket();
m_reply.Write1(NSCCM + NSServerOffset);
m_reply.WriteNT(Detranslit(message));
SendDataToClient(clientnum, m_reply);
}
void SMOnlineRoom::ChatAsServer(const MString& message, unsigned int clientnum) {
MString tmp = SERVER->GetName();
tmp += (": " + Detranslit(message));
if (clientnum < m_clients.size()) {
ChatToClient(tmp, clientnum);
LOG->Write(tmp);
}
}
void SMOnlineRoom::ChatAsServer(const MString& message) {
WriteChatPacket(m_reply, SERVER->GetName() + ": " + Detranslit(message));
SendToAllClients(m_reply);
}
void SMOnlineRoom::WriteChatPacket(PacketFunctions& packet, const MString& message) {
packet.ClearPacket();
packet.Write1(NSCCM + NSServerOffset);
packet.WriteNT(message);
}
void SMOnlineRoom::SendDataToClient(unsigned int clientnum, const PacketFunctions& packet) {
if (clientnum < m_clients.size())
m_clients[clientnum]->SendData(packet);
}
void SMOnlineRoom::SetState(RoomState state) {
if (state == m_state)
return;
m_state = state;
//Since we changed state, alert rooms that know about us.
for (unsigned int x = 0; x < m_joinrooms.size(); x++) {
if (m_joinrooms[x] != NULL)
m_joinrooms[x]->SendRoomList();
else
LOG->Write("ERROR: Send room list of NULL room");
}
}
bool SMOnlineRoom::CheckForBan(SMOnlineClient* client) {
MString name;
unsigned int bannedsize = m_banned.size();
if (bannedsize > 0) {
for (unsigned int x = 0;x < client->GetNumPlayers(); x++) {
if (m_banned.find(client->GetPlayer(x)->GetName()) != m_banned.end())
return true;
}
}
return false;
}
bool SMOnlineRoom::CheckForKick(SMOnlineClient* client) {
MString name;
map<MString, KickClient>::iterator iterator;
for (unsigned int x = 0;x < client->GetNumPlayers(); x++) {
name = client->GetPlayer(x)->GetName();
iterator = m_kicked.find(name);
if (iterator != m_kicked.end()) {
if ((unsigned int)(m_kicked[name].GetTimeStamp() + m_roomKickTime) < (unsigned int)time(NULL)) {
m_kicked.erase(iterator);
return false;
} else {
return true;
}
}
}
return false;
}
void SMOnlineRoom::GenAdditionalInfo(const MString& roomname, const int clientnum)
{
for (unsigned int x = 0; x < m_joinrooms.size(); x++) {
if (m_joinrooms[x]->GetTitle() == roomname) {
MString title, subtitle, artist;
m_reply.ClearPacket();
m_reply.Write1(NSSMONL + NSServerOffset);
m_reply.Write1(3);
if (m_joinrooms[x]->m_roomType == ROOMGame)
{
RoundInfo last = ((SMOnlineGameRoom*)m_joinrooms[x])->LastRound();
title = last.GetTitle();
subtitle = last.GetSubtitle();
artist = last.GetArtist();
}
m_reply.WriteNT(title); //Title
m_reply.WriteNT(subtitle); //Subtitle
m_reply.WriteNT(artist); //Artist
m_reply.Write1((unsigned char)(m_joinrooms[x]->GetTotalPlayers()));
m_reply.Write1((unsigned char)m_maxPlayers);
//Player names
for (unsigned int i = 0; i < m_joinrooms[x]->m_clients.size(); i++)
for (unsigned int j = 0; j < m_joinrooms[x]->m_clients[i]->GetNumPlayers(); j++)
m_reply.WriteNT(m_joinrooms[x]->m_clients[i]->GetPlayer(j)->GetName());
SendDataToClient(clientnum, m_reply);
return;
}
}
}
ChatCommandPack SMOnlineRoom::ParseCommands(MString& data, unsigned int clientNum) {
MString command;
m_cNum = clientNum;
if (data.find("/") != 0) {
return ChatCommandPack(NONE);
}
command = GetCommand(data);
ChatCommandPack ccp;
if (m_clients[clientNum]->IsAdmin())
{
if (Announce(command, data, ccp))
return ccp;
if (Drop(command, data, ccp))
return ccp;
}
if (Kick(command, data, ccp))
return ccp;
if (Ban(command, data, ccp))
return ccp;
if (Msg(command, data, ccp))
return ccp;
return ccp;
}
MString SMOnlineRoom::GetCommand(MString& data) {
MString command;
int pos = data.find(" ");
if (pos > 0) {
command = data.substr(1, pos);
}
return command;
}
bool SMOnlineRoom::Announce(const MString& command, const MString& data, ChatCommandPack& ccp)
{
MString message;
if (command.find("announce") != 0)
return false;
message = data.substr(command.length()+1);
ccp.cmd = ANNOUNCE;
ccp.data.push(data.substr(command.length()+1));
return true;
}
bool SMOnlineRoom::Ban(const MString& command, const MString& data, ChatCommandPack& ccp)
{
MString name;
if (m_roomType == ROOMGame)
if (command.find("ban") == 0) {
if (m_cNum == 0) {
GetPlayerName(data, name);
ccp.cmd = BAN;
ccp.data.push(name);
return true;
}
}
return false;
}
bool SMOnlineRoom::Kick(const MString& command, const MString& data, ChatCommandPack& ccp)
{
MString name;
if (m_roomType == ROOMGame)
if (command.find("kick") == 0) {
if (m_cNum == 0) {
GetPlayerName(data, name);
ccp.cmd = KICK;
ccp.data.push(name);
return true;
}
}
return false;
}
bool SMOnlineRoom::Msg(const MString& command, const MString& data, ChatCommandPack& ccp)
{
MString tmp;
MString user;
MString message;
if (command.find("msg") == 0)
{
tmp = data.substr(command.length()+1);
if (tmp.length() > 0)
{
if (GetPlayerName(data, user))
if (tmp.length() >= (user.length()+3))
{
ccp.cmd = PM;
ccp.data.push(user);
ccp.data.push(tmp.substr(user.length()+3));
return true;
}
}
}
return false;
}
bool SMOnlineRoom::Drop(const MString& command, const MString& data, ChatCommandPack& ccp)
{
MString name;
if (command.find("drop") == 0)
{
GetPlayerName(data, name);
ccp.cmd = DROP;
ccp.data.push(name);
return true;
}
return false;
}
bool SMOnlineRoom::GetPlayerName(const MString& data, MString& name)
{
unsigned int pos1 = data.find('"');
unsigned int pos2 = data.find('"', pos1+1);
if (pos1 != pos2)
if (pos1 != string::npos)
if (pos2 != string::npos) {
pos1++;
name = data.substr(pos1, pos2 - pos1);
return true;
}
return false;
}
/*
* (c) 2003-2004 Joshua Allen
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/