Skip to content

Commit

Permalink
Players premium management improved
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaacquafredda committed Jun 19, 2024
1 parent 4ec6b9b commit f467096
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
75 changes: 65 additions & 10 deletions src/main/java/lar/minecraft/hg/managers/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,27 @@ public static int createTables() {
if (isDatabaseEnabled()) {
try {
Statement statementCreate = dbConnection.createStatement();
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS hg_games (server_id int NOT NULL, id int NOT NULL, winner_uuid varchar(100), win_datetime datetime)");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS played_hg_games (server_id int NOT NULL, id int NOT NULL, player_uuid varchar(100))");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS players (uuid varchar(100), name varchar(100))");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS `hg_games` ("
+ " `server_id` int(11) NOT NULL,"
+ " `id` int(11) NOT NULL,"
+ " `winner_uuid` varchar(100) DEFAULT NULL,"
+ " `win_datetime` datetime DEFAULT NULL,"
+ " `game_start_datetime` datetime DEFAULT NULL,"
+ " UNIQUE KEY `hg_games_server_id_IDX` (`server_id`,`id`) USING BTREE"
+ " )");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS `played_hg_games` ("
+ " `server_id` int(11) NOT NULL,"
+ " `id` int(11) NOT NULL,"
+ " `player_uuid` varchar(100) NOT NULL,"
+ " UNIQUE KEY `played_hg_games_server_id_IDX` (`server_id`,`id`,`player_uuid`) USING BTREE"
+ " )");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS `players` ("
+ " `uuid` varchar(100) NOT NULL,"
+ " `name` varchar(100) NOT NULL,"
+ " `premium_expire_date` date DEFAULT NULL,"
+ " `last_time_online` datetime DEFAULT NULL,"
+ " UNIQUE KEY `players_uuid_IDX` (`uuid`) USING BTREE"
+ " )");
statementCreate.executeUpdate("CREATE OR REPLACE"
+ " ALGORITHM = UNDEFINED VIEW `hunger_games`.`v_Scoreboard` AS ("
+ " select"
Expand All @@ -108,6 +126,20 @@ public static int createTables() {
+ " ((`hunger_games`.`players`.`uuid` = `hunger_games`.`hg_games`.`winner_uuid`)))"
+ " group by"
+ " `hunger_games`.`hg_games`.`winner_uuid`);");
statementCreate.executeUpdate("CREATE OR REPLACE"
+ " ALGORITHM = UNDEFINED VIEW `v_players` AS ("
+ " select"
+ "`players`.`uuid` AS `uuid`,"
+ " `players`.`name` AS `name`,"
+ " (case"
+ " when ((`players`.`premium_expire_date` is not null)"
+ " and (curdate() <= `players`.`premium_expire_date`)) then 1"
+ " else 0"
+ " end) AS `premium`,"
+ " `players`.`premium_expire_date` AS `premium_expire_date`,"
+ " `players`.`last_time_online` AS `last_time_online`"
+ " from"
+ "`players`);");
statementCreate.close();
} catch (SQLException e) {
e.printStackTrace();
Expand Down Expand Up @@ -166,12 +198,10 @@ public static void saveStartingDateTime(int ServerId, int HGGameId) {
}

/**
* Save the information of all players that joined the game
* @param ServerId
* @param HGGameId
* Add player or update the existing record into players table
* @param player
*/
public static void addPlayerJoin(int ServerId, int HGGameId, Player player) {
public static void addPlayer(Player player) {
if (isDatabaseEnabled()) {
try {
Statement statementRead = dbConnection.createStatement();
Expand All @@ -182,12 +212,37 @@ public static void addPlayerJoin(int ServerId, int HGGameId, Player player) {
while (resultSet.next()) {
int foundRows = resultSet.getInt("playerFound");
if (foundRows == 0) {
statementInsert.executeUpdate(String.format("INSERT INTO players (uuid, name) VALUES ('%s', '%s');", player.getUniqueId(), player.getName()));
statementInsert.executeUpdate(String.format("INSERT INTO players (uuid, name, last_time_online) VALUES ('%s', '%s', NOW());", player.getUniqueId(), player.getName()));
}else {
statementInsert.executeUpdate(String.format("UPDATE players SET last_time_online = NOW() WHERE uuid = '%s';", player.getUniqueId()));
}
}

statementRead.close();
statementInsert.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* Save the information of all players that joined the game
* @param ServerId
* @param HGGameId
* @param player
*/
public static void addPlayerJoin(int ServerId, int HGGameId, Player player) {
if (isDatabaseEnabled()) {
try {
Statement statementRead = dbConnection.createStatement();
Statement statementInsert = dbConnection.createStatement();

//Add player into players table if not existing
DatabaseManager.addPlayer(player);

//Add player into played hg games if not existing
resultSet = statementRead.executeQuery(String.format("SELECT COUNT(*) AS playerFound FROM played_hg_games WHERE server_id = %d AND id = %d AND player_uuid = '%s';", ServerId, HGGameId, player.getUniqueId().toString()));
ResultSet resultSet = statementRead.executeQuery(String.format("SELECT COUNT(*) AS playerFound FROM played_hg_games WHERE server_id = %d AND id = %d AND player_uuid = '%s';", ServerId, HGGameId, player.getUniqueId().toString()));
while (resultSet.next()) {
int foundRows = resultSet.getInt("playerFound");
if (foundRows == 0) {
Expand Down Expand Up @@ -257,7 +312,7 @@ public static boolean isPlayerPremium(String playerUUID){
if (isDatabaseEnabled()) {
try {
Statement statementRead = dbConnection.createStatement();
ResultSet resultSet = statementRead.executeQuery(String.format("SELECT premium FROM players WHERE uuid = '%s';", playerUUID));
ResultSet resultSet = statementRead.executeQuery(String.format("SELECT premium FROM v_players WHERE uuid = '%s';", playerUUID));
boolean isPremium = false;

while (resultSet.next()) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/lar/minecraft/hg/managers/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class PlayerManager implements Listener {
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
event.setJoinMessage(null);
//Log player join on database
DatabaseManager.addPlayer(player);
if (SpigotPlugin.isWaitingForStart() || SpigotPlugin.isLobby()) {
// Teleport each player to a random location
Location spawnLocation = ServerManager.getSurfaceRandomLocation(30, SpigotPlugin.newSpawnLocation, 0, 2, 0);
Expand Down

0 comments on commit f467096

Please sign in to comment.