Separating games by their current state
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Sep 2009 21:14:33 +0200
changeset 57fa12b02023a0
parent 56 7c3794f5baa1
child 58 e00be634746c
Separating games by their current state
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt
webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java
     1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 04 20:26:48 2009 +0200
     1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Fri Sep 04 21:14:33 2009 +0200
     1.3 @@ -169,15 +169,20 @@
     1.4          if (v != null) {
     1.5              return v;
     1.6          }
     1.7 -        Object obj =
     1.8 -            base.path("games").queryParam("white", white).
     1.9 -            queryParam("black", black).post(Document.class);
    1.10 -        return welcome();
    1.11 +
    1.12 +        if (user.equals(white) || user.equals(black)) {
    1.13 +            Object obj =
    1.14 +                base.path("games").queryParam("white", white).
    1.15 +                queryParam("black", black).post(Document.class);
    1.16 +            return welcomeImpl();
    1.17 +        } else {
    1.18 +            return welcomeImpl("message", "You (" + user + ") must be white or black!");
    1.19 +        }
    1.20      }
    1.21  
    1.22 -    private Viewable welcomeImpl() {
    1.23 +    private Viewable welcomeImpl(Object... args) {
    1.24          final Document got = base.path("games").accept(MediaType.TEXT_XML).get(Document.class);
    1.25 -        return viewable("index.fmt", got);
    1.26 +        return viewable("index.fmt", got, args);
    1.27      }
    1.28  
    1.29      //
     2.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt	Fri Sep 04 20:26:48 2009 +0200
     2.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/index.fmt	Fri Sep 04 21:14:33 2009 +0200
     2.3 @@ -7,31 +7,68 @@
     2.4    <body>
     2.5        <h1>Quoridor Community Server</h1>
     2.6  
     2.7 +      <#if message?? >
     2.8 +        <p>
     2.9 +            <span style="color: red">${message}</span>
    2.10 +        </p>
    2.11 +      </#if>
    2.12 +
    2.13        <h5>Games where you are expected to move</h5>
    2.14  
    2.15        <ol>
    2.16        <#list doc.gameIds.* as g>
    2.17          <#if (g.@white = user || g.@black = user) && g.@result = "IN_PROGRESS" >
    2.18              <li>
    2.19 -                ${g.@white} vs. ${g.@black} <a href="games/${g.@id}/">board</a>
    2.20 +                ${g.@white} vs. ${g.@black} <a href="/games/${g.@id}/">board</a>
    2.21              </li>
    2.22          </#if>
    2.23        </#list>
    2.24        </ol>
    2.25  
    2.26 +      <h5>Won games</h5>
    2.27 +
    2.28 +      <ol>
    2.29 +      <#list doc.gameIds.* as g>
    2.30 +        <#if 
    2.31 +            (g.@white = user && g.@result = "WHITE_WON") ||
    2.32 +            (g.@black = user && g.@result = "BLACK_WON")
    2.33 +        >
    2.34 +            <li>
    2.35 +                ${g.@white} vs. ${g.@black} <a href="/games/${g.@id}/">board</a>
    2.36 +            </li>
    2.37 +        </#if>
    2.38 +      </#list>
    2.39 +      </ol>
    2.40 +
    2.41 +      <h5>Lost games</h5>
    2.42 +
    2.43 +      <ol>
    2.44 +      <#list doc.gameIds.* as g>
    2.45 +        <#if
    2.46 +            (g.@white = user && g.@result = "BLACK_WON") ||
    2.47 +            (g.@black = user && g.@result = "WHITE_WON")
    2.48 +        >
    2.49 +            <li>
    2.50 +                ${g.@white} vs. ${g.@black} <a href="/games/${g.@id}/">board</a>
    2.51 +            </li>
    2.52 +        </#if>
    2.53 +      </#list>
    2.54 +      </ol>
    2.55 +
    2.56 +      <form action="games/create">
    2.57 +            White: <input type="text" name="white" value="" />
    2.58 +            Black: <input type="text" name="black" value="" />
    2.59 +            <input type="submit" value="Create!" />
    2.60 +      </form>
    2.61 +
    2.62        <h5>All Games</h5>
    2.63  
    2.64        <ol>
    2.65        <#list doc.gameIds.* as g>
    2.66          <li>
    2.67 -            ${g.@white} vs. ${g.@black} <a href="games/${g.@id}/">board</a>
    2.68 +            ${g.@white} vs. ${g.@black} <a href="/games/${g.@id}/">board</a>
    2.69          </li>
    2.70        </#list>
    2.71        </ol>
    2.72 -      <form action="games/create">
    2.73 -            White: <input type="text" name="white" value="" />
    2.74 -            Black: <input type="text" name="black" value="" />
    2.75 -            <input type="submit" value="Create!" />
    2.76 -      </form>
    2.77    </body>
    2.78  </html>
    2.79 \ No newline at end of file
     3.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Fri Sep 04 20:26:48 2009 +0200
     3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Fri Sep 04 21:14:33 2009 +0200
     3.3 @@ -100,6 +100,10 @@
     3.4          }
     3.5  
     3.6          board = getBoard().apply(m);
     3.7 +        if (board.getWinner() != null) {
     3.8 +            GameResult r = board.getWinner() == board.getPlayers().get(0) ? GameResult.WHITE_WON : GameResult.BLACK_WON;
     3.9 +            id = new GameId(id.getId(), id.getWhite(), id.getBlack(), id.getStarted(), r);
    3.10 +        }
    3.11          getMoves().add(m);
    3.12      }
    3.13  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Fri Sep 04 21:14:33 2009 +0200
     4.3 @@ -0,0 +1,106 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * The contents of this file are subject to the terms of either the GNU
     4.8 + * General Public License Version 2 only ("GPL") or the Common
     4.9 + * Development and Distribution License("CDDL") (collectively, the
    4.10 + * "License"). You may not use this file except in compliance with the
    4.11 + * License. You can obtain a copy of the License at
    4.12 + * http://www.netbeans.org/cddl-gplv2.html
    4.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.14 + * specific language governing permissions and limitations under the
    4.15 + * License.  When distributing the software, include this License Header
    4.16 + * Notice in each file and include the License file at
    4.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    4.18 + * particular file as subject to the "Classpath" exception as provided
    4.19 + * by Sun in the GPL Version 2 section of the License file that
    4.20 + * accompanied this code. If applicable, add the following below the
    4.21 + * License Header, with the fields enclosed by brackets [] replaced by
    4.22 + * your own identifying information:
    4.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.24 + *
    4.25 + * Contributor(s):
    4.26 + *
    4.27 + * Portions Copyrighted 2009 Jaroslav Tulach
    4.28 + */
    4.29 +
    4.30 +package cz.xelfi.quoridor.webidor;
    4.31 +
    4.32 +import com.sun.jersey.api.client.GenericType;
    4.33 +import com.sun.jersey.api.client.UniformInterfaceException;
    4.34 +import com.sun.jersey.core.header.MediaTypes;
    4.35 +import com.sun.jersey.test.framework.JerseyTest;
    4.36 +import cz.xelfi.quoridor.Board;
    4.37 +import cz.xelfi.quoridor.Move;
    4.38 +import cz.xelfi.quoridor.webidor.resources.Games;
    4.39 +import java.io.File;
    4.40 +import java.io.FileReader;
    4.41 +import java.io.IOException;
    4.42 +import java.util.List;
    4.43 +import java.util.Map;
    4.44 +import javax.ws.rs.core.MediaType;
    4.45 +import org.junit.Test;
    4.46 +import org.w3c.dom.Document;
    4.47 +import static org.junit.Assert.*;
    4.48 +
    4.49 +/**
    4.50 + *
    4.51 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.52 + */
    4.53 +public class FinishedGameTest extends JerseyTest {
    4.54 +    private File dir;
    4.55 +
    4.56 +    public FinishedGameTest() throws Exception {
    4.57 +        super("cz.xelfi.quoridor.webidor.resources");
    4.58 +    }
    4.59 +
    4.60 +    @Override
    4.61 +    public void setUp() throws Exception {
    4.62 +        dir = File.createTempFile("quoridor", ".dir");
    4.63 +        dir.delete();
    4.64 +        System.setProperty("quoridor.dir", dir.getPath());
    4.65 +        super.setUp();
    4.66 +    }
    4.67 +
    4.68 +    @Override
    4.69 +    public void tearDown() throws Exception {
    4.70 +        super.tearDown();
    4.71 +        deleteRec(dir);
    4.72 +    }
    4.73 +
    4.74 +    static void deleteRec(File dir) throws IOException {
    4.75 +        if (dir == null) {
    4.76 +            return;
    4.77 +        }
    4.78 +        File[] arr = dir.listFiles();
    4.79 +        if (arr != null) {
    4.80 +            for (File f : arr) {
    4.81 +                deleteRec(f);
    4.82 +            }
    4.83 +        }
    4.84 +        dir.delete();
    4.85 +    }
    4.86 +
    4.87 +    @Test public void testCreateAGame() throws Exception {
    4.88 +        webResource = webResource.path("api");
    4.89 +        GameId s = webResource.path("games").queryParam("white", "Jarda")
    4.90 +                .queryParam("black", "Jirka").post(GameId.class);
    4.91 +
    4.92 +        for (int i = 0; i < 3; i++) {
    4.93 +            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    4.94 +            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
    4.95 +        }
    4.96 +
    4.97 +        webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
    4.98 +        webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "SS").put(GameId.class);
    4.99 +
   4.100 +        for (int i = 0; i < 3; i++) {
   4.101 +            webResource.path("games/" + s.getId()).queryParam("player", "Jarda").queryParam("move", "N").put(GameId.class);
   4.102 +            webResource.path("games/" + s.getId()).queryParam("player", "Jirka").queryParam("move", "S").put(GameId.class);
   4.103 +        }
   4.104 +
   4.105 +        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
   4.106 +        assertEquals("BlackWins", GameResult.BLACK_WON, end.getId().getResult());
   4.107 +    }
   4.108 +
   4.109 +}