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 }
2.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Fri Sep 04 18:01:01 2009 +0200
2.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/game.fmt Fri Sep 04 20:26:48 2009 +0200
2.3 @@ -7,8 +7,19 @@
2.4 <body>
2.5 <h1>Game</h1>
2.6 <h3>${doc.game.id.@white} vs. ${doc.game.id.@black}</h3>
2.7 +
2.8 + <p>
2.9 + <a href="/games/${doc.game.id.@id}">Reload</a>
2.10 + </p>
2.11 +
2.12 + <#if message?? >
2.13 + <p>
2.14 + <span style="color: red">${message}</span>
2.15 + </p>
2.16 + </#if>
2.17 +
2.18 <#if user = doc.game.@currentPlayer >
2.19 - <form action="move">
2.20 + <form action="/games/${doc.game.id.@id}/move">
2.21 <input type="hidden" name="type" value="fence" readonly="readonly"/>
2.22 <select name="column">
2.23 <option>A</option>
3.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Fri Sep 04 18:01:01 2009 +0200
3.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Fri Sep 04 20:26:48 2009 +0200
3.3 @@ -6,6 +6,21 @@
3.4 </head>
3.5 <body>
3.6 <h1>Quoridor Community Server</h1>
3.7 +
3.8 + <h5>Games where you are expected to move</h5>
3.9 +
3.10 + <ol>
3.11 + <#list doc.gameIds.* as g>
3.12 + <#if (g.@white = user || g.@black = user) && g.@result = "IN_PROGRESS" >
3.13 + <li>
3.14 + ${g.@white} vs. ${g.@black} <a href="games/${g.@id}/">board</a>
3.15 + </li>
3.16 + </#if>
3.17 + </#list>
3.18 + </ol>
3.19 +
3.20 + <h5>All Games</h5>
3.21 +
3.22 <ol>
3.23 <#list doc.gameIds.* as g>
3.24 <li>
4.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt Fri Sep 04 18:01:01 2009 +0200
4.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/login.fmt Fri Sep 04 20:26:48 2009 +0200
4.3 @@ -8,7 +8,7 @@
4.4 <h1>Quoridor Community Server</h1>
4.5 <h2>Login</h2>
4.6
4.7 - <b>${model!""}</b>
4.8 + <b>${message!""}</b>
4.9
4.10 <form action="login" method="post">
4.11 Name: <input type="text" name="name"/>