statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 222 d783bb2a7956
permissions -rw-r--r--
Changing headers to GPLv3
jaroslav@178
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@178
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@178
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@178
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@178
    17
 */
jaroslav@178
    18
jaroslav@178
    19
package cz.xelfi.quoridor.statistics;
jaroslav@178
    20
jaroslav@178
    21
import java.util.Map;
jaroslav@178
    22
import java.util.HashMap;
jaroslav@178
    23
import java.util.List;
jaroslav@178
    24
import java.util.Collections;
jaroslav@178
    25
import java.util.ArrayList;
jaroslav@178
    26
import javax.xml.bind.annotation.XmlElement;
martin@199
    27
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@178
    28
import javax.xml.bind.annotation.XmlElementWrapper;
jaroslav@178
    29
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@178
    30
import javax.xml.bind.annotation.XmlAccessType;
jaroslav@178
    31
import javax.xml.bind.annotation.XmlAccessorType;
jaroslav@178
    32
/**
jaroslav@178
    33
 *
jaroslav@178
    34
 * @author Martin Rexa
jaroslav@178
    35
 */
jaroslav@178
    36
@XmlRootElement
jaroslav@178
    37
@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@178
    38
public class EloList {
jaroslav@178
    39
    static int INITIAL_ELO = 1250;
jaroslav@178
    40
    static int MAX_ELO_DIFF = 400;
jaroslav@178
    41
    static int KVAL_START = 25;
jaroslav@178
    42
    static int KVAL_FOLLOW = 10;
jaroslav@178
    43
    static int KVAL_LIMIT = 30;
jaroslav@178
    44
martin@199
    45
    Map<String, Double> players;
martin@199
    46
    Map<String, Integer> playerGames;
martin@222
    47
    long published = 0;
martin@211
    48
    boolean originalList;
martin@199
    49
jaroslav@178
    50
jaroslav@178
    51
    public EloList () {
jaroslav@178
    52
        players = new HashMap<String, Double>();
jaroslav@178
    53
        playerGames = new HashMap<String, Integer>();
martin@211
    54
        originalList = true;
jaroslav@178
    55
    }
jaroslav@178
    56
martin@199
    57
    public EloList(EloList src){
martin@199
    58
        this.players = new HashMap<String, Double>(src.players);
martin@199
    59
        this.playerGames = new HashMap<String, Integer>(src.playerGames);
martin@211
    60
        originalList = false;
martin@199
    61
    }
martin@199
    62
martin@222
    63
    public void setEndDate(long endDate){
martin@222
    64
        this.published = endDate;
martin@222
    65
    }
martin@222
    66
martin@222
    67
    public long getEndDate(){
martin@222
    68
        return this.published;
martin@222
    69
    }
martin@222
    70
jaroslav@178
    71
    @XmlElementWrapper(name="elolist")
jaroslav@178
    72
    @XmlElement(name="item")
jaroslav@178
    73
    public List<EloEntry> getFinalList(){
jaroslav@178
    74
        List<EloEntry> finalList = new ArrayList<EloEntry>();
jaroslav@178
    75
        for (Map.Entry<String, Double> player : players.entrySet()){
jaroslav@178
    76
            finalList.add(new EloEntry(player.getKey(), player.getValue(), playerGames.get(player.getKey())));
jaroslav@178
    77
        }
jaroslav@178
    78
        Collections.sort(finalList, EloEntry.BEST_FIRST);
jaroslav@178
    79
        return finalList;
jaroslav@178
    80
    }
jaroslav@178
    81
jaroslav@178
    82
    public EloList putResult(String winner, String looser){
jaroslav@178
    83
        double wElo = getElo(winner);
jaroslav@178
    84
        double lElo = getElo(looser);
jaroslav@178
    85
jaroslav@178
    86
        double eloDiff = wElo - lElo;
jaroslav@178
    87
        if(eloDiff < - MAX_ELO_DIFF)
jaroslav@178
    88
            eloDiff = - MAX_ELO_DIFF;
jaroslav@178
    89
        if(eloDiff > MAX_ELO_DIFF)
jaroslav@178
    90
            eloDiff = MAX_ELO_DIFF;
jaroslav@178
    91
jaroslav@178
    92
        double wDiff = Math.round(100 * getKVal(winner) * (1 - (1 / (1 + Math.pow(10, -eloDiff / MAX_ELO_DIFF)))));
jaroslav@178
    93
        wDiff /= 100.0;
jaroslav@178
    94
        double lDiff = Math.round(100 * getKVal(looser) * (0 - (1 / (1 + Math.pow(10, eloDiff / MAX_ELO_DIFF)))));
jaroslav@178
    95
        lDiff /= 100.0;
jaroslav@178
    96
jaroslav@178
    97
        players.put(winner, wElo + wDiff);
jaroslav@178
    98
        players.put(looser, lElo + lDiff);
jaroslav@178
    99
        return this;
jaroslav@178
   100
    }
jaroslav@178
   101
    
jaroslav@178
   102
    public double getElo(String player){
jaroslav@178
   103
        Double elo = players.get(player);
jaroslav@178
   104
        return (elo == null) ? INITIAL_ELO : elo;
jaroslav@178
   105
    }
jaroslav@178
   106
    
jaroslav@178
   107
    private int getKVal(String player){
jaroslav@178
   108
        Integer games = playerGames.get(player);
jaroslav@178
   109
        int g = (games == null) ? 0 : games;
jaroslav@178
   110
        // Side effect, method should not be called more times for the same game/player
jaroslav@178
   111
        playerGames.put(player, g + 1);
jaroslav@178
   112
        return g < KVAL_LIMIT ? KVAL_START : KVAL_FOLLOW;
jaroslav@178
   113
    }
jaroslav@178
   114
jaroslav@178
   115
}