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 playerVarianceMap = new HashMap(); HashMap playerPrepMatchMap = new HashMap(); List matchList = new ArrayList(); EloPlayerSorter playerSorter = new EloPlayerSorter(1250); int matchId = 1; while (true) { int matchesMade = 0; matchId %= 1500; List assignedMatchIdChecked = new ArrayList(); List 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 matchIterator = matchList.iterator(); matchIterator.hasNext();) { PlayerMatch match = matchIterator.next(); if (match.getPlayerCount() >= 4) { playerSorter.EloMark = match.Elo; List playerList = new ArrayList(); 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(); } } } }