3211: Fix random_map and random_unfinished_map on SQLite server r=def- a=Zwelf

<!-- What is the motivation for the changes of this pull request -->
Fixes #3210 

## Checklist

- [x] Tested the change ingame (both on MySQL and SQLite)
- [x] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [x] Considered possible null pointers and out of bounds array indexing
- [x] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Zwelf <zwelf@strct.cc>
This commit is contained in:
bors[bot] 2020-10-29 15:52:52 +00:00 committed by GitHub
commit 3a3b9b1b1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 8 deletions

View file

@ -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
{

View file

@ -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();

View file

@ -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();

View file

@ -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);