167 lines
4.2 KiB
Java
167 lines
4.2 KiB
Java
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;
|
|
import mineplex.serverdata.DataRepository;
|
|
import mineplex.serverdata.MinecraftServer;
|
|
import mineplex.serverdata.RedisDataRepository;
|
|
import mineplex.serverdata.Region;
|
|
import mineplex.serverdata.ServerManager;
|
|
import mineplex.serverdata.ServerRepository;
|
|
import mineplex.serverdata.transfers.ServerTransfer;
|
|
import mineplex.serverdata.transfers.TransferCommand;
|
|
|
|
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
|
|
*/
|
|
public QueueRepository(String host, int port, Region region)
|
|
{
|
|
this._partyRepository = new RedisDataRepository<QueueParty>(host, port, region,
|
|
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)
|
|
{
|
|
this(ServerManager.DEFAULT_REDIS_HOST, ServerManager.DEFAULT_REDIS_PORT, region);
|
|
}
|
|
|
|
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())
|
|
{
|
|
// Execute a transfer command
|
|
ServerTransfer serverTransfer = new ServerTransfer(playerName, emptyServer.getName());
|
|
TransferCommand transferCommand = new TransferCommand(serverTransfer);
|
|
transferCommand.publish();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |