package org.drooms.launcher.tournament;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.collections15.MultiMap;
import org.apache.commons.collections15.multimap.MultiHashMap;
import org.drooms.api.Player;

public class DroomsTournamentResults extends TournamentResults {

    public DroomsTournamentResults(final String name,
            final Collection<Player> players) {
        super(name, players);
    }

    @Override
    protected List<Collection<Player>> evaluateGame(
            final Collection<Player> players, final GameResults game) {
        // group players by their median results
        final MultiMap<BigDecimal, Player> grouped = new MultiHashMap<>();
        for (final Player p : players) {
            grouped.put(game.getMedian(p), p);
        }
        // order the grouped players
        final SortedMap<BigDecimal, Collection<Player>> ordered = new TreeMap<>();
        for (final Entry<BigDecimal, Collection<Player>> entry : grouped
                .entrySet()) {
            ordered.put(entry.getKey(), entry.getValue());
        }
        // store the results in a reverse order; median biggest to lowest
        final List<Collection<Player>> results = new LinkedList<>(
                ordered.values());
        Collections.reverse(results);
        return Collections.unmodifiableList(results);
    }

    @Override
    protected SortedMap<Long, Collection<Player>> evaluateTournament(
            final Collection<Player> players,
            final Collection<List<Collection<Player>>> gameResults) {
        final Map<Player, Long> points = new HashMap<>();
        for (final Player p : players) {
            points.put(p, 0l);
        }
        // value each player
        for (final List<Collection<Player>> gameResult : gameResults) {
            // for each game result...
            int i = 0;
            for (final Collection<Player> playersSharingPosition : gameResult) {
                // for each player sharing the same position
                for (final Player p : playersSharingPosition) {
                    // add 2^position points
                    final long previousPoints = points.get(p);
                    points.put(p, previousPoints + Math.round(Math.pow(2, i)));
                }
                i++;
            }
        }
        // group players by their results
        final MultiMap<Long, Player> grouped = new MultiHashMap<>();
        for (final Player p : players) {
            grouped.put(points.get(p), p);
        }
        // order the grouped players
        final SortedMap<Long, Collection<Player>> ordered = new TreeMap<>();
        for (final Entry<Long, Collection<Player>> entry : grouped.entrySet()) {
            ordered.put(entry.getKey(), entry.getValue());
        }
        return Collections.unmodifiableSortedMap(ordered);
    }

}

@@ -144,24 +144,50 @@
 il.HashMap;%0A
+import java.util.HashSet;%0A
 import java.
@@ -252,36 +252,8 @@
 ap;%0A
-import java.util.Map.Entry;%0A
 impo
@@ -307,121 +307,8 @@
 p;%0A%0A
-import org.apache.commons.collections15.MultiMap;%0Aimport org.apache.commons.collections15.multimap.MultiHashMap;%0A
 impo
@@ -737,29 +737,30 @@
       final 
-Multi
+Sorted
 Map%3CBigDecim
@@ -763,23 +763,35 @@
 ecimal, 
+Collection%3C
 Player%3E
+%3E
  grouped
@@ -793,33 +793,28 @@
 ouped = new 
-MultiHash
+Tree
 Map%3C%3E();%0A   
@@ -863,28 +863,39 @@
         
-grouped.put(
+final BigDecimal key = 
 game.get
@@ -901,20 +901,16 @@
 tMedian(
-p), 
 p);%0A    
@@ -917,290 +917,150 @@
     
-%7D%0A
     
-    // order the grouped players%0A        final SortedMap%3CBigDecimal, Collection%3CPlayer%3E%3E ordered = new TreeMap%3C%3E();%0A        for (final Entry%3CBigDecimal, Collection%3CPlayer%3E%3E entry : grouped%0A                .entrySet()) %7B%0A            ordered.put(entry.getKey(), entry.getValue()
+if (!grouped.containsKey(key)) %7B%0A                grouped.put(key, new HashSet%3CPlayer%3E());%0A            %7D%0A            grouped.get(key).add(p
 );%0A 
@@ -1225,21 +1225,21 @@
         
-order
+group
 ed.value
@@ -2359,21 +2359,22 @@
   final 
-Multi
+Sorted
 Map%3CLong
@@ -2375,23 +2375,35 @@
 p%3CLong, 
+Collection%3C
 Player%3E
+%3E
  grouped
@@ -2413,17 +2413,12 @@
 new 
-MultiHash
+Tree
 Map%3C
@@ -2475,28 +2475,33 @@
         
-grouped.put(
+final long key = 
 points.g
@@ -2507,12 +2507,8 @@
 get(
-p), 
 p);%0A
@@ -2518,262 +2518,151 @@
     
- %7D%0A
      
-   // order the grouped players%0A        final SortedMap%3CLong, Collection%3CPlayer%3E%3E ordered = new TreeMap%3C%3E();%0A        for (final Entry%3CLong, Collection%3CPlayer%3E%3E entry : grouped.entrySet()) %7B%0A            ordered.put(entry.getKey(), entry.getValue()
+if (!grouped.containsKey(key)) %7B%0A                grouped.put(key, new HashSet%3CPlayer%3E());%0A            %7D%0A            grouped.get(key).add(p
 );%0A 
@@ -2719,21 +2719,21 @@
 rtedMap(
-order
+group
 ed);%0A   

Remove dependency on collections15, which was on the classpath only accidentally anyway.