From b6db348386e4ad04fbaa3eb077f689ef5873816f Mon Sep 17 00:00:00 2001 From: "marcins78@gmail.com" Date: Tue, 1 May 2012 16:17:57 +0000 Subject: [PATCH] Started working on tournaments. --- ...eagueStanding.java => PlayerStanding.java} | 6 +- .../lotro/league/ConstructedLeagueData.java | 5 +- .../com/gempukku/lotro/league/LeagueData.java | 3 +- .../gempukku/lotro/league/LeagueService.java | 47 +-- .../league/NewConstructedLeagueData.java | 5 +- .../lotro/league/NewSealedLeagueData.java | 7 +- .../lotro/league/SealedLeagueData.java | 7 +- .../gempukku/lotro/tournament/Tournament.java | 268 ++++++++++++++++++ .../lotro/tournament/TournamentCallback.java | 9 + .../lotro/tournament/TournamentMatch.java | 31 ++ .../lotro/tournament/TournamentMatchDao.java | 11 + .../lotro/tournament/TournamentPlayerDao.java | 13 + .../gempukku/lotro/server/LeagueResource.java | 12 +- 13 files changed, 381 insertions(+), 43 deletions(-) rename gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/{league/LeagueStanding.java => PlayerStanding.java} (85%) create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentCallback.java create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatch.java create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatchDao.java create mode 100644 gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentPlayerDao.java diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueStanding.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/PlayerStanding.java similarity index 85% rename from gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueStanding.java rename to gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/PlayerStanding.java index ab01e1484..4fa8e0545 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueStanding.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/PlayerStanding.java @@ -1,13 +1,13 @@ -package com.gempukku.lotro.league; +package com.gempukku.lotro; -public class LeagueStanding { +public class PlayerStanding { private String _playerName; private int _points; private int _gamesPlayed; private float _opponentWin; private int _standing; - public LeagueStanding(String playerName, int points, int gamesPlayed) { + public PlayerStanding(String playerName, int points, int gamesPlayed) { _playerName = playerName; _points = points; _gamesPlayed = gamesPlayed; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/ConstructedLeagueData.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/ConstructedLeagueData.java index 749190d1a..9306bf3ab 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/ConstructedLeagueData.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/ConstructedLeagueData.java @@ -1,6 +1,7 @@ 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.db.vo.CollectionType; import com.gempukku.lotro.game.CardCollection; @@ -52,12 +53,12 @@ public class ConstructedLeagueData implements LeagueData { } @Override - public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { + public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { int status = oldStatus; if (status == 0) { LeagueSerieData lastSerie = _series.get(_series.size() - 1); if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) { - for (LeagueStanding leagueStanding : leagueStandings) { + for (PlayerStanding leagueStanding : leagueStandings) { CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), _prizeMultiplier, _leaguePrizePool); if (leaguePrize != null) collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll()); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueData.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueData.java index 4e3d194ce..a2b23d57d 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueData.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueData.java @@ -1,5 +1,6 @@ package com.gempukku.lotro.league; +import com.gempukku.lotro.PlayerStanding; import com.gempukku.lotro.collection.CollectionsManager; import com.gempukku.lotro.game.CardCollection; import com.gempukku.lotro.game.Player; @@ -11,5 +12,5 @@ public interface LeagueData { public CardCollection joinLeague(CollectionsManager collecionsManager, Player player, int currentTime); - public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime); + public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime); } diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java index 965b8d312..984d2cf15 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/LeagueService.java @@ -1,6 +1,7 @@ package com.gempukku.lotro.league; import com.gempukku.lotro.DateUtils; +import com.gempukku.lotro.PlayerStanding; import com.gempukku.lotro.cache.ExpireObjectCache; import com.gempukku.lotro.cache.Producable; import com.gempukku.lotro.collection.CollectionsManager; @@ -23,11 +24,11 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; public class LeagueService { - private Comparator LEAGUE_STANDING_COMPARATOR = - new MultipleComparator( - new DescComparator(new PointsComparator()), + private Comparator LEAGUE_STANDING_COMPARATOR = + new MultipleComparator( + new DescComparator(new PointsComparator()), new GamesPlayedComparator(), - new DescComparator(new OpponentsWinComparator())); + new DescComparator(new OpponentsWinComparator())); private LeagueDAO _leagueDao; private LeaguePointsDAO _leaguePointsDao; @@ -36,8 +37,8 @@ public class LeagueService { private LeagueParticipationDAO _leagueParticipationDAO; private CollectionsManager _collectionsManager; - private Map> _leagueStandings = new ConcurrentHashMap>(); - private Map> _leagueSerieStandings = new ConcurrentHashMap>(); + private Map> _leagueStandings = new ConcurrentHashMap>(); + private Map> _leagueSerieStandings = new ConcurrentHashMap>(); private Map> _playersParticipating = new ConcurrentHashMap>(); private Map> _playersNotParticipating = new ConcurrentHashMap>(); @@ -310,8 +311,8 @@ public class LeagueService { return result; } - public List getLeagueStandings(League league) { - List leagueStandings = _leagueStandings.get(league); + public List getLeagueStandings(League league) { + List leagueStandings = _leagueStandings.get(league); if (leagueStandings == null) { synchronized (this) { leagueStandings = createLeagueStandings(league); @@ -321,8 +322,8 @@ public class LeagueService { return leagueStandings; } - public List getLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { - List serieStandings = _leagueSerieStandings.get(leagueSerie); + public List getLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { + List serieStandings = _leagueSerieStandings.get(leagueSerie); if (serieStandings == null) { synchronized (this) { serieStandings = createLeagueSerieStandings(league, leagueSerie); @@ -332,21 +333,21 @@ public class LeagueService { return serieStandings; } - private List createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { + private List createLeagueSerieStandings(League league, LeagueSerieData leagueSerie) { final Map points = getLeagueSeriePoints(league, leagueSerie); final Collection matches = getLeagueSerieMatches(league, leagueSerie); return createStandingsForMatchesAndPoints(points, matches); } - private List createLeagueStandings(League league) { + private List createLeagueStandings(League league) { final Map points = getLeaguePoints(league); final Collection matches = getLeagueMatches(league); return createStandingsForMatchesAndPoints(points, matches); } - private List createStandingsForMatchesAndPoints(Map points, Collection matches) { + private List createStandingsForMatchesAndPoints(Map points, Collection matches) { Map> playerOpponents = new HashMap>(); Map playerWins = new HashMap(); Map playerLoss = new HashMap(); @@ -356,9 +357,9 @@ public class LeagueService { appendMatch(playerWins, playerLoss, leagueMatch.getWinner(), leagueMatch.getLoser()); } - List leagueStandings = new LinkedList(); + List leagueStandings = new LinkedList(); for (Map.Entry playerPoints : points.entrySet()) { - LeagueStanding standing = new LeagueStanding(playerPoints.getKey(), playerPoints.getValue().getPoints(), playerPoints.getValue().getGamesPlayed()); + PlayerStanding standing = new PlayerStanding(playerPoints.getKey(), playerPoints.getValue().getPoints(), playerPoints.getValue().getGamesPlayed()); List opponents = playerOpponents.get(playerPoints.getKey()); int opponentWins = 0; int opponentGames = 0; @@ -374,8 +375,8 @@ public class LeagueService { int standing = 0; int position = 1; - LeagueStanding lastStanding = null; - for (LeagueStanding leagueStanding : leagueStandings) { + PlayerStanding lastStanding = null; + for (PlayerStanding leagueStanding : leagueStandings) { if (lastStanding == null || LEAGUE_STANDING_COMPARATOR.compare(leagueStanding, lastStanding) != 0) standing = position; leagueStanding.setStanding(standing); @@ -428,23 +429,23 @@ public class LeagueService { return true; } - private class PointsComparator implements Comparator { + private class PointsComparator implements Comparator { @Override - public int compare(LeagueStanding o1, LeagueStanding o2) { + public int compare(PlayerStanding o1, PlayerStanding o2) { return o1.getPoints() - o2.getPoints(); } } - private class GamesPlayedComparator implements Comparator { + private class GamesPlayedComparator implements Comparator { @Override - public int compare(LeagueStanding o1, LeagueStanding o2) { + public int compare(PlayerStanding o1, PlayerStanding o2) { return o1.getGamesPlayed() - o2.getGamesPlayed(); } } - private class OpponentsWinComparator implements Comparator { + private class OpponentsWinComparator implements Comparator { @Override - public int compare(LeagueStanding o1, LeagueStanding o2) { + public int compare(PlayerStanding o1, PlayerStanding o2) { final float diff = o1.getOpponentWin() - o2.getOpponentWin(); if (diff < 0) return -1; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewConstructedLeagueData.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewConstructedLeagueData.java index 73784f4c2..3697c0f5d 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewConstructedLeagueData.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewConstructedLeagueData.java @@ -1,6 +1,7 @@ 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.db.vo.CollectionType; import com.gempukku.lotro.game.CardCollection; @@ -54,12 +55,12 @@ public class NewConstructedLeagueData implements LeagueData { } @Override - public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { + public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { int status = oldStatus; if (status == 0) { LeagueSerieData lastSerie = _series.get(_series.size() - 1); if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) { - for (LeagueStanding leagueStanding : leagueStandings) { + for (PlayerStanding leagueStanding : leagueStandings) { CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), _prizeMultiplier, null); if (leaguePrize != null) collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll()); diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewSealedLeagueData.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewSealedLeagueData.java index 16a56b347..41487c6b8 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewSealedLeagueData.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/NewSealedLeagueData.java @@ -1,6 +1,7 @@ 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.db.vo.CollectionType; import com.gempukku.lotro.game.CardCollection; @@ -64,7 +65,7 @@ public class NewSealedLeagueData implements LeagueData { } @Override - public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { + public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { int status = oldStatus; for (int i = status; i < _series.size(); i++) { @@ -82,12 +83,12 @@ public class NewSealedLeagueData implements LeagueData { if (status == _series.size()) { LeagueSerieData lastSerie = _series.get(_series.size() - 1); if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) { - for (LeagueStanding leagueStanding : leagueStandings) { + for (PlayerStanding leagueStanding : leagueStandings) { CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), 1f, _format); if (leaguePrize != null) collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll()); } -// for (LeagueStanding leagueStanding : leagueStandings) { +// for (PlayerStanding leagueStanding : leagueStandings) { // collectionsManager.moveCollectionToCollection(leagueStanding.getPlayerName(), _collectionType, _prizeCollectionType); // } status++; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/SealedLeagueData.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/SealedLeagueData.java index e83334291..29128d0a2 100644 --- a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/SealedLeagueData.java +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/league/SealedLeagueData.java @@ -1,6 +1,7 @@ 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.db.vo.CollectionType; import com.gempukku.lotro.game.CardCollection; @@ -64,7 +65,7 @@ public class SealedLeagueData implements LeagueData { } @Override - public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { + public int process(CollectionsManager collectionsManager, List leagueStandings, int oldStatus, int currentTime) { int status = oldStatus; for (int i = status; i < _series.size(); i++) { @@ -82,12 +83,12 @@ public class SealedLeagueData implements LeagueData { if (status == _series.size()) { LeagueSerieData lastSerie = _series.get(_series.size() - 1); if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) { - for (LeagueStanding leagueStanding : leagueStandings) { + for (PlayerStanding leagueStanding : leagueStandings) { CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), 1f, _format); if (leaguePrize != null) collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll()); } -// for (LeagueStanding leagueStanding : leagueStandings) { +// for (PlayerStanding leagueStanding : leagueStandings) { // collectionsManager.moveCollectionToCollection(leagueStanding.getPlayerName(), _collectionType, _prizeCollectionType); // } status++; diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java new file mode 100644 index 000000000..a1ef422cb --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/Tournament.java @@ -0,0 +1,268 @@ +package com.gempukku.lotro.tournament; + +import com.gempukku.lotro.PlayerStanding; +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.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; + +public class Tournament { + private Comparator LEAGUE_STANDING_COMPARATOR = + new MultipleComparator( + new DescComparator(new PointsComparator()), + new GamesPlayedComparator(), + new DescComparator(new OpponentsWinComparator())); + + private TournamentMatchDao _tournamentMatchDao; + + private Map _players; + private Set[] _droppedPlayers; + + private int _roundCount; + // Current round + private int _currentRound; + private String _id; + private LotroFormat _format; + + private Set[] _finishedMatches; + + private Map _currentRoundPairings; + private Set _playersWithByes; + + private TournamentTask _nextTournamentTask; + + private List _currentStandings; + + public Tournament(TournamentMatchDao tournamentMatchDao, Map players, Set[] droppedPlayers, int roundCount, int status, String id, LotroFormat format) { + _tournamentMatchDao = tournamentMatchDao; + _droppedPlayers = droppedPlayers; + + _roundCount = roundCount; + _currentRound = status; + _id = id; + _format = format; + + _players = new ConcurrentHashMap(players); + for (int i = 0; i < _currentRound; i++) { + Set allPlayers = new HashSet(players.keySet()); + // Remove all players dropped so far + for (int j = 0; j <= i; j++) + allPlayers.removeAll(droppedPlayers[j]); + + // Fill the information about all the matches finished or scheduled so far + for (TournamentMatch tournamentMatch : _tournamentMatchDao.getMatches(_id, i + 1)) { + if (tournamentMatch.getWinner() != null) + _finishedMatches[i].add(tournamentMatch); + else + _currentRoundPairings.put(tournamentMatch.getPlayerOne(), tournamentMatch.getPlayerTwo()); + // Removed playing (or played) players + allPlayers.remove(tournamentMatch.getPlayerOne()); + allPlayers.remove(tournamentMatch.getPlayerTwo()); + } + // Remaining players had a bye + _playersWithByes.addAll(allPlayers); + } + + if (_currentRoundPairings.isEmpty()) { + _nextTournamentTask = new DelayedTournamentTask() { + @Override + public int executeTask(TournamentCallback tournamentCallback) { + return roundFinished(tournamentCallback); + } + }; + } else { + _nextTournamentTask = new DelayedTournamentTask() { + @Override + public int executeTask(TournamentCallback tournamentCallback) { + tournamentCallback.createGames(_currentRoundPairings, _players); + return _currentRound; + } + }; + } + } + + private int roundFinished(TournamentCallback tournamentCallback) { + if (_currentRound < _roundCount) { + _currentRound++; + boolean success = tryCreatingNewPairings(); + if (success) { + Map currentRoundPairings = _currentRoundPairings; + for (Map.Entry pairing : currentRoundPairings.entrySet()) + _tournamentMatchDao.addMatch(_id, _currentRound, pairing.getKey(), pairing.getValue()); + + tournamentCallback.createGames(currentRoundPairings, _players); + return _currentRound; + } else { + return distributePrizes(tournamentCallback); + } + } else if (_currentRound == _roundCount) { + return distributePrizes(tournamentCallback); + } else { + return _currentRound; + } + } + + private boolean tryCreatingNewPairings() { + return false; + } + + private int distributePrizes(TournamentCallback tournamentCallback) { + _currentRound = _roundCount + 1; + + // Distribute prizes + + return _currentRound; + } + + public synchronized void dropPlayerBeforeRound(int round, String playerName) { + if (_droppedPlayers[round] == null) + _droppedPlayers[round] = new CopyOnWriteArraySet(); + _droppedPlayers[round].add(playerName); + } + + public synchronized void executePendingTasks(TournamentCallback tournamentCallback) { + if (_nextTournamentTask != null && _nextTournamentTask.getExecuteAfter() < System.currentTimeMillis()) { + TournamentTask task = _nextTournamentTask; + _nextTournamentTask = null; + task.executeTask(tournamentCallback); + } + } + + public List getCurrentStandings() { + List result = _currentStandings; + if (result != null) + return result; + + synchronized (this) { + if (_currentStandings == null) + calculateCurrentStandings(); + + return _currentStandings; + } + } + + private void calculateCurrentStandings() { + Map playersStandings = new HashMap(); + 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)); + } + + 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 result = new ArrayList(playersStandings.values()); + + Collections.sort(result, LEAGUE_STANDING_COMPARATOR); + + int standing = 0; + int position = 1; + PlayerStanding lastStanding = null; + for (PlayerStanding playerStanding : result) { + if (lastStanding == null || LEAGUE_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; + } + + private interface TournamentTask { + public int executeTask(TournamentCallback tournamentCallback); + + public long getExecuteAfter(); + } + + private abstract class DelayedTournamentTask implements TournamentTask { + private final long _time; + + private DelayedTournamentTask() { + _time = System.currentTimeMillis(); + } + + @Override + public final long getExecuteAfter() { + return _time + 1000 * 60 * 2; + } + } + + + private class PointsComparator implements Comparator { + @Override + public int compare(PlayerStanding o1, PlayerStanding o2) { + return o1.getPoints() - o2.getPoints(); + } + } + + private class GamesPlayedComparator implements Comparator { + @Override + public int compare(PlayerStanding o1, PlayerStanding o2) { + return o1.getGamesPlayed() - o2.getGamesPlayed(); + } + } + + private class OpponentsWinComparator implements Comparator { + @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; + } + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentCallback.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentCallback.java new file mode 100644 index 000000000..90fe83284 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentCallback.java @@ -0,0 +1,9 @@ +package com.gempukku.lotro.tournament; + +import com.gempukku.lotro.logic.vo.LotroDeck; + +import java.util.Map; + +public interface TournamentCallback { + public void createGames(Map pairings, Map playerDecks); +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatch.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatch.java new file mode 100644 index 000000000..58464cb36 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatch.java @@ -0,0 +1,31 @@ +package com.gempukku.lotro.tournament; + +public class TournamentMatch { + private String _playerOne; + private String _playerTwo; + private String _winner; + private int _round; + + public TournamentMatch(String playerOne, String playerTwo, String winner, int round) { + _playerOne = playerOne; + _playerTwo = playerTwo; + _winner = winner; + _round = round; + } + + public String getPlayerOne() { + return _playerOne; + } + + public String getPlayerTwo() { + return _playerTwo; + } + + public String getWinner() { + return _winner; + } + + public int getRound() { + return _round; + } +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatchDao.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatchDao.java new file mode 100644 index 000000000..5d8f3fb36 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentMatchDao.java @@ -0,0 +1,11 @@ +package com.gempukku.lotro.tournament; + +import java.util.List; + +public interface TournamentMatchDao { + public void addMatch(String tournamentId, int round, String playerOne, String playerTwo); + + public void setMatchResult(String tournamentId, int round, String winner); + + public List getMatches(String tournamentId, int round); +} diff --git a/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentPlayerDao.java b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentPlayerDao.java new file mode 100644 index 000000000..1b306a412 --- /dev/null +++ b/gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/tournament/TournamentPlayerDao.java @@ -0,0 +1,13 @@ +package com.gempukku.lotro.tournament; + +import com.gempukku.lotro.logic.vo.LotroDeck; + +import java.util.Map; + +public interface TournamentPlayerDao { + public void addPlayer(String tournamentId, String playerName, LotroDeck deck); + + public void removePlayer(String tournamentId, String playerName); + + public Map getPlayers(String tournamentId); +} diff --git a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/LeagueResource.java b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/LeagueResource.java index ec623eabc..5d03700b8 100644 --- a/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/LeagueResource.java +++ b/gemp-lotr/gemp-lotr-web-server/src/main/java/com/gempukku/lotro/server/LeagueResource.java @@ -1,13 +1,13 @@ package com.gempukku.lotro.server; import com.gempukku.lotro.DateUtils; +import com.gempukku.lotro.PlayerStanding; import com.gempukku.lotro.db.vo.League; import com.gempukku.lotro.game.Player; import com.gempukku.lotro.game.formats.LotroFormatLibrary; import com.gempukku.lotro.league.LeagueData; import com.gempukku.lotro.league.LeagueSerieData; import com.gempukku.lotro.league.LeagueService; -import com.gempukku.lotro.league.LeagueStanding; import com.sun.jersey.spi.resource.Singleton; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -126,8 +126,8 @@ public class LeagueResource extends AbstractResource { serieElem.setAttribute("collection", serie.getCollectionType().getFullName()); serieElem.setAttribute("limited", String.valueOf(serie.isLimited())); - final List standings = _leagueService.getLeagueSerieStandings(league, serie); - for (LeagueStanding standing : standings) { + final List standings = _leagueService.getLeagueSerieStandings(league, serie); + for (PlayerStanding standing : standings) { Element standingElem = doc.createElement("standing"); setStandingAttributes(standing, standingElem); serieElem.appendChild(standingElem); @@ -136,8 +136,8 @@ public class LeagueResource extends AbstractResource { leagueElem.appendChild(serieElem); } - List leagueStandings = _leagueService.getLeagueStandings(league); - for (LeagueStanding standing : leagueStandings) { + List leagueStandings = _leagueService.getLeagueStandings(league); + for (PlayerStanding standing : leagueStandings) { Element standingElem = doc.createElement("leagueStanding"); setStandingAttributes(standing, standingElem); leagueElem.appendChild(standingElem); @@ -156,7 +156,7 @@ public class LeagueResource extends AbstractResource { return null; } - private void setStandingAttributes(LeagueStanding standing, Element standingElem) { + private void setStandingAttributes(PlayerStanding standing, Element standingElem) { standingElem.setAttribute("player", standing.getPlayerName()); standingElem.setAttribute("standing", String.valueOf(standing.getStanding())); standingElem.setAttribute("points", String.valueOf(standing.getPoints()));