webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
changeset 100 8b899ed24f9f
parent 96 2eeaa41236c3
child 105 6e55d5c85d3c
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sat Sep 19 07:58:56 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sat Sep 19 14:38:29 2009 +0200
     1.3 @@ -43,6 +43,7 @@
     1.4  import java.util.List;
     1.5  import java.util.logging.Level;
     1.6  import java.util.logging.Logger;
     1.7 +import javax.ws.rs.DefaultValue;
     1.8  import javax.ws.rs.GET;
     1.9  import javax.ws.rs.POST;
    1.10  import javax.ws.rs.PUT;
    1.11 @@ -121,23 +122,26 @@
    1.12      @GET
    1.13      @Path("{id}")
    1.14      @Produces("image/png")
    1.15 -    public Image getBoardImage(@PathParam("id") String id) {
    1.16 -        Game g = findGame(id);
    1.17 +    public Image getBoardImage(
    1.18 +        @PathParam("id") String id,
    1.19 +        @QueryParam("fieldSize") @DefaultValue("50") int fieldSize,
    1.20 +        @QueryParam("move") @DefaultValue("-1") int move
    1.21 +    ) {
    1.22 +        Game g = findGame(id, move);
    1.23          if (g == null) {
    1.24              throw new IllegalArgumentException("Unknown game " + id);
    1.25          }
    1.26 -        return BoardImage.draw(g.getBoard());
    1.27 +        return BoardImage.draw(g.getBoard(), fieldSize);
    1.28      }
    1.29  
    1.30      @GET
    1.31      @Path("{id}")
    1.32      @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.33 -    public Game getBoardInfo(@PathParam("id") String id) {
    1.34 -        Game g = findGame(id);
    1.35 -        if (g == null) {
    1.36 -            throw new IllegalArgumentException("Unknown game " + id);
    1.37 -        }
    1.38 -        return g;
    1.39 +    public Game getBoardInfo(
    1.40 +        @PathParam("id") String id,
    1.41 +        @QueryParam("move") @DefaultValue("-1") int move
    1.42 +    ) {
    1.43 +        return findGame(id, move);
    1.44      }
    1.45  
    1.46      @PUT
    1.47 @@ -194,6 +198,18 @@
    1.48          }
    1.49          return null;
    1.50      }
    1.51 +    private Game findGame(String id, int move) {
    1.52 +        Game g = findGame(id);
    1.53 +        if (g == null) {
    1.54 +            throw new IllegalArgumentException("Unknown game " + id);
    1.55 +        }
    1.56 +        try {
    1.57 +            return move == -1 ? g : g.snapshot(move);
    1.58 +        } catch (IllegalPositionException ex) {
    1.59 +            Logger.getLogger(Games.class.getName()).log(Level.SEVERE, null, ex);
    1.60 +            return null;
    1.61 +        }
    1.62 +    }
    1.63  
    1.64      private Game readGame(File f) throws IOException {
    1.65          BufferedReader r = new BufferedReader(new FileReader(f));