# HG changeset patch # User Jaroslav Tulach # Date 1252088808 -7200 # Node ID 7c3794f5baa1057641a49320bee214871d933ed5 # Parent 830e0ba29c041b8bee0e93112b0c7853432b3439 Reload of the game page, warning messages, listing just game in progress diff -r 830e0ba29c04 -r 7c3794f5baa1 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Fri Sep 04 18:01:01 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Fri Sep 04 20:26:48 2009 +0200 @@ -27,6 +27,7 @@ package cz.xelfi.quoridor.freemarkerdor; import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.container.httpserver.HttpServerFactory; import com.sun.jersey.api.core.PackagesResourceConfig; @@ -77,7 +78,7 @@ user = headers.getCookies().get("login").getValue(); return null; } - return new Viewable("login.fmt", null); + return viewable("login.fmt", null); } @POST @@ -96,7 +97,7 @@ if (name != null && password.equals(p.getProperty(name))) { return Response.seeOther(new URI("/")).cookie(new NewCookie("login", name)).entity(welcomeImpl()).build(); } else { - Viewable v = new Viewable("login.fmt", "Invalid name or password: " + name); + Viewable v = viewable("login.fmt", null, "message", "Invalid name or password: " + name); return Response.status(1).entity(v).build(); } } @@ -115,15 +116,15 @@ @Path("games/{id}/") @Produces(MediaType.TEXT_HTML) public Viewable board(@PathParam("id") String id) { + return board(id, null); + } + private Viewable board(@PathParam("id") String id, String msg) { Viewable v = checkLogin(); if (v != null) { return v; } - Map map = new HashMap(); Document doc = base.path("games").path(id).accept(MediaType.TEXT_XML).get(Document.class); - map.put("doc", doc); - map.put("user", user); - return new Viewable("game.fmt", map); + return viewable("game.fmt", doc, "message", msg); } @GET @@ -142,13 +143,17 @@ return v; } WebResource wr = base.path("games").path(id).queryParam("player", user); - if (type.equals("fence")) { - wr.queryParam("move", direction.charAt(0) + column + row).put(); - return board(id); - } - if (type.equals("move")) { - wr.queryParam("move", direction + directionNext).put(); - return board(id); + try { + if (type.equals("fence")) { + wr.queryParam("move", direction.charAt(0) + column + row).put(); + return board(id); + } + if (type.equals("move")) { + wr.queryParam("move", direction + directionNext).put(); + return board(id); + } + } catch (UniformInterfaceException ex) { + return board(id, "WRONG_MOVE"); } return board(id); } @@ -171,8 +176,8 @@ } private Viewable welcomeImpl() { - final Object got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class); - return new Viewable("index.fmt", got); + final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class); + return viewable("index.fmt", got); } // @@ -214,4 +219,14 @@ return server; } + private Viewable viewable(String page, Document doc, Object... more) { + Map map = new HashMap(); + map.put("doc", doc); + map.put("user", user); + for (int i = 0; i < more.length; i += 2) { + map.put((String)more[i],more[i + 1]); + } + return new Viewable(page, map); + } + } diff -r 830e0ba29c04 -r 7c3794f5baa1 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Fri Sep 04 18:01:01 2009 +0200 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Fri Sep 04 20:26:48 2009 +0200 @@ -7,8 +7,19 @@

Game

${doc.game.id.@white} vs. ${doc.game.id.@black}

+ +

+ Reload +

+ + <#if message?? > +

+ ${message} +

+ + <#if user = doc.game.@currentPlayer > -
+