webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 08 Dec 2009 15:34:50 +0100
branchstrict-games-access
changeset 162 c1bfbe98152b
parent 131 19e81456eef2
child 164 2949998db4f6
permissions -rw-r--r--
Only active games are visible for regular users. Need a bit more work to allow them to see history of active games.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     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]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor.resources;
    28 
    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;
    34 import java.io.File;
    35 import java.io.FileInputStream;
    36 import java.io.FileOutputStream;
    37 import java.io.IOException;
    38 import java.io.InputStream;
    39 import java.io.InputStreamReader;
    40 import java.io.OutputStreamWriter;
    41 import java.io.PrintWriter;
    42 import java.io.Writer;
    43 import java.util.ArrayList;
    44 import java.util.Collections;
    45 import java.util.Date;
    46 import java.util.List;
    47 import java.util.logging.Level;
    48 import java.util.logging.Logger;
    49 import java.util.regex.Matcher;
    50 import java.util.regex.Pattern;
    51 import javax.ws.rs.DefaultValue;
    52 import javax.ws.rs.GET;
    53 import javax.ws.rs.POST;
    54 import javax.ws.rs.PUT;
    55 import javax.ws.rs.Path;
    56 import javax.ws.rs.PathParam;
    57 import javax.ws.rs.Produces;
    58 import javax.ws.rs.QueryParam;
    59 import javax.ws.rs.WebApplicationException;
    60 import javax.ws.rs.core.MediaType;
    61 import javax.ws.rs.core.Response.Status;
    62 
    63 /**
    64  *
    65  * @author Jaroslav Tulach <jtulach@netbeans.org>
    66  */
    67 public final class Games {
    68     private final Quoridor quoridor;
    69     private final List<Game> games = new ArrayList<Game>();
    70     private final File dir;
    71     private static final Logger LOG = Logger.getLogger(Games.class.getName());
    72 
    73     public Games(File dir, Quoridor quoridor) {
    74         this.dir = dir;
    75         this.quoridor = quoridor;
    76         File[] arr = dir.listFiles();
    77         if (arr != null) {
    78             for (File f : arr) {
    79                 try {
    80                     Game g = readGame(f);
    81                     games.add(g);
    82                 } catch (IOException ex) {
    83                     LOG.log(Level.WARNING, "Wrong game in " + f, ex);
    84                 }
    85             }
    86         }
    87     }
    88 
    89     @POST
    90     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    91     public GameId createGame(
    92         @QueryParam("loginID") String id,
    93         @QueryParam("white") String white,
    94         @QueryParam("black") String black
    95     ) throws IOException {
    96         String logUser = quoridor.isLoggedIn(id);
    97         if (logUser == null) {
    98             throw new WebApplicationException(Status.UNAUTHORIZED);
    99         }
   100         if (white == null) {
   101             throw new WebApplicationException(Status.PRECONDITION_FAILED);
   102         }
   103         if (black == null) {
   104             throw new WebApplicationException(Status.PRECONDITION_FAILED);
   105         }
   106         if (!logUser.equals(white) && !logUser.equals(black)) {
   107             throw new WebApplicationException(Status.PRECONDITION_FAILED);
   108         }
   109 
   110         Game g = new Game(white, black);
   111         storeGame(g);
   112         games.add(g);
   113         return g.getId();
   114     }
   115 
   116     @GET
   117     @Path("{id}")
   118     @Produces(MediaType.TEXT_PLAIN)
   119     public String getBoard(
   120         @PathParam("id") String id,
   121         @QueryParam("move") @DefaultValue("-1") int move
   122     ) {
   123         Game g = findGame(id, move);
   124         if (g == null) {
   125             throw new IllegalArgumentException("Unknown game " + id);
   126         }
   127         return g.getBoard().toString();
   128     }
   129 
   130     @GET
   131     @Path("{id}")
   132     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   133     public Game getBoardInfo(
   134         @QueryParam("loginID") @DefaultValue("") String loginId,
   135         @PathParam("id") String id,
   136         @QueryParam("move") @DefaultValue("-1") int move
   137     ) {
   138         Game g = findGame(id, move);
   139         if (g.getId().getStatus().isInProgress()) {
   140             return g;
   141         }
   142         String logUser = quoridor.isLoggedIn(loginId);
   143         if (logUser == null) {
   144             throw new WebApplicationException(Status.UNAUTHORIZED);
   145         }
   146         if (logUser.equals(g.getId().getWhite())) {
   147             return g;
   148         }
   149         if (logUser.equals(g.getId().getBlack())) {
   150             return g;
   151         }
   152         throw new WebApplicationException(Status.UNAUTHORIZED);
   153     }
   154 
   155     @PUT
   156     @Path("{id}")
   157     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   158     public GameId applyMove(
   159         @QueryParam("loginID") String loginId,
   160         @PathParam("id") String id,
   161         @QueryParam("player") String player,
   162         @QueryParam("move") String move,
   163         @QueryParam("comment") String comment
   164     ) throws IllegalPositionException {
   165         String logUser = quoridor.isLoggedIn(loginId);
   166         if (logUser == null) {
   167             throw new WebApplicationException(Status.UNAUTHORIZED);
   168         }
   169         if (!logUser.equals(player)) {
   170             throw new WebApplicationException(Status.UNAUTHORIZED);
   171         }
   172         if (comment == null && move == null) {
   173             throw new WebApplicationException(Status.BAD_REQUEST);
   174         }
   175 
   176         Game g = findGame(id);
   177         if (g == null) {
   178             throw new IllegalArgumentException("Unknown game " + id);
   179         }
   180         if (move != null) {
   181             Move m = Move.valueOf(move);
   182             g.apply(player, m, new Date());
   183         }
   184         if (comment != null) {
   185             g.comment(player, comment, new Date());
   186         }
   187         try {
   188             storeGame(g);
   189         } catch (IOException ex) {
   190             LOG.log(Level.WARNING, "Cannot store game " + id, ex);
   191         }
   192         return g.getId();
   193     }
   194 
   195     @GET
   196     @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
   197     public List<GameId> listGames(
   198         @DefaultValue("") @QueryParam("status") String status
   199     ) {
   200         List<GameId> arr = new ArrayList<GameId>(games.size());
   201         for (Game g : games) {
   202             if (status.length() == 0 || g.getId().getStatus().toString().equals(status)) {
   203                 arr.add(g.getId());
   204             }
   205         }
   206         Collections.sort(arr, GameId.NEWEST_FIRST);
   207         return arr;
   208     }
   209 
   210     public List<Game> getGames() {
   211         return games;
   212     }
   213 
   214     private Game findGame(String id) {
   215         for (Game g : games) {
   216             if (g.getId().getId().equals(id)) {
   217                 return g;
   218             }
   219         }
   220         return null;
   221     }
   222     private Game findGame(String id, int move) {
   223         Game g = findGame(id);
   224         if (g == null) {
   225             throw new IllegalArgumentException("Unknown game " + id);
   226         }
   227         try {
   228             return move == -1 ? g : g.snapshot(move);
   229         } catch (IllegalPositionException ex) {
   230             Logger.getLogger(Games.class.getName()).log(Level.SEVERE, null, ex);
   231             return null;
   232         }
   233     }
   234 
   235     private static final Pattern saidWho = Pattern.compile("# *([^ ]*) *@(.*):$");
   236 
   237     private Game readGame(File f) throws IOException {
   238         InputStream is = new FileInputStream(f);
   239         BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
   240         String white = null;
   241         String black = null;
   242         Game g = null;
   243         String who = null;
   244         String when = null;
   245         for (;;) {
   246             String line = r.readLine();
   247             if (line == null) {
   248                 line = "finish";
   249             }
   250             line = line.trim();
   251             if (line.length() == 0) {
   252                 continue;
   253             }
   254             if (line.startsWith("# white: ")) {
   255                 white = line.substring(9);
   256                 continue;
   257             }
   258             if (line.startsWith("# black: ")) {
   259                 black = line.substring(9);
   260                 continue;
   261             }
   262             if (line.startsWith("#")) {
   263                 Matcher m =saidWho.matcher(line);
   264                 if (m.matches()) {
   265                     who = m.group(1);
   266                     when = m.group(2);
   267                     continue;
   268                 }
   269                 if (g == null) {
   270                     continue;
   271                 }
   272                 if (line.startsWith("# ")) {
   273                     line = line.substring(2);
   274                 } else {
   275                     line = line.substring(1);
   276                 }
   277                 Date d = new Date();
   278                 try {
   279                     if (when != null) {
   280                         d = new Date(Date.parse(when));
   281                     }
   282                 } catch (IllegalArgumentException ex) {
   283                     LOG.warning("Unparseable date " + when + " in " + f);
   284                 }
   285                 g.comment(who, line, d);
   286                 who = null;
   287                 when = null;
   288                 continue;
   289             }
   290             if (white == null || black == null) {
   291                 throw new IOException("Missing white and black identification in " + f);
   292             }
   293             if (g == null) {
   294                 GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0);
   295                 g = new Game(id);
   296             }
   297             int hash = line.indexOf('#');
   298             if (hash >= 0) {
   299                 line = line.substring(0, hash);
   300             }
   301             if (line.equals("finish")) {
   302                 break;
   303             }
   304             String[] moves = line.split(" ");
   305             if (moves.length == 0) {
   306                 continue;
   307             }
   308             if (moves.length > 2) {
   309                 throw new IOException("Too much moves on line: " + line);
   310             }
   311             try {
   312                 if (!"...".equals(moves[0])) {
   313                     g.apply(white, Move.valueOf(moves[0]), null);
   314                 }
   315                 if (moves.length == 2) {
   316                     g.apply(black, Move.valueOf(moves[1]), null);
   317                 }
   318             } catch (IllegalPositionException ex) {
   319                 throw new IOException("Wrong move: " + ex.getMessage());
   320             }
   321         }
   322         if (g == null) {
   323             throw new IOException("No moves in " + f);
   324         }
   325         return g;
   326     }
   327 
   328     private void storeGame(Game g) throws IOException {
   329         dir.mkdirs();
   330         File f = new File(dir, g.getId().getId());
   331         storeGame(g, f);
   332     }
   333 
   334     final void storeGame(Game g, File f) throws IOException {
   335         FileOutputStream os = new FileOutputStream(f);
   336         Writer w = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
   337         PrintWriter pw = new PrintWriter(w);
   338         pw.println("# white: " + g.getId().getWhite());
   339         pw.println("# black: " + g.getId().getBlack());
   340         pw.println("# status: " + g.getId().getStatus());
   341         int cnt = 0;
   342         boolean separate = true;
   343         for (CommentedMove m : g.getMoves()) {
   344             if (!separate && cnt % 2 == 1) {
   345                 pw.print("... ");
   346             }
   347             separate = true;
   348             pw.print(m.getMove().toString());
   349             List<Note> notes = m.getComments();
   350             if (notes != null) {
   351                 separate = false;
   352                 pw.println();
   353                 for (Note n : notes) {
   354                     pw.print ("# ");
   355                     pw.print(n.getWho());
   356                     pw.print("@");
   357                     pw.print(n.getWhen());
   358                     pw.println(":");
   359                     for (String l : n.getComment().split("\n")) {
   360                         pw.print("# ");
   361                         pw.println(l);
   362                     }
   363                 }
   364             }
   365 
   366             cnt++;
   367 
   368             if (separate) {
   369                 if (cnt % 2 == 0) {
   370                     pw.println();
   371                 } else {
   372                     pw.print(' ');
   373                 }
   374             }
   375         }
   376         pw.println();
   377         pw.println();
   378         pw.flush();
   379         pw.close();
   380         w.close();
   381     }
   382 }