# HG changeset patch # User Jaroslav Tulach # Date 1260294522 -3600 # Node ID d8d0fd395ff520e888f0a4a47cd1f1b8951cf9de # Parent 119b4c3f2cb9c5695bb8d59cf83b58a4207461fe# Parent 2949998db4f686c85f1e6bf35a02dfd3d5e34b8a Merge: showing just active games to users without permission diff -r 119b4c3f2cb9 -r d8d0fd395ff5 webidor/pom.xml --- a/webidor/pom.xml Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/pom.xml Tue Dec 08 18:48:42 2009 +0100 @@ -9,7 +9,7 @@ org.apidesign webidor jar - 1.8 + 1.9 webidor server http://maven.apache.org @@ -125,6 +125,8 @@ + Server with REST API for playing, inspecting and managing Quoridor games. + diff -r 119b4c3f2cb9 -r d8d0fd395ff5 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java Tue Dec 08 18:48:42 2009 +0100 @@ -106,11 +106,11 @@ if (when == null) { when = new Date(id.getModified()); } + final GameStatus status = GameStatus.valueOf(board); id = new GameId( id.getId(), id.getWhite(), id.getBlack(), - new Date(id.getStarted()), when, - GameStatus.valueOf(board), id.getComments() - ); + new Date(id.getStarted()), when, status, + id.getComments(), !status.isInProgress()); getMoves().add(new CommentedMove(m, getMoves().size() + 1)); } @@ -119,7 +119,7 @@ id = new GameId( id.getId(), id.getWhite(), id.getBlack(), new Date(id.getStarted()), new Date(id.getModified()), - GameStatus.valueOf(board), id.getComments() + 1 + GameStatus.valueOf(board), id.getComments() + 1, id.isFinished() ); getMoves().get(getMoves().size() - 1).addNote(n); } @@ -152,7 +152,7 @@ new GameId( id.getId(), id.getWhite(), id.getBlack(), new Date(id.getStarted()), new Date(id.getModified()), - GameStatus.history, id.getComments() + GameStatus.history, id.getComments(), id.isFinished() ) ); g.board = b; diff -r 119b4c3f2cb9 -r d8d0fd395ff5 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Tue Dec 08 18:48:42 2009 +0100 @@ -58,6 +58,8 @@ private String id; @XmlAttribute private int comments; + @XmlAttribute + private boolean finished; GameId() { } @@ -68,11 +70,15 @@ private GameId(String first, String second, Date d) { this( UUID.randomUUID().toString(), - first, second, d, d, GameStatus.whiteMove, 0 + first, second, d, d, GameStatus.whiteMove, 0, false ); } - public GameId(String id, String first, String second, Date started, Date last, GameStatus result, int comments) { + public GameId( + String id, String first, String second, + Date started, Date last, GameStatus result, + int comments, boolean finished + ) { this.white = first; this.black = second; this.id = id; @@ -80,6 +86,7 @@ this.modified = last.getTime(); this.status = result; this.comments = comments; + this.finished = finished; } public String getId() { @@ -110,6 +117,10 @@ return comments; } + public boolean isFinished() { + return finished; + } + private static final class NewestFirst implements Comparator { public int compare(GameId o1, GameId o2) { if (o1 == o2) { diff -r 119b4c3f2cb9 -r d8d0fd395ff5 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/GameStatus.java Tue Dec 08 18:48:42 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 119b4c3f2cb9 -r d8d0fd395ff5 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 22:07:29 2009 +0100 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java Tue Dec 08 18:48:42 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().isFinished()) { + 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 @@ -276,7 +291,7 @@ throw new IOException("Missing white and black identification in " + f); } if (g == null) { - GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0); + GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0, false); g = new Game(id); } int hash = line.indexOf('#'); diff -r 119b4c3f2cb9 -r d8d0fd395ff5 webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/FinishedGameTest.java Tue Dec 08 18:48:42 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 119b4c3f2cb9 -r d8d0fd395ff5 webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Tue Dec 01 22:07:29 2009 +0100 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/GamesTest.java Tue Dec 08 18:48:42 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());