2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * The contents of this file are subject to the terms of either the GNU
5 * General Public License Version 2 only ("GPL") or the Common
6 * Development and Distribution License("CDDL") (collectively, the
7 * "License"). You may not use this file except in compliance with the
8 * License. You can obtain a copy of the License at
9 * http://www.netbeans.org/cddl-gplv2.html
10 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
11 * specific language governing permissions and limitations under the
12 * License. When distributing the software, include this License Header
13 * Notice in each file and include the License file at
14 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
15 * particular file as subject to the "Classpath" exception as provided
16 * by Sun in the GPL Version 2 section of the License file that
17 * accompanied this code. If applicable, add the following below the
18 * License Header, with the fields enclosed by brackets [] replaced by
19 * your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Portions Copyrighted 2009 Jaroslav Tulach
27 package cz.xelfi.quoridor.webidor.resources;
29 import cz.xelfi.quoridor.IllegalPositionException;
30 import cz.xelfi.quoridor.webidor.*;
31 import cz.xelfi.quoridor.Move;
32 import java.io.BufferedReader;
33 import java.io.BufferedWriter;
35 import java.io.FileReader;
36 import java.io.FileWriter;
37 import java.io.IOException;
38 import java.io.PrintWriter;
39 import java.util.ArrayList;
40 import java.util.Date;
41 import java.util.List;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
44 import javax.ws.rs.GET;
45 import javax.ws.rs.POST;
46 import javax.ws.rs.PUT;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.Produces;
50 import javax.ws.rs.QueryParam;
51 import javax.ws.rs.core.MediaType;
55 * @author Jaroslav Tulach <jtulach@netbeans.org>
57 public final class Games {
58 private final List<Game> games = new ArrayList<Game>();
59 private final File dir;
60 private static final Logger LOG = Logger.getLogger(Games.class.getName());
62 public Games(File dir) {
64 File[] arr = dir.listFiles();
70 } catch (IOException ex) {
71 LOG.log(Level.WARNING, "Wrong game in " + f, ex);
78 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
79 public GameId createGame(
80 @QueryParam("white") String white,
81 @QueryParam("black") String black
82 ) throws IOException {
84 throw new IllegalArgumentException("Must specify white");
87 throw new IllegalArgumentException("Must specify black");
89 Game g = new Game(white, black);
97 @Produces(MediaType.TEXT_PLAIN)
98 public String getBoard(@PathParam("id") String id) {
99 Game g = findGame(id);
101 throw new IllegalArgumentException("Unknown game " + id);
103 return g.getBoard().toString();
108 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
109 public Game getBoardInfo(@PathParam("id") String id) {
110 Game g = findGame(id);
112 throw new IllegalArgumentException("Unknown game " + id);
119 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
120 public GameId applyMove(
121 @PathParam("id") String id,
122 @QueryParam("player") String player,
123 @QueryParam("move") String move
124 ) throws IllegalPositionException {
125 Game g = findGame(id);
127 throw new IllegalArgumentException("Unknown game " + id);
129 Move m = Move.valueOf(move);
133 } catch (IOException ex) {
134 LOG.log(Level.WARNING, "Cannot store game " + id, ex);
140 @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
141 public List<GameId> listGames() {
142 List<GameId> arr = new ArrayList<GameId>(games.size());
143 for (Game g : games) {
149 public List<Game> getGames() {
153 private Game findGame(String id) {
154 for (Game g : games) {
155 if (g.getId().getId().equals(id)) {
162 private Game readGame(File f) throws IOException {
163 BufferedReader r = new BufferedReader(new FileReader(f));
168 String line = r.readLine();
173 if (line.length() == 0) {
176 if (line.startsWith("# white: ")) {
177 white = line.substring(9);
180 if (line.startsWith("# black: ")) {
181 black = line.substring(9);
184 if (line.startsWith("#")) {
187 if (white == null || black == null) {
188 throw new IOException("Missing white and black identification in " + f);
191 GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), GameStatus.whiteMove);
194 if (line.equals("finish")) {
197 String[] moves = line.split(" ");
198 if (moves.length == 0) {
201 if (moves.length > 2) {
202 throw new IOException("Too much moves on line: " + line);
205 g.apply(white, Move.valueOf(moves[0]));
206 if (moves.length == 2) {
207 g.apply(black, Move.valueOf(moves[1]));
209 } catch (IllegalPositionException ex) {
210 throw new IOException("Wrong move: " + ex.getMessage());
214 throw new IOException("No moves in " + f);
219 private void storeGame(Game g) throws IOException {
221 File f = new File(dir, g.getId().getId());
222 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
223 pw.println("# white: " + g.getId().getWhite());
224 pw.println("# black: " + g.getId().getBlack());
225 pw.println("# status: " + g.getId().getStatus());
227 for (Move m : g.getMoves()) {
228 pw.print(m.toString());
229 if (++cnt % 2 == 0) {