Fixing calculating opp win %.

This commit is contained in:
marcins78@gmail.com
2012-03-12 01:18:20 +00:00
parent e93501ed95
commit 46ab7ecf35
2 changed files with 25 additions and 6 deletions

View File

@@ -190,9 +190,12 @@ public class LeagueService {
private List<LeagueStanding> createStandingsForMatchesAndPoints(Map<String, LeaguePointsDAO.Points> points, Collection<LeagueMatch> matches) {
Map<String, List<String>> playerOpponents = new HashMap<String, List<String>>();
Map<String, Integer> playerWins = new HashMap<String, Integer>();
Map<String, Integer> playerLoss = new HashMap<String, Integer>();
for (LeagueMatch leagueMatch : matches) {
appendPlayer(playerOpponents, leagueMatch.getWinner(), leagueMatch.getLoser());
appendPlayer(playerOpponents, leagueMatch.getLoser(), leagueMatch.getWinner());
appendMatch(playerWins, playerLoss, leagueMatch.getWinner(), leagueMatch.getLoser());
}
List<LeagueStanding> leagueStandings = new LinkedList<LeagueStanding>();
@@ -202,9 +205,8 @@ public class LeagueService {
int opponentWins = 0;
int opponentGames = 0;
for (String opponent : opponents) {
final LeaguePointsDAO.Points opponentPoints = points.get(opponent);
opponentWins += opponentPoints.getPoints() - opponentPoints.getGamesPlayed();
opponentGames += opponentPoints.getGamesPlayed();
opponentWins += playerWins.get(opponent);
opponentGames += playerWins.get(opponent) + playerLoss.get(opponent);
}
standing.setOpponentWin(opponentWins * 1f / opponentGames);
leagueStandings.add(standing);
@@ -225,6 +227,23 @@ public class LeagueService {
return leagueStandings;
}
private void appendMatch(Map<String, Integer> playerWins, Map<String, Integer> playerLoss, String winner, String loser) {
append(playerWins, winner);
append(playerLoss, loser);
if (!playerWins.containsKey(loser))
playerWins.put(loser, 0);
if (!playerLoss.containsKey(winner))
playerLoss.put(winner, 0);
}
private void append(Map<String, Integer> playerCounts, String player) {
Integer count = playerCounts.get(player);
if (count == null)
count = 0;
count++;
playerCounts.put(player, count);
}
private void appendPlayer(Map<String, List<String>> playerOpponents, String player, String opponent) {
List<String> opponents = playerOpponents.get(player);
if (opponents == null) {

View File

@@ -22,7 +22,7 @@ public class SealedLeagueDataTest {
CollectionType collectionType = new CollectionType("test", "Test Collection");
for (int i = 20120101; i < 20120108; i++) {
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
Player player = new Player(1, "Test", "A");
Player player = new Player(1, "Test", "A", null);
data.joinLeague(collectionsManager, player, i);
Mockito.verify(collectionsManager, new Times(1)).addPlayerCollection(Mockito.eq(player), Mockito.eq(collectionType), Mockito.argThat(
new BaseMatcher<CardCollection>() {
@@ -57,7 +57,7 @@ public class SealedLeagueDataTest {
CollectionType collectionType = new CollectionType("test", "Test Collection");
for (int i = 20120108; i < 20120115; i++) {
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
Player player = new Player(1, "Test", "A");
Player player = new Player(1, "Test", "A", null);
data.joinLeague(collectionsManager, player, i);
Mockito.verify(collectionsManager, new Times(1)).addPlayerCollection(Mockito.eq(player), Mockito.eq(collectionType), Mockito.argThat(
new BaseMatcher<CardCollection>() {
@@ -124,7 +124,7 @@ public class SealedLeagueDataTest {
for (int i = 20120108; i < 20120115; i++) {
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
Map<Player, CardCollection> playersInLeague = new HashMap<Player, CardCollection>();
Player player = new Player(1, "Test", "A");
Player player = new Player(1, "Test", "A", null);
playersInLeague.put(player, new DefaultCardCollection());
Mockito.when(collectionsManager.getPlayersCollection("test")).thenReturn(playersInLeague);
int result = data.process(collectionsManager, null, 1, i);