Refactored Elo to work correctly.

This commit is contained in:
Jonathan Williams 2016-04-22 00:21:11 -05:00
parent d0e3b094c1
commit 6d00e8b41f
13 changed files with 156 additions and 740 deletions

View File

@ -4,5 +4,5 @@ import mineplex.core.common.util.NautHashMap;
public class EloClientData public class EloClientData
{ {
public NautHashMap<String, Integer> Elos = new NautHashMap<String, Integer>(); public NautHashMap<Integer, Integer> Elos = new NautHashMap<Integer, Integer>();
} }

View File

@ -2,266 +2,124 @@ package mineplex.core.elo;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.UUID;
import mineplex.core.MiniDbClientPlugin; import mineplex.core.MiniDbClientPlugin;
import mineplex.core.account.CoreClientManager; import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.NautHashMap;
import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class EloManager extends MiniDbClientPlugin<EloClientData> public class EloManager extends MiniDbClientPlugin<EloClientData>
{ {
private static Object _playerEloLock = new Object();
private EloRepository _repository; private EloRepository _repository;
private EloRatingSystem _ratingSystem; private EloRatingSystem _ratingSystem;
private NautHashMap<String, NautHashMap<String, Integer>> _playerElos;
private NautHashMap<String, NautHashMap<String, String>> _playerDivisions;
private EloDivision _division;
public EloManager(JavaPlugin plugin, CoreClientManager clientManager) public EloManager(JavaPlugin plugin, CoreClientManager clientManager)
{ {
super("Elo Rating", plugin, clientManager); super("Elo Rating", plugin, clientManager);
_repository = new EloRepository(plugin); _repository = new EloRepository(plugin);
_ratingSystem = new EloRatingSystem(new KFactor(0, 1200, 25), new KFactor(1201, 1600, 20), new KFactor(1601, 2000, 15), new KFactor(2001, 2500, 10)); _ratingSystem = new EloRatingSystem
_playerElos = new NautHashMap<String, NautHashMap<String, Integer>>(); (
new KFactor(0, 1299, 30),
new KFactor(1300, 1899, 25),
new KFactor(1900, 2499, 20),
new KFactor(2500, 3099, 15),
new KFactor(3100, 3699, 10),
new KFactor(3700, 5000, 5)
);
} }
//add an EloPlayer's info to _playerElos public int getElo(Player player, int gameType)
public void addToPlayerElos(EloPlayer player, String gameType)
{ {
NautHashMap<String, Integer> playerInfo = new NautHashMap<String, Integer>(); if (!Get(player).Elos.containsKey(gameType))
playerInfo.put(player.getUniqueId(), player.rating); Get(player).Elos.put(gameType, 1000);
_playerElos.put(gameType, playerInfo);
}
//add a column for this game in database
public void addGameToDatabase(String gameID) throws SQLException
{
_repository.addGameType(gameID);
}
/*
* 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 return Get(player).Elos.get(gameType);
//If this works, we will never hit the return at the bottom
try
{
elo = _repository.getRepoElo(uuid.toString(), gameType);
return elo;
}
catch (SQLException e)
{
e.printStackTrace();
}
synchronized (_playerEloLock)
{
if (_playerElos.containsKey(uuid.toString()))
{
if (_playerElos.get(uuid.toString()).containsKey(gameType))
{
elo = _playerElos.get(uuid.toString()).get(gameType);
}
}
}
return elo;
} }
public EloTeam getNewRatings(EloTeam teamA, EloTeam teamB, GameResult result) public EloTeam getNewRatings(EloTeam teamA, EloTeam teamB, GameResult result)
{ {
EloTeam newTeam = new EloTeam(); EloTeam newTeam = new EloTeam();
//System.out.println("Old " + result + " Team Rating:" + teamA.TotalElo);
int teamASize = teamA.getPlayers().size();
int teamBSize = teamB.getPlayers().size();
if(teamASize < 1)
{
teamASize = 1;
}
int newTotal = _ratingSystem.getNewRating(teamA.TotalElo / teamA.getPlayers().size(), teamB.TotalElo / teamB.getPlayers().size(), result) * teamA.getPlayers().size(); int newTotal = _ratingSystem.getNewRating(teamA.TotalElo / teamA.getPlayers().size(), teamB.TotalElo / teamB.getPlayers().size(), result) * teamA.getPlayers().size();
//System.out.println("New " + result + " Team Rating:" + newTotal);
for (EloPlayer player : teamA.getPlayers()) for (EloPlayer player : teamA.getPlayers())
{ {
EloPlayer newPlayer = new EloPlayer(); int newRating = (int)(player.getRating() + ((double)player.getRating() / (double)teamA.TotalElo) * (newTotal - teamA.TotalElo));
newPlayer.setUniqueId(player.getUniqueId()); EloPlayer newPlayer = new EloPlayer(player.getPlayer(), newRating);
newPlayer.rating = (int)(player.rating + ((double)player.rating / (double)teamA.TotalElo) * (newTotal - teamA.TotalElo));
//System.out.println("Old:");
//player.printInfo();
//System.out.println("New:");
//newPlayer.printInfo();
newTeam.addPlayer(newPlayer); newTeam.addPlayer(newPlayer);
} }
return newTeam; return newTeam;
} }
public void saveElo(UUID uuid, String gameType, int elo) public void saveElo(final Player player, final int gameType, final int elo)
{ {
saveElo(uuid.toString(), gameType, elo); runAsync(new Runnable()
}
public void saveElo(final String uuid, final String gameType, final int elo)
{
//Bukkit.broadcastMessage("EloManager saveElo called");
Bukkit.getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{ {
public void run() public void run()
{ {
boolean success = false;
try { try
_repository.saveElo(uuid, gameType, elo); {
} catch (SQLException e) { success = _repository.saveElo(getClientManager().getAccountId(player), gameType, elo);
// TODO Auto-generated catch block }
catch (SQLException e)
{
e.printStackTrace(); e.printStackTrace();
} }
finally
{
System.out.println("Saving " + player.getName() + "'s new elo rating of " + elo + " for gameType " + gameType + (success ? " SUCCEEDED." : " FAILED."));
}
} }
}); });
Get(player).Elos.put(gameType, elo);
} }
//get a player's Division public String getPlayerDivision(Player player, int gameType)
public String getPlayerDivision(UUID uuid, String gameType)
{ {
//get playerElo for gameType (need to store this to check it against other Elo's) int playerElo = getElo(player, gameType);
int playerElo = 1000; String divisionName = "Player's division";
try { if (playerElo >= 3700)
playerElo = _repository.getRepoElo(uuid.toString(), gameType); divisionName = "Diamond";
} catch (SQLException e) { else if (playerElo < 3700 && playerElo >= 3500)
// TODO Auto-generated catch block divisionName = "Emerald 3";
e.printStackTrace(); else if (playerElo < 3500 && playerElo >= 3300)
} divisionName = "Emerald 2";
String pDivision = CalculateDivision(playerElo, gameType); else if (playerElo < 3300 && playerElo >= 3100)
divisionName = "Emerald 1";
else if (playerElo < 3100 && playerElo >= 2900)
divisionName = "Lapis 3";
else if (playerElo < 2900 && playerElo >= 2700)
divisionName = "Lapis 2";
else if (playerElo < 2700 && playerElo >= 2500)
divisionName = "Lapis 1";
else if (playerElo < 2500 && playerElo >= 2300)
divisionName = "Gold 3";
else if (playerElo < 2300 && playerElo >= 2100)
divisionName = "Gold 2";
else if (playerElo < 2100 && playerElo >= 1900)
divisionName = "Gold 1";
else if (playerElo < 1900 && playerElo >= 1700)
divisionName = "Iron 3";
else if (playerElo < 1700 && playerElo >= 1500)
divisionName = "Iron 2";
else if (playerElo < 1500 && playerElo >= 1300)
divisionName = "Iron 1";
else if (playerElo < 1300 && playerElo >= 1100)
divisionName = "Coal 3";
else if (playerElo < 1100 && playerElo >= 900)
divisionName = "Coal 2";
else if (playerElo < 900)
divisionName = "Coal 1";
return pDivision; return divisionName;
}
//calculate percentile into which player falls (above 99 is the top 1%)
public double getEloPercent(int playerElo, String gameType)
{
//this list will be filled with ELO's from other players (but only for the same game type
ArrayList<Integer> allElos = new ArrayList<Integer>();
try {
allElos = _repository.getAllElos(gameType);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//sort list of Elos (needed for percentile calculations)
Collections.sort(allElos);
double individualValue = 0;
//let's not allow the division of things by 0...
if (allElos.size() != 0)
{
//Calculate how much going up one spot is worth
try
{
individualValue = (100/allElos.size());
}
catch(Error e)
{
e.printStackTrace();
}
}
/* lastIndexOf gets the last instance of player's Elo
* Consequently, it should be easier for rank to go up than down
* and everyone at the same Elo should be in the same division
*/
double percentile = allElos.lastIndexOf(playerElo) * individualValue;
return percentile;
}
//only bother calculating percentile if players actually have above 3500 elo
public String CalculateDiamondPlayers(int playerElo, String gameType)
{
String newDivision = "Emerald 3";
if(getEloPercent(playerElo, gameType) > 99)
{
newDivision = "Diamond";
}
return newDivision;
}
//return a string representing a player's division, based on elo
public String CalculateDivision(int playerElo, String gameType)
{
String pDiv = "Player's division";
if (playerElo >= 3500)
{pDiv = CalculateDiamondPlayers(playerElo, gameType);}
if (playerElo < 3500 && playerElo >= 3300)
{pDiv = "Emerald 2";}
if (playerElo < 3300 && playerElo >= 3100)
{pDiv = "Emerald 1";}
if (playerElo < 3100 && playerElo >= 2900)
{pDiv = "Lapis 3";}
if (playerElo < 2900 && playerElo >= 2700)
{pDiv = "Lapis 2";}
if (playerElo < 2700 && playerElo >= 2500)
{pDiv = "Lapis 1";}
if (playerElo < 2500 && playerElo >= 2300)
{pDiv = "Gold 3";}
if (playerElo < 2300 && playerElo >= 2100)
{pDiv = "Gold 2";}
if (playerElo < 2100 && playerElo >= 1900)
{pDiv = "Gold 1";}
if (playerElo < 1900 && playerElo >= 1700)
{pDiv = "Iron 3";}
if (playerElo < 1700 && playerElo >= 1500)
{pDiv = "Iron 2";}
if (playerElo < 1500 && playerElo >= 1300)
{pDiv = "Iron 1";}
if (playerElo < 1300 && playerElo >= 1100)
{pDiv = "Coal 3";}
if (playerElo < 1100 && playerElo >= 900)
{pDiv = "Coal 2";}
if (playerElo < 900 && playerElo >= 700)
{pDiv = "Coal 1";}
return pDiv;
} }
@Override @Override
@ -279,7 +137,6 @@ public class EloManager extends MiniDbClientPlugin<EloClientData>
@Override @Override
public String getQuery(int accountId, String uuid, String name) public String getQuery(int accountId, String uuid, String name)
{ {
return "SELECT gameType, elo FROM eloRatingTest WHERE uuid = '" + uuid + "';"; return "SELECT gameType, elo FROM eloRating WHERE accountId = '" + accountId + "';";
//return "SELECT gameType, elo FROM eloRating WHERE uuid = '" + uuid + "';";
} }
} }

View File

@ -1,57 +1,34 @@
package mineplex.core.elo; package mineplex.core.elo;
import org.bukkit.entity.Player;
public class EloPlayer public class EloPlayer
{ {
private String _uniqueId; private Player _player;
public int rating; public int _rating;
private int _killBonus;
private boolean _fullBonus = false; public EloPlayer(Player player, int rating)
{
_player = player;
}
public Player getPlayer()
{
return _player;
}
public void setRating(int rating)
{
_rating = rating;
}
public int getRating()
{
return _rating;
}
public void printInfo() public void printInfo()
{ {
System.out.println(_uniqueId + "'s elo is " + rating); System.out.println(_player.getName() + "'s elo is " + _rating);
} }
public String getUniqueId()
{
return _uniqueId;
}
public void setUniqueId(String uuid)
{
_uniqueId = uuid;
}
//bonus points awarded for kills/objectives, but no more than 10
public int getKillBonus()
{
return _killBonus;
}
public void setKillBonus(int bonus)
{
if (_killBonus < 10 && !_fullBonus)
{
_killBonus = bonus;
}
if (_killBonus > 10)
{
_killBonus = 10;
_fullBonus = true;
}
}
public void incrementKillBonus(int toAdd)
{
if (_killBonus < 10 && !_fullBonus)
{
_killBonus += toAdd;
}
if (_killBonus > 10)
{
_killBonus = 10;
_fullBonus = true;
}
}
} }

View File

@ -2,25 +2,17 @@ package mineplex.core.elo;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import mineplex.core.database.MinecraftRepository; import mineplex.core.database.MinecraftRepository;
import mineplex.serverdata.database.DBPool; import mineplex.serverdata.database.DBPool;
import mineplex.serverdata.database.ResultSetCallable;
import mineplex.serverdata.database.column.ColumnInt; import mineplex.serverdata.database.column.ColumnInt;
import mineplex.serverdata.database.column.ColumnVarChar;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class EloRepository extends MinecraftRepository public class EloRepository extends MinecraftRepository
{ {
private static String CREATE_ELO_TABLE = "CREATE TABLE IF NOT EXISTS eloRating (id INT NOT NULL AUTO_INCREMENT, uuid VARCHAR(256), Testing INT, PRIMARY KEY (id), UNIQUE INDEX uuid (uuid));"; private static String INSERT_ELO = "INSERT INTO eloRating (accountId, gameType) VALUES (?, ?);";
private static String INSERT_ELO = "INSERT INTO eloRating (uuid, $gameType$) VALUES (?, ?) ON DUPLICATE KEY UPDATE $gameType$=VALUES($gameType$);"; private static String UPDATE_ELO = "UPDATE eloRating SET elo=? WHERE accountId = ? AND gameType = ?;";
//private static String UPDATE_ELO = "UPDATE eloRating SET $gameType$=? WHERE uuid=?;";
private static String SELECT_ELO_BY_UUID = "SELECT $gameType$ FROM eloRating WHERE uuid = ? LIMIT 1";
private static String GET_ALL_ELOS = "SELECT $gameType$ FROM eloRating;";
private static String ADD_GAME = "ALTER TABLE eloRating ADD $gameType$ INT;";
public EloRepository(JavaPlugin plugin) public EloRepository(JavaPlugin plugin)
{ {
@ -29,69 +21,16 @@ public class EloRepository extends MinecraftRepository
initialize(); initialize();
} }
public void initialize() public void initialize() { }
{
//executeUpdate(CREATE_ELO_TABLE);
}
public void addGameType(String gameType) throws SQLException public boolean saveElo(int accountId, int gameType, int elo) throws SQLException
{ {
String query = ADD_GAME.replace("$gameType$", "MP" + gameType); if (executeUpdate(UPDATE_ELO, new ColumnInt("elo", elo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType)) > 0)
executeUpdate(query, new ColumnInt(gameType)); return true;
} else if (executeUpdate(INSERT_ELO, new ColumnInt("elo", elo), new ColumnInt("accountId", accountId), new ColumnInt("gameType", gameType)) > 0)
return true;
//method to return every Elo from DB
public ArrayList<Integer> getAllElos(String gameType) throws SQLException
{
final ArrayList<Integer> elo = new ArrayList<Integer>();
String query = GET_ALL_ELOS.replace("$gameType$", "MP" + gameType); return false;
executeQuery(query, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
elo.add(Integer.parseInt(resultSet.getString(1)));
}
}
});
return elo;
}
//get an elo from the database
public int getRepoElo(String uuid, String gameType) throws SQLException
{
//elo set to 1000 by default
final List<Integer> elo = new ArrayList<Integer>();
String query = SELECT_ELO_BY_UUID.replace("$gameType$", "MP" + gameType);
//Bukkit.broadcastMessage("GetRepoElo: " + query);
executeQuery(query, new ResultSetCallable()
{
@Override
public void processResultSet(ResultSet resultSet) throws SQLException
{
while (resultSet.next())
{
elo.add(Integer.parseInt(resultSet.getString(1)));
}
}
}, new ColumnVarChar("uuid", 100, uuid));
if (elo.isEmpty())
elo.add(1000);
return elo.get(0);
}
public void saveElo(String uuid, String gameType, int elo) throws SQLException
{
String query = INSERT_ELO.replace("$gameType$", "MP" + gameType);
//Bukkit.broadcastMessage("EloRepository saveElo: " + query);
executeUpdate(query, new ColumnVarChar("uuid", 256, uuid), new ColumnInt(gameType, elo));
} }
public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException public EloClientData loadClientInformation(ResultSet resultSet) throws SQLException
@ -100,7 +39,7 @@ public class EloRepository extends MinecraftRepository
while (resultSet.next()) while (resultSet.next())
{ {
clientData.Elos.put(resultSet.getString(1), resultSet.getInt(2)); clientData.Elos.put(resultSet.getInt(1), resultSet.getInt(2));
} }
return clientData; return clientData;

View File

@ -11,7 +11,7 @@ public class EloTeam
public void addPlayer(EloPlayer player) public void addPlayer(EloPlayer player)
{ {
TotalElo += player.rating; TotalElo += player.getRating();
_players.add(player); _players.add(player);
} }
@ -20,4 +20,14 @@ public class EloTeam
{ {
return _players; return _players;
} }
public void printTeamInfo()
{
System.out.println("TotalElo: " + TotalElo);
for (EloPlayer player : _players)
{
System.out.println(player.getPlayer().getName() + "'s Elo: " + player.getRating());
}
}
} }

View File

@ -73,7 +73,7 @@ public class QueueManager extends MiniPlugin
} }
} }
public void queuePlayer(final String gameType, final Player...players) public void queuePlayer(final int gameType, final Player...players)
{ {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
@ -103,7 +103,7 @@ public class QueueManager extends MiniPlugin
for (Player player : players) for (Player player : players)
{ {
eloCumulative = _eloManager.getElo(player.getUniqueId(), gameType); eloCumulative = _eloManager.getElo(player, gameType);
} }
final int elo = eloCumulative / players.length; final int elo = eloCumulative / players.length;
@ -116,7 +116,7 @@ public class QueueManager extends MiniPlugin
synchronized (_queueLock) synchronized (_queueLock)
{ {
_queuedPlayerMatchList.put(players[0].getName(), matchStatus); _queuedPlayerMatchList.put(players[0].getName(), matchStatus);
} }
synchronized (_queuePlayerListLock) synchronized (_queuePlayerListLock)

View File

@ -12,7 +12,7 @@ public class QueueRepository
{ {
private boolean _us = true; private boolean _us = true;
private static String CREATE_ELO_QUEUE_TABLE = "CREATE TABLE IF NOT EXISTS playerQueue (id INT NOT NULL AUTO_INCREMENT, playerList VARCHAR(256), gameType VARCHAR(256), playerCount INT, elo INT, state VARCHAR(256), time LONG, assignedMatch INT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id), UNIQUE INDEX name_gametype (playerList, gameType));"; private static String CREATE_ELO_QUEUE_TABLE = "CREATE TABLE IF NOT EXISTS playerQueue (id INT NOT NULL AUTO_INCREMENT, playerList VARCHAR(256), gameType INT, playerCount INT, elo INT, state VARCHAR(256), time LONG, assignedMatch INT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id), UNIQUE INDEX name_gametype (playerList, gameType));";
private static String SAVE_STATE_VALUE = "UPDATE playerQueue SET state = ? WHERE id = ?;"; private static String SAVE_STATE_VALUE = "UPDATE playerQueue SET state = ? WHERE id = ?;";
private static String DELETE_QUEUE_RECORD = "DELETE FROM playerQueue WHERE id = ?;"; private static String DELETE_QUEUE_RECORD = "DELETE FROM playerQueue WHERE id = ?;";
private static String INSERT_ACCOUNT = "INSERT INTO playerQueue (playerList, gameType, elo, state, time, playerCount, assignedMatch) VALUES (?, ?, ?, 'Awaiting Match', now(), ?, -1) ON DUPLICATE KEY UPDATE time=VALUES(time);"; private static String INSERT_ACCOUNT = "INSERT INTO playerQueue (playerList, gameType, elo, state, time, playerCount, assignedMatch) VALUES (?, ?, ?, 'Awaiting Match', now(), ?, -1) ON DUPLICATE KEY UPDATE time=VALUES(time);";
@ -100,7 +100,7 @@ public class QueueRepository
} }
} }
public PlayerMatchStatus addQueueRecord(String playerList, int playerCount, String gameType, int elo) public PlayerMatchStatus addQueueRecord(String playerList, int playerCount, int gameType, int elo)
{ {
ResultSet resultSet = null; ResultSet resultSet = null;
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -110,7 +110,7 @@ public class QueueRepository
{ {
preparedStatement = connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS); preparedStatement = connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerList); preparedStatement.setString(1, playerList);
preparedStatement.setString(2, gameType); preparedStatement.setInt(2, gameType);
preparedStatement.setInt(3, elo); preparedStatement.setInt(3, elo);
//preparedStatement.setBoolean(4, _us); //preparedStatement.setBoolean(4, _us);
preparedStatement.setInt(4, playerCount); preparedStatement.setInt(4, playerCount);

View File

@ -105,7 +105,8 @@ public class QueuePage extends ShopPageBase<QueueManager, QueueShop>
@Override @Override
public void onClick(Player player, ClickType clickType) public void onClick(Player player, ClickType clickType)
{ {
queuePlayer("Dominate", player); // TODO GAMEID?
queuePlayer(1, player);
} }
}; };
@ -128,7 +129,7 @@ public class QueuePage extends ShopPageBase<QueueManager, QueueShop>
buildPage(); buildPage();
} }
private void queuePlayer(String gameType, Player player) private void queuePlayer(int gameType, Player player)
{ {
Party party = getPlugin().getPartyManager().GetParty(player); Party party = getPlugin().getPartyManager().GetParty(player);

View File

@ -447,21 +447,6 @@ public abstract class Game implements Listener
return _gameType.GetName(); return _gameType.GetName();
} }
//Add a column to EloRating/EloRatingTest for this game mode
public void addEloColumn()
{
//Bukkit.broadcastMessage("GameID: " + this.GetType().getGameId());
try {
this.Manager.getEloManager().addGameToDatabase(GetType().getGameId() + "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public GameType[] GetWorldHostNames() public GameType[] GetWorldHostNames()
{ {
GameType[] mapSource = new GameType[] GameType[] mapSource = new GameType[]
@ -1055,19 +1040,6 @@ public abstract class Game implements Listener
return SpectatorSpawn; return SpectatorSpawn;
} }
@EventHandler
public void eloStart(PlayerLoginEvent event)
{
if (EloRanking)
{
if (Manager.getEloManager().getElo(event.getPlayer().getUniqueId(), GetType().getGameId() + "") == -1)
{
Manager.getEloManager().saveElo(event.getPlayer().getUniqueId(), GetType().getGameId() + "", EloStart);
}
}
}
@EventHandler @EventHandler
public abstract void ScoreboardUpdate(UpdateEvent event); public abstract void ScoreboardUpdate(UpdateEvent event);
@ -1291,9 +1263,6 @@ public abstract class Game implements Listener
if (AnnounceSilence) if (AnnounceSilence)
Manager.GetChat().Silence(5000, false); Manager.GetChat().Silence(5000, false);
//call adjustPlayerElo
adjustPlayerElo(places);
} }
public void Announce(String message) public void Announce(String message)
@ -1316,203 +1285,6 @@ public abstract class Game implements Listener
System.out.println("[Announcement] " + message); System.out.println("[Announcement] " + message);
} }
//calculate the elo multiplier for a player based on the average elo of all players
public int calculateEloMultiplier(int currentElo, int averageElo)
{
int eloDifference = averageElo - currentElo;
if (Math.abs(eloDifference) <= 50)
{
return 0;
}
if (eloDifference >= 200)
{
return 3;
}
if (eloDifference >= 100)
{
return 2;
}
if (eloDifference > 50)
{
return 1;
}
if (eloDifference <= -200)
{
return -3;
}
if (eloDifference <= -100)
{
return -2;
}
if (eloDifference < -50)
{
return -1;
}
return 0;
}
//INDIVIDUAL ELO -- adjust the elo's of every player, depending on their ranking in the game mode
public void adjustPlayerElo(List<Player> places)
{
int averageElo = 0;
for (Player player : UtilServer.getPlayers())
{
averageElo += Manager.getEloManager().getElo(player.getUniqueId(), GetType().getGameId() + "");
}
//average Elo of all players
averageElo = averageElo/places.size();
//update Elos of all players
assignEloPoints(places, averageElo);
}
// calculate the average Elos of all players among 2 teams
public int getAverageElos(EloTeam team1, EloTeam team2)
{
EloTeam teamWinner = new EloTeam();
EloTeam teamLoser = new EloTeam();
List<EloPlayer> allPlayers = teamWinner.getPlayers();
allPlayers.addAll(teamLoser.getPlayers());
int averageElo = 0;
for (EloPlayer player : allPlayers)
{
averageElo += player.rating;
}
//average Elo of all players
averageElo = averageElo/allPlayers.size();
return averageElo;
}
//INDIVIDUAL ELO -- method to loop through every player and assign their new Elo
public void assignEloPoints(List<Player> places, int averageElo)
{
for (Player player : UtilServer.getPlayers())
{
int currentElo = 1000;
currentElo = Manager.getEloManager().getElo(player.getUniqueId(), GetType().getGameId() + "");
int lossMultiplier = 1;
int sizeModifier = 3;
int eloMultiplier = calculateEloMultiplier(currentElo, averageElo);
//nobody won, add 10 points to all players
//Profitable enough see the match through to the end, but not enough for a stale-mate to be a more desirable outcome than 3rd place
if (places == null || places.isEmpty())
{
int newElo = currentElo + 10;
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
//Top 3 players get 25, 20, and 15 points, respectively
else
{
if (places.size() >= 1)
{
if (player.getUniqueId() == places.get(0).getUniqueId())
{
int newElo = currentElo + 25 + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
}
if (places.size() >= 5)
{
if (player.getUniqueId() == places.get(1).getUniqueId())
{
int newElo = currentElo + 20 + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
}
if (places.size() >= 6)
{
if (player.getUniqueId() == places.get(2).getUniqueId())
{
if (eloMultiplier < -2)
{
eloMultiplier = -2;
}
int newElo = currentElo + 15 + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
}
if (places.size() > 6 && places.size() <= 7)
{
if (player.getUniqueId() == places.get(5).getUniqueId())
{
if(eloMultiplier > 1)
{
eloMultiplier = 0;
}
int newElo = currentElo - 5 + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
}
if (places.size() == 7)
{
if (player.getUniqueId() == places.get(6).getUniqueId())
{
if(eloMultiplier > 2)
{
eloMultiplier = 2;
}
int newElo = currentElo - 10 + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
}
}
if (places.size() >= 8)
{
//for games with 8+ players, this if statement is just going to run 3 times to check 3 different places
while(lossMultiplier != 0)
{
if (player.getUniqueId() == places.get(places.size() - sizeModifier).getUniqueId())
{
if(eloMultiplier > lossMultiplier)
{
eloMultiplier = lossMultiplier;
}
int newElo = currentElo - (5 * lossMultiplier) + (5 * eloMultiplier);
Manager.getEloManager().saveElo(player.getUniqueId(), GetType().getGameId() + "", newElo);
if(sizeModifier >= 1)
{
lossMultiplier++;
sizeModifier--;
}
else
{
lossMultiplier = 0;
}
}
}
}
}
}
}
public boolean AdvertiseText(GameLobbyManager gameLobbyManager, int _advertiseStage) public boolean AdvertiseText(GameLobbyManager gameLobbyManager, int _advertiseStage)
{ {
@ -1775,7 +1547,6 @@ public abstract class Game implements Listener
public void endGame(GameTeam winningTeam) public void endGame(GameTeam winningTeam)
{ {
AnnounceEnd(winningTeam); AnnounceEnd(winningTeam);
for (GameTeam team : GetTeamList()) for (GameTeam team : GetTeamList())
@ -1790,68 +1561,62 @@ public abstract class Game implements Listener
if (player.isOnline()) if (player.isOnline())
AddGems(player, 10, "Participation", false, false); AddGems(player, 10, "Participation", false, false);
} }
//do Elo stuff --
endElo(); endElo();
// End // End
SetState(GameState.End); SetState(GameState.End);
} }
//handle Elo at end of game -- method can be overridden in different game modes to meet their individual needs // Handle Elo at end of game -- method can be overridden in different game modes to meet their individual needs
public void endElo() protected void endElo()
{ {
//Bukkit.broadcastMessage("endElo called");
if (EloRanking) if (EloRanking)
{ {
//make a list of all players to help find average Elo // Make a list of all players to help find average Elo
EloTeam teamWinner = new EloTeam(); EloTeam teamWinner = new EloTeam();
EloTeam teamLoser = new EloTeam(); EloTeam teamLoser = new EloTeam();
//populate teams // Populate teams
for (GameTeam team : GetTeamList()) for (GameTeam team : GetTeamList())
{ {
if (WinnerTeam != null && team.equals(WinnerTeam)) if (WinnerTeam != null && team.equals(WinnerTeam))
{ {
for (Player player : WinnerTeam.GetPlayers(false)) for (Player player : WinnerTeam.GetPlayers(false))
{ {
EloPlayer eloPlayer = new EloPlayer(); teamWinner.addPlayer(new EloPlayer(player, Manager.getEloManager().getElo(player, GetType().getGameId())));
eloPlayer.setUniqueId(player.getUniqueId().toString()); }
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetType().getGameId() + "");
teamWinner.addPlayer(eloPlayer);
}
} }
else else
{ {
for (Player player : team.GetPlayers(false)) for (Player player : team.GetPlayers(false))
{ {
EloPlayer eloPlayer = new EloPlayer(); teamLoser.addPlayer(new EloPlayer(player, Manager.getEloManager().getElo(player, GetType().getGameId())));
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetType().getGameId() + "");
teamLoser.addPlayer(eloPlayer);
} }
} }
} }
//use teams to calculate Elo EloTeam teamWinnerNew = Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win);
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers()) EloTeam teamLoserNew = Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss);
System.out.println("Winning team:");
teamWinner.printTeamInfo();
teamWinnerNew.printTeamInfo();
System.out.println("Losing team:");
teamLoser.printTeamInfo();
teamLoserNew.printTeamInfo();
// Use teams to calculate Elo
for (EloPlayer eloPlayer : teamWinnerNew.getPlayers())
{ {
Manager.getEloManager().saveElo(eloPlayer.getPlayer(), GetType().getGameId(), eloPlayer.getRating());
Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetType().getGameId() + "", eloPlayer.rating);
//Bukkit.broadcastMessage("MP" + String.valueOf(this.GetType().getGameId()) + " endElo " + eloPlayer.getUniqueId() + " New Elo: " + eloPlayer.rating);
} }
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss).getPlayers()) for (EloPlayer eloPlayer : teamLoserNew.getPlayers())
{ {
Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetType().getGameId() + "", eloPlayer.rating); Manager.getEloManager().saveElo(eloPlayer.getPlayer(), GetType().getGameId(), eloPlayer.getRating());
//Bukkit.broadcastMessage("MP" + String.valueOf(this.GetType().getGameId()) + " endElo " + eloPlayer.getUniqueId() + " New Elo: " + eloPlayer.rating);
} }
} }
} }

View File

@ -551,51 +551,10 @@ public class CaptureTheFlag extends TeamGame
if (player.isOnline()) if (player.isOnline())
AddGems(player, 10, "Participation", false, false); AddGems(player, 10, "Participation", false, false);
} }
endElo();
if (EloRanking)
{
EloTeam teamWinner = new EloTeam();
EloTeam teamLoser = new EloTeam();
for (GameTeam team : GetTeamList())
{
if (WinnerTeam != null && team.equals(WinnerTeam))
{
for (Player player : WinnerTeam.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
teamWinner.addPlayer(eloPlayer);
}
}
else
{
for (Player player : team.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
teamLoser.addPlayer(eloPlayer);
}
}
}
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers())
{
Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.rating);
}
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamLoser, teamWinner, GameResult.Loss).getPlayers())
{
Manager.getEloManager().saveElo(eloPlayer.getUniqueId(), GetName(), eloPlayer.rating);
}
}
//End //End
SetState(GameState.End); SetState(GameState.End);
} }
@Override @Override

View File

@ -317,56 +317,7 @@ public class Domination extends TeamGame
AddGems(player, 10, "Participation", false, false); AddGems(player, 10, "Participation", false, false);
} }
if (EloRanking) endElo();
{
EloTeam teamWinner = new EloTeam();
EloTeam teamLoser = new EloTeam();
for (GameTeam team : GetTeamList())
{
if (WinnerTeam != null && team.equals(WinnerTeam))
{
for (Player player : WinnerTeam.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
//Increment Elo (in case it isn't being incremented already)
eloPlayer.rating += 20;
teamWinner.addPlayer(eloPlayer);
}
}
else
{
for (Player player : team.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
//Decrement Elo (in case it isn't being decremented already)
eloPlayer.rating -= 10;
teamLoser.addPlayer(eloPlayer);
}
}
}
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers())
{
eloPlayer.rating += 20;
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.getUniqueId(), GetName(), eloPlayer.rating);
}
}
//End //End
SetState(GameState.End); SetState(GameState.End);

View File

@ -856,50 +856,7 @@ public class TurfForts extends TeamGame
AddGems(player, 10, "Participation", false, false); AddGems(player, 10, "Participation", false, false);
} }
if (EloRanking) endElo();
{
EloTeam teamWinner = new EloTeam();
EloTeam teamLoser = new EloTeam();
for (GameTeam team : GetTeamList())
{
if (WinnerTeam != null && team.equals(WinnerTeam))
{
for (Player player : WinnerTeam.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
teamWinner.addPlayer(eloPlayer);
Bukkit.broadcastMessage(player.getName() + "'s old Elo: " + eloPlayer.rating);
}
}
else
{
for (Player player : team.GetPlayers(false))
{
EloPlayer eloPlayer = new EloPlayer();
eloPlayer.setUniqueId(player.getUniqueId().toString());
eloPlayer.rating = Manager.getEloManager().getElo(player.getUniqueId(), GetName());
teamLoser.addPlayer(eloPlayer);
Bukkit.broadcastMessage(player.getName() + "'s old Elo: " + eloPlayer.rating);
}
}
}
for (EloPlayer eloPlayer : Manager.getEloManager().getNewRatings(teamWinner, teamLoser, GameResult.Win).getPlayers())
{
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.getUniqueId(), GetName(), eloPlayer.rating);
}
}
//End //End
SetState(GameState.End); SetState(GameState.End);

View File

@ -1146,7 +1146,7 @@ public class GameLobbyManager implements Listener
// Remove old // Remove old
entry.getValue().resetScores(_eloMap.get(entry.getKey()) + " "); entry.getValue().resetScores(_eloMap.get(entry.getKey()) + " ");
objective.getScore(Manager.getEloManager().getElo(entry.getKey().getUniqueId(), Manager.GetGame().GetType().getGameId() + "") + " ").setScore(line--); objective.getScore(Manager.getEloManager().getElo(entry.getKey(), Manager.GetGame().GetType().getGameId()) + " ").setScore(line--);
objective.getScore(" ").setScore(line--); objective.getScore(" ").setScore(line--);
} }
@ -1161,7 +1161,7 @@ public class GameLobbyManager implements Listener
entry.getValue().resetScores(_divisionMap.get(entry.getKey()) + " "); entry.getValue().resetScores(_divisionMap.get(entry.getKey()) + " ");
// Set new // Set new
objective.getScore(Manager.getEloManager().getPlayerDivision(entry.getKey().getUniqueId(), Manager.GetGame().GetType().getGameId() + "") + " ").setScore(line--); objective.getScore(Manager.getEloManager().getPlayerDivision(entry.getKey(), Manager.GetGame().GetType().getGameId()) + " ").setScore(line--);
objective.getScore(" ").setScore(line--); objective.getScore(" ").setScore(line--);
} }