Mineplex2018-withcommit/Plugins/Mineplex.Queuer/src/mineplex/queuer/Queuer.java
Jonathan Williams d5ea6c5377 Removed dead project ServerStatus
Tweaked connection strings so they autoreconnect.

Added dragon sound to pre-queue prompt.

Added rest of missing files.
2014-05-28 11:18:30 -07:00

169 lines
4.5 KiB
Java

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;
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>();
List<PlayerMatchStatus> queueRecords = _repository.retrieveQueuedRecords();
System.out.println("Checking " + queueRecords.size() + " queues...");
for (PlayerMatchStatus queueRecord : queueRecords)
{
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.");
if (_repository.isMatchReady(queueRecord.AssignedMatch))
{
_repository.startMatch(queueRecord.AssignedMatch);
_repository.deleteQueuesByAssignedMatch(queueRecord.AssignedMatch);
System.out.println("Starting match '" + 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();
if (match.getPlayerCount() >= 4)
{
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 > 4)
{
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 == 4)
{
System.out.println("Sent match invites for '" + match.MatchId + "'");
for (PlayerMatchStatus player : match.getPlayerRecords())
{
playerPrepMatchMap.remove(player.Id);
_repository.assignMatch(player.Id, match.MatchId);
}
matchIterator.remove();
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();
}
}
}
}