1.1 --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java Wed Jan 13 16:57:00 2010 +0100
1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java Mon Jan 18 10:29:53 2010 +0100
1.3 @@ -35,6 +35,8 @@
1.4 import javax.ws.rs.Produces;
1.5 import javax.ws.rs.core.MediaType;
1.6 import java.util.ArrayList;
1.7 +import java.util.Calendar;
1.8 +import java.util.GregorianCalendar;
1.9
1.10 /**
1.11 *
1.12 @@ -43,36 +45,50 @@
1.13 public final class Elo {
1.14 private EloList list;
1.15 private ArrayList<EloList> listHistory;
1.16 - private long now;
1.17 - private long current;
1.18 + private boolean firstGame;
1.19 static long day = 1000 * 60 * 60 * 24;
1.20
1.21 public Elo(){
1.22 listHistory = new ArrayList<EloList>();
1.23 list = new EloList();
1.24 listHistory.add(list);
1.25 - now = System.currentTimeMillis();
1.26 - current = 0;
1.27 + firstGame = true;
1.28 }
1.29
1.30 public void processGame(GameId gId){
1.31 GameStatus status = gId.getStatus();
1.32 - long modified = gId.getModified();
1.33 - if(current==0){
1.34 - current = (modified / day) * day;
1.35 - }else if(modified - current >= day){
1.36 - list = new EloList(list);
1.37 - listHistory.add(0,list);
1.38 - current = (modified / day) * day;
1.39 + splitList(gId.getModified());
1.40 + if(status.equals(GameStatus.whiteWon)){
1.41 + list.putResult(gId.getWhite(), gId.getBlack());
1.42 + }else if(status.equals(GameStatus.blackWon)){
1.43 + list.putResult(gId.getBlack(), gId.getWhite());
1.44 }
1.45 + }
1.46
1.47 - if(status.equals(GameStatus.whiteWon)){
1.48 - list.putResult(gId.getWhite(), gId.getBlack(),modified);
1.49 - }else if(status.equals(GameStatus.blackWon)){
1.50 - list.putResult(gId.getBlack(), gId.getWhite(),modified);
1.51 + private void splitList(long gameModified){
1.52 + if(firstGame){
1.53 + firstGame = false;
1.54 + list.setEndDate(computeListEndDate(gameModified));
1.55 + }else{
1.56 + if(gameModified > list.getEndDate()){
1.57 + list = new EloList(list);
1.58 + listHistory.add(0,list);
1.59 + list.setEndDate(computeListEndDate(gameModified));
1.60 + }
1.61 }
1.62 }
1.63
1.64 + private long computeListEndDate(long modified){
1.65 + Calendar now = new GregorianCalendar();
1.66 + Calendar cModified = new GregorianCalendar();
1.67 + cModified.setTimeInMillis(modified);
1.68 + Calendar result = new GregorianCalendar();
1.69 + result.clear();
1.70 + result.set(Calendar.YEAR, cModified.get(Calendar.YEAR));
1.71 + result.set(Calendar.MONTH, cModified.get(Calendar.MONTH)+1);
1.72 + return result.getTimeInMillis()-1;
1.73 + }
1.74 +
1.75 @GET
1.76 @Path("list")
1.77 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })