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
This commit is contained in:
Joseph Prezioso Jr. 2016-04-14 17:17:26 -04:00
parent eb67c12108
commit 742807d80e
7 changed files with 79 additions and 32 deletions

View File

@ -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<EloClientData>
@ -36,27 +37,43 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
}
/*
* fill _playerElos with information from database
* If no entry exists, create one
*/
public void generatePlayerElos(ArrayList<Player> 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<EloClientData>
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<EloClientData>
{
public void run()
{
try {
_repository.saveDivision(uuid, gameType, division);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (_playerEloLock)
{

View File

@ -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;
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);
}
}