webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 25 Apr 2010 21:20:09 +0200
changeset 238 a4f6aca595e8
parent 189 6245e1b634aa
child 264 d60370059c3c
permissions -rw-r--r--
Support for permission.resign to allow the emailer cancel long running games
jtulach@36
     1
/*
jtulach@36
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@36
     3
 *
jtulach@36
     4
 * The contents of this file are subject to the terms of either the GNU
jtulach@36
     5
 * General Public License Version 2 only ("GPL") or the Common
jtulach@36
     6
 * Development and Distribution License("CDDL") (collectively, the
jtulach@36
     7
 * "License"). You may not use this file except in compliance with the
jtulach@36
     8
 * License. You can obtain a copy of the License at
jtulach@36
     9
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@36
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@36
    11
 * specific language governing permissions and limitations under the
jtulach@36
    12
 * License.  When distributing the software, include this License Header
jtulach@36
    13
 * Notice in each file and include the License file at
jtulach@36
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@36
    15
 * particular file as subject to the "Classpath" exception as provided
jtulach@36
    16
 * by Sun in the GPL Version 2 section of the License file that
jtulach@36
    17
 * accompanied this code. If applicable, add the following below the
jtulach@36
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@36
    19
 * your own identifying information:
jtulach@36
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@36
    21
 *
jtulach@36
    22
 * Contributor(s):
jtulach@36
    23
 *
jtulach@36
    24
 * Portions Copyrighted 2009 Jaroslav Tulach
jtulach@36
    25
 */
jtulach@36
    26
jtulach@36
    27
package cz.xelfi.quoridor.webidor.resources;
jtulach@36
    28
jaroslav@189
    29
import com.sun.jersey.api.json.JSONWithPadding;
jtulach@36
    30
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@36
    31
import cz.xelfi.quoridor.webidor.*;
jtulach@36
    32
import cz.xelfi.quoridor.Move;
jtulach@37
    33
import java.io.BufferedReader;
jtulach@37
    34
import java.io.BufferedWriter;
jtulach@37
    35
import java.io.File;
jtulach@117
    36
import java.io.FileInputStream;
jtulach@117
    37
import java.io.FileOutputStream;
jtulach@36
    38
import java.io.IOException;
jtulach@117
    39
import java.io.InputStream;
jtulach@117
    40
import java.io.InputStreamReader;
jtulach@117
    41
import java.io.OutputStreamWriter;
jtulach@37
    42
import java.io.PrintWriter;
jtulach@117
    43
import java.io.Writer;
jtulach@36
    44
import java.util.ArrayList;
jaroslav@96
    45
import java.util.Collections;
jaroslav@48
    46
import java.util.Date;
jtulach@36
    47
import java.util.List;
jtulach@37
    48
import java.util.logging.Level;
jtulach@37
    49
import java.util.logging.Logger;
jaroslav@115
    50
import java.util.regex.Matcher;
jaroslav@115
    51
import java.util.regex.Pattern;
jaroslav@100
    52
import javax.ws.rs.DefaultValue;
jtulach@36
    53
import javax.ws.rs.GET;
jtulach@36
    54
import javax.ws.rs.POST;
jtulach@36
    55
import javax.ws.rs.PUT;
jtulach@36
    56
import javax.ws.rs.Path;
jtulach@36
    57
import javax.ws.rs.PathParam;
jtulach@36
    58
import javax.ws.rs.Produces;
jtulach@36
    59
import javax.ws.rs.QueryParam;
jtulach@82
    60
import javax.ws.rs.WebApplicationException;
jaroslav@189
    61
import javax.ws.rs.core.GenericEntity;
jtulach@36
    62
import javax.ws.rs.core.MediaType;
jtulach@82
    63
import javax.ws.rs.core.Response.Status;
jtulach@36
    64
jtulach@36
    65
/**
jtulach@36
    66
 *
jtulach@36
    67
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@36
    68
 */
jtulach@36
    69
public final class Games {
jtulach@82
    70
    private final Quoridor quoridor;
jaroslav@48
    71
    private final List<Game> games = new ArrayList<Game>();
jtulach@37
    72
    private final File dir;
jtulach@37
    73
    private static final Logger LOG = Logger.getLogger(Games.class.getName());
jtulach@37
    74
jtulach@82
    75
    public Games(File dir, Quoridor quoridor) {
jtulach@37
    76
        this.dir = dir;
jtulach@82
    77
        this.quoridor = quoridor;
jtulach@37
    78
        File[] arr = dir.listFiles();
jtulach@37
    79
        if (arr != null) {
jtulach@37
    80
            for (File f : arr) {
jtulach@37
    81
                try {
jtulach@37
    82
                    Game g = readGame(f);
jtulach@37
    83
                    games.add(g);
jtulach@37
    84
                } catch (IOException ex) {
jtulach@37
    85
                    LOG.log(Level.WARNING, "Wrong game in " + f, ex);
jtulach@37
    86
                }
jtulach@37
    87
            }
jtulach@37
    88
        }
jtulach@37
    89
    }
jtulach@36
    90
jtulach@36
    91
    @POST
jaroslav@48
    92
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
    93
    public GameId createGame(
jtulach@82
    94
        @QueryParam("loginID") String id,
jtulach@38
    95
        @QueryParam("white") String white,
jtulach@38
    96
        @QueryParam("black") String black
jaroslav@48
    97
    ) throws IOException {
jtulach@82
    98
        String logUser = quoridor.isLoggedIn(id);
jtulach@82
    99
        if (logUser == null) {
jtulach@82
   100
            throw new WebApplicationException(Status.UNAUTHORIZED);
jtulach@82
   101
        }
jtulach@38
   102
        if (white == null) {
jtulach@82
   103
            throw new WebApplicationException(Status.PRECONDITION_FAILED);
jtulach@38
   104
        }
jtulach@38
   105
        if (black == null) {
jtulach@82
   106
            throw new WebApplicationException(Status.PRECONDITION_FAILED);
jtulach@38
   107
        }
jtulach@82
   108
        if (!logUser.equals(white) && !logUser.equals(black)) {
jtulach@82
   109
            throw new WebApplicationException(Status.PRECONDITION_FAILED);
jtulach@82
   110
        }
jtulach@82
   111
jtulach@38
   112
        Game g = new Game(white, black);
jaroslav@48
   113
        storeGame(g);
jtulach@36
   114
        games.add(g);
jaroslav@48
   115
        return g.getId();
jtulach@36
   116
    }
jtulach@36
   117
jtulach@38
   118
    @GET
jtulach@38
   119
    @Path("{id}")
jtulach@38
   120
    @Produces(MediaType.TEXT_PLAIN)
jaroslav@106
   121
    public String getBoard(
jaroslav@106
   122
        @PathParam("id") String id,
jaroslav@106
   123
        @QueryParam("move") @DefaultValue("-1") int move
jaroslav@106
   124
    ) {
jaroslav@106
   125
        Game g = findGame(id, move);
jtulach@38
   126
        if (g == null) {
jtulach@38
   127
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@38
   128
        }
jtulach@38
   129
        return g.getBoard().toString();
jtulach@38
   130
    }
jtulach@38
   131
jaroslav@100
   132
    public Game getBoardInfo(
jaroslav@162
   133
        @QueryParam("loginID") @DefaultValue("") String loginId,
jaroslav@100
   134
        @PathParam("id") String id,
jaroslav@100
   135
        @QueryParam("move") @DefaultValue("-1") int move
jaroslav@171
   136
    ) throws IOException {
jaroslav@162
   137
        Game g = findGame(id, move);
jtulach@166
   138
        if (canSee(g.getId(), loginId)) {
jaroslav@162
   139
            return g;
jaroslav@162
   140
        }
jtulach@166
   141
        throw new WebApplicationException(Status.UNAUTHORIZED);
jtulach@166
   142
    }
jtulach@166
   143
jaroslav@189
   144
    @GET
jaroslav@189
   145
    @Path("{id}")
jaroslav@189
   146
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
   147
    public JSONWithPadding getBoardInfo(
jaroslav@189
   148
        @QueryParam("callback") String callback,
jaroslav@189
   149
        @QueryParam("loginID") @DefaultValue("") String loginId,
jaroslav@189
   150
        @PathParam("id") String id,
jaroslav@189
   151
        @QueryParam("move") @DefaultValue("-1") int move
jaroslav@189
   152
    ) throws IOException {
jaroslav@189
   153
        return new JSONWithPadding(getBoardInfo(loginId, id, move), callback);
jaroslav@189
   154
    }
jaroslav@189
   155
jaroslav@189
   156
jaroslav@171
   157
    private boolean canSee(GameId id, String loginId) throws IOException {
jtulach@166
   158
        if (!id.isFinished()) {
jtulach@166
   159
            return true;
jtulach@166
   160
        }
jaroslav@162
   161
        String logUser = quoridor.isLoggedIn(loginId);
jaroslav@162
   162
        if (logUser == null) {
jtulach@166
   163
            return false;
jaroslav@162
   164
        }
jtulach@166
   165
        if (logUser.equals(id.getWhite())) {
jtulach@166
   166
            return true;
jaroslav@162
   167
        }
jtulach@166
   168
        if (logUser.equals(id.getBlack())) {
jtulach@166
   169
            return true;
jaroslav@162
   170
        }
jaroslav@171
   171
        User info = quoridor.getUsers().getUserInfo(loginId, logUser);
jaroslav@171
   172
        if (info != null && info.hasPermission("games")) {
jaroslav@171
   173
            return true;
jaroslav@171
   174
        }
jtulach@166
   175
        return false;
jaroslav@46
   176
    }
jaroslav@46
   177
jtulach@36
   178
    @PUT
jtulach@36
   179
    @Path("{id}")
jaroslav@48
   180
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
   181
    public GameId applyMove(
jtulach@82
   182
        @QueryParam("loginID") String loginId,
jtulach@38
   183
        @PathParam("id") String id,
jtulach@38
   184
        @QueryParam("player") String player,
jaroslav@115
   185
        @QueryParam("move") String move,
jaroslav@115
   186
        @QueryParam("comment") String comment
jtulach@238
   187
    ) throws IllegalPositionException, IOException {
jtulach@82
   188
        String logUser = quoridor.isLoggedIn(loginId);
jtulach@82
   189
        if (logUser == null) {
jtulach@82
   190
            throw new WebApplicationException(Status.UNAUTHORIZED);
jtulach@82
   191
        }
jtulach@82
   192
        if (!logUser.equals(player)) {
jtulach@238
   193
            User info = quoridor.getUsers().getUserInfo(loginId, logUser);
jtulach@238
   194
            if (info == null || !info.hasPermission("resign")) {
jtulach@238
   195
                throw new WebApplicationException(Status.UNAUTHORIZED);
jtulach@238
   196
            }
jtulach@82
   197
        }
jaroslav@115
   198
        if (comment == null && move == null) {
jaroslav@115
   199
            throw new WebApplicationException(Status.BAD_REQUEST);
jaroslav@115
   200
        }
jtulach@82
   201
jtulach@36
   202
        Game g = findGame(id);
jtulach@36
   203
        if (g == null) {
jtulach@36
   204
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@36
   205
        }
jaroslav@115
   206
        if (move != null) {
jaroslav@115
   207
            Move m = Move.valueOf(move);
jaroslav@115
   208
            g.apply(player, m, new Date());
jaroslav@115
   209
        }
jaroslav@115
   210
        if (comment != null) {
jaroslav@115
   211
            g.comment(player, comment, new Date());
jaroslav@115
   212
        }
jtulach@37
   213
        try {
jtulach@37
   214
            storeGame(g);
jtulach@37
   215
        } catch (IOException ex) {
jtulach@37
   216
            LOG.log(Level.WARNING, "Cannot store game " + id, ex);
jtulach@37
   217
        }
jaroslav@48
   218
        return g.getId();
jtulach@36
   219
    }
jtulach@36
   220
jtulach@36
   221
    @GET
jaroslav@189
   222
    @Produces({ "application/x-javascript", MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@189
   223
    public JSONWithPadding listGames(
jaroslav@189
   224
        @QueryParam("callback") String callback,
jtulach@166
   225
        @DefaultValue("") @QueryParam("loginID") String loginId,
jaroslav@128
   226
        @DefaultValue("") @QueryParam("status") String status
jaroslav@171
   227
    ) throws IOException {
jaroslav@48
   228
        List<GameId> arr = new ArrayList<GameId>(games.size());
jaroslav@48
   229
        for (Game g : games) {
jtulach@166
   230
            if (!canSee(g.getId(), loginId)) {
jtulach@166
   231
                continue;
jtulach@166
   232
            }
jaroslav@128
   233
            if (status.length() == 0 || g.getId().getStatus().toString().equals(status)) {
jaroslav@128
   234
                arr.add(g.getId());
jaroslav@128
   235
            }
jaroslav@48
   236
        }
jaroslav@96
   237
        Collections.sort(arr, GameId.NEWEST_FIRST);
jaroslav@189
   238
        return new JSONWithPadding(new GenericEntity<List<GameId>>(arr) {}, callback);
jaroslav@48
   239
    }
jaroslav@48
   240
jtulach@36
   241
    public List<Game> getGames() {
jtulach@36
   242
        return games;
jtulach@36
   243
    }
jtulach@36
   244
jtulach@36
   245
    private Game findGame(String id) {
jtulach@36
   246
        for (Game g : games) {
jaroslav@48
   247
            if (g.getId().getId().equals(id)) {
jtulach@36
   248
                return g;
jtulach@36
   249
            }
jtulach@36
   250
        }
jtulach@36
   251
        return null;
jtulach@36
   252
    }
jaroslav@100
   253
    private Game findGame(String id, int move) {
jaroslav@100
   254
        Game g = findGame(id);
jaroslav@100
   255
        if (g == null) {
jaroslav@100
   256
            throw new IllegalArgumentException("Unknown game " + id);
jaroslav@100
   257
        }
jaroslav@100
   258
        try {
jaroslav@100
   259
            return move == -1 ? g : g.snapshot(move);
jaroslav@100
   260
        } catch (IllegalPositionException ex) {
jaroslav@100
   261
            Logger.getLogger(Games.class.getName()).log(Level.SEVERE, null, ex);
jaroslav@100
   262
            return null;
jaroslav@100
   263
        }
jaroslav@100
   264
    }
jtulach@36
   265
jaroslav@115
   266
    private static final Pattern saidWho = Pattern.compile("# *([^ ]*) *@(.*):$");
jaroslav@115
   267
jtulach@37
   268
    private Game readGame(File f) throws IOException {
jtulach@117
   269
        InputStream is = new FileInputStream(f);
jtulach@117
   270
        BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
jtulach@37
   271
        String white = null;
jtulach@37
   272
        String black = null;
jtulach@37
   273
        Game g = null;
jaroslav@115
   274
        String who = null;
jaroslav@115
   275
        String when = null;
jtulach@37
   276
        for (;;) {
jtulach@37
   277
            String line = r.readLine();
jtulach@37
   278
            if (line == null) {
jaroslav@48
   279
                line = "finish";
jtulach@37
   280
            }
jtulach@39
   281
            line = line.trim();
jtulach@39
   282
            if (line.length() == 0) {
jtulach@39
   283
                continue;
jtulach@39
   284
            }
jtulach@37
   285
            if (line.startsWith("# white: ")) {
jtulach@37
   286
                white = line.substring(9);
jtulach@37
   287
                continue;
jtulach@37
   288
            }
jtulach@37
   289
            if (line.startsWith("# black: ")) {
jtulach@37
   290
                black = line.substring(9);
jtulach@37
   291
                continue;
jtulach@37
   292
            }
jtulach@37
   293
            if (line.startsWith("#")) {
jaroslav@115
   294
                Matcher m =saidWho.matcher(line);
jaroslav@115
   295
                if (m.matches()) {
jaroslav@115
   296
                    who = m.group(1);
jaroslav@115
   297
                    when = m.group(2);
jaroslav@115
   298
                    continue;
jaroslav@115
   299
                }
jaroslav@115
   300
                if (g == null) {
jaroslav@115
   301
                    continue;
jaroslav@115
   302
                }
jaroslav@115
   303
                if (line.startsWith("# ")) {
jaroslav@115
   304
                    line = line.substring(2);
jaroslav@115
   305
                } else {
jaroslav@115
   306
                    line = line.substring(1);
jaroslav@115
   307
                }
jaroslav@115
   308
                Date d = new Date();
jaroslav@115
   309
                try {
jaroslav@115
   310
                    if (when != null) {
jaroslav@115
   311
                        d = new Date(Date.parse(when));
jaroslav@115
   312
                    }
jaroslav@115
   313
                } catch (IllegalArgumentException ex) {
jaroslav@115
   314
                    LOG.warning("Unparseable date " + when + " in " + f);
jaroslav@115
   315
                }
jaroslav@115
   316
                g.comment(who, line, d);
jaroslav@115
   317
                who = null;
jaroslav@115
   318
                when = null;
jtulach@37
   319
                continue;
jtulach@37
   320
            }
jtulach@37
   321
            if (white == null || black == null) {
jtulach@37
   322
                throw new IOException("Missing white and black identification in " + f);
jtulach@37
   323
            }
jtulach@37
   324
            if (g == null) {
jtulach@164
   325
                GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), new Date(f.lastModified()), GameStatus.whiteMove, 0, false);
jaroslav@48
   326
                g = new Game(id);
jaroslav@48
   327
            }
jaroslav@114
   328
            int hash = line.indexOf('#');
jaroslav@114
   329
            if (hash >= 0) {
jaroslav@114
   330
                line = line.substring(0, hash);
jaroslav@114
   331
            }
jaroslav@48
   332
            if (line.equals("finish")) {
jaroslav@48
   333
                break;
jtulach@37
   334
            }
jtulach@37
   335
            String[] moves = line.split(" ");
jtulach@37
   336
            if (moves.length == 0) {
jtulach@37
   337
                continue;
jtulach@37
   338
            }
jtulach@37
   339
            if (moves.length > 2) {
jtulach@37
   340
                throw new IOException("Too much moves on line: " + line);
jtulach@37
   341
            }
jtulach@37
   342
            try {
jaroslav@114
   343
                if (!"...".equals(moves[0])) {
jaroslav@114
   344
                    g.apply(white, Move.valueOf(moves[0]), null);
jaroslav@114
   345
                }
jtulach@37
   346
                if (moves.length == 2) {
jtulach@79
   347
                    g.apply(black, Move.valueOf(moves[1]), null);
jtulach@37
   348
                }
jtulach@37
   349
            } catch (IllegalPositionException ex) {
jtulach@37
   350
                throw new IOException("Wrong move: " + ex.getMessage());
jtulach@37
   351
            }
jtulach@37
   352
        }
jtulach@37
   353
        if (g == null) {
jtulach@37
   354
            throw new IOException("No moves in " + f);
jtulach@37
   355
        }
jtulach@37
   356
        return g;
jtulach@36
   357
    }
jtulach@36
   358
jtulach@37
   359
    private void storeGame(Game g) throws IOException {
jtulach@37
   360
        dir.mkdirs();
jaroslav@48
   361
        File f = new File(dir, g.getId().getId());
jaroslav@115
   362
        storeGame(g, f);
jaroslav@115
   363
    }
jaroslav@115
   364
jaroslav@115
   365
    final void storeGame(Game g, File f) throws IOException {
jtulach@117
   366
        FileOutputStream os = new FileOutputStream(f);
jtulach@117
   367
        Writer w = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
jtulach@117
   368
        PrintWriter pw = new PrintWriter(w);
jaroslav@48
   369
        pw.println("# white: " + g.getId().getWhite());
jaroslav@48
   370
        pw.println("# black: " + g.getId().getBlack());
jtulach@77
   371
        pw.println("# status: " + g.getId().getStatus());
jtulach@37
   372
        int cnt = 0;
jaroslav@115
   373
        boolean separate = true;
jaroslav@115
   374
        for (CommentedMove m : g.getMoves()) {
jaroslav@115
   375
            if (!separate && cnt % 2 == 1) {
jaroslav@115
   376
                pw.print("... ");
jaroslav@115
   377
            }
jaroslav@115
   378
            separate = true;
jaroslav@115
   379
            pw.print(m.getMove().toString());
jaroslav@115
   380
            List<Note> notes = m.getComments();
jaroslav@115
   381
            if (notes != null) {
jaroslav@115
   382
                separate = false;
jtulach@37
   383
                pw.println();
jaroslav@115
   384
                for (Note n : notes) {
jaroslav@115
   385
                    pw.print ("# ");
jaroslav@115
   386
                    pw.print(n.getWho());
jaroslav@115
   387
                    pw.print("@");
jaroslav@115
   388
                    pw.print(n.getWhen());
jaroslav@115
   389
                    pw.println(":");
jaroslav@115
   390
                    for (String l : n.getComment().split("\n")) {
jaroslav@115
   391
                        pw.print("# ");
jaroslav@115
   392
                        pw.println(l);
jaroslav@115
   393
                    }
jaroslav@115
   394
                }
jaroslav@115
   395
            }
jaroslav@115
   396
jaroslav@115
   397
            cnt++;
jaroslav@115
   398
jaroslav@115
   399
            if (separate) {
jaroslav@115
   400
                if (cnt % 2 == 0) {
jaroslav@115
   401
                    pw.println();
jaroslav@115
   402
                } else {
jaroslav@115
   403
                    pw.print(' ');
jaroslav@115
   404
                }
jtulach@37
   405
            }
jtulach@37
   406
        }
jtulach@37
   407
        pw.println();
jtulach@37
   408
        pw.println();
jtulach@37
   409
        pw.flush();
jtulach@37
   410
        pw.close();
jtulach@117
   411
        w.close();
jtulach@37
   412
    }
jtulach@36
   413
}