freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
branchstatistics-and-elo
changeset 178 4b78d4f028b3
parent 172 3de0568f7ee8
child 179 c5fbddc4c590
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Wed Dec 23 20:45:19 2009 +0100
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Thu Jan 07 22:34:17 2010 +0100
     1.3 @@ -87,6 +87,7 @@
     1.4          version = p.getProperty("version", "unknown"); // NOI18N
     1.5      }
     1.6      private static WebResource base;
     1.7 +    private static WebResource stat;
     1.8  
     1.9      @Context
    1.10      private HttpHeaders headers;
    1.11 @@ -163,6 +164,7 @@
    1.12          }
    1.13          String txt = wr.accept(MediaType.TEXT_PLAIN).get(String.class);
    1.14          Board b = Board.valueOf(txt);
    1.15 +//        Board b = new Board(txt);
    1.16          ResponseBuilder resp = Response.ok();
    1.17          CacheControl cc = new CacheControl();
    1.18          cc.setNoCache(true);
    1.19 @@ -220,13 +222,25 @@
    1.20  
    1.21          Document doc = url.accept(MediaType.TEXT_XML).get(Document.class);
    1.22          Board b;
    1.23 +        String t = doc.getElementsByTagName("board").item(0).getTextContent();
    1.24          try {
    1.25              b = Board.valueOf(doc.getElementsByTagName("board").item(0).getTextContent());
    1.26 -        } catch (IllegalPositionException ex) {
    1.27 +        } catch (Exception ex) {
    1.28 +//            b = new Board(t);
    1.29 +//        } catch (IllegalStateException ex) {
    1.30              Exceptions.printStackTrace(ex);
    1.31              b = Board.empty();
    1.32          }
    1.33 -        v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b);
    1.34 +        String bCode = null;
    1.35 +        try{
    1.36 +            bCode = stat.path("openings").path(Board.board2HashCode(b)+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class);
    1.37 +        }catch(Exception e){
    1.38 +            bCode = null;
    1.39 +        }
    1.40 +        if(bCode == null || "".equals(bCode))
    1.41 +            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture());
    1.42 +        else
    1.43 +            v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture(),"bCode", bCode);
    1.44          return resp.entity(v).build();
    1.45      }
    1.46  
    1.47 @@ -346,7 +360,72 @@
    1.48  
    1.49          return welcome(10);
    1.50      }
    1.51 +
    1.52 +    @GET
    1.53 +    @Path("elo")
    1.54 +    @Produces(MediaType.TEXT_HTML)
    1.55 +    public Response getEloList(){
    1.56 +        Viewable v = checkLogin();
    1.57 +        if (v != null) {
    1.58 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    1.59 +        }
    1.60 +        final Document got = stat.path("elo").path("list").accept(MediaType.TEXT_XML).get(Document.class);
    1.61 +        return Response.ok(viewable("elo.fmt", got)).build();
    1.62 +    }
    1.63      
    1.64 +    @GET
    1.65 +    @Path("openings")
    1.66 +    @Produces(MediaType.TEXT_HTML)
    1.67 +    public Response getOpeningRoot(){
    1.68 +        return getOpeningNode("ROOT");
    1.69 +    }
    1.70 +
    1.71 +    @GET
    1.72 +    @Path("openings/{code}")
    1.73 +    @Produces(MediaType.TEXT_HTML)
    1.74 +    public Response getOpeningNode(@PathParam("code") String code){
    1.75 +        Viewable v = checkLogin();
    1.76 +        if (v != null) {
    1.77 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    1.78 +        }
    1.79 +        final Document got = stat.path("openings").path(code).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
    1.80 +        Board b;
    1.81 +        try {
    1.82 +            b = Board.valueOf(got.getElementsByTagName("nodeCode").item(0).getTextContent());
    1.83 +        } catch (Exception ex) {
    1.84 +            Exceptions.printStackTrace(ex);
    1.85 +            b = Board.empty();
    1.86 +        }
    1.87 +        return Response.ok(viewable("openings.fmt", got, "whitefences",b.getPlayers().get(0).getFences(),"blackfences",b.getPlayers().get(1).getFences())).build();
    1.88 +    }
    1.89 +
    1.90 +    @GET
    1.91 +    @Path("openings/{code}/{status}")
    1.92 +    @Produces(MediaType.TEXT_HTML)
    1.93 +    public Response getOpeningNodeGames(@PathParam("code") String code, @PathParam("status") String status){
    1.94 +        Viewable v = checkLogin();
    1.95 +        if (v != null) {
    1.96 +            return Response.status(Response.Status.FORBIDDEN).entity(v).build();
    1.97 +        }
    1.98 +        final Document got = stat.path("openings").path(code).path(status).queryParam("loginID", user.getId()).accept(MediaType.TEXT_XML).get(Document.class);
    1.99 +        return Response.ok(viewable("opening_games.fmt", got,"code",code,"color",status)).build();
   1.100 +    }
   1.101 +
   1.102 +    @GET
   1.103 +    @Path("openings/{code}.png")
   1.104 +    @Produces("image/png")
   1.105 +    public Response getOpeningBoardImage(
   1.106 +        @PathParam("code") String code,
   1.107 +        @QueryParam("fieldSize") @DefaultValue("40") int fieldSize
   1.108 +    ) throws IllegalPositionException {
   1.109 +        Board b = new Board(code);
   1.110 +        ResponseBuilder resp = Response.ok();
   1.111 +        CacheControl cc = new CacheControl();
   1.112 +        cc.setNoCache(true);
   1.113 +        resp.cacheControl(cc);
   1.114 +        return resp.entity(BoardImage.draw(b, fieldSize)).build();
   1.115 +    }
   1.116 +
   1.117      //
   1.118      // start the server
   1.119      //
   1.120 @@ -376,12 +455,14 @@
   1.121  
   1.122      static Callable<Void> startServers(int port, String remoteAPI) throws Exception {
   1.123          Client client = new Client();
   1.124 +        Client client1 = new Client();
   1.125  
   1.126          final HttpServer apiServer;
   1.127          if (remoteAPI == null) {
   1.128              throw new IllegalArgumentException("Provide URL to API server"); // NOI18N
   1.129          } else {
   1.130              base = client.resource(new URI(remoteAPI));
   1.131 +            stat = client1.resource(new URI("http://localhost:9444"));
   1.132              apiServer = null;
   1.133          }
   1.134