Refactor leaderboard management: replace UserConfig-based implementation with new Leaderboard SQL class, update related classes to use LeaderboardManager, and fix query/logic for best time retrieval.

This commit is contained in:
2025-10-20 16:39:35 +02:00
committed by Chaoscaot
parent e6848b27a0
commit 9e9f405e30
4 changed files with 115 additions and 64 deletions
@@ -0,0 +1,73 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import de.steamwar.sql.internal.Field;
import de.steamwar.sql.internal.SelectStatement;
import de.steamwar.sql.internal.Statement;
import de.steamwar.sql.internal.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.sql.Timestamp;
import java.util.List;
@AllArgsConstructor
@Getter
public class Leaderboard {
private static final Table<Leaderboard> table = new Table<>(Leaderboard.class);
private static final SelectStatement<Leaderboard> LEADERBOARD = new SelectStatement<>(table, "SELECT * from Leaderboard WHERE LeaderboardName = ? ORDER BY Time ASC LIMIT 5");
private static final SelectStatement<Leaderboard> PLAYER_TIME = new SelectStatement<>(table, "SELECT * FROM Leaderboard WHERE LeaderboardName = ? AND UserId = ?");
private static final Statement PLAYER_PLACEMENT = new Statement("SELECT COUNT(*) AS Placement FROM Leaderboard WHERE LeaderboardName = ? AND time < (SELECT time FROM UserConfig WHERE WHERE = ? AND LeaderboardName = ?)");
private static final Statement INSERT = new Statement("INSERT INTO Leaderboard (UserId, LeaderboardName, Time, BestTime) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Time = VALUES(Time), BestTime = VALUES(BestTime)");
public static List<Leaderboard> getLeaderboard(String leaderboardName) {
return LEADERBOARD.listSelect(leaderboardName);
}
public static Leaderboard getPlayerTime(SteamwarUser user, String leaderboardName) {
return PLAYER_TIME.select(leaderboardName, user);
}
public static int getPlayerPlacement(SteamwarUser user, String leaderboardName) {
return PLAYER_PLACEMENT.select(rs -> {
if(!rs.next())
return Integer.MAX_VALUE;
return rs.getInt("Placement");
}, leaderboardName, user, leaderboardName);
}
public static void upsert(int userId, String leaderboardName, long time, boolean bestTime) {
INSERT.update(userId, leaderboardName, time, bestTime);
}
@Field(keys = Table.PRIMARY)
private final int userId;
@Field(keys = Table.PRIMARY)
private final String leaderboardName;
@Field
private final long time;
@Field
private final Timestamp updatedAt;
@Field
private final boolean bestTime;
}