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
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.resources;
jaroslav@178
    20
jaroslav@178
    21
import cz.xelfi.quoridor.statistics.EloList;
jaroslav@178
    22
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@178
    23
import cz.xelfi.quoridor.webidor.GameStatus;
jaroslav@178
    24
import javax.ws.rs.GET;
jaroslav@178
    25
import javax.ws.rs.Path;
jaroslav@178
    26
import javax.ws.rs.PathParam;
jaroslav@178
    27
import javax.ws.rs.Produces;
jaroslav@178
    28
import javax.ws.rs.core.MediaType;
martin@201
    29
import java.util.ArrayList;
martin@222
    30
import java.util.Calendar;
martin@222
    31
import java.util.GregorianCalendar;
jaroslav@178
    32
jaroslav@178
    33
/**
jaroslav@178
    34
 *
jaroslav@178
    35
 * @author Martin Rexa
jaroslav@178
    36
 */
jaroslav@178
    37
public final class Elo {
jaroslav@178
    38
    private EloList list;
martin@201
    39
    private ArrayList<EloList> listHistory;
martin@222
    40
    private boolean firstGame;
martin@201
    41
    static long day = 1000 * 60 * 60 * 24;
jaroslav@178
    42
jaroslav@178
    43
    public Elo(){
martin@201
    44
        listHistory = new ArrayList<EloList>();
jaroslav@178
    45
        list = new EloList();
martin@201
    46
        listHistory.add(list);
martin@222
    47
        firstGame = true;
jaroslav@178
    48
    }
jaroslav@178
    49
martin@201
    50
    public void processGame(GameId gId){
jaroslav@178
    51
        GameStatus status = gId.getStatus();
martin@222
    52
        splitList(gId.getModified());
martin@222
    53
        if(status.equals(GameStatus.whiteWon)){
martin@222
    54
            list.putResult(gId.getWhite(), gId.getBlack());
martin@222
    55
        }else if(status.equals(GameStatus.blackWon)){
martin@222
    56
            list.putResult(gId.getBlack(), gId.getWhite());
martin@201
    57
        }
martin@222
    58
    }
martin@201
    59
martin@222
    60
    private void splitList(long gameModified){
martin@222
    61
        if(firstGame){
martin@222
    62
            firstGame = false;
martin@222
    63
            list.setEndDate(computeListEndDate(gameModified));
martin@222
    64
        }else{
martin@222
    65
            if(gameModified > list.getEndDate()){
martin@222
    66
                list = new EloList(list);
martin@222
    67
                listHistory.add(0,list);
martin@222
    68
                list.setEndDate(computeListEndDate(gameModified));
martin@222
    69
            }
jaroslav@178
    70
        }
jaroslav@178
    71
    }
jaroslav@178
    72
martin@222
    73
    private long computeListEndDate(long modified){
martin@222
    74
        Calendar now = new GregorianCalendar();
martin@222
    75
        Calendar cModified = new GregorianCalendar();
martin@222
    76
        cModified.setTimeInMillis(modified);
martin@222
    77
        Calendar result = new GregorianCalendar();
martin@222
    78
        result.clear();
martin@222
    79
        result.set(Calendar.YEAR, cModified.get(Calendar.YEAR));
martin@222
    80
        result.set(Calendar.MONTH, cModified.get(Calendar.MONTH)+1);
martin@222
    81
        return result.getTimeInMillis()-1;
martin@222
    82
    }
martin@222
    83
jaroslav@178
    84
    @GET
jaroslav@178
    85
    @Path("list")
jaroslav@178
    86
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@178
    87
    public EloList getList(){
jaroslav@178
    88
        return list;
jaroslav@178
    89
    }
jaroslav@178
    90
jaroslav@178
    91
    @GET
martin@201
    92
    @Path("list/{id}")
martin@201
    93
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
martin@201
    94
    public EloList getHistoryList(@PathParam("id") Integer id){
martin@210
    95
        if(id == null)
martin@210
    96
            return null;
martin@210
    97
        if(id >= listHistory.size())
martin@210
    98
            return null;
martin@201
    99
        return listHistory.get(id);
martin@201
   100
    }
martin@201
   101
martin@201
   102
    @GET
jaroslav@178
   103
    @Path("{username}")
jaroslav@178
   104
    @Produces(MediaType.TEXT_PLAIN)
jaroslav@178
   105
    public String getElo(@PathParam("username") String id){
jaroslav@178
   106
        list.getElo(id);
jaroslav@178
   107
        return Double.toString(list.getElo(id));
jaroslav@178
   108
    }
jaroslav@178
   109
jaroslav@178
   110
}