Debian Codegraph @

code/server/sv_rankings.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // sv_rankings.c -- global rankings interface
23
24 #include "server.h"
25 #include "..\rankings\1.0\gr\grapi.h"
26 #include "..\rankings\1.0\gr\grlog.h"
27
28 typedef struct
29 {
30 GR_CONTEXT context;
31 uint64_t game_id;
32 uint64_t match;
33 uint64_t player_id;
34 GR_PLAYER_TOKEN token;
35 grank_status_t grank_status;
36 grank_status_t final_status; // status to set after cleanup
37 uint32_t grank; // global rank
38 char name[32];
39 } ranked_player_t;
40
41 static int s_rankings_contexts = 0;
42 static qboolean s_rankings_active = qfalse;
43 static GR_CONTEXT s_server_context = 0;
44 static uint64_t s_server_match = 0;
45 static char* s_rankings_game_key = NULL;
46 static uint64_t s_rankings_game_id = 0;
47 static ranked_player_t* s_ranked_players = NULL;
48 static qboolean s_server_quitting = qfalse;
49 static const char s_ascii_encoding[] =
50 "0123456789abcdef"
51 "ghijklmnopqrstuv"
52 "wxyzABCDEFGHIJKL"
53 "MNOPQRSTUVWXYZ[]";
54
55 // private functions
56 static void SV_RankNewGameCBF( GR_NEWGAME* gr_newgame, void* cbf_arg );
57 static void SV_RankUserCBF( GR_LOGIN* gr_login, void* cbf_arg );
58 static void SV_RankJoinGameCBF( GR_JOINGAME* gr_joingame, void* cbf_arg );
59 static void SV_RankSendReportsCBF( GR_STATUS* gr_status, void* cbf_arg );
60 static void SV_RankCleanupCBF( GR_STATUS* gr_status, void* cbf_arg );
61 static void SV_RankCloseContext( ranked_player_t* ranked_player );
62 static int SV_RankAsciiEncode( char* dest, const unsigned char* src,
63 int src_len );
64 static int SV_RankAsciiDecode( unsigned char* dest, const char* src,
65 int src_len );
66 static void SV_RankEncodeGameID( uint64_t game_id, char* result,
67 int len );
68 static uint64_t SV_RankDecodePlayerID( const char* string );
69 static void SV_RankDecodePlayerKey( const char* string, GR_PLAYER_TOKEN key );
70 static char* SV_RankStatusString( GR_STATUS status );
71 static void SV_RankError( const char* fmt, ... ) Q_PRINTF_FUNC(1, 2);
72 static char SV_RankGameKey[64];
73
74 /*
75 ================
76 SV_RankBegin
77 ================
78 */
79 void SV_RankBegin( char *gamekey )
80 {
81 GR_INIT init;
82 GR_STATUS status;
83
84 assert( s_rankings_contexts == 0 );
85 assert( !s_rankings_active );
86 assert( s_ranked_players == NULL );
87
88 if( sv_enableRankings->integer == 0 || Cvar_VariableValue( "g_gametype" ) == GT_SINGLE_PLAYER )
89 {
90 s_rankings_active = qfalse;
91 if( sv_rankingsActive->integer == 1 )
92 {
93 Cvar_Set( "sv_rankingsActive", "0" );
94 }
95 return;
96 }
97
98 // only allow official game key on pure servers
99 if( strcmp(gamekey, GR_GAMEKEY) == 0 )
100 {
101 /*
102 if( Cvar_VariableValue("sv_pure") != 1 )
103 {
104 Cvar_Set( "sv_enableRankings", "0" );
105 return;
106 }
107 */
108
109 // substitute game-specific game key
110 switch( (int)Cvar_VariableValue("g_gametype") )
111 {
112 case GT_FFA:
113 gamekey = "Q3 Free For All";
114 break;
115 case GT_TOURNAMENT:
116 gamekey = "Q3 Tournament";
117 break;
118 case GT_TEAM:
119 gamekey = "Q3 Team Deathmatch";
120 break;
121 case GT_CTF:
122 gamekey = "Q3 Capture the Flag";
123 break;
124 case GT_1FCTF:
125 gamekey = "Q3 One Flag CTF";
126 break;
127 case GT_OBELISK:
128 gamekey = "Q3 Overload";
129 break;
130 case GT_HARVESTER:
131 gamekey = "Q3 Harvester";
132 break;
133 default:
134 break;
135 }
136 }
137 s_rankings_game_key = gamekey;
138
139 // initialize rankings
140 GRankLogLevel( GRLOG_OFF );
141 Q_strncpyz(SV_RankGameKey,gamekey,sizeof(SV_RankGameKey));
142 init = GRankInit( 1, SV_RankGameKey, GR_OPT_POLL, GR_OPT_END );
143 s_server_context = init.context;
144 s_rankings_contexts++;
145 Com_DPrintf( "SV_RankBegin(); GR_GAMEKEY is %s\n", gamekey );
146 Com_DPrintf( "SV_RankBegin(); s_rankings_contexts=%d\n",s_rankings_contexts );
147 Com_DPrintf( "SV_RankBegin(); s_server_context=%d\n",init.context );
148
149 // new game
150 if(!strlen(Cvar_VariableString( "sv_leagueName" )))
151 {
152 status = GRankNewGameAsync
153 (
154 s_server_context,
155 SV_RankNewGameCBF,
156 NULL,
157 GR_OPT_LEAGUENAME,
158 (void*)(Cvar_VariableString( "sv_leagueName" )),
159 GR_OPT_END
160 );
161 }
162 else
163 {
164 status = GRankNewGameAsync
165 (
166 s_server_context,
167 SV_RankNewGameCBF,
168 NULL,
169 GR_OPT_END
170 );
171 }
172
173 if( status != GR_STATUS_PENDING )
174 {
175 SV_RankError( "SV_RankBegin: Expected GR_STATUS_PENDING, got %s",
176 SV_RankStatusString( status ) );
177 return;
178 }
179
180 // logging
181 if( com_developer->value )
182 {
183 GRankLogLevel( GRLOG_TRACE );
184 }
185
186 // allocate rankings info for each player
187 s_ranked_players = Z_Malloc( sv_maxclients->value *
188 sizeof(ranked_player_t) );
189 memset( (void*)s_ranked_players, 0 ,sv_maxclients->value
190 * sizeof(ranked_player_t));
191 }
192
193 /*
194 ================
195 SV_RankEnd
196 ================
197 */
198 void SV_RankEnd( void )
199 {
200 GR_STATUS status;
201 int i;
202
203 Com_DPrintf( "SV_RankEnd();\n" );
204
205 if( !s_rankings_active )
206 {
207 // cleanup after error during game
208 if( s_ranked_players != NULL )
209 {
210 for( i = 0; i < sv_maxclients->value; i++ )
211 {
212 if( s_ranked_players[i].context != 0 )
213 {
214 SV_RankCloseContext( &(s_ranked_players[i]) );
215 }
216 }
217 }
218 if( s_server_context != 0 )
219 {
220 SV_RankCloseContext( NULL );
221 }
222
223 return;
224 }
225
226 for( i = 0; i < sv_maxclients->value; i++ )
227 {
228 if( s_ranked_players[i].grank_status == QGR_STATUS_ACTIVE )
229 {
230 SV_RankUserLogout( i );
231 Com_DPrintf( "SV_RankEnd: SV_RankUserLogout %d\n",i );
232 }
233 }
234
235 assert( s_server_context != 0 );
236
237 // send match reports, proceed to SV_RankSendReportsCBF
238 status = GRankSendReportsAsync
239 (
240 s_server_context,
241 0,
242 SV_RankSendReportsCBF,
243 NULL,
244 GR_OPT_END
245 );
246
247 if( status != GR_STATUS_PENDING )
248 {
249 SV_RankError( "SV_RankEnd: Expected GR_STATUS_PENDING, got %s",
250 SV_RankStatusString( status ) );
251 }
252
253 s_rankings_active = qfalse;
254 Cvar_Set( "sv_rankingsActive", "0" );
255 }
256
257 /*
258 ================
259 SV_RankPoll
260 ================
261 */
262 void SV_RankPoll( void )
263 {
264 GRankPoll();
265 }
266
267 /*
268 ================
269 SV_RankCheckInit
270 ================
271 */
272 qboolean SV_RankCheckInit( void )
273 {
274 return (s_rankings_contexts > 0);
275 }
276
277 /*
278 ================
279 SV_RankActive
280 ================
281 */
282 qboolean SV_RankActive( void )
283 {
284 return s_rankings_active;
285 }
286
287 /*
288 =================
289 SV_RankUserStatus
290 =================
291 */
292 grank_status_t SV_RankUserStatus( int index )
293 {
294 if( !s_rankings_active )
295 {
296 return GR_STATUS_ERROR;
297 }
298
299 assert( s_ranked_players != NULL );
300 assert( index >= 0 );
301 assert( index < sv_maxclients->value );
302
303 return s_ranked_players[index].grank_status;
304 }
305
306 /*
307 ================
308 SV_RankUserGRank
309 ================
310 */
311 int SV_RankUserGrank( int index )
312 {
313 if( !s_rankings_active )
314 {
315 return 0;
316 }
317
318 assert( s_ranked_players != NULL );
319 assert( index >= 0 );
320 assert( index < sv_maxclients->value );
321
322 return s_ranked_players[index].grank;
323 }
324
325 /*
326 ================
327 SV_RankUserReset
328 ================
329 */
330 void SV_RankUserReset( int index )
331 {
332 if( !s_rankings_active )
333 {
334 return;
335 }
336
337 assert( s_ranked_players != NULL );
338 assert( index >= 0 );
339 assert( index < sv_maxclients->value );
340
341 switch( s_ranked_players[index].grank_status )
342 {
343 case QGR_STATUS_SPECTATOR:
344 case QGR_STATUS_NO_USER:
345 case QGR_STATUS_BAD_PASSWORD:
346 case QGR_STATUS_USER_EXISTS:
347 case QGR_STATUS_NO_MEMBERSHIP:
348 case QGR_STATUS_TIMEOUT:
349 case QGR_STATUS_ERROR:
350 s_ranked_players[index].grank_status = QGR_STATUS_NEW;
351 break;
352 default:
353 break;
354 }
355 }
356
357 /*
358 ================
359 SV_RankUserSpectate
360 ================
361 */
362 void SV_RankUserSpectate( int index )
363 {
364 if( !s_rankings_active )
365 {
366 return;
367 }
368
369 assert( s_ranked_players != NULL );
370 assert( index >= 0 );
371 assert( index < sv_maxclients->value );
372
373 // GRANK_FIXME - check current status?
374 s_ranked_players[index].grank_status = QGR_STATUS_SPECTATOR;
375 }
376
377 /*
378 ================
379 SV_RankUserCreate
380 ================
381 */
382 void SV_RankUserCreate( int index, char* username, char* password,
383 char* email )
384 {
385 GR_INIT init;
386 GR_STATUS status;
387
388 assert( index >= 0 );
389 assert( index < sv_maxclients->value );
390 assert( username != NULL );
391 assert( password != NULL );
392 assert( email != NULL );
393 assert( s_ranked_players );
394 assert( s_ranked_players[index].grank_status != QGR_STATUS_ACTIVE );
395
396 Com_DPrintf( "SV_RankUserCreate( %d, %s, \"****\", %s );\n", index,
397 username, email );
398
399 if( !s_rankings_active )
400 {
401 Com_DPrintf( "SV_RankUserCreate: Not ready to create\n" );
402 s_ranked_players[index].grank_status = QGR_STATUS_ERROR;
403 return;
404 }
405
406 if( s_ranked_players[index].grank_status == QGR_STATUS_ACTIVE )
407 {
408 Com_DPrintf( "SV_RankUserCreate: Got Create from active player\n" );
409 return;
410 }
411
412 // get a separate context for the new user
413 init = GRankInit( 0, SV_RankGameKey, GR_OPT_POLL, GR_OPT_END );
414 s_ranked_players[index].context = init.context;
415 s_rankings_contexts++;
416 Com_DPrintf( "SV_RankUserCreate(); s_rankings_contexts=%d\n",s_rankings_contexts );
417 Com_DPrintf( "SV_RankUserCreate(); s_ranked_players[%d].context=%d\n",index,init.context );
418
419 // attempt to create a new account, proceed to SV_RankUserCBF
420 status = GRankUserCreateAsync
421 (
422 s_ranked_players[index].context,
423 username,
424 password,
425 email,
426 SV_RankUserCBF,
427 (void*)&s_ranked_players[index],
428 GR_OPT_END
429 );
430
431 if( status == GR_STATUS_PENDING )
432 {
433 s_ranked_players[index].grank_status = QGR_STATUS_PENDING;
434 s_ranked_players[index].final_status = QGR_STATUS_NEW;
435 }
436 else
437 {
438 SV_RankError( "SV_RankUserCreate: Expected GR_STATUS_PENDING, got %s",
439 SV_RankStatusString( status ) );
440 }
441 }
442
443 /*
444 ================
445 SV_RankUserLogin
446 ================
447 */
448 void SV_RankUserLogin( int index, char* username, char* password )
449 {
450 GR_INIT init;
451 GR_STATUS status;
452
453 assert( index >= 0 );
454 assert( index < sv_maxclients->value );
455 assert( username != NULL );
456 assert( password != NULL );
457 assert( s_ranked_players );
458 assert( s_ranked_players[index].grank_status != QGR_STATUS_ACTIVE );
459
460 Com_DPrintf( "SV_RankUserLogin( %d, %s, \"****\" );\n", index, username );
461
462 if( !s_rankings_active )
463 {
464 Com_DPrintf( "SV_RankUserLogin: Not ready for login\n" );
465 s_ranked_players[index].grank_status = QGR_STATUS_ERROR;
466 return;
467 }
468
469 if( s_ranked_players[index].grank_status == QGR_STATUS_ACTIVE )
470 {
471 Com_DPrintf( "SV_RankUserLogin: Got Login from active player\n" );
472 return;
473 }
474
475 // get a separate context for the new user
476 init = GRankInit( 0, SV_RankGameKey, GR_OPT_POLL, GR_OPT_END );
477 s_ranked_players[index].context = init.context;
478 s_rankings_contexts++;
479 Com_DPrintf( "SV_RankUserLogin(); s_rankings_contexts=%d\n",s_rankings_contexts );
480 Com_DPrintf( "SV_RankUserLogin(); s_ranked_players[%d].context=%d\n",index,init.context );
481
482 // login user, proceed to SV_RankUserCBF
483 status = GRankUserLoginAsync
484 (
485 s_ranked_players[index].context,
486 username,
487 password,
488 SV_RankUserCBF,
489 (void*)&s_ranked_players[index],
490 GR_OPT_END
491 );
492
493 if( status == GR_STATUS_PENDING )
494 {
495 s_ranked_players[index].grank_status = QGR_STATUS_PENDING;
496 s_ranked_players[index].final_status = QGR_STATUS_NEW;
497 }
498 else
499 {
500 SV_RankError( "SV_RankUserLogin: Expected GR_STATUS_PENDING, got %s",
501 SV_RankStatusString( status ) );
502 }
503 }
504
505 /*
506 ===================
507 SV_RankUserValidate
508 ===================
509 */
510 qboolean SV_RankUserValidate( int index, const char* player_id, const char* key, int token_len, int rank, char* name )
511 {
512 GR_INIT init;
513 GR_STATUS status;
514 qboolean rVal;
515 ranked_player_t* ranked_player;
516 int i;
517
518 assert( s_ranked_players );
519 assert( s_ranked_players[index].grank_status != QGR_STATUS_ACTIVE );
520
521 rVal = qfalse;
522
523 if( !s_rankings_active )
524 {
525 Com_DPrintf( "SV_RankUserValidate: Not ready to validate\n" );
526 s_ranked_players[index].grank_status = QGR_STATUS_ERROR;
527 return rVal;
528 }
529
530 ranked_player = &(s_ranked_players[index]);
531
532 if ( (player_id != NULL) && (key != NULL))
533 {
534 // the real player_id and key is set when SV_RankJoinGameCBF
535 // is called we do this so that SV_RankUserValidate
536 // can be shared by both server side login and client side login
537
538 // for client side logined in players
539 // server is creating GR_OPT_PLAYERCONTEXT
540 init = GRankInit( 0, SV_RankGameKey, GR_OPT_POLL, GR_OPT_END );
541 ranked_player->context = init.context;
542 s_rankings_contexts++;
543 Com_DPrintf( "SV_RankUserValidate(); s_rankings_contexts=%d\n",s_rankings_contexts );
544 Com_DPrintf( "SV_RankUserValidate(); s_ranked_players[%d].context=%d\n",index,init.context );
545
546 // uudecode player id and player token
547 ranked_player->player_id = SV_RankDecodePlayerID(player_id);
548 Com_DPrintf( "SV_RankUserValidate(); ranked_player->player_id =%u\n", (uint32_t)ranked_player->player_id );
549 SV_RankDecodePlayerKey(key, ranked_player->token);
550
551 // save name and check for duplicates
552 Q_strncpyz( ranked_player->name, name, sizeof(ranked_player->name) );
553 for( i = 0; i < sv_maxclients->value; i++ )
554 {
555 if( (i != index) && (s_ranked_players[i].grank_status == QGR_STATUS_ACTIVE) &&
556 (strcmp( s_ranked_players[i].name, name ) == 0) )
557 {
558 Com_DPrintf( "SV_RankUserValidate: Duplicate login\n" );
559 ranked_player->grank_status = QGR_STATUS_NO_USER;
560 ranked_player->final_status = QGR_STATUS_NEW;
561 ranked_player->grank = 0;
562 return qfalse;
563 }
564 }
565
566 // then validate
567 status = GRankPlayerValidate(
568 s_server_context,
569 ranked_player->player_id,
570 ranked_player->token,
571 token_len,
572 GR_OPT_PLAYERCONTEXT,
573 ranked_player->context,
574 GR_OPT_END);
575 }
576 else
577 {
578 // make server side login (bots) happy
579 status = GR_STATUS_OK;
580 }
581
582 if (status == GR_STATUS_OK)
583 {
584 ranked_player->grank_status = QGR_STATUS_ACTIVE;
585 ranked_player->final_status = QGR_STATUS_NEW;
586 ranked_player->grank = rank;
587 rVal = qtrue;
588 }
589 else if (status == GR_STATUS_INVALIDUSER)
590 {
591 ranked_player->grank_status = QGR_STATUS_INVALIDUSER;
592 ranked_player->final_status = QGR_STATUS_NEW;
593 ranked_player->grank = 0;
594 rVal = qfalse;
595 }
596 else
597 {
598 SV_RankError( "SV_RankUserValidate: Unexpected status %s",
599 SV_RankStatusString( status ) );
600 s_ranked_players[index].grank_status = QGR_STATUS_ERROR;
601 ranked_player->grank = 0;
602 }
603
604 return rVal;
605 }
606
607 /*
608 ================
609 SV_RankUserLogout
610 ================
611 */
612 void SV_RankUserLogout( int index )
613 {
614 GR_STATUS status;
615 GR_STATUS cleanup_status;
616
617 if( !s_rankings_active )
618 {
619 return;
620 }
621
622 assert( index >= 0 );
623 assert( index < sv_maxclients->value );
624 assert( s_ranked_players );
625
626 if( s_ranked_players[index].context == 0 ) {
627 return;
628 }
629
630 Com_DPrintf( "SV_RankUserLogout( %d );\n", index );
631
632 // masqueraded player may not be active yet, if they fail validation,
633 // but still they have a context needs to be cleaned
634 // what matters is the s_ranked_players[index].context
635
636 // send reports, proceed to SV_RankSendReportsCBF
637 status = GRankSendReportsAsync
638 (
639 s_ranked_players[index].context,
640 0,
641 SV_RankSendReportsCBF,
642 (void*)&s_ranked_players[index],
643 GR_OPT_END
644 );
645
646 if( status == GR_STATUS_PENDING )
647 {
648 s_ranked_players[index].grank_status = QGR_STATUS_PENDING;
649 s_ranked_players[index].final_status = QGR_STATUS_NEW;
650 }
651 else
652 {
653 SV_RankError( "SV_RankUserLogout: Expected GR_STATUS_PENDING, got %s",
654 SV_RankStatusString( status ) );
655
656 cleanup_status = GRankCleanupAsync
657 (
658 s_ranked_players[index].context,
659 0,
660 SV_RankCleanupCBF,
661 (void*)&s_ranked_players[index],
662 GR_OPT_END
663 );
664
665 if( cleanup_status != GR_STATUS_PENDING )
666 {
667 SV_RankError( "SV_RankUserLogout: Expected "
668 "GR_STATUS_PENDING from GRankCleanupAsync, got %s",
669 SV_RankStatusString( cleanup_status ) );
670 SV_RankCloseContext( &(s_ranked_players[index]) );
671 }
672 }
673 }
674
675 /*
676 ================
677 SV_RankReportInt
678 ================
679 */
680 void SV_RankReportInt( int index1, int index2, int key, int value,
681 qboolean accum )
682 {
683 GR_STATUS status;
684 GR_CONTEXT context;
685 uint64_t match;
686 uint64_t user1;
687 uint64_t user2;
688 int opt_accum;
689
690 if( !s_rankings_active )
691 {
692 return;
693 }
694
695 assert( index1 >= -1 );
696 assert( index1 < sv_maxclients->value );
697 assert( index2 >= -1 );
698 assert( index2 < sv_maxclients->value );
699 assert( s_ranked_players );
700
701 // Com_DPrintf( "SV_RankReportInt( %d, %d, %d, %d, %d );\n", index1, index2,
702 // key, value, accum );
703
704 // get context, match, and player_id for player index1
705 if( index1 == -1 )
706 {
707 context = s_server_context;
708 match = s_server_match;
709 user1 = 0;
710 }
711 else
712 {
713 if( s_ranked_players[index1].grank_status != QGR_STATUS_ACTIVE )
714 {
715 Com_DPrintf( "SV_RankReportInt: Expecting QGR_STATUS_ACTIVE"
716 " Got Unexpected status %d for player %d\n",
717 s_ranked_players[index1].grank_status, index1 );
718 return;
719 }
720
721 context = s_ranked_players[index1].context;
722 match = s_ranked_players[index1].match;
723 user1 = s_ranked_players[index1].player_id;
724 }
725
726 // get player_id for player index2
727 if( index2 == -1 )
728 {
729 user2 = 0;
730 }
731 else
732 {
733 if( s_ranked_players[index2].grank_status != QGR_STATUS_ACTIVE )
734 {
735 Com_DPrintf( "SV_RankReportInt: Expecting QGR_STATUS_ACTIVE"
736 " Got Unexpected status %d for player %d\n",
737 s_ranked_players[index2].grank_status, index2 );
738 return;
739 }
740
741 user2 = s_ranked_players[index2].player_id;
742 }
743
744 opt_accum = accum ? GR_OPT_ACCUM : GR_OPT_END;
745
746 status = GRankReportInt
747 (
748 context,
749 match,
750 user1,
751 user2,
752 key,
753 value,
754 opt_accum,
755 GR_OPT_END
756 );
757
758 if( status != GR_STATUS_OK )
759 {
760 SV_RankError( "SV_RankReportInt: Unexpected status %s",
761 SV_RankStatusString( status ) );
762 }
763
764 if( user2 != 0 )
765 {
766 context = s_ranked_players[index2].context;
767 match = s_ranked_players[index2].match;
768
769 status = GRankReportInt
770 (
771 context,
772 match,
773 user1,
774 user2,
775 key,
776 value,
777 opt_accum,
778 GR_OPT_END
779 );
780
781 if( status != GR_STATUS_OK )
782 {
783 SV_RankError( "SV_RankReportInt: Unexpected status %s",
784 SV_RankStatusString( status ) );
785 }
786 }
787 }
788
789 /*
790 ================
791 SV_RankReportStr
792 ================
793 */
794 void SV_RankReportStr( int index1, int index2, int key, char* value )
795 {
796 GR_STATUS status;
797 GR_CONTEXT context;
798 uint64_t match;
799 uint64_t user1;
800 uint64_t user2;
801
802 if( !s_rankings_active )
803 {
804 return;
805 }
806
807 assert( index1 >= -1 );
808 assert( index1 < sv_maxclients->value );
809 assert( index2 >= -1 );
810 assert( index2 < sv_maxclients->value );
811 assert( s_ranked_players );
812
813 // Com_DPrintf( "SV_RankReportStr( %d, %d, %d, \"%s\" );\n", index1, index2,
814 // key, value );
815
816 // get context, match, and player_id for player index1
817 if( index1 == -1 )
818 {
819 context = s_server_context;
820 match = s_server_match;
821 user1 = 0;
822 }
823 else
824 {
825 if( s_ranked_players[index1].grank_status != QGR_STATUS_ACTIVE )
826 {
827 Com_DPrintf( "SV_RankReportStr: Unexpected status %d\n",
828 s_ranked_players[index1].grank_status );
829 return;
830 }
831
832 context = s_ranked_players[index1].context;
833 match = s_ranked_players[index1].match;
834 user1 = s_ranked_players[index1].player_id;
835 }
836
837 // get player_id for player index2
838 if( index2 == -1 )
839 {
840 user2 = 0;
841 }
842 else
843 {
844 if( s_ranked_players[index2].grank_status != QGR_STATUS_ACTIVE )
845 {
846 Com_DPrintf( "SV_RankReportStr: Unexpected status %d\n",
847 s_ranked_players[index2].grank_status );
848 return;
849 }
850
851 user2 = s_ranked_players[index2].player_id;
852 }
853
854 status = GRankReportStr
855 (
856 context,
857 match,
858 user1,
859 user2,
860 key,
861 value,
862 GR_OPT_END
863 );
864
865 if( status != GR_STATUS_OK )
866 {
867 SV_RankError( "SV_RankReportStr: Unexpected status %s",
868 SV_RankStatusString( status ) );
869 }
870
871 if( user2 != 0 )
872 {
873 context = s_ranked_players[index2].context;
874 match = s_ranked_players[index2].match;
875
876 status = GRankReportStr
877 (
878 context,
879 match,
880 user1,
881 user2,
882 key,
883 value,
884 GR_OPT_END
885 );
886
887 if( status != GR_STATUS_OK )
888 {
889 SV_RankError( "SV_RankReportInt: Unexpected status %s",
890 SV_RankStatusString( status ) );
891 }
892 }
893 }
894
895 /*
896 ================
897 SV_RankQuit
898 ================
899 */
900 void SV_RankQuit( void )
901 {
902 int i;
903 int j = 0;
904 // yuck
905
906 while( s_rankings_contexts > 1 )
907 {
908 assert(s_ranked_players);
909 if( s_ranked_players != NULL )
910 {
911 for( i = 0; i < sv_maxclients->value; i++ )
912 {
913 // check for players that weren't yet active in SV_RankEnd
914 if( s_ranked_players[i].grank_status == QGR_STATUS_ACTIVE )
915 {
916 SV_RankUserLogout( i );
917 Com_DPrintf( "SV_RankQuit: SV_RankUserLogout %d\n",i );
918 }
919 else
920 {
921 if( s_ranked_players[i].context )
922 {
923 GR_STATUS cleanup_status;
924 cleanup_status = GRankCleanupAsync
925 (
926 s_ranked_players[i].context,
927 0,
928 SV_RankCleanupCBF,
929 (void*)&(s_ranked_players[i]),
930 GR_OPT_END
931 );
932
933 if( cleanup_status != GR_STATUS_PENDING )
934 {
935 SV_RankError( "SV_RankQuit: Expected "
936 "GR_STATUS_PENDING from GRankCleanupAsync, got %s",
937 SV_RankStatusString( cleanup_status ) );
938 }
939 }
940 }
941 }
942 }
943 SV_RankPoll();
944
945 // should've finished by now
946 assert( (j++) < 68 );
947 }
948 }
949
950 /*
951 ==============================================================================
952
953 Private Functions
954
955 ==============================================================================
956 */
957
958 /*
959 =================
960 SV_RankNewGameCBF
961 =================
962 */
963 static void SV_RankNewGameCBF( GR_NEWGAME* gr_newgame, void* cbf_arg )
964 {
965 GR_MATCH match;
966 int i;
967
968 assert( gr_newgame != NULL );
969 assert( cbf_arg == NULL );
970
971 Com_DPrintf( "SV_RankNewGameCBF( %08X, %08X );\n", gr_newgame, cbf_arg );
972
973 if( gr_newgame->status == GR_STATUS_OK )
974 {
975 char info[MAX_INFO_STRING];
976 char gameid[sizeof(s_ranked_players[i].game_id) * 4 / 3 + 2];
977
978 // save game id
979 s_rankings_game_id = gr_newgame->game_id;
980
981 // encode gameid
982 memset(gameid,0,sizeof(gameid));
983 SV_RankEncodeGameID(s_rankings_game_id,gameid,sizeof(gameid));
984
985 // set CS_GRANK rankingsGameID to pass to client
986 memset(info,0,sizeof(info));
987 Info_SetValueForKey( info, "rankingsGameKey", s_rankings_game_key );
988 Info_SetValueForKey( info, "rankingsGameID", gameid );
989 SV_SetConfigstring( CS_GRANK, info );
990
991 // initialize client status
992 for( i = 0; i < sv_maxclients->value; i++ )
993 s_ranked_players[i].grank_status = QGR_STATUS_NEW;
994
995 // start new match
996 match = GRankStartMatch( s_server_context );
997 s_server_match = match.match;
998
999 // ready to go
1000 s_rankings_active = qtrue;
1001 Cvar_Set( "sv_rankingsActive", "1" );
1002
1003 }
1004 else if( gr_newgame->status == GR_STATUS_BADLEAGUE )
1005 {
1006 SV_RankError( "SV_RankNewGameCBF: Invalid League name" );
1007 }
1008 else
1009 {
1010 //GRank handle new game failure
1011 // force SV_RankEnd() to run
1012 //SV_RankEnd();
1013 SV_RankError( "SV_RankNewGameCBF: Unexpected status %s",
1014 SV_RankStatusString( gr_newgame->status ) );
1015 }
1016 }
1017
1018 /*
1019 ================
1020 SV_RankUserCBF
1021 ================
1022 */
1023 static void SV_RankUserCBF( GR_LOGIN* gr_login, void* cbf_arg )
1024 {
1025 ranked_player_t* ranked_player;
1026 GR_STATUS join_status;
1027 GR_STATUS cleanup_status;
1028
1029 assert( gr_login != NULL );
1030 assert( cbf_arg != NULL );
1031
1032 Com_DPrintf( "SV_RankUserCBF( %08X, %08X );\n", gr_login, cbf_arg );
1033
1034 ranked_player = (ranked_player_t*)cbf_arg;
1035 assert(ranked_player);
1036 assert( ranked_player->context );
1037
1038 switch( gr_login->status )
1039 {
1040 case GR_STATUS_OK:
1041 // attempt to join the game, proceed to SV_RankJoinGameCBF
1042 join_status = GRankJoinGameAsync
1043 (
1044 ranked_player->context,
1045 s_rankings_game_id,
1046 SV_RankJoinGameCBF,
1047 cbf_arg,
1048 GR_OPT_END
1049 );
1050
1051 if( join_status != GR_STATUS_PENDING )
1052 {
1053 SV_RankError( "SV_RankUserCBF: Expected GR_STATUS_PENDING "
1054 "from GRankJoinGameAsync, got %s",
1055 SV_RankStatusString( join_status ) );
1056 }
1057 break;
1058 case GR_STATUS_NOUSER:
1059 Com_DPrintf( "SV_RankUserCBF: Got status %s\n",
1060 SV_RankStatusString( gr_login->status ) );
1061 ranked_player->final_status = QGR_STATUS_NO_USER;
1062 break;
1063 case GR_STATUS_BADPASSWORD:
1064 Com_DPrintf( "SV_RankUserCBF: Got status %s\n",
1065 SV_RankStatusString( gr_login->status ) );
1066 ranked_player->final_status = QGR_STATUS_BAD_PASSWORD;
1067 break;
1068 case GR_STATUS_TIMEOUT:
1069 Com_DPrintf( "SV_RankUserCBF: Got status %s\n",
1070 SV_RankStatusString( gr_login->status ) );
1071 ranked_player->final_status = QGR_STATUS_TIMEOUT;
1072 break;
1073 default:
1074 Com_DPrintf( "SV_RankUserCBF: Unexpected status %s\n",
1075 SV_RankStatusString( gr_login->status ) );
1076 ranked_player->final_status = QGR_STATUS_ERROR;
1077 break;
1078 }
1079
1080 if( ranked_player->final_status != QGR_STATUS_NEW )
1081 {
1082 // login or create failed, so clean up before the next attempt
1083 cleanup_status = GRankCleanupAsync
1084 (
1085 ranked_player->context,
1086 0,
1087 SV_RankCleanupCBF,
1088 (void*)ranked_player,
1089 GR_OPT_END
1090 );
1091
1092 if( cleanup_status != GR_STATUS_PENDING )
1093 {
1094 SV_RankError( "SV_RankUserCBF: Expected GR_STATUS_PENDING "
1095 "from GRankCleanupAsync, got %s",
1096 SV_RankStatusString( cleanup_status ) );
1097 SV_RankCloseContext( ranked_player );
1098 }
1099 }
1100 }
1101
1102 /*
1103 ================
1104 SV_RankJoinGameCBF
1105 ================
1106 */
1107 static void SV_RankJoinGameCBF( GR_JOINGAME* gr_joingame, void* cbf_arg )
1108 {
1109 ranked_player_t* ranked_player;
1110 GR_MATCH match;
1111 GR_STATUS cleanup_status;
1112
1113 assert( gr_joingame != NULL );
1114 assert( cbf_arg != NULL );
1115
1116 Com_DPrintf( "SV_RankJoinGameCBF( %08X, %08X );\n", gr_joingame, cbf_arg );
1117
1118 ranked_player = (ranked_player_t*)cbf_arg;
1119
1120 assert( ranked_player );
1121 assert( ranked_player->context != 0 );
1122
1123 if( gr_joingame->status == GR_STATUS_OK )
1124 {
1125 int i;
1126 // save user id
1127 ranked_player->player_id = gr_joingame->player_id;
1128 memcpy(ranked_player->token,gr_joingame->token,
1129 sizeof(GR_PLAYER_TOKEN)) ;
1130 match = GRankStartMatch( ranked_player->context );
1131 ranked_player->match = match.match;
1132 ranked_player->grank = gr_joingame->rank;
1133
1134 // find the index and call SV_RankUserValidate
1135 for (i=0;i<sv_maxclients->value;i++)
1136 if ( ranked_player == &s_ranked_players[i] )
1137 SV_RankUserValidate(i,NULL,NULL,0, gr_joingame->rank,ranked_player->name);
1138 }
1139 else
1140 {
1141 //GRand handle join game failure
1142 SV_RankError( "SV_RankJoinGameCBF: Unexpected status %s",
1143 SV_RankStatusString( gr_joingame->status ) );
1144
1145 cleanup_status = GRankCleanupAsync
1146 (
1147 ranked_player->context,
1148 0,
1149 SV_RankCleanupCBF,
1150 cbf_arg,
1151 GR_OPT_END
1152 );
1153
1154 if( cleanup_status != GR_STATUS_PENDING )
1155 {
1156 SV_RankError( "SV_RankJoinGameCBF: Expected "
1157 "GR_STATUS_PENDING from GRankCleanupAsync, got %s",
1158 SV_RankStatusString( cleanup_status ) );
1159 SV_RankCloseContext( ranked_player );
1160 }
1161 }
1162 }
1163
1164 /*
1165 ================
1166 SV_RankSendReportsCBF
1167 ================
1168 */
1169 static void SV_RankSendReportsCBF( GR_STATUS* status, void* cbf_arg )
1170 {
1171 ranked_player_t* ranked_player;
1172 GR_CONTEXT context;
1173 GR_STATUS cleanup_status;
1174
1175 assert( status != NULL );
1176 // NULL cbf_arg means server is sending match reports
1177
1178 Com_DPrintf( "SV_RankSendReportsCBF( %08X, %08X );\n", status, cbf_arg );
1179
1180 ranked_player = (ranked_player_t*)cbf_arg;
1181 if( ranked_player == NULL )
1182 {
1183 Com_DPrintf( "SV_RankSendReportsCBF: server\n" );
1184 context = s_server_context;
1185 }
1186 else
1187 {
1188 Com_DPrintf( "SV_RankSendReportsCBF: player\n" );
1189 context = ranked_player->context;
1190 }
1191
1192 //assert( context != 0 );
1193 if( *status != GR_STATUS_OK )
1194 {
1195 SV_RankError( "SV_RankSendReportsCBF: Unexpected status %s",
1196 SV_RankStatusString( *status ) );
1197 }
1198
1199 if( context == 0 )
1200 {
1201 Com_DPrintf( "SV_RankSendReportsCBF: WARNING: context == 0" );
1202 SV_RankCloseContext( ranked_player );
1203 }
1204 else
1205 {
1206 cleanup_status = GRankCleanupAsync
1207 (
1208 context,
1209 0,
1210 SV_RankCleanupCBF,
1211 cbf_arg,
1212 GR_OPT_END
1213 );
1214
1215 if( cleanup_status != GR_STATUS_PENDING )
1216 {
1217 SV_RankError( "SV_RankSendReportsCBF: Expected "
1218 "GR_STATUS_PENDING from GRankCleanupAsync, got %s",
1219 SV_RankStatusString( cleanup_status ) );
1220 SV_RankCloseContext( ranked_player );
1221 }
1222 }
1223 }
1224
1225 /*
1226 ================
1227 SV_RankCleanupCBF
1228 ================
1229 */
1230 static void SV_RankCleanupCBF( GR_STATUS* status, void* cbf_arg )
1231 {
1232 ranked_player_t* ranked_player;
1233 ranked_player = (ranked_player_t*)cbf_arg;
1234
1235 assert( status != NULL );
1236 // NULL cbf_arg means server is cleaning up
1237
1238 Com_DPrintf( "SV_RankCleanupCBF( %08X, %08X );\n", status, cbf_arg );
1239
1240 if( *status != GR_STATUS_OK )
1241 {
1242 SV_RankError( "SV_RankCleanupCBF: Unexpected status %s",
1243 SV_RankStatusString( *status ) );
1244 }
1245
1246 SV_RankCloseContext( ranked_player );
1247 }
1248
1249 /*
1250 ================
1251 SV_RankCloseContext
1252 ================
1253 */
1254 static void SV_RankCloseContext( ranked_player_t* ranked_player )
1255 {
1256 if( ranked_player == NULL )
1257 {
1258 // server cleanup
1259 if( s_server_context == 0 )
1260 {
1261 return;
1262 }
1263 s_server_context = 0;
1264 s_server_match = 0;
1265 }
1266 else
1267 {
1268 // player cleanup
1269 if( s_ranked_players == NULL )
1270 {
1271 return;
1272 }
1273 if( ranked_player->context == 0 )
1274 {
1275 return;
1276 }
1277 ranked_player->context = 0;
1278 ranked_player->match = 0;
1279 ranked_player->player_id = 0;
1280 memset( ranked_player->token, 0, sizeof(GR_PLAYER_TOKEN) );
1281 ranked_player->grank_status = ranked_player->final_status;
1282 ranked_player->final_status = QGR_STATUS_NEW;
1283 ranked_player->name[0] = '\0';
1284 }
1285
1286 assert( s_rankings_contexts > 0 );
1287 s_rankings_contexts--;
1288 Com_DPrintf( "SV_RankCloseContext: s_rankings_contexts = %d\n",
1289 s_rankings_contexts );
1290
1291 if( s_rankings_contexts == 0 )
1292 {
1293 GRankLogLevel( GRLOG_OFF );
1294
1295 if( s_ranked_players != NULL )
1296 {
1297 Z_Free( s_ranked_players );
1298 s_ranked_players = NULL;
1299 }
1300
1301 s_rankings_active = qfalse;
1302 Cvar_Set( "sv_rankingsActive", "0" );
1303 }
1304 }
1305
1306 /*
1307 ================
1308 SV_RankAsciiEncode
1309
1310 Encodes src_len bytes of binary data from the src buffer as ASCII text,
1311 using 6 bits per character. The result string is null-terminated and
1312 stored in the dest buffer.
1313
1314 The dest buffer must be at least (src_len * 4) / 3 + 2 bytes in length.
1315
1316 Returns the length of the result string, not including the null.
1317 ================
1318 */
1319 static int SV_RankAsciiEncode( char* dest, const unsigned char* src,
1320 int src_len )
1321 {
1322 unsigned char bin[3];
1323 unsigned char txt[4];
1324 int dest_len = 0;
1325 int i;
1326 int j;
1327 int num_chars;
1328
1329 assert( dest != NULL );
1330 assert( src != NULL );
1331
1332 for( i = 0; i < src_len; i += 3 )
1333 {
1334 // read three bytes of input
1335 for( j = 0; j < 3; j++ )
1336 {
1337 bin[j] = (i + j < src_len) ? src[i + j] : 0;
1338 }
1339
1340 // get four 6-bit values from three bytes
1341 txt[0] = bin[0] >> 2;
1342 txt[1] = ((bin[0] << 4) | (bin[1] >> 4)) & 63;
1343 txt[2] = ((bin[1] << 2) | (bin[2] >> 6)) & 63;
1344 txt[3] = bin[2] & 63;
1345
1346 // store ASCII encoding of 6-bit values
1347 num_chars = (i + 2 < src_len) ? 4 : ((src_len - i) * 4) / 3 + 1;
1348 for( j = 0; j < num_chars; j++ )
1349 {
1350 dest[dest_len++] = s_ascii_encoding[txt[j]];
1351 }
1352 }
1353
1354 dest[dest_len] = '\0';
1355
1356 return dest_len;
1357 }
1358
1359 /*
1360 ================
1361 SV_RankAsciiDecode
1362
1363 Decodes src_len characters of ASCII text from the src buffer, stores
1364 the binary result in the dest buffer.
1365
1366 The dest buffer must be at least (src_len * 3) / 4 bytes in length.
1367
1368 Returns the length of the binary result, or zero for invalid input.
1369 ================
1370 */
1371 static int SV_RankAsciiDecode( unsigned char* dest, const char* src,
1372 int src_len )
1373 {
1374 static unsigned char s_inverse_encoding[256];
1375 static char s_init = 0;
1376
1377 unsigned char bin[3];
1378 unsigned char txt[4];
1379 int dest_len = 0;
1380 int i;
1381 int j;
1382 int num_bytes;
1383
1384 assert( dest != NULL );
1385 assert( src != NULL );
1386
1387 if( !s_init )
1388 {
1389 // initialize lookup table for decoding
1390 memset( s_inverse_encoding, 255, sizeof(s_inverse_encoding) );
1391 for( i = 0; i < 64; i++ )
1392 {
1393 s_inverse_encoding[s_ascii_encoding[i]] = i;
1394 }
1395 s_init = 1;
1396 }
1397
1398 for( i = 0; i < src_len; i += 4 )
1399 {
1400 // read four characters of input, decode them to 6-bit values
1401 for( j = 0; j < 4; j++ )
1402 {
1403 txt[j] = (i + j < src_len) ? s_inverse_encoding[src[i + j]] : 0;
1404 if (txt[j] == 255)
1405 {
1406 return 0; // invalid input character
1407 }
1408 }
1409
1410 // get three bytes from four 6-bit values
1411 bin[0] = (txt[0] << 2) | (txt[1] >> 4);
1412 bin[1] = (txt[1] << 4) | (txt[2] >> 2);
1413 bin[2] = (txt[2] << 6) | txt[3];
1414
1415 // store binary data
1416 num_bytes = (i + 3 < src_len) ? 3 : ((src_len - i) * 3) / 4;
1417 for( j = 0; j < num_bytes; j++ )
1418 {
1419 dest[dest_len++] = bin[j];
1420 }
1421 }
1422
1423 return dest_len;
1424 }
1425
1426 /*
1427 ================
1428 SV_RankEncodeGameID
1429 ================
1430 */
1431 static void SV_RankEncodeGameID( uint64_t game_id, char* result,
1432 int len )
1433 {
1434 assert( result != NULL );
1435
1436 if( len < ( ( sizeof(game_id) * 4) / 3 + 2) )
1437 {
1438 Com_DPrintf( "SV_RankEncodeGameID: result buffer too small\n" );
1439 result[0] = '\0';
1440 }
1441 else
1442 {
1443 qint64 gameid = LittleLong64(*(qint64*)&game_id);
1444 SV_RankAsciiEncode( result, (unsigned char*)&gameid,
1445 sizeof(qint64) );
1446 }
1447 }
1448
1449 /*
1450 ================
1451 SV_RankDecodePlayerID
1452 ================
1453 */
1454 static uint64_t SV_RankDecodePlayerID( const char* string )
1455 {
1456 unsigned char buffer[9];
1457 int len;
1458 qint64 player_id;
1459
1460 assert( string != NULL );
1461
1462 len = strlen (string) ;
1463 Com_DPrintf( "SV_RankDecodePlayerID: string length %d\n",len );
1464 SV_RankAsciiDecode( buffer, string, len );
1465 player_id = LittleLong64(*(qint64*)buffer);
1466 return *(uint64_t*)&player_id;
1467 }
1468
1469 /*
1470 ================
1471 SV_RankDecodePlayerKey
1472 ================
1473 */
1474 static void SV_RankDecodePlayerKey( const char* string, GR_PLAYER_TOKEN key )
1475 {
1476 unsigned char buffer[1400];
1477 int len;
1478 assert( string != NULL );
1479
1480 len = strlen (string) ;
1481 Com_DPrintf( "SV_RankDecodePlayerKey: string length %d\n",len );
1482
1483 memset(key,0,sizeof(GR_PLAYER_TOKEN));
1484 memset(buffer,0,sizeof(buffer));
1485 memcpy( key, buffer, SV_RankAsciiDecode( buffer, string, len ) );
1486 }
1487
1488 /*
1489 ================
1490 SV_RankStatusString
1491 ================
1492 */
1493 static char* SV_RankStatusString( GR_STATUS status )
1494 {
1495 switch( status )
1496 {
1497 case GR_STATUS_OK: return "GR_STATUS_OK";
1498 case GR_STATUS_ERROR: return "GR_STATUS_ERROR";
1499 case GR_STATUS_BADPARAMS: return "GR_STATUS_BADPARAMS";
1500 case GR_STATUS_NETWORK: return "GR_STATUS_NETWORK";
1501 case GR_STATUS_NOUSER: return "GR_STATUS_NOUSER";
1502 case GR_STATUS_BADPASSWORD: return "GR_STATUS_BADPASSWORD";
1503 case GR_STATUS_BADGAME: return "GR_STATUS_BADGAME";
1504 case GR_STATUS_PENDING: return "GR_STATUS_PENDING";
1505 case GR_STATUS_BADDOMAIN: return "GR_STATUS_BADDOMAIN";
1506 case GR_STATUS_DOMAINLOCK: return "GR_STATUS_DOMAINLOCK";
1507 case GR_STATUS_TIMEOUT: return "GR_STATUS_TIMEOUT";
1508 case GR_STATUS_INVALIDUSER: return "GR_STATUS_INVALIDUSER";
1509 case GR_STATUS_INVALIDCONTEXT: return "GR_STATUS_INVALIDCONTEXT";
1510 default: return "(UNKNOWN)";
1511 }
1512 }
1513
1514 /*
1515 ================
1516 SV_RankError
1517 ================
1518 */
1519 static void SV_RankError( const char* fmt, ... )
1520 {
1521 va_list arg_ptr;
1522 char text[1024];
1523
1524 va_start( arg_ptr, fmt );
1525 Q_vsnprintf(text, sizeof(text), fmt, arg_ptr );
1526 va_end( arg_ptr );
1527
1528 Com_DPrintf( "****************************************\n" );
1529 Com_DPrintf( "SV_RankError: %s\n", text );
1530 Com_DPrintf( "****************************************\n" );
1531
1532 s_rankings_active = qfalse;
1533 Cvar_Set( "sv_rankingsActive", "0" );
1534 // FIXME - attempt clean shutdown?
1535 }
1536