statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java
author Martin Rexa <martin.rexa@centrum.cz>
Wed, 13 Jan 2010 16:57:00 +0100
changeset 216 17893fd11f6e
parent 210 92d4dbc49d7e
child 222 d783bb2a7956
permissions -rw-r--r--
each new elo list should cover games from one day
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.resources;
jaroslav@178
    28
jaroslav@178
    29
import cz.xelfi.quoridor.statistics.EloList;
jaroslav@178
    30
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@178
    31
import cz.xelfi.quoridor.webidor.GameStatus;
jaroslav@178
    32
import javax.ws.rs.GET;
jaroslav@178
    33
import javax.ws.rs.Path;
jaroslav@178
    34
import javax.ws.rs.PathParam;
jaroslav@178
    35
import javax.ws.rs.Produces;
jaroslav@178
    36
import javax.ws.rs.core.MediaType;
martin@201
    37
import java.util.ArrayList;
jaroslav@178
    38
jaroslav@178
    39
/**
jaroslav@178
    40
 *
jaroslav@178
    41
 * @author Martin Rexa
jaroslav@178
    42
 */
jaroslav@178
    43
public final class Elo {
jaroslav@178
    44
    private EloList list;
martin@201
    45
    private ArrayList<EloList> listHistory;
martin@201
    46
    private long now;
martin@201
    47
    private long current;
martin@201
    48
    static long day = 1000 * 60 * 60 * 24;
jaroslav@178
    49
jaroslav@178
    50
    public Elo(){
martin@201
    51
        listHistory = new ArrayList<EloList>();
jaroslav@178
    52
        list = new EloList();
martin@201
    53
        listHistory.add(list);
martin@201
    54
        now = System.currentTimeMillis();
martin@201
    55
        current = 0;
jaroslav@178
    56
    }
jaroslav@178
    57
martin@201
    58
    public void processGame(GameId gId){
jaroslav@178
    59
        GameStatus status = gId.getStatus();
martin@201
    60
        long modified = gId.getModified();
martin@201
    61
        if(current==0){
martin@210
    62
            current = (modified / day) * day;
martin@216
    63
        }else if(modified - current >= day){
martin@201
    64
            list = new EloList(list);
martin@201
    65
            listHistory.add(0,list);
martin@210
    66
            current = (modified / day) * day;
martin@201
    67
        }
martin@201
    68
jaroslav@178
    69
        if(status.equals(GameStatus.whiteWon)){
martin@201
    70
            list.putResult(gId.getWhite(), gId.getBlack(),modified);
jaroslav@178
    71
        }else if(status.equals(GameStatus.blackWon)){
martin@201
    72
            list.putResult(gId.getBlack(), gId.getWhite(),modified);
jaroslav@178
    73
        }
jaroslav@178
    74
    }
jaroslav@178
    75
jaroslav@178
    76
    @GET
jaroslav@178
    77
    @Path("list")
jaroslav@178
    78
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@178
    79
    public EloList getList(){
jaroslav@178
    80
        return list;
jaroslav@178
    81
    }
jaroslav@178
    82
jaroslav@178
    83
    @GET
martin@201
    84
    @Path("list/{id}")
martin@201
    85
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
martin@201
    86
    public EloList getHistoryList(@PathParam("id") Integer id){
martin@210
    87
        if(id == null)
martin@210
    88
            return null;
martin@210
    89
        if(id >= listHistory.size())
martin@210
    90
            return null;
martin@201
    91
        return listHistory.get(id);
martin@201
    92
    }
martin@201
    93
martin@201
    94
    @GET
jaroslav@178
    95
    @Path("{username}")
jaroslav@178
    96
    @Produces(MediaType.TEXT_PLAIN)
jaroslav@178
    97
    public String getElo(@PathParam("username") String id){
jaroslav@178
    98
        list.getElo(id);
jaroslav@178
    99
        return Double.toString(list.getElo(id));
jaroslav@178
   100
    }
jaroslav@178
   101
jaroslav@178
   102
}