statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Statistics.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 187 ad48f5c7dfef
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2010 Martin Rexa
    25  */
    26 
    27 package cz.xelfi.quoridor.statistics.resources;
    28 
    29 import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    30 import com.sun.jersey.api.core.PackagesResourceConfig;
    31 import com.sun.jersey.api.core.ResourceConfig;
    32 import com.sun.jersey.spi.resource.Singleton;
    33 import com.sun.net.httpserver.HttpServer;
    34 import java.io.File;
    35 import java.io.IOException;
    36 import java.util.Comparator;
    37 import java.util.List;
    38 import java.util.Collections;
    39 import java.net.ServerSocket;
    40 import javax.ws.rs.Path;
    41 import cz.xelfi.quoridor.webidor.resources.Games;
    42 import cz.xelfi.quoridor.webidor.Game;
    43 
    44 /**
    45  *
    46  * @author Martin Rexa
    47  */
    48 @Path("/")
    49 @Singleton
    50 public class Statistics {
    51     static final Comparator<Game> OLDEST_FIRST = new OldestFirst();
    52     static Elo elo;
    53     static Openings openings;
    54 
    55     @Path("elo")
    56     public Elo getElo() {
    57         return elo;
    58     }
    59 
    60     @Path("openings")
    61     public Openings getOpenings() {
    62         return openings;
    63     }
    64 
    65     public static void main( String[] args ) throws IOException, InterruptedException
    66     {
    67         int port = 9444;
    68         // timeout between reprocessing games in miliseconds
    69 //        long timeout = 1000 * 60 * 60;
    70         long timeout = 1000 * 10;
    71 
    72         try {
    73             port = Integer.parseInt(args[0]);
    74         } catch (Exception ex) {
    75             // OK
    76         }
    77 
    78         if (System.getProperty("quoridor.dir") == null) {
    79             File home = new File(System.getProperty("user.home"));
    80             File quoridor = new File(home, ".quoridor");
    81             System.setProperty("quoridor.dir", quoridor.getPath());
    82         }
    83 
    84         processGames();
    85         HttpServer s = start(port);
    86         System.out.println("Statistics started at port " + port);
    87         Object monitor = new Object();
    88         for(;;){
    89             synchronized (monitor) {
    90                 monitor.wait(timeout);
    91             }
    92             processGames();
    93         }
    94     }
    95 
    96     public static HttpServer start(int port) throws IOException {
    97         if (port == -1) {
    98             ServerSocket ss = new ServerSocket(0);
    99             port =ss.getLocalPort();
   100             ss.close();
   101         }
   102         final String baseUri = "http://localhost:" + port + "/";
   103 
   104         if (System.getProperty("quoridor.dir") == null) {
   105             File home = new File(System.getProperty("user.home"));
   106             File quoridor = new File(home, ".quoridor");
   107             System.setProperty("quoridor.dir", quoridor.getPath());
   108         }
   109 
   110         ResourceConfig rc = new PackagesResourceConfig("cz.xelfi.quoridor.statistics");
   111         HttpServer server = HttpServerFactory.create(baseUri, rc);
   112         server.start();
   113         return server;
   114     }
   115 
   116     public static void processGames(){
   117         Elo eloTmp = new Elo();
   118         Openings openingsTmp = new Openings();
   119 
   120         final String prop = System.getProperty("quoridor.dir"); // NOI18N
   121         if (prop == null) {
   122             throw new IllegalStateException("quoridor.dir property must be specified"); // NOI18N
   123         }
   124         File path = new File(prop);
   125         path.mkdirs();
   126         File fGames = new File(path, "games");
   127         //fGames.mkdirs();
   128         Games games = new Games(fGames, null);
   129         List<Game> lGames = games.getGames();
   130         Collections.sort(lGames, OLDEST_FIRST);
   131 
   132         // process games
   133         for(Game g: lGames){
   134             if(g.getId().isFinished()){
   135                 eloTmp.processGame(g);
   136                 openingsTmp.processGame(g);
   137             }
   138         }
   139 
   140         elo = eloTmp;
   141         openings = openingsTmp;
   142     }
   143 
   144     private static final class OldestFirst implements Comparator<Game> {
   145         public int compare(Game g1, Game g2) {
   146             if(g1.getId() == g2.getId())
   147                 return 0;
   148             long diff = g2.getId().getModified() - g1.getId().getModified();
   149             if (diff != 0) {
   150                 return diff < 0 ? -1 : 1;
   151             }
   152             return g1.getId().getId().compareTo(g2.getId().getId());
   153         }
   154     }
   155 
   156 }