# HG changeset patch # User Jaroslav Tulach # Date 1260282890 -3600 # Node ID c1bfbe98152b081a2e2373789ff68b1490f9fa0b # Parent 9e862a62de851575579f9ac5ede0bae191361c11 Only active games are visible for regular users. Need a bit more work to allow them to see history of active games. diff -r 9e862a62de85 -r c1bfbe98152b webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Tue Dec 01 09:40:54 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Tue Dec 08 15:34:50 2009 +0100 @@ -52,4 +52,10 @@ return board.getCurrentPlayer() == board.getPlayers().get(0) ? GameStatus.whiteMove : GameStatus.blackMove; } } + + /** @return true if the game is in progress + */ + public boolean isInProgress() { + return this == whiteMove || this == blackMove; + } } diff -r 9e862a62de85 -r c1bfbe98152b webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Tue Dec 01 09:40:54 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Tue Dec 08 15:34:50 2009 +0100 @@ -131,10 +131,25 @@ @Path("{id}") @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML }) public Game getBoardInfo( + @QueryParam("loginID") @DefaultValue("") String loginId, @PathParam("id") String id, @QueryParam("move") @DefaultValue("-1") int move ) { - return findGame(id, move); + Game g = findGame(id, move); + if (g.getId().getStatus().isInProgress()) { + return g; + } + String logUser = quoridor.isLoggedIn(loginId); + if (logUser == null) { + throw new WebApplicationException(Status.UNAUTHORIZED); + } + if (logUser.equals(g.getId().getWhite())) { + return g; + } + if (logUser.equals(g.getId().getBlack())) { + return g; + } + throw new WebApplicationException(Status.UNAUTHORIZED); } @PUT diff -r 9e862a62de85 -r c1bfbe98152b webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Tue Dec 01 09:40:54 2009 +0100 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Tue Dec 08 15:34:50 2009 +0100 @@ -27,6 +27,7 @@ package cz.xelfi.quoridor.webidor; import com.sun.jersey.api.client.GenericType; +import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.test.framework.JerseyTest; import java.io.File; import java.io.FileOutputStream; @@ -135,7 +136,13 @@ } - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class); + try { + Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class); + fail("If the game is finished, one cannot get its status without login"); + } catch (UniformInterfaceException ex) { + // OK + } + Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJirka).accept(MediaType.TEXT_XML).get(Game.class); assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus()); assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer()); @@ -156,13 +163,23 @@ .queryParam("loginID", logJarda) .queryParam("black", "Jirka").post(GameId.class); + assertTrue("In progress", s.getStatus().isInProgress()); + webResource.path("games/" + s.getId()). queryParam("loginID", logJarda). queryParam("player", "Jarda"). queryParam("move", "RESIGN").put(GameId.class); - Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class); + try { + Game end = webResource.path("games/" + s.getId()).accept(MediaType.TEXT_XML).get(Game.class); + fail("Should not be able to get game when finished"); + } catch (UniformInterfaceException ex) { + // OK + } + Game end = webResource.path("games/" + s.getId()).queryParam("loginID", logJarda).accept(MediaType.TEXT_XML).get(Game.class); assertEquals("BlackWins", GameStatus.blackWon, end.getId().getStatus()); assertEquals("Jirka wins", "Jirka", end.getCurrentPlayer()); + + assertFalse("is finished", end.getId().getStatus().isInProgress()); } } diff -r 9e862a62de85 -r c1bfbe98152b webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Tue Dec 01 09:40:54 2009 +0100 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Tue Dec 08 15:34:50 2009 +0100 @@ -86,7 +86,7 @@ Thread.sleep(1000); Games games = new Games(dir, new Quoridor()); - Game g = games.getBoardInfo("x", -1); + Game g = games.getBoardInfo("", "x", -1); assertNotNull("Game found", g); assertNotNull("Board found", g.getBoard()); assertEquals("List of moves has two", 2, g.getMoves().size()); @@ -108,7 +108,7 @@ Thread.sleep(1000); Games games = new Games(dir, new Quoridor()); - Game g = games.getBoardInfo("x", -1); + Game g = games.getBoardInfo("", "x", -1); assertNotNull("Game found", g); assertNotNull("Board found", g.getBoard()); assertEquals("List of moves has two", 2, g.getMoves().size()); @@ -133,7 +133,7 @@ Thread.sleep(1000); Games games = new Games(dir, new Quoridor()); - Game g = games.getBoardInfo("x", -1); + Game g = games.getBoardInfo("", "x", -1); assertNotNull("Game found", g); assertNotNull("Board found", g.getBoard()); assertEquals("List of moves has two", 2, g.getMoves().size());