freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
changeset 56 7c3794f5baa1
parent 55 830e0ba29c04
child 57 fa12b02023a0
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 04 18:01:01 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 04 20:26:48 2009 +0200
     1.3 @@ -27,6 +27,7 @@
     1.4  package cz.xelfi.quoridor.freemarkerdor;
     1.5  
     1.6  import com.sun.jersey.api.client.Client;
     1.7 +import com.sun.jersey.api.client.UniformInterfaceException;
     1.8  import com.sun.jersey.api.client.WebResource;
     1.9  import com.sun.jersey.api.container.httpserver.HttpServerFactory;
    1.10  import com.sun.jersey.api.core.PackagesResourceConfig;
    1.11 @@ -77,7 +78,7 @@
    1.12              user = headers.getCookies().get("login").getValue();
    1.13              return null;
    1.14          }
    1.15 -        return new Viewable("login.fmt", null);
    1.16 +        return viewable("login.fmt", null);
    1.17      }
    1.18  
    1.19      @POST
    1.20 @@ -96,7 +97,7 @@
    1.21          if (name != null && password.equals(p.getProperty(name))) {
    1.22              return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build();
    1.23          } else {
    1.24 -            Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name);
    1.25 +            Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name);
    1.26              return Response.status(1).entity(v).build();
    1.27          }
    1.28      }
    1.29 @@ -115,15 +116,15 @@
    1.30      @Path("games/{id}/")
    1.31      @Produces(MediaType.TEXT_HTML)
    1.32      public Viewable board(@PathParam("id") String id) {
    1.33 +        return board(id, null);
    1.34 +    }
    1.35 +    private Viewable board(@PathParam("id") String id, String msg) {
    1.36          Viewable v = checkLogin();
    1.37          if (v != null) {
    1.38              return v;
    1.39          }
    1.40 -        Map<String,Object> map = new HashMap<String,Object>();
    1.41          Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class);
    1.42 -        map.put("doc", doc);
    1.43 -        map.put("user", user);
    1.44 -        return new Viewable("game.fmt", map);
    1.45 +        return viewable("game.fmt", doc, "message", msg);
    1.46      }
    1.47  
    1.48      @GET
    1.49 @@ -142,13 +143,17 @@
    1.50              return v;
    1.51          }
    1.52          WebResource wr = base.path("games").path(id).queryParam("player", user);
    1.53 -        if (type.equals("fence")) {
    1.54 -            wr.queryParam("move", direction.charAt(0) + column + row).put();
    1.55 -            return board(id);
    1.56 -        }
    1.57 -        if (type.equals("move")) {
    1.58 -            wr.queryParam("move", direction + directionNext).put();
    1.59 -            return board(id);
    1.60 +        try {
    1.61 +            if (type.equals("fence")) {
    1.62 +                wr.queryParam("move", direction.charAt(0) + column + row).put();
    1.63 +                return board(id);
    1.64 +            }
    1.65 +            if (type.equals("move")) {
    1.66 +                wr.queryParam("move", direction + directionNext).put();
    1.67 +                return board(id);
    1.68 +            }
    1.69 +        } catch (UniformInterfaceException ex) {
    1.70 +            return board(id, "WRONG_MOVE");
    1.71          }
    1.72          return board(id);
    1.73      }
    1.74 @@ -171,8 +176,8 @@
    1.75      }
    1.76  
    1.77      private Viewable welcomeImpl() {
    1.78 -        final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
    1.79 -        return new Viewable("index.fmt", got);
    1.80 +        final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
    1.81 +        return viewable("index.fmt", got);
    1.82      }
    1.83  
    1.84      //
    1.85 @@ -214,4 +219,14 @@
    1.86          return server;
    1.87      }
    1.88  
    1.89 +    private Viewable viewable(String page, Document doc, Object... more) {
    1.90 +        Map<String,Object> map = new HashMap<String,Object>();
    1.91 +        map.put("doc", doc);
    1.92 +        map.put("user", user);
    1.93 +        for (int i = 0; i < more.length; i += 2) {
    1.94 +            map.put((String)more[i],more[i + 1]);
    1.95 +        }
    1.96 +        return new Viewable(page, map);
    1.97 +    }
    1.98 +
    1.99  }