2014-05-28 20:18:30 +02:00
|
|
|
package mineplex.queuer;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
2014-09-22 02:42:47 +02:00
|
|
|
import java.util.Collection;
|
2014-05-28 20:18:30 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2014-09-22 02:42:47 +02:00
|
|
|
import java.util.HashSet;
|
2014-05-28 20:18:30 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2014-09-22 02:42:47 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
import mineplex.serverdata.Region;
|
2014-05-30 04:47:57 +02:00
|
|
|
|
2014-05-28 20:18:30 +02:00
|
|
|
public class Queuer
|
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
private static QueueRepository _repo;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
public static void main (String args[])
|
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
Region region = (!new File("eu.dat").exists()) ? Region.US : Region.EU;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
_repo = new QueueRepository(region);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
HashMap<Integer, Integer> playerVarianceMap = new HashMap<Integer, Integer>();
|
2014-09-22 02:42:47 +02:00
|
|
|
HashMap<Integer, Match> playerPrepMatchMap = new HashMap<Integer, Match>();
|
|
|
|
Set<Match> matches = new HashSet<Match>();
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
QueuePartySorter partySorter = new QueuePartySorter();
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
int matchId = 1;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int matchesMade = 0;
|
|
|
|
matchId %= 1500;
|
|
|
|
|
|
|
|
List<Integer> assignedMatchIdChecked = new ArrayList<Integer>();
|
2014-09-22 02:42:47 +02:00
|
|
|
Map<Integer, QueueParty> queueParties = _repo.getMappedQueueParties();
|
2014-05-30 04:47:57 +02:00
|
|
|
|
|
|
|
int matchPlayerCount = 2;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
System.out.println("Checking " + queueParties.size() + " queues...");
|
|
|
|
for (QueueParty queueParty : queueParties.values())
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
int partyId = queueParty.getId();
|
|
|
|
int variance = playerVarianceMap.containsKey(partyId) ? playerVarianceMap.get(partyId) : 0;
|
|
|
|
variance += 25;
|
|
|
|
playerVarianceMap.put(partyId, variance);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
if (queueParty.hasAssignedMatch())
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
for (Match match : matches)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (Math.abs(match.getAverageElo() - queueParty.getAverageElo()) <= variance)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (playerPrepMatchMap.containsKey(partyId))
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (playerPrepMatchMap.get(partyId) == match)
|
2014-05-28 20:18:30 +02:00
|
|
|
break;
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
playerPrepMatchMap.get(partyId).quitQueueParty(queueParty);
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
match.joinQueueParty(queueParty);
|
|
|
|
playerPrepMatchMap.put(partyId, match);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
log("Found prep match for '" + queueParty.getId() + "'");
|
2014-05-28 20:18:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
if (!playerPrepMatchMap.containsKey(partyId))
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
Match match = new Match(matchId++, queueParty.getAverageElo(), queueParty);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
playerPrepMatchMap.put(partyId, match);
|
|
|
|
matches.add(match);
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-22 02:42:47 +02:00
|
|
|
else if (!assignedMatchIdChecked.contains(queueParty.getAssignedMatch()))
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
int assignedMatchId = queueParty.getAssignedMatch();
|
|
|
|
log("Checking if match '" + assignedMatchId + "' is ready.");
|
|
|
|
//List<String> matchStatuses = _repo.getMatchStatuses(queueRecord.AssignedMatch);
|
|
|
|
Collection<QueueParty> joinedParties = _repo.getJoinedQueueParties(assignedMatchId);
|
2014-05-30 04:47:57 +02:00
|
|
|
boolean matchReady = true;
|
|
|
|
boolean matchDeny = false;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty joinedParty : joinedParties)
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
String partyState = joinedParty.getState();
|
|
|
|
if (partyState.equalsIgnoreCase("Deny"))
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
|
|
|
matchDeny = true;
|
|
|
|
matchReady = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-09-22 02:42:47 +02:00
|
|
|
else if (!partyState.equalsIgnoreCase("Ready"))
|
|
|
|
{
|
2014-05-30 04:47:57 +02:00
|
|
|
matchReady = false;
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
2014-05-30 04:47:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (matchReady)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
_repo.startMatch(assignedMatchId);
|
|
|
|
_repo.deleteAssignedParties(assignedMatchId);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
System.out.println("Starting match '" + assignedMatchId + "'");
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
2014-05-30 04:47:57 +02:00
|
|
|
else if (matchDeny)
|
2014-09-22 02:42:47 +02:00
|
|
|
{
|
|
|
|
_repo.deleteMatch(assignedMatchId);
|
2014-05-30 04:47:57 +02:00
|
|
|
}
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
assignedMatchIdChecked.add(assignedMatchId);
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
System.out.println("Checking " + matches.size() + " matches...");
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
// Check for and kick off invites for ready matches
|
2014-09-22 02:42:47 +02:00
|
|
|
for (Iterator<Match> matchIterator = matches.iterator(); matchIterator.hasNext();)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
Match match = matchIterator.next();
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
// Don't give me crap about not using iterator...can't cuz of stupid thing.
|
2014-09-22 02:42:47 +02:00
|
|
|
Set<QueueParty> partiesToRemove = new HashSet<QueueParty>();
|
2014-05-30 04:47:57 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty queueParty : match.getParties())
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (!queueParties.containsKey(queueParty.getId()))
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
log("Removing matchStatus : " + queueParty.getId());
|
|
|
|
partiesToRemove.add(queueParty);
|
2014-05-30 04:47:57 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
if (match.isWaitingForInvites())
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
_repo.deleteMatch(match.getId());
|
|
|
|
match.setWaitingForInvites(false);
|
2014-05-30 04:47:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty party : partiesToRemove)
|
2014-05-30 04:47:57 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
match.quitQueueParty(party);
|
2014-05-30 04:47:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
if (match.isWaitingForInvites())
|
2014-06-01 07:46:43 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if ((match.getWaitDuration()) > 15000)
|
2014-06-01 07:46:43 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty queueParty : match.getParties())
|
2014-06-01 07:46:43 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (!queueParty.getState().equalsIgnoreCase("Ready"))
|
|
|
|
{
|
|
|
|
_repo.deleteQueueParty(queueParty.getId());
|
|
|
|
}
|
2014-06-01 07:46:43 +02:00
|
|
|
}
|
2014-09-22 02:42:47 +02:00
|
|
|
|
|
|
|
_repo.deleteMatch(match.getId());
|
|
|
|
match.setWaitingForInvites(false);
|
2014-06-01 07:46:43 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
continue;
|
2014-06-01 07:46:43 +02:00
|
|
|
}
|
2014-05-30 04:47:57 +02:00
|
|
|
|
|
|
|
if (match.getPlayerCount() >= matchPlayerCount)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
List<QueueParty> partyList = new ArrayList<QueueParty>(match.getParties());
|
|
|
|
Collections.sort(partyList, partySorter);
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
int playerCount = 0;
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty party : partyList)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
if (playerCount + party.getPlayerCount() > matchPlayerCount)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
match.quitQueueParty(party);
|
|
|
|
playerPrepMatchMap.remove(party.getId());
|
|
|
|
log("Oops hit player cap, can't fit you in this match.");
|
2014-05-28 20:18:30 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
playerCount += party.getPlayerCount();
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
if (playerCount == matchPlayerCount)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
log("Sent match invites for '" + match.getId() + "'");
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
for (QueueParty party : match.getParties())
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
2014-09-22 02:42:47 +02:00
|
|
|
playerPrepMatchMap.remove(party.getId());
|
|
|
|
_repo.assignMatch(party, match);
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
2014-09-22 02:42:47 +02:00
|
|
|
match.setWaitingForInvites(true);
|
|
|
|
matchesMade++;
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (match.getPlayerCount() == 0)
|
2014-09-22 02:42:47 +02:00
|
|
|
{
|
2014-05-28 20:18:30 +02:00
|
|
|
matchIterator.remove();
|
2014-09-22 02:42:47 +02:00
|
|
|
}
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (matchesMade > 0)
|
|
|
|
System.out.println("Made " + matchesMade + " matches.");
|
|
|
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2014-09-22 02:42:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void log(String message)
|
|
|
|
{
|
|
|
|
System.out.println(message);
|
2014-05-28 20:18:30 +02:00
|
|
|
}
|
|
|
|
}
|