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.WebApplicationException;
52 import javax.ws.rs.core.MediaType;
53 import javax.ws.rs.core.Response.Status;
57 * @author Jaroslav Tulach <jtulach@netbeans.org>
59 public final class Games {
60 private final Quoridor quoridor;
61 private final List<Game> games = new ArrayList<Game>();
62 private final File dir;
63 private static final Logger LOG = Logger.getLogger(Games.class.getName());
65 public Games(File dir, Quoridor quoridor) {
67 this.quoridor = quoridor;
68 File[] arr = dir.listFiles();
74 } catch (IOException ex) {
75 LOG.log(Level.WARNING, "Wrong game in " + f, ex);
82 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
83 public GameId createGame(
84 @QueryParam("loginID") String id,
85 @QueryParam("white") String white,
86 @QueryParam("black") String black
87 ) throws IOException {
88 String logUser = quoridor.isLoggedIn(id);
89 if (logUser == null) {
90 throw new WebApplicationException(Status.UNAUTHORIZED);
93 throw new WebApplicationException(Status.PRECONDITION_FAILED);
96 throw new WebApplicationException(Status.PRECONDITION_FAILED);
98 if (!logUser.equals(white) && !logUser.equals(black)) {
99 throw new WebApplicationException(Status.PRECONDITION_FAILED);
102 Game g = new Game(white, black);
110 @Produces(MediaType.TEXT_PLAIN)
111 public String getBoard(@PathParam("id") String id) {
112 Game g = findGame(id);
114 throw new IllegalArgumentException("Unknown game " + id);
116 return g.getBoard().toString();
121 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
122 public Game getBoardInfo(@PathParam("id") String id) {
123 Game g = findGame(id);
125 throw new IllegalArgumentException("Unknown game " + id);
132 @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
133 public GameId applyMove(
134 @QueryParam("loginID") String loginId,
135 @PathParam("id") String id,
136 @QueryParam("player") String player,
137 @QueryParam("move") String move
138 ) throws IllegalPositionException {
139 String logUser = quoridor.isLoggedIn(loginId);
140 if (logUser == null) {
141 throw new WebApplicationException(Status.UNAUTHORIZED);
143 if (!logUser.equals(player)) {
144 throw new WebApplicationException(Status.UNAUTHORIZED);
147 Game g = findGame(id);
149 throw new IllegalArgumentException("Unknown game " + id);
151 Move m = Move.valueOf(move);
152 g.apply(player, m, new Date());
155 } catch (IOException ex) {
156 LOG.log(Level.WARNING, "Cannot store game " + id, ex);
162 @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
163 public List<GameId> listGames() {
164 List<GameId> arr = new ArrayList<GameId>(games.size());
165 for (Game g : games) {
171 public List<Game> getGames() {
175 private Game findGame(String id) {
176 for (Game g : games) {
177 if (g.getId().getId().equals(id)) {
184 private Game readGame(File f) throws IOException {
185 BufferedReader r = new BufferedReader(new FileReader(f));
190 String line = r.readLine();
195 if (line.length() == 0) {
198 if (line.startsWith("# white: ")) {
199 white = line.substring(9);
202 if (line.startsWith("# black: ")) {
203 black = line.substring(9);
206 if (line.startsWith("#")) {
209 if (white == null || black == null) {
210 throw new IOException("Missing white and black identification in " + f);
213 GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove);
216 if (line.equals("finish")) {
219 String[] moves = line.split(" ");
220 if (moves.length == 0) {
223 if (moves.length > 2) {
224 throw new IOException("Too much moves on line: " + line);
227 g.apply(white, Move.valueOf(moves[0]), null);
228 if (moves.length == 2) {
229 g.apply(black, Move.valueOf(moves[1]), null);
231 } catch (IllegalPositionException ex) {
232 throw new IOException("Wrong move: " + ex.getMessage());
236 throw new IOException("No moves in " + f);
241 private void storeGame(Game g) throws IOException {
243 File f = new File(dir, g.getId().getId());
244 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
245 pw.println("# white: " + g.getId().getWhite());
246 pw.println("# black: " + g.getId().getBlack());
247 pw.println("# status: " + g.getId().getStatus());
249 for (Move m : g.getMoves()) {
250 pw.print(m.toString());
251 if (++cnt % 2 == 0) {