Merge: showing just active games to users without permission
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 08 Dec 2009 18:48:42 +0100
changeset 165d8d0fd395ff5
parent 161 119b4c3f2cb9
parent 164 2949998db4f6
child 167 6402b5d2c19a
Merge: showing just active games to users without permission
     1.1 --- a/webidor/pom.xml	Tue Dec 01 22:07:29 2009 +0100
     1.2 +++ b/webidor/pom.xml	Tue Dec 08 18:48:42 2009 +0100
     1.3 @@ -9,7 +9,7 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>webidor</artifactId>
     1.6    <packaging>jar</packaging>
     1.7 -  <version>1.8</version>
     1.8 +  <version>1.9</version>
     1.9    <name>webidor server</name>
    1.10    <url>http://maven.apache.org</url>
    1.11    <repositories>
    1.12 @@ -125,6 +125,8 @@
    1.13        </plugin>
    1.14      </plugins>
    1.15    </build>
    1.16 +    <description>Server with REST API for playing, inspecting and managing Quoridor games.</description>
    1.17  </project>
    1.18  
    1.19  
    1.20 +
     2.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Tue Dec 01 22:07:29 2009 +0100
     2.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java	Tue Dec 08 18:48:42 2009 +0100
     2.3 @@ -106,11 +106,11 @@
     2.4          if (when == null) {
     2.5              when = new Date(id.getModified());
     2.6          }
     2.7 +        final GameStatus status = GameStatus.valueOf(board);
     2.8          id = new GameId(
     2.9              id.getId(), id.getWhite(), id.getBlack(),
    2.10 -            new Date(id.getStarted()), when,
    2.11 -            GameStatus.valueOf(board), id.getComments()
    2.12 -        );
    2.13 +            new Date(id.getStarted()), when, status,
    2.14 +            id.getComments(), !status.isInProgress());
    2.15          getMoves().add(new CommentedMove(m, getMoves().size() + 1));
    2.16      }
    2.17  
    2.18 @@ -119,7 +119,7 @@
    2.19          id = new GameId(
    2.20              id.getId(), id.getWhite(), id.getBlack(),
    2.21              new Date(id.getStarted()), new Date(id.getModified()),
    2.22 -            GameStatus.valueOf(board), id.getComments() + 1
    2.23 +            GameStatus.valueOf(board), id.getComments() + 1, id.isFinished()
    2.24          );
    2.25          getMoves().get(getMoves().size() - 1).addNote(n);
    2.26      }
    2.27 @@ -152,7 +152,7 @@
    2.28              new GameId(
    2.29                  id.getId(), id.getWhite(), id.getBlack(),
    2.30                  new Date(id.getStarted()), new Date(id.getModified()),
    2.31 -                GameStatus.history, id.getComments()
    2.32 +                GameStatus.history, id.getComments(), id.isFinished()
    2.33              )
    2.34          );
    2.35          g.board = b;
     3.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java	Tue Dec 01 22:07:29 2009 +0100
     3.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java	Tue Dec 08 18:48:42 2009 +0100
     3.3 @@ -58,6 +58,8 @@
     3.4      private String id;
     3.5      @XmlAttribute
     3.6      private int comments;
     3.7 +    @XmlAttribute
     3.8 +    private boolean finished;
     3.9  
    3.10      GameId() {
    3.11      }
    3.12 @@ -68,11 +70,15 @@
    3.13      private GameId(String first, String second, Date d) {
    3.14          this(
    3.15              UUID.randomUUID().toString(),
    3.16 -            first, second, d, d, GameStatus.whiteMove, 0
    3.17 +            first, second, d, d, GameStatus.whiteMove, 0, false
    3.18          );
    3.19      }
    3.20  
    3.21 -    public GameId(String id, String first, String second, Date started, Date last, GameStatus result, int comments) {
    3.22 +    public GameId(
    3.23 +        String id, String first, String second,
    3.24 +        Date started, Date last, GameStatus result,
    3.25 +        int comments, boolean finished
    3.26 +    ) {
    3.27          this.white = first;
    3.28          this.black = second;
    3.29          this.id = id;
    3.30 @@ -80,6 +86,7 @@
    3.31          this.modified = last.getTime();
    3.32          this.status = result;
    3.33          this.comments = comments;
    3.34 +        this.finished = finished;
    3.35      }
    3.36  
    3.37      public String getId() {
    3.38 @@ -110,6 +117,10 @@
    3.39          return comments;
    3.40      }
    3.41  
    3.42 +    public boolean isFinished() {
    3.43 +        return finished;
    3.44 +    }
    3.45 +
    3.46      private static final class NewestFirst implements Comparator<GameId> {
    3.47          public int compare(GameId o1, GameId o2) {
    3.48              if (o1 == o2) {
     4.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java	Tue Dec 01 22:07:29 2009 +0100
     4.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java	Tue Dec 08 18:48:42 2009 +0100
     4.3 @@ -52,4 +52,10 @@
     4.4              return board.getCurrentPlayer() == board.getPlayers().get(0) ? GameStatus.whiteMove : GameStatus.blackMove;
     4.5          }
     4.6      }
     4.7 +
     4.8 +    /** @return true if the game is in progress
     4.9 +     */
    4.10 +    public boolean isInProgress() {
    4.11 +        return this == whiteMove || this == blackMove;
    4.12 +    }
    4.13  }
     5.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Dec 01 22:07:29 2009 +0100
     5.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Tue Dec 08 18:48:42 2009 +0100
     5.3 @@ -131,10 +131,25 @@
     5.4      @Path("{id}")
     5.5      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
     5.6      public Game getBoardInfo(
     5.7 +        @QueryParam("loginID") @DefaultValue("") String loginId,
     5.8          @PathParam("id") String id,
     5.9          @QueryParam("move") @DefaultValue("-1") int move
    5.10      ) {
    5.11 -        return findGame(id, move);
    5.12 +        Game g = findGame(id, move);
    5.13 +        if (!g.getId().isFinished()) {
    5.14 +            return g;
    5.15 +        }
    5.16 +        String logUser = quoridor.isLoggedIn(loginId);
    5.17 +        if (logUser == null) {
    5.18 +            throw new WebApplicationException(Status.UNAUTHORIZED);
    5.19 +        }
    5.20 +        if (logUser.equals(g.getId().getWhite())) {
    5.21 +            return g;
    5.22 +        }
    5.23 +        if (logUser.equals(g.getId().getBlack())) {
    5.24 +            return g;
    5.25 +        }
    5.26 +        throw new WebApplicationException(Status.UNAUTHORIZED);
    5.27      }
    5.28  
    5.29      @PUT
    5.30 @@ -276,7 +291,7 @@
    5.31                  throw new IOException("Missing white and black identification in " + f);
    5.32              }
    5.33              if (g == null) {
    5.34 -                GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0);
    5.35 +                GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0, false);
    5.36                  g = new Game(id);
    5.37              }
    5.38              int hash = line.indexOf('#');
     6.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Tue Dec 01 22:07:29 2009 +0100
     6.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java	Tue Dec 08 18:48:42 2009 +0100
     6.3 @@ -27,6 +27,7 @@
     6.4  package cz.xelfi.quoridor.webidor;
     6.5  
     6.6  import com.sun.jersey.api.client.GenericType;
     6.7 +import com.sun.jersey.api.client.UniformInterfaceException;
     6.8  import com.sun.jersey.test.framework.JerseyTest;
     6.9  import java.io.File;
    6.10  import java.io.FileOutputStream;
    6.11 @@ -135,7 +136,13 @@
    6.12          }
    6.13  
    6.14  
    6.15 -        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    6.16 +        try {
    6.17 +            Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    6.18 +            fail("If the game is finished, one cannot get its status without login");
    6.19 +        } catch (UniformInterfaceException ex) {
    6.20 +            // OK
    6.21 +        }
    6.22 +        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class);
    6.23          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    6.24  
    6.25          assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
    6.26 @@ -156,13 +163,23 @@
    6.27                  .queryParam("loginID", logJarda)
    6.28                  .queryParam("black", "Jirka").post(GameId.class);
    6.29  
    6.30 +        assertTrue("In progress", s.getStatus().isInProgress());
    6.31 +
    6.32          webResource.path("games/" + s.getId()).
    6.33              queryParam("loginID", logJarda).
    6.34              queryParam("player", "Jarda").
    6.35              queryParam("move", "RESIGN").put(GameId.class);
    6.36 -        Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    6.37 +        try {
    6.38 +            Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class);
    6.39 +            fail("Should not be able to get game when finished");
    6.40 +        } catch (UniformInterfaceException ex) {
    6.41 +            // OK
    6.42 +        }
    6.43 +        Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class);
    6.44          assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus());
    6.45          assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer());
    6.46 +
    6.47 +        assertFalse("is finished", end.getId().getStatus().isInProgress());
    6.48      }
    6.49  
    6.50  }
     7.1 --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Tue Dec 01 22:07:29 2009 +0100
     7.2 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java	Tue Dec 08 18:48:42 2009 +0100
     7.3 @@ -86,7 +86,7 @@
     7.4          Thread.sleep(1000);
     7.5  
     7.6          Games games = new Games(dir, new Quoridor());
     7.7 -        Game g = games.getBoardInfo("x", -1);
     7.8 +        Game g = games.getBoardInfo("", "x", -1);
     7.9          assertNotNull("Game found", g);
    7.10          assertNotNull("Board found", g.getBoard());
    7.11          assertEquals("List of moves has two", 2, g.getMoves().size());
    7.12 @@ -108,7 +108,7 @@
    7.13          Thread.sleep(1000);
    7.14  
    7.15          Games games = new Games(dir, new Quoridor());
    7.16 -        Game g = games.getBoardInfo("x", -1);
    7.17 +        Game g = games.getBoardInfo("", "x", -1);
    7.18          assertNotNull("Game found", g);
    7.19          assertNotNull("Board found", g.getBoard());
    7.20          assertEquals("List of moves has two", 2, g.getMoves().size());
    7.21 @@ -133,7 +133,7 @@
    7.22          Thread.sleep(1000);
    7.23  
    7.24          Games games = new Games(dir, new Quoridor());
    7.25 -        Game g = games.getBoardInfo("x", -1);
    7.26 +        Game g = games.getBoardInfo("", "x", -1);
    7.27          assertNotNull("Game found", g);
    7.28          assertNotNull("Board found", g.getBoard());
    7.29          assertEquals("List of moves has two", 2, g.getMoves().size());