From 55b78e4430414111716db46e0180d58aef65d8e5 Mon Sep 17 00:00:00 2001 From: zolex Date: Thu, 6 Oct 2011 19:02:34 +0200 Subject: [PATCH] added orderby to the ranking command --- racesow/progs/gametypes/racesow/commands.as | 26 +++++++++++++++++--- source/game/g_ascript.c | 9 ++++--- source/game/g_racesow.c | 27 ++++++++++++++++----- source/game/g_racesow.h | 3 ++- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/racesow/progs/gametypes/racesow/commands.as b/racesow/progs/gametypes/racesow/commands.as index f949c1f4..bf793973 100644 --- a/racesow/progs/gametypes/racesow/commands.as +++ b/racesow/progs/gametypes/racesow/commands.as @@ -266,11 +266,13 @@ class Command_Top : Racesow_Command class Command_Ranking : Racesow_Command { int page; - + cString order; + bool validate(Racesow_Player @player, cString &args, int argc) { this.page = 1; - + this.order = "points"; + if ( mysqlConnected == 0 ) { player.sendMessage("This server doesn't store the best times, this command is useless\n" ); @@ -293,13 +295,29 @@ class Command_Ranking : Racesow_Command } } + if ( argc > 1 ) + { + cString secondToken = args.getToken(1); + if ( secondToken == "points" || secondToken == "diff_points" || + secondToken == "maps" || secondToken == "races" || + secondToken == "playtime" ) { + + this.order = secondToken; + + } else if ( secondToken != "" ) { + + player.sendErrorMessage("invalid order given"); + return false; + } + } + return true; } bool execute(Racesow_Player @player, cString &args, int argc) { player.isWaitingForCommand = true; - RS_MysqlLoadRanking(player.getClient().playerNum(), this.page); + RS_MysqlLoadRanking(player.getClient().playerNum(), this.page, this.order); return true; } @@ -1141,7 +1159,7 @@ void RS_CreateCommands() Command_Ranking ranking; ranking.name = "ranking"; ranking.description = "Print the server ranking"; - ranking.usage = "ranking "; + ranking.usage = "ranking "; ranking.modFlag = MODFLAG_RACE | MODFLAG_DURACE | MODFLAG_DRACE | MODFLAG_TRACE; @commands[commandCount] = @ranking; commandCount++; diff --git a/source/game/g_ascript.c b/source/game/g_ascript.c index da6f400e..3b5b9024 100644 --- a/source/game/g_ascript.c +++ b/source/game/g_ascript.c @@ -6437,16 +6437,17 @@ static void asFunc_asGeneric_RS_MysqlLoadHighscores( void *gen ) } // RS_MysqlLoadRanking -static qboolean asFunc_RS_MysqlLoadRanking( int playerNum, int page ) +static qboolean asFunc_RS_MysqlLoadRanking( int playerNum, int page, asstring_t *order ) { - return RS_MysqlLoadRanking(playerNum, page); + return RS_MysqlLoadRanking(playerNum, page, order->buffer); } static void asFunc_asGeneric_RS_MysqlLoadRanking( void *gen ) { G_asGeneric_SetReturnBool(gen, asFunc_RS_MysqlLoadRanking( (int)G_asGeneric_GetArgInt(gen, 0), - (int)G_asGeneric_GetArgInt(gen, 1))); + (int)G_asGeneric_GetArgInt(gen, 1), + (asstring_t *)G_asGeneric_GetArgAddress(gen, 2))); } // RS_MysqlSetOneliner @@ -7109,7 +7110,7 @@ static asglobfuncs_t asGlobFuncs[] = { "bool RS_MysqlLoadMap()", asFunc_RS_MysqlLoadMap, asFunc_asGeneric_RS_MysqlLoadMap }, { "bool RS_MysqlInsertRace( int, int, int, int, int, int, int, cString &, bool )", asFunc_RS_MysqlInsertRace, asFunc_asGeneric_RS_MysqlInsertRace }, { "bool RS_MysqlLoadHighscores( int, int, int, cString &, int)", asFunc_RS_MysqlLoadHighscores, asFunc_asGeneric_RS_MysqlLoadHighscores }, - { "bool RS_MysqlLoadRanking( int, int )", asFunc_RS_MysqlLoadRanking, asFunc_asGeneric_RS_MysqlLoadRanking }, + { "bool RS_MysqlLoadRanking( int, int, cString & )", asFunc_RS_MysqlLoadRanking, asFunc_asGeneric_RS_MysqlLoadRanking }, { "bool RS_MysqlSetOneliner( int, int, int, cString &)", asFunc_RS_MysqlSetOneliner, asFunc_asGeneric_RS_MysqlSetOneliner }, { "bool RS_PopCallbackQueue( int &out, int &out, int &out, int &out, int &out, int &out, int &out, int &out )", asFunc_RS_PopCallbackQueue, asFunc_asGeneric_RS_PopCallbackQueue }, { "bool RS_MapFilter( int, cString &, int )", asFunc_RS_MapFilter, asFunc_asGeneric_RS_MapFilter}, diff --git a/source/game/g_racesow.c b/source/game/g_racesow.c index 4e4cb74e..b33e291c 100644 --- a/source/game/g_racesow.c +++ b/source/game/g_racesow.c @@ -2469,7 +2469,7 @@ void *RS_MysqlLoadHighscores_Thread( void* in ) { * @param int page * @return qboolean */ -qboolean RS_MysqlLoadRanking( int playerNum, int page ) +qboolean RS_MysqlLoadRanking( int playerNum, int page, char *order ) { pthread_t thread; int returnCode; @@ -2478,6 +2478,7 @@ qboolean RS_MysqlLoadRanking( int playerNum, int page ) rankingData->playerNum = playerNum; rankingData->page = page; + rankingData->order = strdup(order); returnCode = pthread_create(&thread, &threadAttr, RS_MysqlLoadRanking_Thread, (void *)rankingData); if (returnCode) { @@ -2503,19 +2504,21 @@ void *RS_MysqlLoadRanking_Thread( void* in ) { int offset; int page; char ranking[10000]; + char *order; struct rankingDataStruct *rankingData; rankingData = (struct rankingData *)in; playerNum = rankingData->playerNum; page = rankingData->page; - + order = strdup(rankingData->order); + RS_StartMysqlThread(); // get top players on map limit = 20; offset = (page - 1) * limit; //SELECT `name`, `points`, `diff_points`, `races`, `maps`, `playtime` FROM player ORDER BY `%s` %s LIMIT %d, %d; - sprintf(query, rs_queryLoadRanking->string, "points", "DESC", offset, limit); + sprintf(query, rs_queryLoadRanking->string, order, "DESC", offset, limit); mysql_real_query(&mysql, query, strlen(query)); RS_CheckMysqlThreadError(query); mysql_res = mysql_store_result(&mysql); @@ -2532,13 +2535,23 @@ void *RS_MysqlLoadRanking_Thread( void* in ) { unsigned int position = offset; Q_strncatz(ranking, va( "%sServer ranking, page %d\n", S_COLOR_ORANGE, page ), sizeof(ranking)); - Q_strncatz(ranking, va( "%sPlayer points (diff)\n", S_COLOR_ORANGE ), sizeof(ranking)); + if ( !Q_stricmp(order, "points") || !Q_stricmp(order, "diff_points") ) + Q_strncatz(ranking, va( "%sPlayer points (diff)\n", S_COLOR_WHITE ), sizeof(ranking)); + else + Q_strncatz(ranking, va( "%sPlayer %s\n", S_COLOR_WHITE, order ), sizeof(ranking)); while( ( row = mysql_fetch_row( mysql_res ) ) != NULL ) { - position++; - Q_strncatz( ranking, va( "%s%d. %s %s%d (%d)\n", S_COLOR_WHITE, position, row[0], S_COLOR_WHITE, atoi(row[1]), atoi(row[2]) ), sizeof(ranking) ); + + if ( !Q_stricmp(order, "points") || !Q_stricmp(order, "diff_points") ) + Q_strncatz( ranking, va( "%s%d. %s %s%d (%d)\n", S_COLOR_WHITE, position, row[0], S_COLOR_WHITE, atoi(row[1]), atoi(row[2]) ), sizeof(ranking) ); + else if ( !Q_stricmp(order, "races") ) + Q_strncatz( ranking, va( "%s%d. %s %s%d\n", S_COLOR_WHITE, position, row[0], S_COLOR_WHITE, atoi(row[3]) ), sizeof(ranking) ); + else if ( !Q_stricmp(order, "maps") ) + Q_strncatz( ranking, va( "%s%d. %s %s%d\n", S_COLOR_WHITE, position, row[0], S_COLOR_WHITE, atoi(row[4]) ), sizeof(ranking) ); + else if ( !Q_stricmp(order, "playtime") ) + Q_strncatz( ranking, va( "%s%d. %s %s%d\n", S_COLOR_WHITE, position, row[0], S_COLOR_WHITE, atoi(row[5]) ), sizeof(ranking) ); } } @@ -2550,7 +2563,9 @@ void *RS_MysqlLoadRanking_Thread( void* in ) { RS_PushCallbackQueue(RACESOW_CALLBACK_RANKING, playerNum, 0, 0, 0, 0, 0, 0); + free(rankingData->order); free(rankingData); + free(order); RS_EndMysqlThread(); return NULL; } diff --git a/source/game/g_racesow.h b/source/game/g_racesow.h index 4ee97e6b..4e1deda2 100644 --- a/source/game/g_racesow.h +++ b/source/game/g_racesow.h @@ -78,6 +78,7 @@ struct rankingDataStruct { int playerNum; unsigned int page; + char *order; }; struct filterDataStruct { @@ -136,7 +137,7 @@ void *RS_UpdatePlayerNick_Thread( void *in ); qboolean RS_MysqlLoadMaplist( int is_freestyle ); qboolean RS_MysqlLoadHighscores( int playerNum, int limit, int map_id, char *mapname, pjflag prejumpflag ); void *RS_MysqlLoadHighscores_Thread( void *in ); -qboolean RS_MysqlLoadRanking( int playerNum, int page ); +qboolean RS_MysqlLoadRanking( int playerNum, int page, char *order ); void *RS_MysqlLoadRanking_Thread( void *in ); qboolean RS_MysqlSetOneliner( int playerNum, int player_id, int map_id, char *oneliner); void *RS_MysqlSetOneliner_Thread( void *in );