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