Racing cleanup

This commit is contained in:
Gent S 2020-12-14 16:59:34 -05:00
parent e6da454c73
commit b9013149f3
3 changed files with 31 additions and 22 deletions

View File

@ -1758,25 +1758,29 @@ bool Database::sendEmail(EmailData* data, std::vector<sItemBase> attachments) {
return true; return true;
} }
Database::RaceRanking Database::getTopRaceRanking(int epID) { Database::RaceRanking Database::getTopRaceRanking(int epID, int playerID) {
std::lock_guard<std::mutex> lock(dbCrit); std::lock_guard<std::mutex> lock(dbCrit);
const char* sql = R"( std::string sql(R"(
SELECT SELECT
"EPID", EPID, PlayerID, Score, RingCount, Time, Timestamp
"PlayerID", FROM RaceResults
"Score", WHERE EPID = ?
"RingCount", )");
"Time",
"Timestamp" if (playerID > -1)
FROM "RaceResults" sql += " AND PlayerID = ? ";
WHERE "EPID" = ?
ORDER BY "Score" DESC sql += R"(
ORDER BY Score DESC
LIMIT 1; LIMIT 1;
)"; )";
sqlite3_stmt* stmt; sqlite3_stmt* stmt;
sqlite3_prepare_v2(db, sql, -1, &stmt, NULL); sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, epID); sqlite3_bind_int(stmt, 1, epID);
if(playerID > -1)
sqlite3_bind_int(stmt, 2, playerID);
Database::RaceRanking ranking = {}; Database::RaceRanking ranking = {};
if (sqlite3_step(stmt) != SQLITE_ROW) { if (sqlite3_step(stmt) != SQLITE_ROW) {
@ -1802,8 +1806,8 @@ void Database::postRaceRanking(Database::RaceRanking ranking) {
std::lock_guard<std::mutex> lock(dbCrit); std::lock_guard<std::mutex> lock(dbCrit);
const char* sql = R"( const char* sql = R"(
INSERT INTO "RaceResults" INSERT INTO RaceResults
("EPID", "PlayerID", "Score", "RingCount", "Time", "Timestamp") (EPID, PlayerID, Score, RingCount, Time, Timestamp)
VALUES(?, ?, ?, ?, ?, ?); VALUES(?, ?, ?, ?, ?, ?);
)"; )";
sqlite3_stmt* stmt; sqlite3_stmt* stmt;

View File

@ -98,6 +98,6 @@ namespace Database {
bool sendEmail(EmailData* data, std::vector<sItemBase> attachments); bool sendEmail(EmailData* data, std::vector<sItemBase> attachments);
// racing // racing
RaceRanking getTopRaceRanking(int epID); RaceRanking getTopRaceRanking(int epID, int playerID);
void postRaceRanking(RaceRanking ranking); void postRaceRanking(RaceRanking ranking);
} }

View File

@ -87,7 +87,8 @@ void RacingManager::racingEnd(CNSocket* sock, CNPacketData* data) {
int timeDiff = now - EPRaces[sock].startTime; int timeDiff = now - EPRaces[sock].startTime;
int score = 500 * EPRaces[sock].ringCount - 10 * timeDiff; int score = 500 * EPRaces[sock].ringCount - 10 * timeDiff;
int fm = score * plr->level * (1.0f / 36) * (1.0f / 3); if (score < 0) score = 0; // lol
int fm = score * plr->level * (1.0f / 36) * 0.3f;
// we submit the ranking first... // we submit the ranking first...
Database::RaceRanking postRanking = {}; Database::RaceRanking postRanking = {};
@ -100,17 +101,21 @@ void RacingManager::racingEnd(CNSocket* sock, CNPacketData* data) {
Database::postRaceRanking(postRanking); Database::postRaceRanking(postRanking);
// ...then we get the top ranking, which may or may not be what we just submitted // ...then we get the top ranking, which may or may not be what we just submitted
Database::RaceRanking topRanking = Database::getTopRaceRanking(EPData[mapNum].EPID); Database::RaceRanking topRankingGlobal = Database::getTopRaceRanking(EPData[mapNum].EPID, -1);
Database::RaceRanking topRankingPlayer = Database::getTopRaceRanking(EPData[mapNum].EPID, plr->iID);
INITSTRUCT(sP_FE2CL_REP_EP_RACE_END_SUCC, resp); INITSTRUCT(sP_FE2CL_REP_EP_RACE_END_SUCC, resp);
resp.iEPTopRank = 5; // top score is always 5 stars, since we're doing it relative to first place resp.iEPTopRank = 1; // top score is always 5 stars, since we're doing it relative to first place
resp.iEPTopRingCount = topRanking.RingCount; resp.iEPTopRingCount = topRankingPlayer.RingCount;
resp.iEPTopScore = topRanking.Score; resp.iEPTopScore = topRankingPlayer.Score;
resp.iEPTopTime = topRanking.Time; resp.iEPTopTime = topRankingPlayer.Time;
resp.iEPRaceMode = EPRaces[sock].mode; resp.iEPRaceMode = EPRaces[sock].mode;
resp.iEPRank = (int)ceil((5.0 * score) / topRanking.Score); // [1, 5] out of 5, relative to top score if (topRankingGlobal.Score == 0)
resp.iEPRank = 5; // don't divide by zero, just give them the single star they deserve
else
resp.iEPRank = 5 - ((score * 4.0) / topRankingGlobal.Score); // 5 - [0, 4] = [5, 1]
resp.iEPRingCnt = postRanking.RingCount; resp.iEPRingCnt = postRanking.RingCount;
resp.iEPScore = postRanking.Score; resp.iEPScore = postRanking.Score;
resp.iEPRaceTime = postRanking.Time; resp.iEPRaceTime = postRanking.Time;