Skip to content

Commit

Permalink
Database new data tracking, buy premium link added
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaacquafredda committed Jun 25, 2024
1 parent 49ee961 commit e8f61e8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 18 deletions.
13 changes: 9 additions & 4 deletions src/main/java/lar/minecraft/hg/ServerSchedulers.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static int getWorldBorderSize() {
public static void waitingPhase() {
SpigotPlugin.setPhase(HGPhase.WAITING);
plugin.getLogger().info(SpigotPlugin.getPhase() + " phase");
// Register new HungerGames game on Database
currentHGGameId = DatabaseManager.createHGGame(SpigotPlugin.serverId);
DatabaseManager.saveGamePhase(SpigotPlugin.serverId, currentHGGameId, HGPhase.WAITING.name());

ServerManager.getLivingPlayers().forEach(p -> {
p.setGameMode(GameMode.ADVENTURE);
p.getInventory().clear();
Expand All @@ -91,10 +95,11 @@ public void run() {
public static void lobbyPhase() {
SpigotPlugin.setPhase(HGPhase.LOBBY);
plugin.getLogger().info(SpigotPlugin.getPhase() + " phase");
DatabaseManager.saveGamePhase(SpigotPlugin.serverId, currentHGGameId, HGPhase.LOBBY.name());
ServerManager.getLivingPlayers().forEach(p -> {
p.setGameMode(GameMode.ADVENTURE);
p.getInventory().clear();
p.getInventory().addItem(ServerManager.getGameInstructionsBook());
p.getInventory().addItem(ServerManager.getGameInstructionsBook(PlayerManager.playerExtras.get(p.getUniqueId())));
});
gameStartTime = 0;
lobbyPhaseTaskId = server.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
Expand Down Expand Up @@ -127,9 +132,7 @@ public void run() {
public static void safeAreaPhase() {
SpigotPlugin.setPhase(HGPhase.SAFE_AREA);
plugin.getLogger().info(SpigotPlugin.getPhase() + " phase");

// Register new HungerGames game on Database
currentHGGameId = DatabaseManager.createHGGame(SpigotPlugin.serverId);
DatabaseManager.saveGamePhase(SpigotPlugin.serverId, currentHGGameId, HGPhase.SAFE_AREA.name());

safeAreaTime = 0;
// Notify all players that Hunger Games is starting
Expand Down Expand Up @@ -179,6 +182,7 @@ public void run() {
public static void playingPhase() {
SpigotPlugin.setPhase(HGPhase.PLAYING);
plugin.getLogger().info(SpigotPlugin.getPhase() + " phase");
DatabaseManager.saveGamePhase(SpigotPlugin.serverId, currentHGGameId, HGPhase.PLAYING.name());

worldBorderCollapseTime = 0;
winnerCelebrationsTime = 0;
Expand Down Expand Up @@ -284,6 +288,7 @@ public void run() {
private static void fireworkEffect(Player winner) {
SpigotPlugin.setPhase(HGPhase.WINNING);
plugin.getLogger().info(SpigotPlugin.getPhase() + " phase");
DatabaseManager.saveGamePhase(SpigotPlugin.serverId, currentHGGameId, HGPhase.WINNING.name());

fireworksEffectsTime = 0;
fireworksEffectsTaskId = server.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/lar/minecraft/hg/enums/MessageKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum MessageKey {
class_selection_lobby,
class_wrong,
class_premium,
become_premium_link,
class_not_selected,
class_instructions,
class_instructions_premium,
Expand Down
33 changes: 26 additions & 7 deletions src/main/java/lar/minecraft/hg/managers/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static int createTables() {
+ " `winner_uuid` varchar(100) DEFAULT NULL,"
+ " `win_datetime` datetime DEFAULT NULL,"
+ " `game_start_datetime` datetime DEFAULT NULL,"
+ " `game_phase` varchar(100) DEFAULT NULL,"
+ " UNIQUE KEY `hg_games_server_id_IDX` (`server_id`,`id`) USING BTREE"
+ " )");
statementCreate.executeUpdate("CREATE TABLE IF NOT EXISTS `played_hg_games` ("
Expand All @@ -116,16 +117,17 @@ public static int createTables() {
+ " UNIQUE KEY `players_uuid_IDX` (`uuid`) USING BTREE"
+ " )");
statementCreate.executeUpdate("CREATE OR REPLACE"
+ " ALGORITHM = UNDEFINED VIEW `hunger_games`.`v_Scoreboard` AS ("
+ " ALGORITHM = UNDEFINED VIEW `v_Scoreboard` AS ("
+ " select"
+ " `hunger_games`.`players`.`name` AS `name`,"
+ " count(`hunger_games`.`hg_games`.`winner_uuid`) AS `wins_count`"
+ " `players`.`uuid` AS `uuid`,"
+ " `players`.`name` AS `name`,"
+ " count(`hg_games`.`winner_uuid`) AS `wins_count`"
+ " from"
+ " (`hunger_games`.`hg_games`"
+ " right outer join `hunger_games`.`players` on"
+ " ((`hunger_games`.`players`.`uuid` = `hunger_games`.`hg_games`.`winner_uuid`)))"
+ " (`players`"
+ " left join `hg_games` on"
+ " ((`players`.`uuid` = `hg_games`.`winner_uuid`)))"
+ " group by"
+ " `hunger_games`.`hg_games`.`winner_uuid`);");
+ " `players`.`uuid`);");
statementCreate.executeUpdate("CREATE OR REPLACE"
+ " ALGORITHM = UNDEFINED VIEW `v_players` AS ("
+ " select"
Expand Down Expand Up @@ -197,6 +199,23 @@ public static void saveStartingDateTime(int ServerId, int HGGameId) {
}
}

/**
* Save game phase
* @param ServerId
* @param HGGameId
*/
public static void saveGamePhase(int ServerId, int HGGameId, String phase) {
if (isDatabaseEnabled()) {
try {
Statement statementUpdate = dbConnection.createStatement();
statementUpdate.executeUpdate(String.format("UPDATE hg_games SET game_phase = '%s' WHERE server_id = %d AND id = %d;", phase, ServerId, HGGameId));
statementUpdate.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

/**
* Add player or update the existing record into players table
* @param player
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/lar/minecraft/hg/managers/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public void onPlayerJoin(PlayerJoinEvent event) {
Location spawnLocation = ServerManager.getSurfaceRandomLocation(30, SpigotPlugin.newSpawnLocation, 0, 2, 0);
player.teleport(spawnLocation);

// Set player gamemode, send welcome message and give instructions book
player.setGameMode(GameMode.ADVENTURE);
player.playSound(player, Sound.BLOCK_END_PORTAL_FRAME_FILL, 10.0f, 1.0f);
player.sendMessage(MessageUtils.getMessage(MessageKey.welcome_message, player.getDisplayName()));
player.getInventory().addItem(ServerManager.getGameInstructionsBook());

// Check if the player is the winner of the last match or he is premium and create PlayerExtra to track it
String lastWinner = DatabaseManager.getLastWinner(SpigotPlugin.serverId);
boolean isLastWinner = false;
Expand All @@ -74,11 +68,18 @@ public void run() {
}, 20, 10); // 1 second = 20 ticks
}
}

boolean isPremium = DatabaseManager.isPlayerPremium(player.getUniqueId().toString());
int winCount = DatabaseManager.getPlayerWinCount(player.getUniqueId().toString());
PlayerExtra playerExtra = new PlayerExtra(player.getUniqueId(), player.getName(), isLastWinner, isPremium, winCount);
PlayerManager.playerExtras.put(player.getUniqueId(), playerExtra);

// Set player gamemode, send welcome message and give instructions book
player.setGameMode(GameMode.ADVENTURE);
player.playSound(player, Sound.BLOCK_END_PORTAL_FRAME_FILL, 10.0f, 1.0f);
player.sendMessage(MessageUtils.getMessage(MessageKey.welcome_message, player.getDisplayName()));
player.getInventory().addItem(ServerManager.getGameInstructionsBook(playerExtra));

// Used to track player position witouth pressing F3
createPlayerLocationBossBar(player);
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/lar/minecraft/hg/managers/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lar.minecraft.hg.ServerSchedulers;
import lar.minecraft.hg.SpigotPlugin;
import lar.minecraft.hg.entities.ItemStackProbability;
import lar.minecraft.hg.entities.PlayerExtra;
import lar.minecraft.hg.enums.MessageKey;
import lar.minecraft.hg.enums.PlayerClass;
import lar.minecraft.hg.utils.MessageUtils;
Expand Down Expand Up @@ -112,7 +113,7 @@ public static Location getSurfaceRandomLocation(int range, Location startingLoca
* Each class is in a single page
* @return An book with the instruction and materials for each class
*/
public static ItemStack getGameInstructionsBook() {
public static ItemStack getGameInstructionsBook(PlayerExtra playerExtra) {
// Prepare the book
ItemStack book = new ItemStack(Material.WRITTEN_BOOK);

Expand All @@ -122,6 +123,25 @@ public static ItemStack getGameInstructionsBook() {
meta.setAuthor(SpigotPlugin.class.getName());

StringBuilder commandInstructions = new StringBuilder();
commandInstructions.append(ChatColor.GRAY);
commandInstructions.append("Welcome in " + ChatColor.DARK_RED + "Minecraft Hunger Games.");
commandInstructions.append("\n\n");
commandInstructions.append(ChatColor.GRAY + "Choose a class and prepare to fight.");
commandInstructions.append("\n");
if (!playerExtra.isPremium()) {
commandInstructions.append("You must win last match on this server or become premium to choose " + ChatColor.RED + "premium" + ChatColor.GRAY + " classes.");
commandInstructions.append("\n\n");
commandInstructions.append(ChatColor.BLACK + "Do not waste time.");
commandInstructions.append("\n");
commandInstructions.append(ChatColor.RED + "BECOME PREMIUM NOW");
commandInstructions.append("\n");
BaseComponent[] page = new ComponentBuilder(commandInstructions.toString())
.event(new ClickEvent(ClickEvent.Action.OPEN_URL, MessageUtils.getMessage(MessageKey.become_premium_link, playerExtra.getUuid().toString())))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("Click here to buy premium")))
.create();

meta.spigot().addPage(page);
}

Arrays.asList(PlayerClass.values()).forEach(c -> {
// Empty the text that will be added to the page for each page in order to have a clear one
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class_selected:
- "&6{0}&7 class selected!"
class_wrong: "&7There's no &6&m{0}&7 class"
class_premium: "&7You must have won last match or be &6premium &7to use this class!"
become_premium_link: "http://185.25.204.223/lar-hunger-games/index.php?uuid={0}"
class_not_selected: "&7Class selection is only available before starting the game"
class_instructions: "&0/class &6{0}"
class_instructions_premium: "&cPREMIUM CLASS"
Expand Down

0 comments on commit e8f61e8

Please sign in to comment.