statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java
author Martin Rexa <martin.rexa@centrum.cz>
Wed, 13 Jan 2010 14:35:28 +0100
changeset 211 f861f40cabc3
parent 199 e23dcb2ef35e
child 217 2e300319c9a8
permissions -rw-r--r--
List knows whether it is last list in the history
jaroslav@178
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2010 Martin Rexa
jaroslav@178
    25
 */
jaroslav@178
    26
jaroslav@178
    27
package cz.xelfi.quoridor.statistics;
jaroslav@178
    28
jaroslav@178
    29
import java.util.Map;
jaroslav@178
    30
import java.util.HashMap;
jaroslav@178
    31
import java.util.List;
jaroslav@178
    32
import java.util.Collections;
jaroslav@178
    33
import java.util.ArrayList;
jaroslav@178
    34
import javax.xml.bind.annotation.XmlElement;
martin@199
    35
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@178
    36
import javax.xml.bind.annotation.XmlElementWrapper;
jaroslav@178
    37
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@178
    38
import javax.xml.bind.annotation.XmlAccessType;
jaroslav@178
    39
import javax.xml.bind.annotation.XmlAccessorType;
jaroslav@178
    40
/**
jaroslav@178
    41
 *
jaroslav@178
    42
 * @author Martin Rexa
jaroslav@178
    43
 */
jaroslav@178
    44
@XmlRootElement
jaroslav@178
    45
@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@178
    46
public class EloList {
jaroslav@178
    47
    static int INITIAL_ELO = 1250;
jaroslav@178
    48
    static int MAX_ELO_DIFF = 400;
jaroslav@178
    49
    static int KVAL_START = 25;
jaroslav@178
    50
    static int KVAL_FOLLOW = 10;
jaroslav@178
    51
    static int KVAL_LIMIT = 30;
jaroslav@178
    52
martin@199
    53
    Map<String, Double> players;
martin@199
    54
    Map<String, Integer> playerGames;
martin@199
    55
    long modified = 0;
martin@211
    56
    boolean originalList;
martin@199
    57
jaroslav@178
    58
jaroslav@178
    59
    public EloList () {
jaroslav@178
    60
        players = new HashMap<String, Double>();
jaroslav@178
    61
        playerGames = new HashMap<String, Integer>();
martin@211
    62
        originalList = true;
jaroslav@178
    63
    }
jaroslav@178
    64
martin@199
    65
    public EloList(EloList src){
martin@199
    66
        this.players = new HashMap<String, Double>(src.players);
martin@199
    67
        this.playerGames = new HashMap<String, Integer>(src.playerGames);
martin@199
    68
        this.modified = src.modified;
martin@211
    69
        originalList = false;
martin@199
    70
    }
martin@199
    71
jaroslav@178
    72
    @XmlElementWrapper(name="elolist")
jaroslav@178
    73
    @XmlElement(name="item")
jaroslav@178
    74
    public List<EloEntry> getFinalList(){
jaroslav@178
    75
        List<EloEntry> finalList = new ArrayList<EloEntry>();
jaroslav@178
    76
        for (Map.Entry<String, Double> player : players.entrySet()){
jaroslav@178
    77
            finalList.add(new EloEntry(player.getKey(), player.getValue(), playerGames.get(player.getKey())));
jaroslav@178
    78
        }
jaroslav@178
    79
        Collections.sort(finalList, EloEntry.BEST_FIRST);
jaroslav@178
    80
        return finalList;
jaroslav@178
    81
    }
jaroslav@178
    82
jaroslav@178
    83
    public EloList putResult(String winner, String looser){
martin@199
    84
        return putResult(winner, looser, System.currentTimeMillis());
martin@199
    85
    }
martin@199
    86
martin@199
    87
    public EloList putResult(String winner, String looser, long modified){
martin@199
    88
        this.modified = modified;
jaroslav@178
    89
        double wElo = getElo(winner);
jaroslav@178
    90
        double lElo = getElo(looser);
jaroslav@178
    91
jaroslav@178
    92
        double eloDiff = wElo - lElo;
jaroslav@178
    93
        if(eloDiff < - MAX_ELO_DIFF)
jaroslav@178
    94
            eloDiff = - MAX_ELO_DIFF;
jaroslav@178
    95
        if(eloDiff > MAX_ELO_DIFF)
jaroslav@178
    96
            eloDiff = MAX_ELO_DIFF;
jaroslav@178
    97
jaroslav@178
    98
        double wDiff = Math.round(100 * getKVal(winner) * (1 - (1 / (1 + Math.pow(10, -eloDiff / MAX_ELO_DIFF)))));
jaroslav@178
    99
        wDiff /= 100.0;
jaroslav@178
   100
        double lDiff = Math.round(100 * getKVal(looser) * (0 - (1 / (1 + Math.pow(10, eloDiff / MAX_ELO_DIFF)))));
jaroslav@178
   101
        lDiff /= 100.0;
jaroslav@178
   102
jaroslav@178
   103
        players.put(winner, wElo + wDiff);
jaroslav@178
   104
        players.put(looser, lElo + lDiff);
jaroslav@178
   105
        return this;
jaroslav@178
   106
    }
jaroslav@178
   107
    
jaroslav@178
   108
    public double getElo(String player){
jaroslav@178
   109
        Double elo = players.get(player);
jaroslav@178
   110
        return (elo == null) ? INITIAL_ELO : elo;
jaroslav@178
   111
    }
jaroslav@178
   112
    
jaroslav@178
   113
    private int getKVal(String player){
jaroslav@178
   114
        Integer games = playerGames.get(player);
jaroslav@178
   115
        int g = (games == null) ? 0 : games;
jaroslav@178
   116
        // Side effect, method should not be called more times for the same game/player
jaroslav@178
   117
        playerGames.put(player, g + 1);
jaroslav@178
   118
        return g < KVAL_LIMIT ? KVAL_START : KVAL_FOLLOW;
jaroslav@178
   119
    }
jaroslav@178
   120
jaroslav@178
   121
}