Having one common standings calculator for tournaments and leagues.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.gempukku.lotro.competitive;
|
||||
|
||||
public interface CompetitiveMatchResult {
|
||||
public String getWinner();
|
||||
|
||||
public String getLoser();
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.gempukku.lotro;
|
||||
package com.gempukku.lotro.competitive;
|
||||
|
||||
public class PlayerStanding {
|
||||
private String _playerName;
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.gempukku.lotro.competitive;
|
||||
|
||||
import com.gempukku.util.DescComparator;
|
||||
import com.gempukku.util.MultipleComparator;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class StandingsProducer {
|
||||
private static Comparator<PlayerStanding> LEAGUE_STANDING_COMPARATOR =
|
||||
new MultipleComparator<PlayerStanding>(
|
||||
new DescComparator<PlayerStanding>(new PointsComparator()),
|
||||
new GamesPlayedComparator(),
|
||||
new DescComparator<PlayerStanding>(new OpponentsWinComparator()));
|
||||
|
||||
|
||||
public static List<PlayerStanding> produceStandings(Collection<String> participants, Collection<? extends CompetitiveMatchResult> matches,
|
||||
int pointsForWin, int pointsForLoss, Set<String> playersWithByes) {
|
||||
Map<String, List<String>> playerOpponents = new HashMap<String, List<String>>();
|
||||
Map<String, AtomicInteger> playerWinCounts = new HashMap<String, AtomicInteger>();
|
||||
Map<String, AtomicInteger> playerLossCounts = new HashMap<String, AtomicInteger>();
|
||||
|
||||
// Initialize the list
|
||||
for (String playerName : participants) {
|
||||
playerOpponents.put(playerName, new ArrayList<String>());
|
||||
playerWinCounts.put(playerName, new AtomicInteger(0));
|
||||
playerLossCounts.put(playerName, new AtomicInteger(0));
|
||||
}
|
||||
|
||||
for (CompetitiveMatchResult leagueMatch : matches) {
|
||||
playerOpponents.get(leagueMatch.getWinner()).add(leagueMatch.getLoser());
|
||||
playerOpponents.get(leagueMatch.getLoser()).add(leagueMatch.getWinner());
|
||||
playerWinCounts.get(leagueMatch.getWinner()).incrementAndGet();
|
||||
playerLossCounts.get(leagueMatch.getLoser()).incrementAndGet();
|
||||
}
|
||||
|
||||
List<PlayerStanding> leagueStandings = new LinkedList<PlayerStanding>();
|
||||
for (String playerName : participants) {
|
||||
int points = playerWinCounts.get(playerName).intValue() * pointsForWin + playerLossCounts.get(playerName).intValue() * pointsForLoss;
|
||||
int gamesPlayed = playerWinCounts.get(playerName).intValue() + playerLossCounts.get(playerName).intValue();
|
||||
|
||||
if (playersWithByes.contains(playerName))
|
||||
points += pointsForWin;
|
||||
|
||||
PlayerStanding standing = new PlayerStanding(playerName, points, gamesPlayed);
|
||||
List<String> opponents = playerOpponents.get(playerName);
|
||||
int opponentWins = 0;
|
||||
int opponentGames = 0;
|
||||
for (String opponent : opponents) {
|
||||
opponentWins += playerWinCounts.get(opponent).intValue();
|
||||
opponentGames += playerWinCounts.get(opponent).intValue() + playerLossCounts.get(opponent).intValue();
|
||||
}
|
||||
if (opponentGames != 0)
|
||||
standing.setOpponentWin(opponentWins * 1f / opponentGames);
|
||||
else
|
||||
standing.setOpponentWin(0f);
|
||||
leagueStandings.add(standing);
|
||||
}
|
||||
|
||||
Collections.sort(leagueStandings, LEAGUE_STANDING_COMPARATOR);
|
||||
|
||||
int standing = 0;
|
||||
int position = 1;
|
||||
PlayerStanding lastStanding = null;
|
||||
for (PlayerStanding leagueStanding : leagueStandings) {
|
||||
if (lastStanding == null || LEAGUE_STANDING_COMPARATOR.compare(leagueStanding, lastStanding) != 0)
|
||||
standing = position;
|
||||
leagueStanding.setStanding(standing);
|
||||
position++;
|
||||
lastStanding = leagueStanding;
|
||||
}
|
||||
return leagueStandings;
|
||||
|
||||
}
|
||||
|
||||
private static class PointsComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getPoints() - o2.getPoints();
|
||||
}
|
||||
}
|
||||
|
||||
private static class GamesPlayedComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getGamesPlayed() - o2.getGamesPlayed();
|
||||
}
|
||||
}
|
||||
|
||||
private static class OpponentsWinComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
final float diff = o1.getOpponentWin() - o2.getOpponentWin();
|
||||
if (diff < 0)
|
||||
return -1;
|
||||
if (diff > 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatchResult;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.sql.Connection;
|
||||
@@ -20,7 +20,7 @@ public class DbLeagueMatchDAO implements LeagueMatchDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league) {
|
||||
public Collection<LeagueMatchResult> getLeagueMatches(League league) {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
@@ -29,13 +29,13 @@ public class DbLeagueMatchDAO implements LeagueMatchDAO {
|
||||
statement.setString(1, league.getType());
|
||||
ResultSet rs = statement.executeQuery();
|
||||
try {
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
Set<LeagueMatchResult> result = new HashSet<LeagueMatchResult>();
|
||||
while (rs.next()) {
|
||||
String winner = rs.getString(1);
|
||||
String loser = rs.getString(2);
|
||||
String serie = rs.getString(3);
|
||||
|
||||
result.add(new LeagueMatch(serie, winner, loser));
|
||||
result.add(new LeagueMatchResult(serie, winner, loser));
|
||||
}
|
||||
return result;
|
||||
} finally {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.gempukku.lotro.db;
|
||||
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatchResult;
|
||||
import com.gempukku.lotro.league.LeagueSerieData;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface LeagueMatchDAO {
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league);
|
||||
public Collection<LeagueMatchResult> getLeagueMatches(League league);
|
||||
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, String winner, String loser);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.gempukku.lotro.db.vo;
|
||||
|
||||
public class LeagueMatch {
|
||||
import com.gempukku.lotro.competitive.CompetitiveMatchResult;
|
||||
|
||||
public class LeagueMatchResult implements CompetitiveMatchResult {
|
||||
private String _winner;
|
||||
private String _loser;
|
||||
private String _serieName;
|
||||
|
||||
public LeagueMatch(String serieName, String winner, String loser) {
|
||||
public LeagueMatchResult(String serieName, String winner, String loser) {
|
||||
_serieName = serieName;
|
||||
_winner = winner;
|
||||
_loser = loser;
|
||||
@@ -2,7 +2,7 @@ package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatchResult;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -16,17 +16,17 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
|
||||
private LeagueMatchDAO _leagueMatchDAO;
|
||||
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
|
||||
|
||||
private Map<String, Collection<LeagueMatch>> _cachedMatches = new ConcurrentHashMap<String, Collection<LeagueMatch>>();
|
||||
private Map<String, Collection<LeagueMatchResult>> _cachedMatches = new ConcurrentHashMap<String, Collection<LeagueMatchResult>>();
|
||||
|
||||
public CachedLeagueMatchDAO(LeagueMatchDAO leagueMatchDAO) {
|
||||
_leagueMatchDAO = leagueMatchDAO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<LeagueMatch> getLeagueMatches(League league) {
|
||||
public Collection<LeagueMatchResult> getLeagueMatches(League league) {
|
||||
_readWriteLock.readLock().lock();
|
||||
try {
|
||||
Collection<LeagueMatch> leagueMatches = _cachedMatches.get(LeagueMapKeys.getLeagueMapKey(league));
|
||||
Collection<LeagueMatchResult> leagueMatches = _cachedMatches.get(LeagueMapKeys.getLeagueMapKey(league));
|
||||
if (leagueMatches == null) {
|
||||
_readWriteLock.readLock().unlock();
|
||||
_readWriteLock.writeLock().lock();
|
||||
@@ -43,11 +43,11 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getLeagueMatchesInWriteLock(League league) {
|
||||
Collection<LeagueMatch> leagueMatches;
|
||||
private Collection<LeagueMatchResult> getLeagueMatchesInWriteLock(League league) {
|
||||
Collection<LeagueMatchResult> leagueMatches;
|
||||
leagueMatches = _cachedMatches.get(LeagueMapKeys.getLeagueMapKey(league));
|
||||
if (leagueMatches == null) {
|
||||
leagueMatches = new CopyOnWriteArraySet<LeagueMatch>(_leagueMatchDAO.getLeagueMatches(league));
|
||||
leagueMatches = new CopyOnWriteArraySet<LeagueMatchResult>(_leagueMatchDAO.getLeagueMatches(league));
|
||||
_cachedMatches.put(LeagueMapKeys.getLeagueMapKey(league), leagueMatches);
|
||||
}
|
||||
return leagueMatches;
|
||||
@@ -57,7 +57,7 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
|
||||
public void addPlayedMatch(League league, LeagueSerieData leagueSerie, String winner, String loser) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
LeagueMatch match = new LeagueMatch(leagueSerie.getName(), winner, loser);
|
||||
LeagueMatchResult match = new LeagueMatchResult(leagueSerie.getName(), winner, loser);
|
||||
|
||||
getLeagueMatchesInWriteLock(league).add(match);
|
||||
_leagueMatchDAO.addPlayedMatch(league, leagueSerie, winner, loser);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
|
||||
@@ -1,32 +1,24 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.competitive.StandingsProducer;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
import com.gempukku.lotro.db.LeagueParticipationDAO;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatchResult;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.gempukku.util.DescComparator;
|
||||
import com.gempukku.util.MultipleComparator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class LeagueService {
|
||||
private Comparator<PlayerStanding> LEAGUE_STANDING_COMPARATOR =
|
||||
new MultipleComparator<PlayerStanding>(
|
||||
new DescComparator<PlayerStanding>(new PointsComparator()),
|
||||
new GamesPlayedComparator(),
|
||||
new DescComparator<PlayerStanding>(new OpponentsWinComparator()));
|
||||
|
||||
private LeagueDAO _leagueDao;
|
||||
|
||||
// Cached on this layer
|
||||
@@ -156,8 +148,8 @@ public class LeagueService {
|
||||
|
||||
private void awardPrizesToPlayer(League league, LeagueSerieData serie, String player, boolean winner) {
|
||||
int count = 0;
|
||||
Collection<LeagueMatch> playerMatchesPlayedOn = getPlayerMatchesInSerie(league, serie, player);
|
||||
for (LeagueMatch leagueMatch : playerMatchesPlayedOn) {
|
||||
Collection<LeagueMatchResult> playerMatchesPlayedOn = getPlayerMatchesInSerie(league, serie, player);
|
||||
for (LeagueMatchResult leagueMatch : playerMatchesPlayedOn) {
|
||||
if (leagueMatch.getWinner().equals(player))
|
||||
count++;
|
||||
}
|
||||
@@ -171,10 +163,10 @@ public class LeagueService {
|
||||
_collectionsManager.addItemsToPlayerCollection(player, new CollectionType("permanent", "My cards"), prize.getAll());
|
||||
}
|
||||
|
||||
private Collection<LeagueMatch> getPlayerMatchesInSerie(League league, LeagueSerieData serie, String player) {
|
||||
final Collection<LeagueMatch> allMatches = _leagueMatchDao.getLeagueMatches(league);
|
||||
Set<LeagueMatch> result = new HashSet<LeagueMatch>();
|
||||
for (LeagueMatch match : allMatches) {
|
||||
private Collection<LeagueMatchResult> getPlayerMatchesInSerie(League league, LeagueSerieData serie, String player) {
|
||||
final Collection<LeagueMatchResult> allMatches = _leagueMatchDao.getLeagueMatches(league);
|
||||
Set<LeagueMatchResult> result = new HashSet<LeagueMatchResult>();
|
||||
for (LeagueMatchResult match : allMatches) {
|
||||
if (match.getSerieName().equals(serie.getName()) && (match.getWinner().equals(player) || match.getLoser().equals(player)))
|
||||
result.add(match);
|
||||
}
|
||||
@@ -205,10 +197,10 @@ public class LeagueService {
|
||||
|
||||
private List<PlayerStanding> createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) {
|
||||
final Collection<String> playersParticipating = _leagueParticipationDAO.getUsersParticipating(league);
|
||||
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueMatches(league);
|
||||
final Collection<LeagueMatchResult> matches = _leagueMatchDao.getLeagueMatches(league);
|
||||
|
||||
Set<LeagueMatch> matchesInSerie = new HashSet<LeagueMatch>();
|
||||
for (LeagueMatch match : matches) {
|
||||
Set<LeagueMatchResult> matchesInSerie = new HashSet<LeagueMatchResult>();
|
||||
for (LeagueMatchResult match : matches) {
|
||||
if (match.getSerieName().equals(leagueSerie.getName()))
|
||||
matchesInSerie.add(match);
|
||||
}
|
||||
@@ -218,104 +210,29 @@ public class LeagueService {
|
||||
|
||||
private List<PlayerStanding> createLeagueStandings(League league) {
|
||||
final Collection<String> playersParticipating = _leagueParticipationDAO.getUsersParticipating(league);
|
||||
final Collection<LeagueMatch> matches = _leagueMatchDao.getLeagueMatches(league);
|
||||
final Collection<LeagueMatchResult> matches = _leagueMatchDao.getLeagueMatches(league);
|
||||
|
||||
return createStandingsForMatchesAndPoints(playersParticipating, matches);
|
||||
}
|
||||
|
||||
private List<PlayerStanding> createStandingsForMatchesAndPoints(Collection<String> playersParticipating, Collection<LeagueMatch> matches) {
|
||||
Map<String, List<String>> playerOpponents = new HashMap<String, List<String>>();
|
||||
Map<String, AtomicInteger> playerWinCounts = new HashMap<String, AtomicInteger>();
|
||||
Map<String, AtomicInteger> playerLossCounts = new HashMap<String, AtomicInteger>();
|
||||
|
||||
// Initialize the list
|
||||
for (String playerName : playersParticipating) {
|
||||
playerOpponents.put(playerName, new ArrayList<String>());
|
||||
playerWinCounts.put(playerName, new AtomicInteger(0));
|
||||
playerLossCounts.put(playerName, new AtomicInteger(0));
|
||||
}
|
||||
|
||||
for (LeagueMatch leagueMatch : matches) {
|
||||
playerOpponents.get(leagueMatch.getWinner()).add(leagueMatch.getLoser());
|
||||
playerOpponents.get(leagueMatch.getLoser()).add(leagueMatch.getWinner());
|
||||
playerWinCounts.get(leagueMatch.getWinner()).incrementAndGet();
|
||||
playerLossCounts.get(leagueMatch.getLoser()).incrementAndGet();
|
||||
}
|
||||
|
||||
List<PlayerStanding> leagueStandings = new LinkedList<PlayerStanding>();
|
||||
for (String playerName : playersParticipating) {
|
||||
int points = playerWinCounts.get(playerName).intValue() * 2 + playerLossCounts.get(playerName).intValue();
|
||||
int gamesPlayed = playerWinCounts.get(playerName).intValue() + playerLossCounts.get(playerName).intValue();
|
||||
PlayerStanding standing = new PlayerStanding(playerName, points, gamesPlayed);
|
||||
List<String> opponents = playerOpponents.get(playerName);
|
||||
int opponentWins = 0;
|
||||
int opponentGames = 0;
|
||||
for (String opponent : opponents) {
|
||||
opponentWins += playerWinCounts.get(opponent).intValue();
|
||||
opponentGames += playerWinCounts.get(opponent).intValue() + playerLossCounts.get(opponent).intValue();
|
||||
}
|
||||
if (opponentGames != 0)
|
||||
standing.setOpponentWin(opponentWins * 1f / opponentGames);
|
||||
else
|
||||
standing.setOpponentWin(0f);
|
||||
leagueStandings.add(standing);
|
||||
}
|
||||
|
||||
Collections.sort(leagueStandings, LEAGUE_STANDING_COMPARATOR);
|
||||
|
||||
int standing = 0;
|
||||
int position = 1;
|
||||
PlayerStanding lastStanding = null;
|
||||
for (PlayerStanding leagueStanding : leagueStandings) {
|
||||
if (lastStanding == null || LEAGUE_STANDING_COMPARATOR.compare(leagueStanding, lastStanding) != 0)
|
||||
standing = position;
|
||||
leagueStanding.setStanding(standing);
|
||||
position++;
|
||||
lastStanding = leagueStanding;
|
||||
}
|
||||
return leagueStandings;
|
||||
private List<PlayerStanding> createStandingsForMatchesAndPoints(Collection<String> playersParticipating, Collection<LeagueMatchResult> matches) {
|
||||
return StandingsProducer.produceStandings(playersParticipating, matches, 2, 1, Collections.<String>emptySet());
|
||||
}
|
||||
|
||||
public boolean canPlayRankedGame(League league, LeagueSerieData season, String player) {
|
||||
int maxMatches = season.getMaxMatches();
|
||||
Collection<LeagueMatch> playedInSeason = getPlayerMatchesInSerie(league, season, player);
|
||||
Collection<LeagueMatchResult> playedInSeason = getPlayerMatchesInSerie(league, season, player);
|
||||
if (playedInSeason.size() >= maxMatches)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canPlayRankedGameAgainst(League league, LeagueSerieData season, String playerOne, String playerTwo) {
|
||||
Collection<LeagueMatch> playedInSeason = getPlayerMatchesInSerie(league, season, playerOne);
|
||||
for (LeagueMatch leagueMatch : playedInSeason) {
|
||||
Collection<LeagueMatchResult> playedInSeason = getPlayerMatchesInSerie(league, season, playerOne);
|
||||
for (LeagueMatchResult leagueMatch : playedInSeason) {
|
||||
if (playerTwo.equals(leagueMatch.getWinner()) || playerTwo.equals(leagueMatch.getLoser()))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private class PointsComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getPoints() - o2.getPoints();
|
||||
}
|
||||
}
|
||||
|
||||
private class GamesPlayedComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getGamesPlayed() - o2.getGamesPlayed();
|
||||
}
|
||||
}
|
||||
|
||||
private class OpponentsWinComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
final float diff = o1.getOpponentWin() - o2.getOpponentWin();
|
||||
if (diff < 0)
|
||||
return -1;
|
||||
if (diff > 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
|
||||
@@ -1,22 +1,18 @@
|
||||
package com.gempukku.lotro.tournament;
|
||||
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.competitive.StandingsProducer;
|
||||
import com.gempukku.lotro.game.LotroFormat;
|
||||
import com.gempukku.lotro.logic.vo.LotroDeck;
|
||||
import com.gempukku.util.DescComparator;
|
||||
import com.gempukku.util.MultipleComparator;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class Tournament {
|
||||
private Comparator<PlayerStanding> STANDING_COMPARATOR =
|
||||
new MultipleComparator<PlayerStanding>(
|
||||
new DescComparator<PlayerStanding>(new PointsComparator()),
|
||||
new GamesPlayedComparator(),
|
||||
new DescComparator<PlayerStanding>(new OpponentsWinComparator()));
|
||||
|
||||
private TournamentMatchDao _tournamentMatchDao;
|
||||
|
||||
private Map<String, LotroDeck> _players;
|
||||
@@ -146,78 +142,13 @@ public class Tournament {
|
||||
}
|
||||
|
||||
private void calculateCurrentStandings() {
|
||||
Map<String, PlayerStanding> playersStandings = new HashMap<String, PlayerStanding>();
|
||||
for (String player : _players.keySet()) {
|
||||
int points = 0;
|
||||
int gamesPlayed = 0;
|
||||
for (int i = 0; i < _currentRound; i++) {
|
||||
if (_droppedPlayers[i] != null && _droppedPlayers[i].contains(player))
|
||||
break;
|
||||
|
||||
TournamentMatch match = getPlayerMatch(i, player);
|
||||
if (match != null) {
|
||||
gamesPlayed++;
|
||||
if (match.getWinner().equals(player))
|
||||
points++;
|
||||
} else if (_currentRoundPairings.containsKey(player) || _currentRoundPairings.containsValue(player)) {
|
||||
// Do nothing
|
||||
} else {
|
||||
// Bye
|
||||
points++;
|
||||
gamesPlayed++;
|
||||
}
|
||||
}
|
||||
|
||||
playersStandings.put(player, new PlayerStanding(player, points, gamesPlayed));
|
||||
Set<TournamentMatch> standingMatches = new HashSet<TournamentMatch>();
|
||||
for (Set<TournamentMatch> finishedMatchesInRound : _finishedMatches) {
|
||||
if (finishedMatchesInRound != null)
|
||||
standingMatches.addAll(finishedMatchesInRound);
|
||||
}
|
||||
|
||||
for (String player : _players.keySet()) {
|
||||
int opponentPoints = 0;
|
||||
int opponentGamesPlayed = 0;
|
||||
for (int i = 0; i < _currentRound; i++) {
|
||||
if (_droppedPlayers[i] != null && _droppedPlayers[i].contains(player))
|
||||
break;
|
||||
TournamentMatch match = getPlayerMatch(i, player);
|
||||
if (match != null) {
|
||||
String opponent;
|
||||
if (match.getPlayerOne().equals(player))
|
||||
opponent = match.getPlayerTwo();
|
||||
else
|
||||
opponent = match.getPlayerOne();
|
||||
PlayerStanding opponentStanding = playersStandings.get(opponent);
|
||||
opponentPoints += opponentStanding.getPoints();
|
||||
opponentGamesPlayed += opponentStanding.getGamesPlayed();
|
||||
}
|
||||
}
|
||||
float opponentWinPerc = opponentPoints * 1f / opponentGamesPlayed;
|
||||
playersStandings.get(player).setOpponentWin(opponentWinPerc);
|
||||
}
|
||||
|
||||
List<PlayerStanding> result = new ArrayList<PlayerStanding>(playersStandings.values());
|
||||
|
||||
Collections.sort(result, STANDING_COMPARATOR);
|
||||
|
||||
int standing = 0;
|
||||
int position = 1;
|
||||
PlayerStanding lastStanding = null;
|
||||
for (PlayerStanding playerStanding : result) {
|
||||
if (lastStanding == null || STANDING_COMPARATOR.compare(playerStanding, lastStanding) != 0)
|
||||
standing = position;
|
||||
playerStanding.setStanding(standing);
|
||||
position++;
|
||||
lastStanding = playerStanding;
|
||||
}
|
||||
|
||||
_currentStandings = Collections.unmodifiableList(result);
|
||||
}
|
||||
|
||||
private TournamentMatch getPlayerMatch(int roundIndex, String player) {
|
||||
for (TournamentMatch tournamentMatch : _finishedMatches[roundIndex]) {
|
||||
if (tournamentMatch.getPlayerOne().equals(player)
|
||||
|| tournamentMatch.getPlayerTwo().equals(player))
|
||||
return tournamentMatch;
|
||||
}
|
||||
return null;
|
||||
_currentStandings = StandingsProducer.produceStandings(_players.keySet(), standingMatches, 1, 0, _playersWithByes);
|
||||
}
|
||||
|
||||
private interface TournamentTask {
|
||||
@@ -238,31 +169,4 @@ public class Tournament {
|
||||
return _time + 1000 * 60 * 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class PointsComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getPoints() - o2.getPoints();
|
||||
}
|
||||
}
|
||||
|
||||
private class GamesPlayedComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
return o1.getGamesPlayed() - o2.getGamesPlayed();
|
||||
}
|
||||
}
|
||||
|
||||
private class OpponentsWinComparator implements Comparator<PlayerStanding> {
|
||||
@Override
|
||||
public int compare(PlayerStanding o1, PlayerStanding o2) {
|
||||
final float diff = o1.getOpponentWin() - o2.getOpponentWin();
|
||||
if (diff < 0)
|
||||
return -1;
|
||||
if (diff > 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.gempukku.lotro.tournament;
|
||||
|
||||
public class TournamentMatch {
|
||||
import com.gempukku.lotro.competitive.CompetitiveMatchResult;
|
||||
|
||||
public class TournamentMatch implements CompetitiveMatchResult {
|
||||
private String _playerOne;
|
||||
private String _playerTwo;
|
||||
private String _winner;
|
||||
@@ -21,10 +23,19 @@ public class TournamentMatch {
|
||||
return _playerTwo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getWinner() {
|
||||
return _winner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLoser() {
|
||||
if (_playerOne.equals(_winner))
|
||||
return _playerTwo;
|
||||
else
|
||||
return _playerOne;
|
||||
}
|
||||
|
||||
public int getRound() {
|
||||
return _round;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
import com.gempukku.lotro.db.LeagueParticipationDAO;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatch;
|
||||
import com.gempukku.lotro.db.vo.LeagueMatchResult;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
@@ -38,9 +38,9 @@ public class LeagueServiceTest {
|
||||
|
||||
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
|
||||
|
||||
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
|
||||
Set<LeagueMatchResult> matches = new HashSet<LeagueMatchResult>();
|
||||
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatchResult>(matches));
|
||||
|
||||
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
|
||||
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
|
||||
@@ -91,10 +91,10 @@ public class LeagueServiceTest {
|
||||
|
||||
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
|
||||
|
||||
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
|
||||
matches.add(new LeagueMatch(leagueSerie.getName(), "player1", "player2"));
|
||||
Set<LeagueMatchResult> matches = new HashSet<LeagueMatchResult>();
|
||||
matches.add(new LeagueMatchResult(leagueSerie.getName(), "player1", "player2"));
|
||||
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatchResult>(matches));
|
||||
|
||||
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
|
||||
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
|
||||
@@ -137,9 +137,9 @@ public class LeagueServiceTest {
|
||||
|
||||
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
|
||||
|
||||
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
|
||||
Set<LeagueMatchResult> matches = new HashSet<LeagueMatchResult>();
|
||||
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
|
||||
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatchResult>(matches));
|
||||
|
||||
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
|
||||
Set<String> players = new HashSet<String>();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.PlayerStanding;
|
||||
import com.gempukku.lotro.competitive.PlayerStanding;
|
||||
import com.gempukku.lotro.db.vo.League;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
import com.gempukku.lotro.game.formats.LotroFormatLibrary;
|
||||
|
||||
Reference in New Issue
Block a user