statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.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
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.statistics.resources;
    20 
    21 import cz.xelfi.quoridor.statistics.EloList;
    22 import cz.xelfi.quoridor.webidor.GameId;
    23 import cz.xelfi.quoridor.webidor.GameStatus;
    24 import javax.ws.rs.GET;
    25 import javax.ws.rs.Path;
    26 import javax.ws.rs.PathParam;
    27 import javax.ws.rs.Produces;
    28 import javax.ws.rs.core.MediaType;
    29 import java.util.ArrayList;
    30 import java.util.Calendar;
    31 import java.util.GregorianCalendar;
    32 
    33 /**
    34  *
    35  * @author Martin Rexa
    36  */
    37 public final class Elo {
    38     private EloList list;
    39     private ArrayList<EloList> listHistory;
    40     private boolean firstGame;
    41     static long day = 1000 * 60 * 60 * 24;
    42 
    43     public Elo(){
    44         listHistory = new ArrayList<EloList>();
    45         list = new EloList();
    46         listHistory.add(list);
    47         firstGame = true;
    48     }
    49 
    50     public void processGame(GameId gId){
    51         GameStatus status = gId.getStatus();
    52         splitList(gId.getModified());
    53         if(status.equals(GameStatus.whiteWon)){
    54             list.putResult(gId.getWhite(), gId.getBlack());
    55         }else if(status.equals(GameStatus.blackWon)){
    56             list.putResult(gId.getBlack(), gId.getWhite());
    57         }
    58     }
    59 
    60     private void splitList(long gameModified){
    61         if(firstGame){
    62             firstGame = false;
    63             list.setEndDate(computeListEndDate(gameModified));
    64         }else{
    65             if(gameModified > list.getEndDate()){
    66                 list = new EloList(list);
    67                 listHistory.add(0,list);
    68                 list.setEndDate(computeListEndDate(gameModified));
    69             }
    70         }
    71     }
    72 
    73     private long computeListEndDate(long modified){
    74         Calendar now = new GregorianCalendar();
    75         Calendar cModified = new GregorianCalendar();
    76         cModified.setTimeInMillis(modified);
    77         Calendar result = new GregorianCalendar();
    78         result.clear();
    79         result.set(Calendar.YEAR, cModified.get(Calendar.YEAR));
    80         result.set(Calendar.MONTH, cModified.get(Calendar.MONTH)+1);
    81         return result.getTimeInMillis()-1;
    82     }
    83 
    84     @GET
    85     @Path("list")
    86     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    87     public EloList getList(){
    88         return list;
    89     }
    90 
    91     @GET
    92     @Path("list/{id}")
    93     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    94     public EloList getHistoryList(@PathParam("id") Integer id){
    95         if(id == null)
    96             return null;
    97         if(id >= listHistory.size())
    98             return null;
    99         return listHistory.get(id);
   100     }
   101 
   102     @GET
   103     @Path("{username}")
   104     @Produces(MediaType.TEXT_PLAIN)
   105     public String getElo(@PathParam("username") String id){
   106         list.getElo(id);
   107         return Double.toString(list.getElo(id));
   108     }
   109 
   110 }