webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
changeset 48 69e897fe8140
parent 46 71e4cf307c93
child 77 d574ac6e44cc
     1.1 --- a/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sat Aug 29 15:55:53 2009 +0200
     1.2 +++ b/webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java	Sun Aug 30 14:37:47 2009 +0200
     1.3 @@ -37,11 +37,8 @@
     1.4  import java.io.IOException;
     1.5  import java.io.PrintWriter;
     1.6  import java.util.ArrayList;
     1.7 -import java.util.Arrays;
     1.8 -import java.util.HashMap;
     1.9 +import java.util.Date;
    1.10  import java.util.List;
    1.11 -import java.util.Map;
    1.12 -import java.util.TreeMap;
    1.13  import java.util.logging.Level;
    1.14  import java.util.logging.Logger;
    1.15  import javax.ws.rs.GET;
    1.16 @@ -52,14 +49,13 @@
    1.17  import javax.ws.rs.Produces;
    1.18  import javax.ws.rs.QueryParam;
    1.19  import javax.ws.rs.core.MediaType;
    1.20 -import org.codehaus.jettison.json.JSONObject;
    1.21  
    1.22  /**
    1.23   *
    1.24   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.25   */
    1.26  public final class Games {
    1.27 -    private List<Game> games = new ArrayList<Game>();
    1.28 +    private final List<Game> games = new ArrayList<Game>();
    1.29      private final File dir;
    1.30      private static final Logger LOG = Logger.getLogger(Games.class.getName());
    1.31  
    1.32 @@ -79,11 +75,11 @@
    1.33      }
    1.34  
    1.35      @POST
    1.36 -    @Produces(MediaType.APPLICATION_JSON)
    1.37 -    public Game createGame(
    1.38 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.39 +    public GameId createGame(
    1.40          @QueryParam("white") String white,
    1.41          @QueryParam("black") String black
    1.42 -    ) {
    1.43 +    ) throws IOException {
    1.44          if (white == null) {
    1.45              throw new IllegalArgumentException("Must specify white");
    1.46          }
    1.47 @@ -91,8 +87,9 @@
    1.48              throw new IllegalArgumentException("Must specify black");
    1.49          }
    1.50          Game g = new Game(white, black);
    1.51 +        storeGame(g);
    1.52          games.add(g);
    1.53 -        return g;
    1.54 +        return g.getId();
    1.55      }
    1.56  
    1.57      @GET
    1.58 @@ -108,22 +105,19 @@
    1.59  
    1.60      @GET
    1.61      @Path("{id}")
    1.62 -    @Produces(MediaType.APPLICATION_JSON)
    1.63 -    public Object getBoardInfo(@PathParam("id") String id) {
    1.64 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.65 +    public Game getBoardInfo(@PathParam("id") String id) {
    1.66          Game g = findGame(id);
    1.67          if (g == null) {
    1.68              throw new IllegalArgumentException("Unknown game " + id);
    1.69          }
    1.70 -        Map<String,Object> data = new HashMap<String, Object>();
    1.71 -        data.put("board", g.getBoard().toString());
    1.72 -        data.put("game", g);
    1.73 -        return data;
    1.74 +        return g;
    1.75      }
    1.76  
    1.77      @PUT
    1.78      @Path("{id}")
    1.79 -    @Produces(MediaType.APPLICATION_JSON)
    1.80 -    public Game applyMove(
    1.81 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.82 +    public GameId applyMove(
    1.83          @PathParam("id") String id,
    1.84          @QueryParam("player") String player,
    1.85          @QueryParam("move") String move
    1.86 @@ -139,18 +133,26 @@
    1.87          } catch (IOException ex) {
    1.88              LOG.log(Level.WARNING, "Cannot store game " + id, ex);
    1.89          }
    1.90 -        return g;
    1.91 +        return g.getId();
    1.92      }
    1.93  
    1.94      @GET
    1.95 -    @Produces(MediaType.APPLICATION_JSON)
    1.96 +    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.97 +    public List<GameId> listGames() {
    1.98 +        List<GameId> arr = new ArrayList<GameId>(games.size());
    1.99 +        for (Game g : games) {
   1.100 +            arr.add(g.getId());
   1.101 +        }
   1.102 +        return arr;
   1.103 +    }
   1.104 +
   1.105      public List<Game> getGames() {
   1.106          return games;
   1.107      }
   1.108  
   1.109      private Game findGame(String id) {
   1.110          for (Game g : games) {
   1.111 -            if (g.getId().equals(id)) {
   1.112 +            if (g.getId().getId().equals(id)) {
   1.113                  return g;
   1.114              }
   1.115          }
   1.116 @@ -165,7 +167,7 @@
   1.117          for (;;) {
   1.118              String line = r.readLine();
   1.119              if (line == null) {
   1.120 -                break;
   1.121 +                line = "finish";
   1.122              }
   1.123              line = line.trim();
   1.124              if (line.length() == 0) {
   1.125 @@ -186,7 +188,11 @@
   1.126                  throw new IOException("Missing white and black identification in " + f);
   1.127              }
   1.128              if (g == null) {
   1.129 -                g = new Game(f.getName(), white, black);
   1.130 +                GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), GameResult.IN_PROGRESS);
   1.131 +                g = new Game(id);
   1.132 +            }
   1.133 +            if (line.equals("finish")) {
   1.134 +                break;
   1.135              }
   1.136              String[] moves = line.split(" ");
   1.137              if (moves.length == 0) {
   1.138 @@ -212,10 +218,11 @@
   1.139  
   1.140      private void storeGame(Game g) throws IOException {
   1.141          dir.mkdirs();
   1.142 -        File f = new File(dir, g.getId());
   1.143 +        File f = new File(dir, g.getId().getId());
   1.144          PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
   1.145 -        pw.println("# white: " + g.getWhite());
   1.146 -        pw.println("# black: " + g.getBlack());
   1.147 +        pw.println("# white: " + g.getId().getWhite());
   1.148 +        pw.println("# black: " + g.getId().getBlack());
   1.149 +        pw.println("# status: " + g.getId().getResult());
   1.150          int cnt = 0;
   1.151          for (Move m : g.getMoves()) {
   1.152              pw.print(m.toString());