add player listener
All checks were successful
Build / Explore-Gitea-Actions (push) Successful in 44s
All checks were successful
Build / Explore-Gitea-Actions (push) Successful in 44s
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package net.ardakaz.griefalert.database;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class PlayerRepository {
|
||||
private Logger logger;
|
||||
private final DatabaseService database;
|
||||
|
||||
public PlayerRepository(DatabaseService database, Logger logger) {
|
||||
this.database = database;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public CompletableFuture<PlayerEntity> get(String uuid) {
|
||||
return database.submit(connection -> {
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement("SELECT * FROM players WHERE id=?")) {
|
||||
ps.setString(1, uuid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
return new PlayerEntity(
|
||||
rs.getString("id"),
|
||||
Arrays.stream(rs.getString("names").split(",")).toList(),
|
||||
rs.getLong("playtime"),
|
||||
rs.getLong("firstseen"),
|
||||
rs.getLong("lastseen")
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.severe("DB error: " + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> upsert(PlayerEntity entity) {
|
||||
return database.submit(connection -> {
|
||||
String upsert = """
|
||||
INSERT INTO players(id, names, firstseen, lastseen) VALUES(?,?,?,?)
|
||||
ON CONFLICT(id)
|
||||
DO UPDATE SET names=excluded.names, playtime=?, lastseen=excluded.lastseen;
|
||||
""";
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement(upsert)) {
|
||||
ps.setString(1, entity.id());
|
||||
String names = String.join(",", entity.names());
|
||||
ps.setString(2, names);
|
||||
ps.setLong(3, entity.firstSeen());
|
||||
ps.setLong(4, entity.lastSeen());
|
||||
ps.setLong(5, entity.playtime());
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
logger.severe("Failed to save player: " + entity);
|
||||
logger.severe(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> updateNames(String id, List<String> names) {
|
||||
return database.submit(connection -> {
|
||||
String update = """
|
||||
UPDATE players
|
||||
SET names = ?
|
||||
WHERE id = ?
|
||||
""";
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement(update)) {
|
||||
ps.setString(1, String.join(",", names));
|
||||
ps.setString(2, id);
|
||||
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
logger.severe("Failed to update names for playerID: " + id);
|
||||
logger.severe(e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user