2014-05-28 20:18:30 +02:00
|
|
|
package mineplex.queuer;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
import repository.PlayerMatchStatus;
|
|
|
|
import repository.Repository;
|
|
|
|
|
2014-05-28 20:18:30 +02:00
|
|
|
public class Queuer
|
|
|
|
{
|
|
|
|
private static Repository _repository = new Repository();
|
|
|
|
|
|
|
|
public static void main (String args[])
|
|
|
|
{
|
|
|
|
boolean us = !new File("eu.dat").exists();
|
|
|
|
|
|
|
|
_repository.initialize(us);
|
|
|
|
|
|
|
|
HashMap<Integer, Integer> playerVarianceMap = new HashMap<Integer, Integer>();
|
|
|
|
HashMap<Integer, PlayerMatch> playerPrepMatchMap = new HashMap<Integer, PlayerMatch>();
|
|
|
|
List<PlayerMatch> matchList = new ArrayList<PlayerMatch>();
|
|
|
|
|
|
|
|
EloPlayerSorter playerSorter = new EloPlayerSorter(1250);
|
|
|
|
|
|
|
|
int matchId = 1;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int matchesMade = 0;
|
|
|
|
matchId %= 1500;
|
|
|
|
|
|
|
|
List<Integer> assignedMatchIdChecked = new ArrayList<Integer>();
|
2014-05-30 04:47:57 +02:00
|
|
|
HashMap<Integer, PlayerMatchStatus> queueRecords = _repository.retrieveQueuedRecords();
|
|
|
|
|
|
|
|
int matchPlayerCount = 2;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
System.out.println("Checking " + queueRecords.size() + " queues...");
|
2014-05-30 04:47:57 +02:00
|
|
|
for (PlayerMatchStatus queueRecord : queueRecords.values())
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
|
|
|
Integer keyId = queueRecord.Id;
|
|
|
|
|
|
|
|
// Add or increase variance mapping
|
|
|
|
if (playerVarianceMap.containsKey(keyId))
|
|
|
|
playerVarianceMap.put(keyId, playerVarianceMap.get(keyId) + 25);
|
|
|
|
else
|
|
|
|
playerVarianceMap.put(keyId, 25);
|
|
|
|
|
|
|
|
int playerVariance = playerVarianceMap.get(keyId);
|
|
|
|
|
|
|
|
if (queueRecord.AssignedMatch == -1)
|
|
|
|
{
|
|
|
|
for (PlayerMatch match : matchList)
|
|
|
|
{
|
|
|
|
if (Math.abs(match.Elo - queueRecord.Elo) <= playerVariance)
|
|
|
|
{
|
|
|
|
if (playerPrepMatchMap.containsKey(keyId))
|
|
|
|
{
|
|
|
|
if (playerPrepMatchMap.get(keyId) == match)
|
|
|
|
break;
|
|
|
|
|
|
|
|
playerPrepMatchMap.get(keyId).removePlayerRecord(queueRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
match.addPlayerRecord(queueRecord);
|
|
|
|
playerPrepMatchMap.put(keyId, match);
|
|
|
|
|
|
|
|
System.out.println("Found prep match for '" + queueRecord.Id + "'");
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!playerPrepMatchMap.containsKey(keyId))
|
|
|
|
{
|
|
|
|
PlayerMatch match = new PlayerMatch();
|
|
|
|
match.Elo = queueRecord.Elo;
|
|
|
|
match.MatchId = matchId;
|
|
|
|
match.addPlayerRecord(queueRecord);
|
|
|
|
|
|
|
|
playerPrepMatchMap.put(keyId, match);
|
|
|
|
matchList.add(match);
|
|
|
|
|
|
|
|
matchId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!assignedMatchIdChecked.contains(queueRecord.AssignedMatch))
|
|
|
|
{
|
|
|
|
System.out.println("Checking if match '" + queueRecord.AssignedMatch + "' is ready.");
|
2014-05-30 04:47:57 +02:00
|
|
|
List<String> matchStatuses = _repository.getMatchStatuses(queueRecord.AssignedMatch);
|
|
|
|
boolean matchReady = true;
|
|
|
|
boolean matchDeny = false;
|
2014-05-28 20:18:30 +02:00
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
for (String matchStatus : matchStatuses)
|
|
|
|
{
|
|
|
|
if (matchStatus.equalsIgnoreCase("Deny"))
|
|
|
|
{
|
|
|
|
matchDeny = true;
|
|
|
|
matchReady = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!matchStatus.equalsIgnoreCase("Ready"))
|
|
|
|
matchReady = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchReady)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
|
|
|
_repository.startMatch(queueRecord.AssignedMatch);
|
|
|
|
_repository.deleteQueuesByAssignedMatch(queueRecord.AssignedMatch);
|
|
|
|
|
|
|
|
System.out.println("Starting match '" + queueRecord.AssignedMatch + "'");
|
|
|
|
}
|
2014-05-30 04:47:57 +02:00
|
|
|
else if (matchDeny)
|
|
|
|
{
|
|
|
|
_repository.removeAssignedMatch(queueRecord.AssignedMatch);
|
|
|
|
}
|
2014-05-28 20:18:30 +02:00
|
|
|
|
|
|
|
assignedMatchIdChecked.add(queueRecord.AssignedMatch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("Checking " + matchList.size() + " matches...");
|
|
|
|
|
|
|
|
// Check for and kick off invites for ready matches
|
|
|
|
for (Iterator<PlayerMatch> matchIterator = matchList.iterator(); matchIterator.hasNext();)
|
|
|
|
{
|
|
|
|
PlayerMatch match = matchIterator.next();
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
// Don't give me crap about not using iterator...can't cuz of stupid thing.
|
|
|
|
List<PlayerMatchStatus> matchStatusesToRemove = new ArrayList<PlayerMatchStatus>();
|
|
|
|
|
|
|
|
for (PlayerMatchStatus matchStatus : match.getPlayerRecords())
|
|
|
|
{
|
|
|
|
if (!queueRecords.containsKey(matchStatus.Id))
|
|
|
|
{
|
|
|
|
System.out.println("Removing matchStatus : " + matchStatus.Id);
|
|
|
|
matchStatusesToRemove.add(matchStatus);
|
|
|
|
|
|
|
|
if (match.WaitingForInvites)
|
|
|
|
{
|
|
|
|
_repository.removeAssignedMatch(match.MatchId);
|
|
|
|
match.WaitingForInvites = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (PlayerMatchStatus matchStatus : matchStatusesToRemove)
|
|
|
|
{
|
|
|
|
match.removePlayerRecord(matchStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match.WaitingForInvites)
|
2014-06-01 07:46:43 +02:00
|
|
|
{
|
|
|
|
if ((System.currentTimeMillis() - match.WaitingStarted) > 15000)
|
|
|
|
{
|
|
|
|
for (PlayerMatchStatus matchStatus : match.getPlayerRecords())
|
|
|
|
{
|
|
|
|
if (!matchStatus.State.equalsIgnoreCase("Ready"))
|
|
|
|
_repository.deleteQueuesById(matchStatus.Id);
|
|
|
|
}
|
|
|
|
_repository.removeAssignedMatch(match.MatchId);
|
|
|
|
match.WaitingForInvites = false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
playerSorter.EloMark = match.Elo;
|
|
|
|
|
|
|
|
List<PlayerMatchStatus> playerList = new ArrayList<PlayerMatchStatus>();
|
|
|
|
playerList.addAll(match.getPlayerRecords());
|
|
|
|
|
|
|
|
Collections.sort(playerList, playerSorter);
|
|
|
|
|
|
|
|
int playerCount = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < playerList.size(); i++)
|
|
|
|
{
|
|
|
|
PlayerMatchStatus player = playerList.get(i);
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
if (playerCount + player.PlayerCount > matchPlayerCount)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
|
|
|
match.removePlayerRecord(player);
|
|
|
|
playerPrepMatchMap.remove(player.Id);
|
|
|
|
|
|
|
|
System.out.println("Oops hit player cap, can't fit you in this match.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
playerCount += player.PlayerCount;
|
|
|
|
}
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
if (playerCount == matchPlayerCount)
|
2014-05-28 20:18:30 +02:00
|
|
|
{
|
|
|
|
System.out.println("Sent match invites for '" + match.MatchId + "'");
|
|
|
|
|
|
|
|
for (PlayerMatchStatus player : match.getPlayerRecords())
|
|
|
|
{
|
|
|
|
playerPrepMatchMap.remove(player.Id);
|
|
|
|
_repository.assignMatch(player.Id, match.MatchId);
|
|
|
|
}
|
|
|
|
|
2014-05-30 04:47:57 +02:00
|
|
|
match.WaitingForInvites = true;
|
2014-06-01 07:46:43 +02:00
|
|
|
match.WaitingStarted = System.currentTimeMillis();
|
2014-05-28 20:18:30 +02:00
|
|
|
matchesMade += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (match.getPlayerCount() == 0)
|
|
|
|
matchIterator.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
_repository.clearOldServerTransfers();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (matchesMade > 0)
|
|
|
|
System.out.println("Made " + matchesMade + " matches.");
|
|
|
|
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|