From 742807d80ec0d64f636939ff5c26118b8fb0e98c Mon Sep 17 00:00:00 2001 From: "Joseph Prezioso Jr." Date: Thu, 14 Apr 2016 17:17:26 -0400 Subject: [PATCH] Added generatePlayerElos and updated saveDivision Added method to fill EloManager's playerElos hashmap with values --This may not work as intended --Currently only implemented in Team Death Match --- .../src/mineplex/core/elo/EloManager.java | 48 ++++++++++++++----- .../src/mineplex/core/elo/EloPlayer.java | 12 ++++- .../src/mineplex/core/elo/EloRepository.java | 19 +++++++- .../game/games/common/CaptureTheFlag.java | 8 ++-- .../arcade/game/games/common/Domination.java | 8 ++-- .../game/games/common/TeamDeathmatch.java | 6 ++- .../game/games/turfforts/TurfForts.java | 10 ++-- 7 files changed, 79 insertions(+), 32 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java index 0f9194ded..c92ca5c54 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloManager.java @@ -13,6 +13,7 @@ import mineplex.serverdata.database.column.ColumnInt; import mineplex.serverdata.database.column.ColumnVarChar; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; public class EloManager extends MiniDbClientPlugin @@ -36,27 +37,43 @@ public class EloManager extends MiniDbClientPlugin } + /* + * fill _playerElos with information from database + * If no entry exists, create one + */ + public void generatePlayerElos(ArrayList players, String gameType) + { + for (Player player : players) + { + int elo = getElo(player.getUniqueId(), gameType); + _playerElos.get(player.getUniqueId().toString()).put(gameType, elo); + } + } + public int getElo(UUID uuid, String gameType) { int elo = 1000; + //let's try getting Elo directly from the repository + //If this works, we will never hit the return at the bottom + try + { + elo = _repository.getRepoElo(uuid.toString(), gameType); + Bukkit.broadcastMessage("Fetching Elo..."); + return elo; + } + catch (SQLException e) + { + e.printStackTrace(); + } + synchronized (_playerEloLock) { if (_playerElos.containsKey(uuid.toString())) { if (_playerElos.get(uuid.toString()).containsKey(gameType)) { - //let's try getting Elo directly from the repository - try - { - elo = _repository.getRepoElo(uuid.toString(), gameType); - Bukkit.broadcastMessage("Fetching Elo..."); - } - catch (SQLException e) - { - e.printStackTrace(); - } - //elo = _playerElos.get(uuid.toString()).get(gameType); + elo = _playerElos.get(uuid.toString()).get(gameType); } } } @@ -77,7 +94,7 @@ public class EloManager extends MiniDbClientPlugin for (EloPlayer player : teamA.getPlayers()) { EloPlayer newPlayer = new EloPlayer(); - newPlayer.UniqueId = player.UniqueId; + newPlayer.setUniqueId(player.getUniqueId()); newPlayer.Rating = (int)(player.Rating + ((double)player.Rating / (double)teamA.TotalElo) * (newTotal - teamA.TotalElo)); System.out.println("Old:"); @@ -134,7 +151,12 @@ public class EloManager extends MiniDbClientPlugin { public void run() { - _repository.saveDivision(uuid, gameType, division); + try { + _repository.saveDivision(uuid, gameType, division); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } synchronized (_playerEloLock) { diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloPlayer.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloPlayer.java index fac0369d9..066ea4ec6 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloPlayer.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloPlayer.java @@ -2,14 +2,22 @@ package mineplex.core.elo; public class EloPlayer { - public String UniqueId; + private String _uniqueId; public int Rating; public void printInfo() { - System.out.println(UniqueId + "'s elo is " + Rating); + System.out.println(_uniqueId + "'s elo is " + Rating); } + public String getUniqueId() + { + return _uniqueId; + } + public void setUniqueId(String uuid) + { + _uniqueId = uuid; + } } diff --git a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java index b357811bd..61159d1f4 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/elo/EloRepository.java @@ -88,9 +88,24 @@ public class EloRepository extends MinecraftRepository } - public void saveDivision(String uuid, String gameType, String division) + public void saveDivision(String uuid, String gameType, String division) throws SQLException { - executeUpdate(INSERT_ELO, new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("gameType", 100, gameType), new ColumnVarChar("division", 100, division)); + Connection con = getConnection(); + java.sql.Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE); + String selectQuery = "SELECT gameType, division FROM eloRating WHERE uuid = '" + uuid + "';"; + ResultSet rs = stmt.executeQuery(selectQuery); + + boolean contains = false; + + while(rs.next()) + { + String updateQuery = "UPDATE eloRating SET division='" + division + "' WHERE uuid = '" + uuid + "' AND gametype = '" + gameType + "';"; + executeQuery(updateQuery, null, new ColumnVarChar("elo", 100, uuid)); + } + if(!contains) + { + executeUpdate(INSERT_DIVISION, new ColumnVarChar("uuid", 100, uuid), new ColumnVarChar("gameType", 100, gameType), new ColumnVarChar("division", 100, division)); + } } public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java index d2d8dd2e8..613df95e9 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/CaptureTheFlag.java @@ -564,7 +564,7 @@ public class CaptureTheFlag extends TeamGame for (Player player : WinnerTeam.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamWinner.addPlayer(eloPlayer); @@ -575,7 +575,7 @@ public class CaptureTheFlag extends TeamGame for (Player player : team.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamLoser.addPlayer(eloPlayer); @@ -585,12 +585,12 @@ public class CaptureTheFlag extends TeamGame for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers()) { - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); } for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss).getPlayers()) { - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/Domination.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/Domination.java index 3550e866b..29aad952f 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/Domination.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/Domination.java @@ -329,7 +329,7 @@ public class Domination extends TeamGame for (Player player : WinnerTeam.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); //Increment Elo (in case it isn't being incremented already) @@ -343,7 +343,7 @@ public class Domination extends TeamGame for (Player player : team.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); //Decrement Elo (in case it isn't being decremented already) @@ -357,14 +357,14 @@ public class Domination extends TeamGame for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers()) { eloPlayer.Rating += 20; - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); } for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss).getPlayers()) { eloPlayer.Rating -= 10; - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/TeamDeathmatch.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/TeamDeathmatch.java index 02794e31b..fb265835f 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/TeamDeathmatch.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/common/TeamDeathmatch.java @@ -79,8 +79,10 @@ public class TeamDeathmatch extends TeamGame this.HungerSet = 20; this.WorldTimeSet = 2000; this.CompassEnabled = true; + this.EloRanking = true; this.EloSetting.setEloSetting(2); + this.Manager.getEloManager().generatePlayerElos(this.GetPlayers(false), this.GetName()); this.Manager.GetDamage().UseSimpleWeaponDamage = false; @@ -365,7 +367,7 @@ public class TeamDeathmatch extends TeamGame for (Player player : WinnerTeam.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamWinner.addPlayer(eloPlayer); @@ -384,7 +386,7 @@ public class TeamDeathmatch extends TeamGame for (Player player : team.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString();; + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamLoser.addPlayer(eloPlayer); diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/turfforts/TurfForts.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/turfforts/TurfForts.java index 995629274..550482f26 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/turfforts/TurfForts.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/games/turfforts/TurfForts.java @@ -868,7 +868,7 @@ public class TurfForts extends TeamGame for (Player player : WinnerTeam.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamWinner.addPlayer(eloPlayer); @@ -880,7 +880,7 @@ public class TurfForts extends TeamGame for (Player player : team.GetPlayers(false)) { EloPlayer eloPlayer = new EloPlayer(); - eloPlayer.UniqueId = player.getUniqueId().toString(); + eloPlayer.setUniqueId(player.getUniqueId().toString()); eloPlayer.Rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName()); teamLoser.addPlayer(eloPlayer); @@ -891,13 +891,13 @@ public class TurfForts extends TeamGame for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers()) { - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); - Bukkit.broadcastMessage(eloPlayer.UniqueId + "'s new Elo: " + eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); + Bukkit.broadcastMessage(eloPlayer.getUniqueId() + "'s new Elo: " + eloPlayer.Rating); } for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss).getPlayers()) { - Manager.getEloManager().saveElo(eloPlayer.UniqueId, GetName(), eloPlayer.Rating); + Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.Rating); } }