# HG changeset patch # User Jaroslav Tulach # Date 1252091673 -7200 # Node ID fa12b02023a0666e61d15d515b39a0c025cb2c90 # Parent 7c3794f5baa1057641a49320bee214871d933ed5 Separating games by their current state diff -r 7c3794f5baa1 -r fa12b02023a0 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Fri Sep 04 20:26:48 2009 +0200 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Fri Sep 04 21:14:33 2009 +0200 @@ -169,15 +169,20 @@ if (v != null) { return v; } - Object obj = - base.path("games").queryParam("white", white). - queryParam("black", black).post(Document.class); - return welcome(); + + if (user.equals(white) || user.equals(black)) { + Object obj = + base.path("games").queryParam("white", white). + queryParam("black", black).post(Document.class); + return welcomeImpl(); + } else { + return welcomeImpl("message", "You (" + user + ") must be white or black!"); + } } - private Viewable welcomeImpl() { + private Viewable welcomeImpl(Object... args) { final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class); - return viewable("index.fmt", got); + return viewable("index.fmt", got, args); } // diff -r 7c3794f5baa1 -r fa12b02023a0 freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Fri Sep 04 20:26:48 2009 +0200 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt Fri Sep 04 21:14:33 2009 +0200 @@ -7,31 +7,68 @@

Quoridor Community Server

+ <#if message?? > +

+ ${message} +

+ +
Games where you are expected to move
    <#list doc.gameIds.* as g> <#if (g.@white = user || g.@black = user) && g.@result = "IN_PROGRESS" >
  1. - ${g.@white} vs. ${g.@black} board + ${g.@white} vs. ${g.@black} board
+
Won games
+ +
    + <#list doc.gameIds.* as g> + <#if + (g.@white = user && g.@result = "WHITE_WON") || + (g.@black = user && g.@result = "BLACK_WON") + > +
  1. + ${g.@white} vs. ${g.@black} board +
  2. + + +
+ +
Lost games
+ +
    + <#list doc.gameIds.* as g> + <#if + (g.@white = user && g.@result = "BLACK_WON") || + (g.@black = user && g.@result = "WHITE_WON") + > +
  1. + ${g.@white} vs. ${g.@black} board +
  2. + + +
+ +
+ White: + Black: + +
+
All Games
    <#list doc.gameIds.* as g>
  1. - ${g.@white} vs. ${g.@black} board + ${g.@white} vs. ${g.@black} board
-
- White: - Black: - -
\ No newline at end of file diff -r 7c3794f5baa1 -r fa12b02023a0 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Fri Sep 04 20:26:48 2009 +0200 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Fri Sep 04 21:14:33 2009 +0200 @@ -100,6 +100,10 @@ } board = getBoard().apply(m); + if (board.getWinner() != null) { + GameResult r = board.getWinner() == board.getPlayers().get(0) ? GameResult.WHITE_WON : GameResult.BLACK_WON; + id = new GameId(id.getId(), id.getWhite(), id.getBlack(), id.getStarted(), r); + } getMoves().add(m); } diff -r 7c3794f5baa1 -r fa12b02023a0 webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Fri Sep 04 21:14:33 2009 +0200 @@ -0,0 +1,106 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * Portions Copyrighted 2009 Jaroslav Tulach + */ + +package cz.xelfi.quoridor.webidor; + +import com.sun.jersey.api.client.GenericType; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.core.header.MediaTypes; +import com.sun.jersey.test.framework.JerseyTest; +import cz.xelfi.quoridor.Board; +import cz.xelfi.quoridor.Move; +import cz.xelfi.quoridor.webidor.resources.Games; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.MediaType; +import org.junit.Test; +import org.w3c.dom.Document; +import static org.junit.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +public class FinishedGameTest extends JerseyTest { + private File dir; + + public FinishedGameTest() throws Exception { + super("cz.xelfi.quoridor.webidor.resources"); + } + + @Override + public void setUp() throws Exception { + dir = File.createTempFile("quoridor", ".dir"); + dir.delete(); + System.setProperty("quoridor.dir", dir.getPath()); + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + deleteRec(dir); + } + + static void deleteRec(File dir) throws IOException { + if (dir == null) { + return; + } + File[] arr = dir.listFiles(); + if (arr != null) { + for (File f : arr) { + deleteRec(f); + } + } + dir.delete(); + } + + @Test public void testCreateAGame() throws Exception { + webResource = webResource.path("api"); + GameId s = webResource.path("games").queryParam("white", "Jarda") + .queryParam("black", "Jirka").post(GameId.class); + + for (int i = 0; i < 3; i++) { + webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class); + webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class); + } + + webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class); + webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class); + + for (int i = 0; i < 3; i++) { + webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class); + webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class); + } + + Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class); + assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult()); + } + +}