Search in sources :

Example 1 with SQLManager

use of com.leomelonseeds.missilewars.utilities.SQLManager in project MissileWars by Leomelonseeds.

the class Arena method endGame.

/**
 * End a MissileWars game with a winning team
 *
 * @param winningTeam the winning team
 */
public void endGame(MissileWarsTeam winningTeam) {
    // Ignore if game isn't running
    if (!running) {
        return;
    }
    MissileWarsPlugin plugin = MissileWarsPlugin.getPlugin();
    // Cancel all tasks
    for (BukkitTask task : tasks) {
        task.cancel();
    }
    running = false;
    resetting = true;
    waitingForTie = false;
    redTeam.stopDeckItems();
    blueTeam.stopDeckItems();
    // Produce winner/discord messages
    TextChannel discordChannel = DiscordSRV.getPlugin().getMainTextChannel();
    String discordMessage;
    String winner;
    // Notify players and discord users of win
    if (winningTeam == null) {
        // Send titles
        redTeam.sendTitle("tie");
        blueTeam.sendTitle("tie");
        // Set notify messages
        discordMessage = ":pencil: A game was tied in arena " + this.getName();
        winner = "&e&lNONE";
    } else if (winningTeam == blueTeam) {
        // Send titles
        blueTeam.sendTitle("victory");
        redTeam.sendTitle("defeat");
        // Set notify messages
        List<String> blueList = new ArrayList<>();
        for (MissileWarsPlayer player : players) {
            if (blueTeam.containsPlayer(player.getMCPlayerId())) {
                blueList.add(player.getMCPlayer().getName());
            }
        }
        String blueWinners = String.join(", ", blueList);
        discordMessage = ":tada: Team **blue** (" + blueWinners + ") has won a game in arena " + this.getName();
        winner = "&9&lBLUE";
    } else {
        // Send titles
        redTeam.sendTitle("victory");
        blueTeam.sendTitle("defeat");
        // Set notify messages
        List<String> redList = new ArrayList<>();
        for (MissileWarsPlayer player : players) {
            if (redTeam.containsPlayer(player.getMCPlayerId())) {
                redList.add(player.getMCPlayer().getName());
            }
        }
        String redWinners = String.join(", ", redList);
        discordMessage = ":tada: Team **red** (" + redWinners + ") has won a game in arena " + this.getName();
        winner = "&c&lRED";
    }
    discordChannel.sendMessage(discordMessage).queue();
    // Setup player variables
    List<String> winningMessages = ConfigUtils.getConfigTextList("messages.classic-end", null, null, null);
    String earnMessage = ConfigUtils.getConfigText("messages.earn-currency", null, null, null);
    FileConfiguration ranksConfig = ConfigUtils.getConfigFile(MissileWarsPlugin.getPlugin().getDataFolder().toString(), "ranks.yml");
    int spawn_missile = ranksConfig.getInt("experience.spawn_missile");
    int use_utility = ranksConfig.getInt("experience.use_utility");
    int kill = ranksConfig.getInt("experience.kill");
    int portal_broken = ranksConfig.getInt("experience.portal_broken");
    int shield_health = ranksConfig.getInt("experience.shield_health");
    int win = ranksConfig.getInt("experience.win");
    int red_portal_amount = (blueTeam.getFirstPortalStatus() ? portal_broken : 0) + (blueTeam.getSecondPortalStatus() ? portal_broken : 0);
    int blue_portal_amount = (redTeam.getFirstPortalStatus() ? portal_broken : 0) + (redTeam.getSecondPortalStatus() ? portal_broken : 0);
    int red_shield_health_amount = ((int) ((100 - blueTeam.getShieldHealth())) / 10) * shield_health;
    int blue_shield_health_amount = ((int) ((100 - redTeam.getShieldHealth())) / 10) * shield_health;
    // Find players with most deaths and kills
    List<MissileWarsPlayer> mostKills = new ArrayList<>();
    List<MissileWarsPlayer> mostDeaths = new ArrayList<>();
    for (MissileWarsPlayer player : players) {
        if (!getTeam(player.getMCPlayerId()).equals("no team")) {
            if (mostKills.isEmpty() || mostKills.get(0).getKills() < player.getKills()) {
                mostKills.clear();
                mostKills.add(player);
            } else if (mostKills.get(0).getKills() == player.getKills()) {
                mostKills.add(player);
            }
            if (mostDeaths.isEmpty() || mostDeaths.get(0).getDeaths() < player.getDeaths()) {
                mostDeaths.clear();
                mostDeaths.add(player);
            } else if (mostDeaths.get(0).getDeaths() == player.getDeaths()) {
                mostDeaths.add(player);
            }
        }
    }
    // Produce most kills/deaths list
    List<String> mostKillsList = new ArrayList<>();
    for (MissileWarsPlayer player : mostKills) {
        mostKillsList.add(ConfigUtils.getFocusName(player.getMCPlayer()));
    }
    String most_kills = String.join(", ", mostKillsList);
    List<String> mostDeathsList = new ArrayList<>();
    for (MissileWarsPlayer player : mostDeaths) {
        mostDeathsList.add(ConfigUtils.getFocusName(player.getMCPlayer()));
    }
    String most_deaths = String.join(", ", mostDeathsList);
    int most_kills_amount = mostKills.isEmpty() ? 0 : mostKills.get(0).getKills();
    int most_deaths_amount = mostDeaths.isEmpty() ? 0 : mostDeaths.get(0).getDeaths();
    Economy econ = MissileWarsPlugin.getPlugin().getEconomy();
    LocalDateTime endTime = LocalDateTime.now();
    long gameTime = Duration.between(startTime, endTime).toSeconds();
    // Update stats for each player
    for (MissileWarsPlayer player : players) {
        player.getMCPlayer().setGameMode(GameMode.SPECTATOR);
        // Send win message
        for (String s : winningMessages) {
            s = s.replaceAll("%umw_winning_team%", winner);
            s = s.replaceAll("%umw_most_kills_amount%", Integer.toString(most_kills_amount));
            s = s.replaceAll("%umw_most_deaths_amount%", Integer.toString(most_deaths_amount));
            s = s.replaceAll("%umw_most_kills%", most_kills);
            s = s.replaceAll("%umw_most_deaths%", most_deaths);
            player.getMCPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', s));
        }
        // -1 = TIE, 0 = LOST, 1 = WIN
        int won = winningTeam == null ? -1 : 0;
        // Calculate currency gain per-game
        int amountEarned = 0;
        int playerAmount = 0;
        int teamAmount = 0;
        UUID uuid = player.getMCPlayerId();
        if (!getTeam(uuid).equals("no team")) {
            playerAmount = spawn_missile * player.getMissiles() + use_utility * player.getUtility() + kill * player.getKills();
            if (blueTeam.containsPlayer(uuid)) {
                teamAmount = blue_portal_amount + blue_shield_health_amount;
                if (winningTeam == blueTeam) {
                    teamAmount += win;
                    won = 1;
                }
            } else {
                teamAmount = red_portal_amount + red_shield_health_amount;
                if (winningTeam == redTeam) {
                    teamAmount += win;
                    won = 1;
                }
            }
            long playTime = Duration.between(player.getJoinTime(), endTime).toSeconds();
            double percentPlayed = (double) playTime / gameTime;
            amountEarned = playerAmount + (int) (percentPlayed * teamAmount);
            // Update player stats
            SQLManager sql = MissileWarsPlugin.getPlugin().getSQL();
            sql.updateClassicStats(uuid, won, 1, player.getKills(), player.getMissiles(), player.getUtility(), player.getDeaths());
            sql.updateWinstreak(uuid, mapType, won);
            sql.updateExp(uuid, amountEarned);
            String earnMessagePlayer = earnMessage.replaceAll("%umw_amount_earned%", Integer.toString(amountEarned));
            player.getMCPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', earnMessagePlayer));
        }
        econ.depositPlayer(player.getMCPlayer(), amountEarned);
    }
    long waitTime = plugin.getConfig().getInt("victory-wait-time") * 20L;
    // Remove all players after a short time or immediately if none exist
    if (plugin.isEnabled() && players.size() > 0) {
        new BukkitRunnable() {

            @Override
            public void run() {
                removePlayers();
                startTime = null;
            }
        }.runTaskLater(plugin, waitTime);
        new BukkitRunnable() {

            @Override
            public void run() {
                resetWorld();
            }
        }.runTaskLater(plugin, waitTime + 40L);
    } else {
        removePlayers();
        resetWorld();
        startTime = null;
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) BukkitTask(org.bukkit.scheduler.BukkitTask) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MissileWarsPlugin(com.leomelonseeds.missilewars.MissileWarsPlugin) SQLManager(com.leomelonseeds.missilewars.utilities.SQLManager) FileConfiguration(org.bukkit.configuration.file.FileConfiguration) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) MissileWarsPlayer(com.leomelonseeds.missilewars.teams.MissileWarsPlayer) Economy(net.milkbowl.vault.economy.Economy) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UUID(java.util.UUID)

Example 2 with SQLManager

use of com.leomelonseeds.missilewars.utilities.SQLManager in project MissileWars by Leomelonseeds.

the class MissileWarsPlugin method setupDatabase.

/**
 * Setup the server database
 */
private void setupDatabase() {
    sqlManager = new SQLManager(this);
    log("Testing MySQL connection...");
    if (!sqlManager.testConnection()) {
        log("The plugin cannot function without the database. Shutting down now...");
        this.getPluginLoader().disablePlugin(this);
    }
    log("MySQL connection test complete.");
    log("Setting up default tables...");
    sqlManager.setupTables();
}
Also used : SQLManager(com.leomelonseeds.missilewars.utilities.SQLManager)

Aggregations

SQLManager (com.leomelonseeds.missilewars.utilities.SQLManager)2 MissileWarsPlugin (com.leomelonseeds.missilewars.MissileWarsPlugin)1 MissileWarsPlayer (com.leomelonseeds.missilewars.teams.MissileWarsPlayer)1 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1 Economy (net.milkbowl.vault.economy.Economy)1 FileConfiguration (org.bukkit.configuration.file.FileConfiguration)1 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)1 BukkitTask (org.bukkit.scheduler.BukkitTask)1