From 6eb95c6b96477d6ac00a3cc7f515f8aa2dfa3717 Mon Sep 17 00:00:00 2001 From: Zwelf Date: Wed, 28 Oct 2020 22:34:48 +0100 Subject: [PATCH] Fix random_map and random_unfinished_map on SQLite server --- src/engine/server/databases/connection.h | 2 ++ src/engine/server/databases/mysql.h | 1 + src/engine/server/databases/sqlite.h | 1 + src/game/server/score.cpp | 16 ++++++++-------- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/engine/server/databases/connection.h b/src/engine/server/databases/connection.h index d3611be2d..a5ae66d64 100644 --- a/src/engine/server/databases/connection.h +++ b/src/engine/server/databases/connection.h @@ -33,6 +33,8 @@ public: virtual const char *CollateNocase() const = 0; // syntax to insert a row into table or ignore if it already exists virtual const char *InsertIgnore() const = 0; + // ORDER BY RANDOM()/RAND() + virtual const char *Random() const = 0; enum Status { diff --git a/src/engine/server/databases/mysql.h b/src/engine/server/databases/mysql.h index 4dbda92bd..6eb70c635 100644 --- a/src/engine/server/databases/mysql.h +++ b/src/engine/server/databases/mysql.h @@ -34,6 +34,7 @@ public: virtual const char *InsertTimestampAsUtc() const { return "?"; } virtual const char *CollateNocase() const { return "CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci"; } virtual const char *InsertIgnore() const { return "INSERT IGNORE"; }; + virtual const char *Random() const { return "RAND()"; }; virtual Status Connect(); virtual void Disconnect(); diff --git a/src/engine/server/databases/sqlite.h b/src/engine/server/databases/sqlite.h index 21dbc2d39..b1b68f4c6 100644 --- a/src/engine/server/databases/sqlite.h +++ b/src/engine/server/databases/sqlite.h @@ -21,6 +21,7 @@ public: virtual const char *InsertTimestampAsUtc() const { return "DATETIME(?, 'utc')"; } virtual const char *CollateNocase() const { return "? COLLATE NOCASE"; } virtual const char *InsertIgnore() const { return "INSERT OR IGNORE"; }; + virtual const char *Random() const { return "RANDOM()"; }; virtual Status Connect(); virtual void Disconnect(); diff --git a/src/game/server/score.cpp b/src/game/server/score.cpp index ce453eef9..136aba683 100644 --- a/src/game/server/score.cpp +++ b/src/game/server/score.cpp @@ -1144,8 +1144,8 @@ bool CScore::RandomMapThread(IDbConnection *pSqlServer, const ISqlData *pGameDat str_format(aBuf, sizeof(aBuf), "SELECT Map FROM %s_maps " "WHERE Server = ? AND Map != ? AND Stars = ? " - "ORDER BY RAND() LIMIT 1;", - pSqlServer->GetPrefix()); + "ORDER BY %s LIMIT 1;", + pSqlServer->GetPrefix(), pSqlServer->Random()); pSqlServer->PrepareStatement(aBuf); pSqlServer->BindInt(3, pData->m_Stars); } @@ -1154,8 +1154,8 @@ bool CScore::RandomMapThread(IDbConnection *pSqlServer, const ISqlData *pGameDat str_format(aBuf, sizeof(aBuf), "SELECT Map FROM %s_maps " "WHERE Server = ? AND Map != ? " - "ORDER BY RAND() LIMIT 1;", - pSqlServer->GetPrefix()); + "ORDER BY %s LIMIT 1;", + pSqlServer->GetPrefix(), pSqlServer->Random()); pSqlServer->PrepareStatement(aBuf); } pSqlServer->BindString(1, pData->m_ServerType); @@ -1202,9 +1202,9 @@ bool CScore::RandomUnfinishedMapThread(IDbConnection *pSqlServer, const ISqlData " SELECT Map " " FROM %s_race " " WHERE Name = ?" - ") ORDER BY RAND() " + ") ORDER BY %s " "LIMIT 1;", - pSqlServer->GetPrefix(), pSqlServer->GetPrefix()); + pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->Random()); pSqlServer->PrepareStatement(aBuf); pSqlServer->BindString(1, pData->m_ServerType); pSqlServer->BindString(2, pData->m_CurrentMap); @@ -1220,9 +1220,9 @@ bool CScore::RandomUnfinishedMapThread(IDbConnection *pSqlServer, const ISqlData " SELECT Map " " FROM %s_race as race " " WHERE Name = ?" - ") ORDER BY RAND() " + ") ORDER BY %s " "LIMIT 1;", - pSqlServer->GetPrefix(), pSqlServer->GetPrefix()); + pSqlServer->GetPrefix(), pSqlServer->GetPrefix(), pSqlServer->Random()); pSqlServer->PrepareStatement(aBuf); pSqlServer->BindString(1, pData->m_ServerType); pSqlServer->BindString(2, pData->m_CurrentMap);