Fixed list history
authorMartin Rexa <martin.rexa@centrum.cz>
Mon, 18 Jan 2010 10:29:53 +0100
changeset 222d783bb2a7956
parent 221 2d54f52b82f0
child 223 7416b11ad09f
Fixed list history
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt
statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java
statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java
     1.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt	Mon Jan 18 10:29:14 2010 +0100
     1.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/elo.fmt	Mon Jan 18 10:29:53 2010 +0100
     1.3 @@ -6,10 +6,12 @@
     1.4    </head>
     1.5    <body bgcolor="white">
     1.6        <h3><a href="/">${bundle.TITLE_PLAIN}</a></h3>
     1.7 -      <#assign first=toDate[doc.eloList.firstGame?string]/>
     1.8 -      <#assign last=toDate[doc.eloList.lastGame?string]/>
     1.9 -      <h4>${bundle.ELO_LIST} - ${last?date?string.long}</h4>
    1.10 -      <!-- New games: ${first?datetime?string.long} - ${last?datetime?string.long} -->
    1.11 +      <#assign published=toDate[doc.eloList.published?string]/>
    1.12 +      <#if now &gt; doc.eloList.published?number>
    1.13 +      <h4>${bundle.ELO_LIST} - ${published?date?string('MM/yyyy')}</h4>
    1.14 +      <#else>
    1.15 +      <h4>${bundle.ELO_LIST}</h4>
    1.16 +      </#if>
    1.17        <table border="1">
    1.18            <thead>
    1.19                <tr>
     2.1 --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java	Mon Jan 18 10:29:14 2010 +0100
     2.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/EloList.java	Mon Jan 18 10:29:53 2010 +0100
     2.3 @@ -52,8 +52,7 @@
     2.4  
     2.5      Map<String, Double> players;
     2.6      Map<String, Integer> playerGames;
     2.7 -    long firstGame = 0;
     2.8 -    long lastGame = 0;
     2.9 +    long published = 0;
    2.10      boolean originalList;
    2.11  
    2.12  
    2.13 @@ -69,6 +68,14 @@
    2.14          originalList = false;
    2.15      }
    2.16  
    2.17 +    public void setEndDate(long endDate){
    2.18 +        this.published = endDate;
    2.19 +    }
    2.20 +
    2.21 +    public long getEndDate(){
    2.22 +        return this.published;
    2.23 +    }
    2.24 +
    2.25      @XmlElementWrapper(name="elolist")
    2.26      @XmlElement(name="item")
    2.27      public List<EloEntry> getFinalList(){
    2.28 @@ -81,13 +88,6 @@
    2.29      }
    2.30  
    2.31      public EloList putResult(String winner, String looser){
    2.32 -        return putResult(winner, looser, System.currentTimeMillis());
    2.33 -    }
    2.34 -
    2.35 -    public EloList putResult(String winner, String looser, long modified){
    2.36 -        this.lastGame = modified;
    2.37 -        if(this.firstGame == 0)
    2.38 -            this.firstGame = modified;
    2.39          double wElo = getElo(winner);
    2.40          double lElo = getElo(looser);
    2.41  
     3.1 --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java	Mon Jan 18 10:29:14 2010 +0100
     3.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Elo.java	Mon Jan 18 10:29:53 2010 +0100
     3.3 @@ -35,6 +35,8 @@
     3.4  import javax.ws.rs.Produces;
     3.5  import javax.ws.rs.core.MediaType;
     3.6  import java.util.ArrayList;
     3.7 +import java.util.Calendar;
     3.8 +import java.util.GregorianCalendar;
     3.9  
    3.10  /**
    3.11   *
    3.12 @@ -43,36 +45,50 @@
    3.13  public final class Elo {
    3.14      private EloList list;
    3.15      private ArrayList<EloList> listHistory;
    3.16 -    private long now;
    3.17 -    private long current;
    3.18 +    private boolean firstGame;
    3.19      static long day = 1000 * 60 * 60 * 24;
    3.20  
    3.21      public Elo(){
    3.22          listHistory = new ArrayList<EloList>();
    3.23          list = new EloList();
    3.24          listHistory.add(list);
    3.25 -        now = System.currentTimeMillis();
    3.26 -        current = 0;
    3.27 +        firstGame = true;
    3.28      }
    3.29  
    3.30      public void processGame(GameId gId){
    3.31          GameStatus status = gId.getStatus();
    3.32 -        long modified = gId.getModified();
    3.33 -        if(current==0){
    3.34 -            current = (modified / day) * day;
    3.35 -        }else if(modified - current >= day){
    3.36 -            list = new EloList(list);
    3.37 -            listHistory.add(0,list);
    3.38 -            current = (modified / day) * day;
    3.39 +        splitList(gId.getModified());
    3.40 +        if(status.equals(GameStatus.whiteWon)){
    3.41 +            list.putResult(gId.getWhite(), gId.getBlack());
    3.42 +        }else if(status.equals(GameStatus.blackWon)){
    3.43 +            list.putResult(gId.getBlack(), gId.getWhite());
    3.44          }
    3.45 +    }
    3.46  
    3.47 -        if(status.equals(GameStatus.whiteWon)){
    3.48 -            list.putResult(gId.getWhite(), gId.getBlack(),modified);
    3.49 -        }else if(status.equals(GameStatus.blackWon)){
    3.50 -            list.putResult(gId.getBlack(), gId.getWhite(),modified);
    3.51 +    private void splitList(long gameModified){
    3.52 +        if(firstGame){
    3.53 +            firstGame = false;
    3.54 +            list.setEndDate(computeListEndDate(gameModified));
    3.55 +        }else{
    3.56 +            if(gameModified > list.getEndDate()){
    3.57 +                list = new EloList(list);
    3.58 +                listHistory.add(0,list);
    3.59 +                list.setEndDate(computeListEndDate(gameModified));
    3.60 +            }
    3.61          }
    3.62      }
    3.63  
    3.64 +    private long computeListEndDate(long modified){
    3.65 +        Calendar now = new GregorianCalendar();
    3.66 +        Calendar cModified = new GregorianCalendar();
    3.67 +        cModified.setTimeInMillis(modified);
    3.68 +        Calendar result = new GregorianCalendar();
    3.69 +        result.clear();
    3.70 +        result.set(Calendar.YEAR, cModified.get(Calendar.YEAR));
    3.71 +        result.set(Calendar.MONTH, cModified.get(Calendar.MONTH)+1);
    3.72 +        return result.getTimeInMillis()-1;
    3.73 +    }
    3.74 +
    3.75      @GET
    3.76      @Path("list")
    3.77      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })