# HG changeset patch # User Martin Rexa # Date 1263806993 -3600 # Node ID d783bb2a795696b7d9c9259d76096d29685a0167 # Parent 2d54f52b82f068df41fb477d03a53cfabae033de Fixed list history diff -r 2d54f52b82f0 -r d783bb2a7956 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt Mon Jan 18 10:29:14 2010 +0100 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt Mon Jan 18 10:29:53 2010 +0100 @@ -6,10 +6,12 @@

${bundle.TITLE_PLAIN}

- <#assign first=toDate[doc.eloList.firstGame?string]/> - <#assign last=toDate[doc.eloList.lastGame?string]/> -

${bundle.ELO_LIST} - ${last?date?string.long}

- + <#assign published=toDate[doc.eloList.published?string]/> + <#if now > doc.eloList.published?number> +

${bundle.ELO_LIST} - ${published?date?string('MM/yyyy')}

+ <#else> +

${bundle.ELO_LIST}

+ diff -r 2d54f52b82f0 -r d783bb2a7956 statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java Mon Jan 18 10:29:14 2010 +0100 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java Mon Jan 18 10:29:53 2010 +0100 @@ -52,8 +52,7 @@ Map players; Map playerGames; - long firstGame = 0; - long lastGame = 0; + long published = 0; boolean originalList; @@ -69,6 +68,14 @@ originalList = false; } + public void setEndDate(long endDate){ + this.published = endDate; + } + + public long getEndDate(){ + return this.published; + } + @XmlElementWrapper(name="elolist") @XmlElement(name="item") public List getFinalList(){ @@ -81,13 +88,6 @@ } public EloList putResult(String winner, String looser){ - return putResult(winner, looser, System.currentTimeMillis()); - } - - public EloList putResult(String winner, String looser, long modified){ - this.lastGame = modified; - if(this.firstGame == 0) - this.firstGame = modified; double wElo = getElo(winner); double lElo = getElo(looser); diff -r 2d54f52b82f0 -r d783bb2a7956 statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java Mon Jan 18 10:29:14 2010 +0100 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java Mon Jan 18 10:29:53 2010 +0100 @@ -35,6 +35,8 @@ import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; /** * @@ -43,36 +45,50 @@ public final class Elo { private EloList list; private ArrayList listHistory; - private long now; - private long current; + private boolean firstGame; static long day = 1000 * 60 * 60 * 24; public Elo(){ listHistory = new ArrayList(); list = new EloList(); listHistory.add(list); - now = System.currentTimeMillis(); - current = 0; + firstGame = true; } public void processGame(GameId gId){ GameStatus status = gId.getStatus(); - long modified = gId.getModified(); - if(current==0){ - current = (modified / day) * day; - }else if(modified - current >= day){ - list = new EloList(list); - listHistory.add(0,list); - current = (modified / day) * day; + splitList(gId.getModified()); + if(status.equals(GameStatus.whiteWon)){ + list.putResult(gId.getWhite(), gId.getBlack()); + }else if(status.equals(GameStatus.blackWon)){ + list.putResult(gId.getBlack(), gId.getWhite()); } + } - if(status.equals(GameStatus.whiteWon)){ - list.putResult(gId.getWhite(), gId.getBlack(),modified); - }else if(status.equals(GameStatus.blackWon)){ - list.putResult(gId.getBlack(), gId.getWhite(),modified); + private void splitList(long gameModified){ + if(firstGame){ + firstGame = false; + list.setEndDate(computeListEndDate(gameModified)); + }else{ + if(gameModified > list.getEndDate()){ + list = new EloList(list); + listHistory.add(0,list); + list.setEndDate(computeListEndDate(gameModified)); + } } } + private long computeListEndDate(long modified){ + Calendar now = new GregorianCalendar(); + Calendar cModified = new GregorianCalendar(); + cModified.setTimeInMillis(modified); + Calendar result = new GregorianCalendar(); + result.clear(); + result.set(Calendar.YEAR, cModified.get(Calendar.YEAR)); + result.set(Calendar.MONTH, cModified.get(Calendar.MONTH)+1); + return result.getTimeInMillis()-1; + } + @GET @Path("list") @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })