1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java Thu Jan 07 22:34:17 2010 +0100
1.3 @@ -0,0 +1,104 @@
1.4 +/*
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * The contents of this file are subject to the terms of either the GNU
1.8 + * General Public License Version 2 only ("GPL") or the Common
1.9 + * Development and Distribution License("CDDL") (collectively, the
1.10 + * "License"). You may not use this file except in compliance with the
1.11 + * License. You can obtain a copy of the License at
1.12 + * http://www.netbeans.org/cddl-gplv2.html
1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.14 + * specific language governing permissions and limitations under the
1.15 + * License. When distributing the software, include this License Header
1.16 + * Notice in each file and include the License file at
1.17 + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.18 + * particular file as subject to the "Classpath" exception as provided
1.19 + * by Sun in the GPL Version 2 section of the License file that
1.20 + * accompanied this code. If applicable, add the following below the
1.21 + * License Header, with the fields enclosed by brackets [] replaced by
1.22 + * your own identifying information:
1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
1.24 + *
1.25 + * Contributor(s):
1.26 + *
1.27 + * Portions Copyrighted 2010 Martin Rexa
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.statistics;
1.31 +
1.32 +import java.util.Map;
1.33 +import java.util.HashMap;
1.34 +import java.util.List;
1.35 +import java.util.Collections;
1.36 +import java.util.ArrayList;
1.37 +import javax.xml.bind.annotation.XmlElement;
1.38 +import javax.xml.bind.annotation.XmlElementWrapper;
1.39 +import javax.xml.bind.annotation.XmlRootElement;
1.40 +import javax.xml.bind.annotation.XmlAccessType;
1.41 +import javax.xml.bind.annotation.XmlAccessorType;
1.42 +/**
1.43 + *
1.44 + * @author Martin Rexa
1.45 + */
1.46 +@XmlRootElement
1.47 +@XmlAccessorType(XmlAccessType.FIELD)
1.48 +public class EloList {
1.49 + static int INITIAL_ELO = 1250;
1.50 + static int MAX_ELO_DIFF = 400;
1.51 + static int KVAL_START = 25;
1.52 + static int KVAL_FOLLOW = 10;
1.53 + static int KVAL_LIMIT = 30;
1.54 +
1.55 + private Map<String, Double> players;
1.56 + private Map<String, Integer> playerGames;
1.57 +
1.58 + public EloList () {
1.59 + players = new HashMap<String, Double>();
1.60 + playerGames = new HashMap<String, Integer>();
1.61 + }
1.62 +
1.63 + @XmlElementWrapper(name="elolist")
1.64 + @XmlElement(name="item")
1.65 + public List<EloEntry> getFinalList(){
1.66 + List<EloEntry> finalList = new ArrayList<EloEntry>();
1.67 + for (Map.Entry<String, Double> player : players.entrySet()){
1.68 + finalList.add(new EloEntry(player.getKey(), player.getValue(), playerGames.get(player.getKey())));
1.69 + }
1.70 + Collections.sort(finalList, EloEntry.BEST_FIRST);
1.71 + return finalList;
1.72 + }
1.73 +
1.74 + public EloList putResult(String winner, String looser){
1.75 + double wElo = getElo(winner);
1.76 + double lElo = getElo(looser);
1.77 +
1.78 + double eloDiff = wElo - lElo;
1.79 + if(eloDiff < - MAX_ELO_DIFF)
1.80 + eloDiff = - MAX_ELO_DIFF;
1.81 + if(eloDiff > MAX_ELO_DIFF)
1.82 + eloDiff = MAX_ELO_DIFF;
1.83 +
1.84 + double wDiff = Math.round(100 * getKVal(winner) * (1 - (1 / (1 + Math.pow(10, -eloDiff / MAX_ELO_DIFF)))));
1.85 + wDiff /= 100.0;
1.86 + double lDiff = Math.round(100 * getKVal(looser) * (0 - (1 / (1 + Math.pow(10, eloDiff / MAX_ELO_DIFF)))));
1.87 + lDiff /= 100.0;
1.88 +
1.89 + players.put(winner, wElo + wDiff);
1.90 + players.put(looser, lElo + lDiff);
1.91 + return this;
1.92 + }
1.93 +
1.94 + public double getElo(String player){
1.95 + Double elo = players.get(player);
1.96 + return (elo == null) ? INITIAL_ELO : elo;
1.97 + }
1.98 +
1.99 + private int getKVal(String player){
1.100 + Integer games = playerGames.get(player);
1.101 + int g = (games == null) ? 0 : games;
1.102 + // Side effect, method should not be called more times for the same game/player
1.103 + playerGames.put(player, g + 1);
1.104 + return g < KVAL_LIMIT ? KVAL_START : KVAL_FOLLOW;
1.105 + }
1.106 +
1.107 +}