2014-09-22 02:42:47 +02:00
|
|
|
package mineplex.queuer;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import redis.clients.jedis.JedisPool;
|
|
|
|
import redis.clients.jedis.JedisPoolConfig;
|
2015-03-08 05:19:35 +01:00
|
|
|
import mineplex.serverdata.ConnectionData;
|
2014-09-22 02:42:47 +02:00
|
|
|
import mineplex.serverdata.DataRepository;
|
|
|
|
import mineplex.serverdata.MinecraftServer;
|
|
|
|
import mineplex.serverdata.RedisDataRepository;
|
|
|
|
import mineplex.serverdata.Region;
|
|
|
|
import mineplex.serverdata.ServerManager;
|
|
|
|
import mineplex.serverdata.ServerRepository;
|
2015-03-09 00:17:28 +01:00
|
|
|
import mineplex.serverdata.Utility;
|
2014-09-25 23:17:38 +02:00
|
|
|
import mineplex.serverdata.transfers.ServerTransfer;
|
|
|
|
import mineplex.serverdata.transfers.TransferCommand;
|
2014-09-22 02:42:47 +02:00
|
|
|
|
|
|
|
public class QueueRepository
|
|
|
|
{
|
|
|
|
|
|
|
|
private DataRepository<QueueParty> _partyRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class constructor
|
|
|
|
* @param host - the host to connect the QueueRepository to
|
|
|
|
* @param port - the designated port of the QueueRepository database
|
|
|
|
* @param region - the region of server queues to manage
|
|
|
|
*/
|
2015-03-08 05:19:35 +01:00
|
|
|
public QueueRepository(ConnectionData connectionData, Region region)
|
2014-09-22 02:42:47 +02:00
|
|
|
{
|
2015-03-08 05:19:35 +01:00
|
|
|
this._partyRepository = new RedisDataRepository<QueueParty>(connectionData, region,
|
2014-09-22 02:42:47 +02:00
|
|
|
QueueParty.class, "queue-parties");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@code host} defaults to {@value ServerManager#DEFAULT_REDIS_HOST} and
|
|
|
|
* {@code port} defaults to {@value ServerManager#DEFAULT_REDIS_PORT}
|
|
|
|
*
|
|
|
|
* @see #QueueRepository(String, int, Region)
|
|
|
|
*/
|
|
|
|
public QueueRepository(Region region)
|
|
|
|
{
|
2015-03-09 00:17:28 +01:00
|
|
|
this(ServerManager.getMasterConnection(), region);
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public QueueParty getQueueParty(int partyId)
|
|
|
|
{
|
|
|
|
return _partyRepository.getElement(Integer.toString(partyId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public QueueParty createQueueParty(Collection<String> players, String gameType, int averageElo)
|
|
|
|
{
|
|
|
|
QueueParty queueParty = new QueueParty(players, gameType, averageElo);
|
|
|
|
updateQueueParty(queueParty);
|
|
|
|
return queueParty;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateQueueParty(QueueParty queueParty)
|
|
|
|
{
|
|
|
|
_partyRepository.addElement(queueParty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteQueueParty(int partyId)
|
|
|
|
{
|
|
|
|
_partyRepository.removeElement(Integer.toString(partyId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteQueueParty(QueueParty party)
|
|
|
|
{
|
|
|
|
deleteQueueParty(party.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteAssignedParties(int matchId)
|
|
|
|
{
|
|
|
|
for (QueueParty queueParty : getJoinedQueueParties(matchId))
|
|
|
|
{
|
|
|
|
deleteQueueParty(queueParty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<QueueParty> getQueueParties()
|
|
|
|
{
|
|
|
|
return _partyRepository.getElements();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<QueueParty> getJoinedQueueParties(int matchId)
|
|
|
|
{
|
|
|
|
Collection<QueueParty> queueParties = new HashSet<QueueParty>();
|
|
|
|
|
|
|
|
for (QueueParty queueParty : getQueueParties())
|
|
|
|
{
|
|
|
|
if (queueParty.getAssignedMatch() == matchId)
|
|
|
|
{
|
|
|
|
queueParties.add(queueParty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return queueParties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<Integer, QueueParty> getMappedQueueParties()
|
|
|
|
{
|
|
|
|
Map<Integer, QueueParty> queueParties = new HashMap<Integer, QueueParty>();
|
|
|
|
|
|
|
|
for (QueueParty queueParty : getQueueParties())
|
|
|
|
{
|
|
|
|
queueParties.put(queueParty.getId(), queueParty);
|
|
|
|
}
|
|
|
|
|
|
|
|
return queueParties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void assignMatch(QueueParty queueParty, Match match)
|
|
|
|
{
|
|
|
|
queueParty.setAssignedMatch(match.getId());
|
|
|
|
queueParty.setState("Awaiting Confirmation");
|
|
|
|
updateQueueParty(queueParty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startMatch(int matchId)
|
|
|
|
{
|
|
|
|
MinecraftServer emptyServer = getEmptyServer();
|
|
|
|
|
|
|
|
if (emptyServer != null)
|
|
|
|
{
|
|
|
|
for (QueueParty queueParty : getJoinedQueueParties(matchId))
|
|
|
|
{
|
|
|
|
for (String playerName : queueParty.getPlayers())
|
|
|
|
{
|
2014-09-25 23:17:38 +02:00
|
|
|
// Execute a transfer command
|
|
|
|
ServerTransfer serverTransfer = new ServerTransfer(playerName, emptyServer.getName());
|
|
|
|
TransferCommand transferCommand = new TransferCommand(serverTransfer);
|
|
|
|
transferCommand.publish();
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected MinecraftServer getEmptyServer()
|
|
|
|
{
|
|
|
|
ServerRepository serverRepository = ServerManager.getServerRepository(Region.US);
|
|
|
|
Collection<MinecraftServer> servers = serverRepository.getServersByGroup("DominateElo");
|
|
|
|
|
|
|
|
for (MinecraftServer server : servers)
|
|
|
|
{
|
|
|
|
if (server.getPlayerCount() == 0)
|
|
|
|
{
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteMatch(int matchId)
|
|
|
|
{
|
|
|
|
for (QueueParty queueParty : getJoinedQueueParties(matchId))
|
|
|
|
{
|
|
|
|
queueParty.setAssignedMatch(-1);
|
|
|
|
queueParty.setState("Awaiting Match");
|
|
|
|
updateQueueParty(queueParty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|