File: peerPlayers.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (824 lines) | stat: -rw-r--r-- 15,232 bytes parent folder | download | duplicates (2)
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
/*
GameSpy Peer SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

/*************
** INCLUDES **
*************/
#include <string.h>
#include "peerPlayers.h"
#include "peerPing.h"
#include "peerCallbacks.h"
#include "peerKeys.h"

/************
** DEFINES **
************/
#define PI_PLAYERS_NUM_BUCKETS     32
#define PI_PLAYER_INFO_SIZE        (sizeof(piPlayer) - PI_NICK_MAX_LEN)
#define PI_PLAYER_INFO(player)     ((char *)(player) + PI_NICK_MAX_LEN)

/**************
** FUNCTIONS **
**************/
static int piPlayersTableHashFn
(
	const void *elem,
	int numBuckets
)
{
	piPlayer * player = (piPlayer *)elem;
	int c;
	const char * str;
	unsigned int hash;

	assert(player);
	assert(player->nick[0]);

	// Get the hash.
	////////////////
	str = player->nick;
	hash = 0;
	while((c = *str++) != '\0')
		hash += (unsigned int)tolower(c);
	hash %= (unsigned int)numBuckets;

	return (int)hash;
}

static int GS_STATIC_CALLBACK piPlayersTableCompareFn
(
	const void *elem1,
	const void *elem2
)
{
	piPlayer * player1 = (piPlayer *)elem1;
	piPlayer * player2 = (piPlayer *)elem2;
	assert(player1);
	assert(player1->nick[0]);
	assert(player2);
	assert(player2->nick[0]);

	return strcasecmp(player1->nick, player2->nick);
}

static void piPlayersTableElementFreeFn
(
	void *elem
)
{
	piPlayer * player = (piPlayer *)elem;
	assert(player);
	assert(player->nick);
	GSI_UNUSED(player);
}

PEERBool piPlayersInit
(
	PEER peer
)
{
	int i;

	PEER_CONNECTION;

	if(connection->stayInTitleRoom)
		return PEERTrue;

	// Setup the player table.
	//////////////////////////
	connection->players = TableNew(sizeof(piPlayer), PI_PLAYERS_NUM_BUCKETS, piPlayersTableHashFn, piPlayersTableCompareFn, piPlayersTableElementFreeFn);
	if(!connection->players)
		return PEERFalse;

	// No players in any room.
	//////////////////////////
	for(i = 0 ; i < NumRooms ; i++)
		connection->numPlayers[i] = 0;

	return PEERTrue;
}

void piPlayersCleanup
(
	PEER peer
)
{
	int i;

	PEER_CONNECTION;

	if(connection->stayInTitleRoom)
		return;

	// gsifree the player table.
	/////////////////////////
	if(connection->players)
		TableFree(connection->players);
	connection->players = NULL;
	for(i = 0 ; i < NumRooms ; i++)
		connection->numPlayers[i] = 0;
}

static piPlayer * piAddPlayer
(
	PEER peer,
	const char * nick,
	PEERBool initialize
)
{
	piPlayer * player;
	piPlayer playerMatch;

	PEER_CONNECTION;

	assert(!piGetPlayer(peer, nick));

	// Setup the player.
	////////////////////
	player = &playerMatch;
	memset(player, 0, sizeof(piPlayer));
	strzcpy(player->nick, nick, PI_NICK_MAX_LEN);
	if(initialize)
	{
		int roomType;

		player->local = (PEERBool)(strcasecmp(nick, connection->nick) == 0);

		for(roomType = 0 ; roomType < NumRooms ; roomType++)
		{
			player->inRoom[roomType] = PEERFalse;
			player->flags[roomType] = 0;
		}

		player->IP = 0;
		player->profileID = 0;
		player->gotIPAndProfileID = PEERFalse;

		if(!piPingInitPlayer(peer, player))
			return NULL;
	}

	// Add the player.
	//////////////////
	TableEnter(connection->players, player);

	player = piGetPlayer(peer, nick);
	assert(player);

	return player;
}

static void piRemovePlayer
(
	PEER peer,
	piPlayer * player
)
{
	PEER_CONNECTION;

	assert(player);

	// Remove it.
	/////////////
	TableRemove(connection->players, player);
}

piPlayer * piPlayerJoinedRoom
(
	PEER peer,
	const char * nick,
	RoomType roomType,
	int mode
)
{
	piPlayer * player;

	PEER_CONNECTION;
	
	assert(nick);
	assert(nick[0]);
	ASSERT_ROOMTYPE(roomType);
	assert(IN_ROOM || ENTERING_ROOM);

	// Check that we're in/entering this room.
	//////////////////////////////////////////
	if(!ENTERING_ROOM && !IN_ROOM)
		return NULL;

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, nick);

	// Is it a new player?
	//////////////////////
	if(!player)
	{
		// Add the player.
		//////////////////
		player = piAddPlayer(peer, nick, PEERTrue);
	}

	// In this room.
	////////////////
	assert(!player->inRoom[roomType]);
	player->inRoom[roomType] = PEERTrue;
	connection->numPlayers[roomType]++;
	player->flags[roomType] = 0;
	if(mode & CHAT_OP)
		player->flags[roomType] |= PEER_FLAG_OP;
	if(mode & CHAT_VOICE)
		player->flags[roomType] |= PEER_FLAG_VOICE;

	// Do ping stuff.
	/////////////////
	piPingPlayerJoinedRoom(peer, player, roomType);

	return player;
}

void piPlayerLeftRoom
(
	PEER peer,
	const char * nick,
	RoomType roomType
)
{
	piPlayer * player;

	PEER_CONNECTION;

	assert(nick);
	assert(nick[0]);
	ASSERT_ROOMTYPE(roomType);

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, nick);
	assert(player);
	if(!player)
		return;

	// Leave the room.
	//////////////////
	assert(player->inRoom[roomType]);
	player->inRoom[roomType] = PEERFalse;
	connection->numPlayers[roomType]--;
	player->flags[roomType] = 0;

	// Do ping stuff.
	/////////////////
	piPingPlayerLeftRoom(peer, player);

	// Are we not in any rooms now?
	///////////////////////////////
	if(!player->inRoom[0] && !player->inRoom[1] && !player->inRoom[2])
	{
		// This player is out.
		//////////////////////
		piRemovePlayer(peer, player);
	}

	// Cleanse the key cache.
	/////////////////////////
	piKeyCacheCleanse(peer);
}

void piPlayerChangedNick
(
	PEER peer,
	const char * oldNick,
	const char * newNick
)
{
	char playerInfoBuffer[PI_PLAYER_INFO_SIZE];
	piPlayer * player;

	assert(oldNick);
	assert(oldNick[0]);
	assert(newNick);
	assert(newNick[0]);

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, oldNick);

	// Check if we can't find this player
	// (we could have already done the nick change
	// if we're in more than one channel together).
	///////////////////////////////////////////////
	if(!player)
		return;

	// Save off the player info.
	////////////////////////////
	memcpy(playerInfoBuffer, PI_PLAYER_INFO(player), PI_PLAYER_INFO_SIZE);

	// Remove the old nick.
	///////////////////////
	piRemovePlayer(peer, player);

	// Add the new one.
	///////////////////
	player = piAddPlayer(peer, newNick, PEERFalse);
	assert(player);
	if(!player)
		return;

	// Copy the player info back in.
	////////////////////////////////
	memcpy(PI_PLAYER_INFO(player), playerInfoBuffer, PI_PLAYER_INFO_SIZE);

	// Update the key cache.
	////////////////////////
	piKeyCachePlayerChangedNick(peer, oldNick, newNick);
}

typedef struct piLeftRoomData
{
	PEER peer;
	RoomType roomType;
} piLeftRoomData;

static void piLeftRoomMapFn
(
	void *elem,
	void *clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	piLeftRoomData * data = (piLeftRoomData *)clientdata;
	assert(player);
	assert(player->nick);
	assert(data);

	// Is the player in this room?
	//////////////////////////////
	if(player->inRoom[data->roomType])
	{
		// Leave the room.
		//////////////////
		piPlayerLeftRoom(data->peer, player->nick, data->roomType);
	}
}

void piClearRoomPlayers
(
	PEER peer,
	RoomType roomType
)
{
	piLeftRoomData data;

	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);

	// Go through all the players.
	//////////////////////////////
	data.peer = peer;
	data.roomType = roomType;
	TableMapSafe(connection->players, piLeftRoomMapFn, &data);
}

piPlayer * piGetPlayer
(
	PEER peer,
	const char * nick
)
{
	piPlayer playerMatch;
	piPlayer * player;

	PEER_CONNECTION;

	assert(nick);
	assert(nick[0]);

	// Check for no table.
	//////////////////////
	if(!connection->players)
		return NULL;

	// Lookup this player.
	//////////////////////
	strzcpy(playerMatch.nick, nick, PI_NICK_MAX_LEN);
	player = (piPlayer *)TableLookup(connection->players, &playerMatch);

	return player;
}

typedef struct piEnumRoomPlayersData
{
	PEER peer;
	RoomType roomType;
	int count;
	piEnumRoomPlayersCallback callback;
	void * param;
} piEnumRoomPlayersData;

static void piEnumRoomPlayersMap
(
	void *elem,
	void *clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	piEnumRoomPlayersData * data = (piEnumRoomPlayersData *)clientdata;
	// Is this player in the room?
	//////////////////////////////
	if(player->inRoom[data->roomType])
	{
		// Call the callback.
		/////////////////////
		data->callback(data->peer, data->roomType, player, data->count, data->param);

		// One more player.
		///////////////////
		data->count++;
	}
}

void piEnumRoomPlayers
(
	PEER peer,
	RoomType roomType,
	piEnumRoomPlayersCallback callback,
	void * param
)
{
	piEnumRoomPlayersData data;

	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);
	assert(callback);

	// Init the data.
	/////////////////
	data.peer = peer;
	data.roomType = roomType;
	data.count = 0;
	data.callback = callback;
	data.param = param;

	// Enum through the players.
	////////////////////////////
	TableMap(connection->players, piEnumRoomPlayersMap, &data);

	// Call the callback once to terminate the enum.
	////////////////////////////////////////////////
	callback(peer, roomType, NULL, -1, param);
}

static int piFindPlayersByIPMap
(
	void * elem,
	void * clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	unsigned int IP;

	IP = *(unsigned int *)clientdata;

	// Is this the same IP?
	///////////////////////
	if(player->gotIPAndProfileID && (player->IP == IP))
		return 0;

	return 1;
}

piPlayer * piFindPlayerByIP
(
	PEER peer,
	unsigned int IP
)
{
	PEER_CONNECTION;

	return (piPlayer *)TableMap2(connection->players, piFindPlayersByIPMap, &IP);
}

void piSetPlayerIPAndProfileID
(
	PEER peer,
	const char * nick,
	unsigned int IP,
	int profileID
)
{
	piPlayer * player;

	assert(nick);
	assert(nick[0]);

	if(!nick)
		return;

	// Cache the info.
	//////////////////
	player = piGetPlayer(peer, nick);
	if(player)
	{
		player->IP = IP;
		player->profileID = profileID;

		if(!player->gotIPAndProfileID)
		{
			player->gotIPAndProfileID = PEERTrue;

			// The IP is now available, send a ping
			////////////////////////////////////////
			if(player->inRoom[TitleRoom])
				piPingPlayerJoinedRoom(peer, player, TitleRoom);
			if(player->inRoom[GroupRoom])
				piPingPlayerJoinedRoom(peer, player, GroupRoom);
			if(player->inRoom[StagingRoom])
				piPingPlayerJoinedRoom(peer, player, StagingRoom);
		}

	}
}

static void piSetNewPlayerFlags
(
	PEER peer,
	const char * nick,
	RoomType roomType,
	int flags
)
{
	piPlayer * player;
	int oldFlags;

	assert(nick);
	assert(nick[0]);

	if(!nick)
		return;

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, nick);
	if(!player || !player->inRoom[roomType])
		return;

	// Copy off the old flags.
	//////////////////////////
	oldFlags = player->flags[roomType];

	// Check for no change.
	///////////////////////
	if(flags == oldFlags)
		return;

	// Set the new flags.
	/////////////////////
	player->flags[roomType] = flags;

	// If ready changed in the staging room, call a callback.
	/////////////////////////////////////////////////////////
	if((roomType == StagingRoom) && ((oldFlags & PEER_FLAG_READY) != (flags & PEER_FLAG_READY)))
		piAddReadyChangedCallback(peer, player->nick, (PEERBool)((player->flags[roomType] & PEER_FLAG_READY) != 0));

	// Call the flags changed callback.
	///////////////////////////////////
	piAddPlayerFlagsChangedCallback(peer, roomType, nick, oldFlags, flags);
}

int piParseFlags
(
	const char * flags
)
{
	int nFlags = 0;

	if(strchr(flags, 's'))
		nFlags |= PEER_FLAG_STAGING;
	if(strchr(flags, 'r'))
		nFlags |= PEER_FLAG_READY;
	if(strchr(flags, 'g'))
		nFlags |= PEER_FLAG_PLAYING;
	if(strchr(flags, 'a'))
		nFlags |= PEER_FLAG_AWAY;
	if(strchr(flags, 'h'))
		nFlags |= PEER_FLAG_HOST;

	return nFlags;
}

void piSetPlayerRoomFlags
(
	PEER peer,
	const char * nick,
	RoomType roomType,
	const char * flags
)
{
	piPlayer * player;
	int nFlags;

	assert(nick);
	assert(nick[0]);

	if(!nick)
		return;

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, nick);
	if(!player || !player->inRoom[roomType])
		return;

	// Get the mode.
	////////////////
	nFlags = (player->flags[roomType] & (PEER_FLAG_OP | PEER_FLAG_VOICE));

	// Add the new flags.
	/////////////////////
	nFlags |= piParseFlags(flags);

	// Set them.
	////////////
	piSetNewPlayerFlags(peer, nick, roomType, nFlags);
}

void piSetPlayerModeFlags
(
	PEER peer,
	const char * nick,
	RoomType roomType,
	int mode
)
{
	piPlayer * player;
	int nFlags;

	assert(nick);
	assert(nick[0]);

	if(!nick)
		return;

	// Find the player.
	///////////////////
	player = piGetPlayer(peer, nick);
	if(!player || !player->inRoom[roomType])
		return;

	// Get the non-mode flags.
	//////////////////////////
	nFlags = (player->flags[roomType] & (~(PEER_FLAG_OP | PEER_FLAG_VOICE)));

	// Add the new flags.
	/////////////////////
	if(mode & CHAT_OP)
		nFlags |= PEER_FLAG_OP;
	if(mode & CHAT_VOICE)
		nFlags |= PEER_FLAG_VOICE;

	// Set them.
	////////////
	piSetNewPlayerFlags(peer, nick, roomType, nFlags);
}

PEERBool piIsPlayerVIP
(
	piPlayer * player,
	RoomType roomType
)
{
	if(!player)
		return PEERFalse;

	if(!player->inRoom[roomType])
		return PEERFalse;

	return (PEERBool)((player->flags[roomType] & (PEER_FLAG_OP | PEER_FLAG_VOICE)) != 0);
}

PEERBool piIsPlayerHost(piPlayer * player)
{
	if(!player)
		return PEERFalse;

	if(!player->inRoom[StagingRoom])
		return PEERFalse;

	return (PEERBool)((player->flags[StagingRoom] & (PEER_FLAG_OP | PEER_FLAG_HOST)) == (PEER_FLAG_OP | PEER_FLAG_HOST));
}

PEERBool piIsPlayerOp(piPlayer * player)
{
	if(!player)
		return PEERFalse;

	if(!player->inRoom[StagingRoom])
		return PEERFalse;

	return (PEERBool)(player->flags[StagingRoom] & PEER_FLAG_OP);
}

typedef struct piFindPlayerByIndexData
{
	int index;
	int count;
	RoomType roomType;
} piFindPlayerByIndexData;

static int piFindPlayerByIndexMap
(
	void * elem,
	void * clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	piFindPlayerByIndexData * data = (piFindPlayerByIndexData *)clientdata;

	// check if the player is in the room
	if(player->inRoom[data->roomType])
	{
		// check if this is the one we're looking for
		if(data->index == data->count)
			return 0;

		// one more player found
		data->count++;
	}

	return 1;
}

piPlayer * piFindPlayerByIndex
(
	PEER peer,
	RoomType roomType,
	int index
)
{
	piFindPlayerByIndexData data;
	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);

	// setup the data
	data.index = index;
	data.count = 0;
	data.roomType = roomType;

	// enum through the players
	return TableMap2(connection->players, piFindPlayerByIndexMap, &data);
}

typedef struct piCountRoomOpsMapData
{
	int count;
	RoomType roomType;
	const char * exclude;
} piCountRoomOpsMapData;

static void piCountRoomOpsMap
(
	void * elem,
	void * clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	piCountRoomOpsMapData * data = (piCountRoomOpsMapData *)clientdata;

	// check the exlude nick
	if(data->exclude && (strcasecmp(data->exclude, player->nick) == 0))
		return;

	// is this player an op?
	if(player->flags[data->roomType] & PEER_FLAG_OP)
		data->count++;
}

int piCountRoomOps(PEER peer, RoomType roomType, const char * exclude)
{
	piCountRoomOpsMapData data;

	PEER_CONNECTION;

	ASSERT_ROOMTYPE(roomType);

	// setup the data struct
	data.count = 0;
	data.roomType = roomType;
	data.exclude = exclude;

	// enum through the players
	TableMap(connection->players, piCountRoomOpsMap, &data);

	// return the count
	return data.count;
}