diff options
author | M. Kristall <mkpdev@gmail.com> | 2011-01-11 04:21:15 +0000 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-01-03 00:17:50 +0000 |
commit | 4da76278ada8d004598e84753e4833fce79019ef (patch) | |
tree | 906d3f2a83ccc27103f30edcd289d86cce1c4ab8 | |
parent | bb4db330cb3f223ee786a57cfd2b603c1bfa053e (diff) |
* Refactor code handling player name matching
-rw-r--r-- | src/game/g_admin.c | 65 | ||||
-rw-r--r-- | src/game/g_cmds.c | 127 | ||||
-rw-r--r-- | src/game/g_local.h | 3 | ||||
-rw-r--r-- | src/game/g_svcmds.c | 20 |
4 files changed, 90 insertions, 125 deletions
diff --git a/src/game/g_admin.c b/src/game/g_admin.c index 893187d4..c524bd54 100644 --- a/src/game/g_admin.c +++ b/src/game/g_admin.c @@ -1460,7 +1460,7 @@ int G_admin_parse_time( const char *time ) qboolean G_admin_kick( gentity_t *ent ) { - int pids[ MAX_CLIENTS ], found; + int pid; char name[ MAX_NAME_LENGTH ], *reason, err[ MAX_STRING_CHARS ]; int minargc; gentity_t *vic; @@ -1476,13 +1476,12 @@ qboolean G_admin_kick( gentity_t *ent ) } trap_Argv( 1, name, sizeof( name ) ); reason = ConcatArgs( 2 ); - if( ( found = G_ClientNumbersFromString( name, pids, MAX_CLIENTS ) ) != 1 ) + if( ( pid = G_ClientNumberFromString( name, err, sizeof( err ) ) ) == -1 ) { - G_MatchOnePlayer( pids, found, err, sizeof( err ) ); - ADMP( va( "^3kick: ^7%s\n", err ) ); + ADMP( va( "^3kick: ^7%s", err ) ); return qfalse; } - vic = &g_entities[ pids[ 0 ] ]; + vic = &g_entities[ pid ]; if( !admin_higher( ent, vic ) ) { ADMP( "^3kick: ^7sorry, but your intended victim has a higher admin" @@ -1877,7 +1876,7 @@ qboolean G_admin_adjustban( gentity_t *ent ) qboolean G_admin_putteam( gentity_t *ent ) { - int pids[ MAX_CLIENTS ], found; + int pid; char name[ MAX_NAME_LENGTH ], team[ sizeof( "spectators" ) ], err[ MAX_STRING_CHARS ]; gentity_t *vic; @@ -1891,19 +1890,18 @@ qboolean G_admin_putteam( gentity_t *ent ) return qfalse; } - if( ( found = G_ClientNumbersFromString( name, pids, MAX_CLIENTS ) ) != 1 ) + if( ( pid = G_ClientNumberFromString( name, err, sizeof( err ) ) ) == -1 ) { - G_MatchOnePlayer( pids, found, err, sizeof( err ) ); - ADMP( va( "^3putteam: ^7%s\n", err ) ); + ADMP( va( "^3putteam: ^7%s", err ) ); return qfalse; } - if( !admin_higher( ent, &g_entities[ pids[ 0 ] ] ) ) + vic = &g_entities[ pid ]; + if( !admin_higher( ent, vic ) ) { ADMP( "^3putteam: ^7sorry, but your intended victim has a higher " " admin level than you\n" ); return qfalse; } - vic = &g_entities[ pids[ 0 ] ]; teamnum = G_TeamFromString( team ); if( teamnum == NUM_TEAMS ) { @@ -1965,7 +1963,7 @@ qboolean G_admin_changemap( gentity_t *ent ) qboolean G_admin_mute( gentity_t *ent ) { - int pids[ MAX_CLIENTS ], found; + int pid; char name[ MAX_NAME_LENGTH ], err[ MAX_STRING_CHARS ]; char command[ MAX_ADMIN_CMD_LEN ]; gentity_t *vic; @@ -1977,19 +1975,18 @@ qboolean G_admin_mute( gentity_t *ent ) return qfalse; } trap_Argv( 1, name, sizeof( name ) ); - if( ( found = G_ClientNumbersFromString( name, pids, MAX_CLIENTS ) ) != 1 ) + if( ( pid = G_ClientNumberFromString( name, err, sizeof( err ) ) ) == -1 ) { - G_MatchOnePlayer( pids, found, err, sizeof( err ) ); - ADMP( va( "^3%s: ^7%s\n", command, err ) ); + ADMP( va( "^3%s: ^7%s", command, err ) ); return qfalse; } - if( !admin_higher( ent, &g_entities[ pids[ 0 ] ] ) ) + vic = &g_entities[ pid ]; + if( !admin_higher( ent, vic ) ) { ADMP( va( "^3%s: ^7sorry, but your intended victim has a higher admin" " level than you\n", command ) ); return qfalse; } - vic = &g_entities[ pids[ 0 ] ]; if( vic->client->pers.namelog->muted ) { if( !Q_stricmp( command, "mute" ) ) @@ -1998,7 +1995,7 @@ qboolean G_admin_mute( gentity_t *ent ) return qtrue; } vic->client->pers.namelog->muted = qfalse; - CPx( pids[ 0 ], "cp \"^1You have been unmuted\"" ); + CPx( pid, "cp \"^1You have been unmuted\"" ); AP( va( "print \"^3unmute: ^7%s^7 has been unmuted by %s\n\"", vic->client->pers.netname, ( ent ) ? ent->client->pers.netname : "console" ) ); @@ -2011,7 +2008,7 @@ qboolean G_admin_mute( gentity_t *ent ) return qtrue; } vic->client->pers.namelog->muted = qtrue; - CPx( pids[ 0 ], "cp \"^1You've been muted\"" ); + CPx( pid, "cp \"^1You've been muted\"" ); AP( va( "print \"^3mute: ^7%s^7 has been muted by ^7%s\n\"", vic->client->pers.netname, ( ent ) ? ent->client->pers.netname : "console" ) ); @@ -2021,7 +2018,7 @@ qboolean G_admin_mute( gentity_t *ent ) qboolean G_admin_denybuild( gentity_t *ent ) { - int pids[ MAX_CLIENTS ], found; + int pid; char name[ MAX_NAME_LENGTH ], err[ MAX_STRING_CHARS ]; char command[ MAX_ADMIN_CMD_LEN ]; gentity_t *vic; @@ -2033,19 +2030,18 @@ qboolean G_admin_denybuild( gentity_t *ent ) return qfalse; } trap_Argv( 1, name, sizeof( name ) ); - if( ( found = G_ClientNumbersFromString( name, pids, MAX_CLIENTS ) ) != 1 ) + if( ( pid = G_ClientNumberFromString( name, err, sizeof( err ) ) ) == -1 ) { - G_MatchOnePlayer( pids, found, err, sizeof( err ) ); - ADMP( va( "^3%s: ^7%s\n", command, err ) ); + ADMP( va( "^3%s: ^7%s", command, err ) ); return qfalse; } - if( !admin_higher( ent, &g_entities[ pids[ 0 ] ] ) ) + vic = &g_entities[ pid ]; + if( !admin_higher( ent, vic ) ) { ADMP( va( "^3%s: ^7sorry, but your intended victim has a higher admin" " level than you\n", command ) ); return qfalse; } - vic = &g_entities[ pids[ 0 ] ]; if( vic->client->pers.namelog->denyBuild ) { if( !Q_stricmp( command, "denybuild" ) ) @@ -2054,7 +2050,7 @@ qboolean G_admin_denybuild( gentity_t *ent ) return qtrue; } vic->client->pers.namelog->denyBuild = qfalse; - CPx( pids[ 0 ], "cp \"^1You've regained your building rights\"" ); + CPx( pid, "cp \"^1You've regained your building rights\"" ); AP( va( "print \"^3allowbuild: ^7building rights for ^7%s^7 restored by %s\n\"", vic->client->pers.netname, @@ -2069,7 +2065,7 @@ qboolean G_admin_denybuild( gentity_t *ent ) } vic->client->pers.namelog->denyBuild = qtrue; vic->client->ps.stats[ STAT_BUILDABLE ] = BA_NONE; - CPx( pids[ 0 ], "cp \"^1You've lost your building rights\"" ); + CPx( pid, "cp \"^1You've lost your building rights\"" ); AP( va( "print \"^3denybuild: ^7building rights for ^7%s^7 revoked by ^7%s\n\"", vic->client->pers.netname, @@ -2650,7 +2646,7 @@ qboolean G_admin_spec999( gentity_t *ent ) qboolean G_admin_rename( gentity_t *ent ) { - int pids[ MAX_CLIENTS ], found; + int pid; char name[ MAX_NAME_LENGTH ]; char newname[ MAX_NAME_LENGTH ]; char err[ MAX_STRING_CHARS ]; @@ -2664,13 +2660,12 @@ qboolean G_admin_rename( gentity_t *ent ) } trap_Argv( 1, name, sizeof( name ) ); Q_strncpyz( newname, ConcatArgs( 2 ), sizeof( newname ) ); - if( ( found = G_ClientNumbersFromString( name, pids, MAX_CLIENTS ) ) != 1 ) + if( ( pid = G_ClientNumberFromString( name, err, sizeof( err ) ) ) == -1 ) { - G_MatchOnePlayer( pids, found, err, sizeof( err ) ); - ADMP( va( "^3rename: ^7%s\n", err ) ); + ADMP( va( "^3rename: ^7%s", err ) ); return qfalse; } - victim = &g_entities[ pids[ 0 ] ]; + victim = &g_entities[ pid ]; if( !admin_higher( ent, victim ) ) { ADMP( "^3rename: ^7sorry, but your intended victim has a higher admin" @@ -2687,14 +2682,14 @@ qboolean G_admin_rename( gentity_t *ent ) ADMP( "^3rename: ^7sorry, but your intended victim is still connecting\n" ); return qfalse; } - trap_GetUserinfo( pids[ 0 ], userinfo, sizeof( userinfo ) ); + trap_GetUserinfo( pid, userinfo, sizeof( userinfo ) ); AP( va( "print \"^3rename: ^7%s^7 has been renamed to %s^7 by %s\n\"", victim->client->pers.netname, newname, ( ent ) ? ent->client->pers.netname : "console" ) ); Info_SetValueForKey( userinfo, "name", newname ); - trap_SetUserinfo( pids[ 0 ], userinfo ); - ClientUserinfoChanged( pids[ 0 ], qtrue ); + trap_SetUserinfo( pid, userinfo ); + ClientUserinfoChanged( pid, qtrue ); return qtrue; } diff --git a/src/game/g_cmds.c b/src/game/g_cmds.c index 87165d13..bd2a9a38 100644 --- a/src/game/g_cmds.c +++ b/src/game/g_cmds.c @@ -57,15 +57,26 @@ void G_SanitiseString( char *in, char *out, int len ) G_ClientNumberFromString Returns a player number for either a number or name string -Returns -1 if invalid +Returns -1 and optionally sets err if invalid or not exactly 1 match +err will have a trailing \n if set ================== */ -int G_ClientNumberFromString( char *s ) +int G_ClientNumberFromString( char *s, char *err, int len ) { gclient_t *cl; - int i; + int i, found = 0, m = -1; char s2[ MAX_NAME_LENGTH ]; char n2[ MAX_NAME_LENGTH ]; + char *p = err; + int l, l2 = len; + + if( !s[ 0 ] ) + { + if( p ) + Q_strncpyz( p, "no player name or slot # provided\n", len ); + + return -1; + } // numeric values are just slot numbers for( i = 0; s[ i ] && isdigit( s[ i ] ); i++ ); @@ -84,6 +95,15 @@ int G_ClientNumberFromString( char *s ) return i; } + if( p ) + { + Q_strncpyz( p, "more than one player name matches. " + "be more specific or use the slot #:\n", l2 ); + l = strlen( p ); + p += l; + l2 -= l; + } + // check for a name match G_SanitiseString( s, s2, sizeof( s2 ) ); @@ -96,48 +116,28 @@ int G_ClientNumberFromString( char *s ) if( !strcmp( n2, s2 ) ) return i; - } - - return -1; -} + if( strstr( n2, s2 ) ) + { + if( p ) + { + l = Q_snprintf( p, l2, "%-2d - %s^7\n", i, cl->pers.netname ); + p += l; + l2 -= l; + } -/* -================== -G_MatchOnePlayer + found++; + m = i; + } + } -This is a companion function to G_ClientNumbersFromString() + if( found == 1 ) + return m; -err will be populated with an error message. -================== -*/ -void G_MatchOnePlayer( int *plist, int num, char *err, int len ) -{ - gclient_t *cl; - int i; - char line[ MAX_NAME_LENGTH + 10 ] = {""}; + if( found == 0 && err ) + Q_strncpyz( err, "no connected player by that name or slot #\n", len ); - err[ 0 ] = '\0'; - if( num == 0 ) - { - Q_strcat( err, len, "no connected player by that name or slot #" ); - } - else if( num > 1 ) - { - Q_strcat( err, len, "more than one player name matches. " - "be more specific or use the slot #:\n" ); - for( i = 0; i < num; i++ ) - { - cl = &level.clients[ plist[ i ] ]; - if( cl->pers.connected == CON_DISCONNECTED ) - continue; - Com_sprintf( line, sizeof( line ), "%2i - %s^7\n", - plist[ i ], cl->pers.netname ); - if( strlen( err ) + strlen( line ) > len ) - break; - Q_strcat( err, len, line ); - } - } + return -1; } /* @@ -1144,8 +1144,6 @@ void Cmd_CallVote_f( gentity_t *ent ) !Q_stricmp( vote, "mute" ) || !Q_stricmp( vote, "unmute" ) || !Q_stricmp( vote, "denybuild" ) || !Q_stricmp( vote, "allowbuild" ) ) { - int clientNums[ MAX_CLIENTS ]; - int matches; char err[ MAX_STRING_CHARS ]; if( !arg[ 0 ] ) @@ -1156,29 +1154,16 @@ void Cmd_CallVote_f( gentity_t *ent ) } // with a little extra work only players from the right team are considered - matches = G_ClientNumbersFromString( arg, clientNums, MAX_CLIENTS ); - if( matches == 1 ) - clientNum = clientNums[ 0 ]; - else - clientNum = G_ClientNumberFromString( arg ); + clientNum = G_ClientNumberFromString( arg, err, sizeof( err ) ); - if( clientNum != -1 ) - { - G_DecolorString( level.clients[ clientNum ].pers.netname, name, sizeof( name ) ); - } - else if( matches > 1 ) - { - G_MatchOnePlayer( clientNums, matches, err, sizeof( err ) ); - ADMP( va( "%s: %s\n", cmd, err ) ); - return; - } - else + if( clientNum == -1 ) { - trap_SendServerCommand( ent-g_entities, - va( "print \"%s: invalid player\n\"", cmd ) ); + ADMP( va( "%s: %s", cmd, err ) ); return; } + G_DecolorString( level.clients[ clientNum ].pers.netname, name, sizeof( name ) ); + if( !Q_stricmp( vote, "kick" ) || !Q_stricmp( vote, "mute" ) || !Q_stricmp( vote, "denybuild" ) ) { @@ -2687,7 +2672,6 @@ Cmd_Follow_f void Cmd_Follow_f( gentity_t *ent ) { int i; - int pids[ MAX_CLIENTS ]; char arg[ MAX_NAME_LENGTH ]; // won't work unless spectating @@ -2700,21 +2684,16 @@ void Cmd_Follow_f( gentity_t *ent ) } else { + char err[ MAX_STRING_CHARS ]; trap_Argv( 1, arg, sizeof( arg ) ); - if( G_ClientNumbersFromString( arg, pids, MAX_CLIENTS ) == 1 ) - { - i = pids[ 0 ]; - } - else - { - i = G_ClientNumberFromString( arg ); - if( i == -1 ) - { - trap_SendServerCommand( ent - g_entities, - "print \"follow: invalid player\n\"" ); - return; - } + i = G_ClientNumberFromString( arg, err, sizeof( err ) ); + + if( i == -1 ) + { + trap_SendServerCommand( ent - g_entities, + va( "print \"follow: %s\"", err ) ); + return; } // can't follow self diff --git a/src/game/g_local.h b/src/game/g_local.h index 8f1a6502..f55503c8 100644 --- a/src/game/g_local.h +++ b/src/game/g_local.h @@ -721,8 +721,7 @@ void G_StopFromFollowing( gentity_t *ent ); void G_FollowLockView( gentity_t *ent ); qboolean G_FollowNewClient( gentity_t *ent, int dir ); void G_ToggleFollow( gentity_t *ent ); -void G_MatchOnePlayer( int *plist, int num, char *err, int len ); -int G_ClientNumberFromString( char *s ); +int G_ClientNumberFromString( char *s, char *err, int len ); int G_ClientNumbersFromString( char *s, int *plist, int max ); char *ConcatArgs( int start ); void G_Say( gentity_t *ent, saymode_t mode, const char *chatText ); diff --git a/src/game/g_svcmds.c b/src/game/g_svcmds.c index ff9b0895..9338766f 100644 --- a/src/game/g_svcmds.c +++ b/src/game/g_svcmds.c @@ -117,23 +117,15 @@ void Svcmd_EntityList_f( void ) static gclient_t *ClientForString( char *s ) { - int idnum, count; - int pids[ MAX_CLIENTS ]; + int idnum; + char err[ MAX_STRING_CHARS ]; - if( ( count = G_ClientNumbersFromString( s, pids, MAX_CLIENTS ) ) != 1 ) + idnum = G_ClientNumberFromString( s, err, sizeof( err ) ); + if( idnum == -1 ) { - idnum = G_ClientNumberFromString( s ); - - if( idnum == -1 ) - { - char err[ MAX_STRING_CHARS ]; - G_MatchOnePlayer( pids, count, err, sizeof( err ) ); - G_Printf( "%s\n", err ); - return NULL; - } + G_Printf( "%s", err ); + return NULL; } - else - idnum = pids[ 0 ]; return &level.clients[ idnum ]; } |