Mineplex2018-withcommit/Plugins/Mineplex.Queuer/src/mineplex/queuer/Queuer.java

233 lines
6.3 KiB
Java
Raw Normal View History

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;
import repository.PlayerMatchStatus;
import repository.Repository;
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>();
HashMap<Integer, PlayerMatchStatus> queueRecords = _repository.retrieveQueuedRecords();
int matchPlayerCount = 2;
System.out.println("Checking " + queueRecords.size() + " queues...");
for (PlayerMatchStatus queueRecord : queueRecords.values())
{
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.");
List<String> matchStatuses = _repository.getMatchStatuses(queueRecord.AssignedMatch);
boolean matchReady = true;
boolean matchDeny = false;
for (String matchStatus : matchStatuses)
{
if (matchStatus.equalsIgnoreCase("Deny"))
{
matchDeny = true;
matchReady = false;
break;
}
else if (!matchStatus.equalsIgnoreCase("Ready"))
matchReady = false;
}
if (matchReady)
{
_repository.startMatch(queueRecord.AssignedMatch);
_repository.deleteQueuesByAssignedMatch(queueRecord.AssignedMatch);
System.out.println("Starting match '" + queueRecord.AssignedMatch + "'");
}
else if (matchDeny)
{
_repository.removeAssignedMatch(queueRecord.AssignedMatch);
}
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();
// 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)
{
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;
}
continue;
}
if (match.getPlayerCount() >= matchPlayerCount)
{
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);
if (playerCount + player.PlayerCount > matchPlayerCount)
{
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;
}
if (playerCount == matchPlayerCount)
{
System.out.println("Sent match invites for '" + match.MatchId + "'");
for (PlayerMatchStatus player : match.getPlayerRecords())
{
playerPrepMatchMap.remove(player.Id);
_repository.assignMatch(player.Id, match.MatchId);
}
match.WaitingForInvites = true;
match.WaitingStarted = System.currentTimeMillis();
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();
}
}
}
}