Remove unused queue system

This commit is contained in:
Sam 2018-07-31 15:28:43 +01:00 committed by Alexander Meech
parent 211459a0d0
commit 8982d4b47f
5 changed files with 0 additions and 798 deletions

View File

@ -1,13 +0,0 @@
package mineplex.hub.queue;
import java.util.ArrayList;
import java.util.List;
public class PlayerMatchStatus
{
public int Id = -1;
public String State = "Awaiting Match";
public int AssignedMatch = -1;
public List<String> OtherStatuses = new ArrayList<String>();
public boolean Prompted = false;
}

View File

@ -1,262 +0,0 @@
package mineplex.hub.queue;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import mineplex.core.MiniPlugin;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.donation.DonationManager;
import mineplex.core.elo.EloManager;
import mineplex.core.party.PartyManager;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
public class QueueManager extends MiniPlugin
{
private static Object _queueLock = new Object();
private static Object _queuePlayerListLock = new Object();
private EloManager _eloManager;
private PartyManager _partyManager;
private QueueRepository _repository;
private NautHashMap<String, PlayerMatchStatus> _queuedPlayerMatchList = new NautHashMap<String, PlayerMatchStatus>();
private HashSet<String> _queueingPlayers = new HashSet<String>();
public QueueManager(JavaPlugin plugin, CoreClientManager clientManager, DonationManager donationManager, EloManager eloManager, PartyManager partyManager)
{
super("Queue Manager", plugin);
setupConfigValues();
_eloManager = eloManager;
_partyManager = partyManager;
_repository = new QueueRepository(plugin.getConfig().getBoolean("queue.us"));
}
private void setupConfigValues()
{
try
{
getPlugin().getConfig().addDefault("queue.us", true);
getPlugin().getConfig().set("queue.us", getPlugin().getConfig().getBoolean("queue.us"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public boolean isQueued(Player player)
{
synchronized (_queueLock)
{
return _queuedPlayerMatchList.containsKey(player.getName());
}
}
public PlayerMatchStatus getQueuedPlayerStatus(Player player)
{
synchronized (_queueLock)
{
return _queuedPlayerMatchList.get(player.getName());
}
}
public void queuePlayer(final int gameType, final Player...players)
{
StringBuilder stringBuilder = new StringBuilder();
for (Player player : players)
{
stringBuilder.append(player.getName() + ", ");
}
final String playerList = stringBuilder.toString().substring(0, stringBuilder.length() - 2);
// Prevent spamming it up.
synchronized (_queuePlayerListLock)
{
for (Player player : players)
{
if (_queueingPlayers.contains(player.getName()))
return;
}
for (Player player : players)
{
_queueingPlayers.add(player.getName());
}
}
int eloCumulative = 0;
for (Player player : players)
{
eloCumulative = _eloManager.getElo(player, gameType);
}
final int elo = eloCumulative / players.length;
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
PlayerMatchStatus matchStatus = _repository.addQueueRecord(playerList, players.length, gameType, elo);
synchronized (_queueLock)
{
_queuedPlayerMatchList.put(players[0].getName(), matchStatus);
}
synchronized (_queuePlayerListLock)
{
for (Player player : players)
{
_queueingPlayers.remove(player.getName());
}
}
}
});
}
@EventHandler
public void removeDisconnectingPlayer(PlayerQuitEvent event)
{
synchronized (_queueLock)
{
final PlayerMatchStatus matchStatus = _queuedPlayerMatchList.remove(event.getPlayer().getName());
if (matchStatus != null)
{
Bukkit.getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
_repository.deleteQueueRecord(matchStatus);
}
});
}
}
}
@EventHandler
public void updateQueues(UpdateEvent event)
{
if (event.getType() != UpdateType.SEC)
return;
final NautHashMap<String, PlayerMatchStatus> queuedPlayerMatchCopy = new NautHashMap<String, PlayerMatchStatus>();
synchronized (_queueLock)
{
for (Entry<String, PlayerMatchStatus> entry : _queuedPlayerMatchList.entrySet())
{
queuedPlayerMatchCopy.put(entry.getKey(), entry.getValue());
}
}
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
for (Iterator<Entry<String, PlayerMatchStatus>> iterator = queuedPlayerMatchCopy.entrySet().iterator(); iterator.hasNext();)
{
Entry<String, PlayerMatchStatus> entry = iterator.next();
boolean prompted = entry.getValue().Prompted;
PlayerMatchStatus newStatus = _repository.updateQueueStatus(entry.getValue().Id);
synchronized (_queueLock)
{
// Player queue got removed due to match or he hit cancel.
if (newStatus == null)
_queuedPlayerMatchList.remove(entry.getKey());
else
{
if (newStatus.AssignedMatch != -1)
newStatus = _repository.updateOtherPlayersMatchStatus(newStatus);
newStatus.Prompted = prompted;
_queuedPlayerMatchList.put(entry.getKey(), newStatus);
}
}
}
}
});
}
public List<Player> findPlayersNeedingPrompt()
{
List<Player> players = new ArrayList<Player>();
synchronized (_queueLock)
{
for (Entry<String, PlayerMatchStatus> entry : _queuedPlayerMatchList.entrySet())
{
if (entry.getValue().AssignedMatch != -1 && !entry.getValue().Prompted)
{
Player player = Bukkit.getPlayer(entry.getKey());
if (player == null)
continue;
players.add(player);
entry.getValue().Prompted = true;
}
}
}
return players;
}
public void respondToInvite(final Player player, final boolean accepted)
{
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()
{
public void run()
{
PlayerMatchStatus matchStatus = null;
synchronized (_queueLock)
{
matchStatus = _queuedPlayerMatchList.get(player.getName());
if (!accepted)
_queuedPlayerMatchList.remove(player.getName());
}
if (matchStatus == null)
return;
matchStatus.State = accepted ? "Ready" : "Denied";
_repository.updateState(matchStatus);
if (!accepted)
_repository.deleteQueueRecord(matchStatus);
}
});
}
public EloManager getEloManager()
{
return _eloManager;
}
public PartyManager getPartyManager()
{
return _partyManager;
}
}

View File

@ -1,271 +0,0 @@
package mineplex.hub.queue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import mineplex.serverdata.database.DBPool;
public class QueueRepository
{
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 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 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 RETRIEVE_MATCH_STATUS = "SELECT state, assignedMatch FROM playerQueue WHERE id = ?;";
private static String RETRIEVE_OTHER_MATCH_STATUS = "SELECT state, playerCount FROM playerQueue WHERE assignedMatch = ? AND id != ? ORDER BY id DESC;";
public QueueRepository(boolean us)
{
_us = us;
initialize();
}
public void initialize()
{
}
public void deleteQueueRecord(PlayerMatchStatus matchStatus)
{
PreparedStatement preparedStatement = null;
try (Connection connection = DBPool.getQueue().getConnection())
{
preparedStatement = connection.prepareStatement(DELETE_QUEUE_RECORD);
preparedStatement.setInt(1, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
System.out.println("Error deleting queue record.");
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public void updateState(PlayerMatchStatus matchStatus)
{
PreparedStatement preparedStatement = null;
try (Connection connection = DBPool.getQueue().getConnection())
{
preparedStatement = connection.prepareStatement(SAVE_STATE_VALUE);
preparedStatement.setString(1, matchStatus.State);
preparedStatement.setInt(2, matchStatus.Id);
if (preparedStatement.executeUpdate() == 0)
{
System.out.println("Error updating state.");
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
public PlayerMatchStatus addQueueRecord(String playerList, int playerCount, int gameType, int elo)
{
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
PlayerMatchStatus matchStatus = new PlayerMatchStatus();
try (Connection connection = DBPool.getQueue().getConnection())
{
preparedStatement = connection.prepareStatement(INSERT_ACCOUNT, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, playerList);
preparedStatement.setInt(2, gameType);
preparedStatement.setInt(3, elo);
//preparedStatement.setBoolean(4, _us);
preparedStatement.setInt(4, playerCount);
preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
while (resultSet.next())
{
matchStatus.Id = resultSet.getInt(1);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return matchStatus;
}
public PlayerMatchStatus updateQueueStatus(int id)
{
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
PlayerMatchStatus matchStatus = null;
try (Connection connection = DBPool.getQueue().getConnection())
{
preparedStatement = connection.prepareStatement(RETRIEVE_MATCH_STATUS);
preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
matchStatus = new PlayerMatchStatus();
matchStatus.Id = id;
matchStatus.State = resultSet.getString(1);
matchStatus.AssignedMatch = resultSet.getInt(2);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return matchStatus;
}
public PlayerMatchStatus updateOtherPlayersMatchStatus(PlayerMatchStatus matchStatus)
{
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
try (Connection connection = DBPool.getQueue().getConnection())
{
preparedStatement = connection.prepareStatement(RETRIEVE_OTHER_MATCH_STATUS);
preparedStatement.setInt(1, matchStatus.AssignedMatch);
preparedStatement.setInt(2, matchStatus.Id);
resultSet = preparedStatement.executeQuery();
matchStatus.OtherStatuses.clear();
while (resultSet.next())
{
int playerCount = resultSet.getInt(2);
for (int i = 0; i < playerCount; i++)
matchStatus.OtherStatuses.add(resultSet.getString(1));
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (resultSet != null)
{
try
{
resultSet.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return matchStatus;
}
}

View File

@ -1,204 +0,0 @@
package mineplex.hub.queue.ui;
import mineplex.core.account.CoreClientManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.party.Party;
import mineplex.core.shop.item.IButton;
import mineplex.core.shop.item.ShopItem;
import mineplex.core.shop.page.ShopPageBase;
import mineplex.hub.queue.PlayerMatchStatus;
import mineplex.hub.queue.QueueManager;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import java.util.List;
public class QueuePage extends ShopPageBase<QueueManager, QueueShop>
{
private boolean _closeOnNextUpdate;
private int _circleIndex = 0;
public QueuePage(QueueManager plugin, QueueShop shop, CoreClientManager clientManager, DonationManager donationManager, String name, Player player)
{
super(plugin, shop, clientManager, donationManager, name, player, 54);
buildPage();
}
@Override
protected void buildPage()
{
clear();
if (getPlugin().isQueued(getPlayer()))
{
PlayerMatchStatus matchStatus = getPlugin().getQueuedPlayerStatus(getPlayer());
if (matchStatus.AssignedMatch != -1)
{
if (matchStatus.State.equalsIgnoreCase("Ready"))
{
setItem(22, new ShopItem(Material.BOOK, "Waiting for players to accept.", new String[] { }, 1, false));
int i = 45;
for (String state : matchStatus.OtherStatuses)
{
if (state.equalsIgnoreCase("Awaiting Confirmation"))
{
setItem(i, new ShopItem(Material.WOOL, "Waiting for reply...", new String[] { }, 1, false));
_closeOnNextUpdate = false;
}
else if (state.equalsIgnoreCase("Denied"))
{
setItem(i, new ShopItem(Material.WOOL, (byte)14, "Denied match.", new String[] { }, 1, false, false));
_closeOnNextUpdate = false;
}
else if (state.equalsIgnoreCase("Ready"))
{
setItem(i, new ShopItem(Material.WOOL, (byte)5, "Accepted match.", new String[] { }, 1, false, false));
_closeOnNextUpdate = true;
}
i++;
}
}
else
{
IButton okClicked = new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
OkClicked(player);
}
};
IButton cancelClicked = new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
CancelClicked(player);
}
};
buildSquareAt(19, new ShopItem(Material.EMERALD_BLOCK, (byte)0, ChatColor.GREEN + "OK", null, 1, false, true), okClicked);
buildSquareAt(23, new ShopItem(Material.REDSTONE_BLOCK, (byte)0, ChatColor.RED + "CANCEL", null, 1, false, true), cancelClicked);}
}
else
{
setItem(22, new ShopItem(Material.BOOK, "Looking for match...", new String[] { "Average wait time : DERP DERP DERP." }, 1, false));
drawCircle();
}
}
else
{
IButton queueButton = new IButton()
{
@Override
public void onClick(Player player, ClickType clickType)
{
// TODO GAMEID?
queuePlayer(1, player);
}
};
addButton(22, new ShopItem(Material.BOOK, "Play", new String[]{"Click me to enter play queue."}, 1, false), queueButton);
}
}
public void Update()
{
if (_closeOnNextUpdate)
{
getPlayer().closeInventory();
System.out.println(this.getClass().getName() + " 138");
return;
}
_circleIndex++;
_circleIndex %= 3;
buildPage();
}
private void queuePlayer(int gameType, Player player)
{
Party party = getPlugin().getPartyManager().getPartyByPlayer(player);
if(party == null)
{
getPlugin().queuePlayer(gameType, player);
return;
}
if(party.getOwnerName().equalsIgnoreCase(player.getName()))
{
List<Player> players = party.getMembers();
getPlugin().queuePlayer(gameType, players.toArray(new Player[players.size()]));
}
buildPage();
}
protected void OkClicked(Player player)
{
getPlugin().respondToInvite(player, true);
buildPage();
}
protected void CancelClicked(Player player)
{
getPlugin().respondToInvite(player, false);
player.closeInventory();
System.out.println(this.getClass().getName() + " 191");
}
private void buildSquareAt(int slot, ShopItem item, IButton button)
{
addButton(slot, item, button);
addButton(slot + 1, item, button);
addButton(slot + 2, item, button);
slot += 9;
addButton(slot, item, button);
addButton(slot + 1, item, button);
addButton(slot + 2, item, button);
slot += 9;
addButton(slot, item, button);
addButton(slot + 1, item, button);
addButton(slot + 2, item, button);
}
private void drawCircle()
{
if (_circleIndex == 0)
{
setItem(3, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(15, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(41, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(29, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
}
else if (_circleIndex == 1)
{
setItem(4, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(24, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(40, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(20, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
}
else if (_circleIndex == 2)
{
setItem(5, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(33, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(39, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
setItem(11, new ShopItem(Material.EYE_OF_ENDER, "Beep", 1, false));
}
}
}

View File

@ -1,48 +0,0 @@
package mineplex.hub.queue.ui;
import mineplex.core.account.CoreClientManager;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.party.Party;
import mineplex.core.shop.ShopBase;
import mineplex.core.shop.page.ShopPageBase;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.hub.queue.QueueManager;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import java.util.Iterator;
public class QueueShop extends ShopBase<QueueManager>
{
public QueueShop(QueueManager plugin, CoreClientManager clientManager, mineplex.core.donation.DonationManager donationManager, String name)
{
super(plugin, clientManager, donationManager, name);
}
@Override
protected ShopPageBase<QueueManager, ? extends ShopBase<QueueManager>> buildPagesFor(Player player)
{
return new QueuePage(getPlugin(), this, getClientManager(), getDonationManager(), " " + ChatColor.UNDERLINE + "Queuer 9001", player);
}
@EventHandler
public void UpdatePages(UpdateEvent event)
{
if (event.getType() != UpdateType.FASTER)
return;
for (Iterator<ShopPageBase<QueueManager, ? extends ShopBase<QueueManager>>> iterator = getPlayerPageMap().values().iterator(); iterator.hasNext();)
{
ShopPageBase<QueueManager, ? extends ShopBase<QueueManager>> page = iterator.next();
if (page instanceof QueuePage)
{
((QueuePage)page).Update();
}
}
}
}