webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 29 Jul 2009 18:48:04 +0200
changeset 39 6b889f2717e9
parent 38 373f537e0153
child 46 71e4cf307c93
permissions -rw-r--r--
Ability to read stored 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
jtulach@36
    29
import cz.xelfi.quoridor.IllegalPositionException;
jtulach@36
    30
import cz.xelfi.quoridor.webidor.*;
jtulach@36
    31
import cz.xelfi.quoridor.Move;
jtulach@37
    32
import java.io.BufferedReader;
jtulach@37
    33
import java.io.BufferedWriter;
jtulach@37
    34
import java.io.File;
jtulach@37
    35
import java.io.FileReader;
jtulach@37
    36
import java.io.FileWriter;
jtulach@36
    37
import java.io.IOException;
jtulach@37
    38
import java.io.PrintWriter;
jtulach@36
    39
import java.util.ArrayList;
jtulach@39
    40
import java.util.Arrays;
jtulach@36
    41
import java.util.List;
jtulach@37
    42
import java.util.logging.Level;
jtulach@37
    43
import java.util.logging.Logger;
jtulach@36
    44
import javax.ws.rs.GET;
jtulach@36
    45
import javax.ws.rs.POST;
jtulach@36
    46
import javax.ws.rs.PUT;
jtulach@36
    47
import javax.ws.rs.Path;
jtulach@36
    48
import javax.ws.rs.PathParam;
jtulach@36
    49
import javax.ws.rs.Produces;
jtulach@36
    50
import javax.ws.rs.QueryParam;
jtulach@36
    51
import javax.ws.rs.core.MediaType;
jtulach@36
    52
jtulach@36
    53
/**
jtulach@36
    54
 *
jtulach@36
    55
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jtulach@36
    56
 */
jtulach@36
    57
public final class Games {
jtulach@36
    58
    private List<Game> games = new ArrayList<Game>();
jtulach@37
    59
    private final File dir;
jtulach@37
    60
    private static final Logger LOG = Logger.getLogger(Games.class.getName());
jtulach@37
    61
jtulach@37
    62
    public Games(File dir) {
jtulach@37
    63
        this.dir = dir;
jtulach@37
    64
        File[] arr = dir.listFiles();
jtulach@37
    65
        if (arr != null) {
jtulach@37
    66
            for (File f : arr) {
jtulach@37
    67
                try {
jtulach@37
    68
                    Game g = readGame(f);
jtulach@37
    69
                    games.add(g);
jtulach@37
    70
                } catch (IOException ex) {
jtulach@37
    71
                    LOG.log(Level.WARNING, "Wrong game in " + f, ex);
jtulach@37
    72
                }
jtulach@37
    73
            }
jtulach@37
    74
        }
jtulach@37
    75
    }
jtulach@36
    76
jtulach@36
    77
    @POST
jtulach@38
    78
    @Produces(MediaType.APPLICATION_JSON)
jtulach@38
    79
    public Game createGame(
jtulach@38
    80
        @QueryParam("white") String white,
jtulach@38
    81
        @QueryParam("black") String black
jtulach@38
    82
    ) {
jtulach@38
    83
        if (white == null) {
jtulach@38
    84
            throw new IllegalArgumentException("Must specify white");
jtulach@38
    85
        }
jtulach@38
    86
        if (black == null) {
jtulach@38
    87
            throw new IllegalArgumentException("Must specify black");
jtulach@38
    88
        }
jtulach@38
    89
        Game g = new Game(white, black);
jtulach@36
    90
        games.add(g);
jtulach@36
    91
        return g;
jtulach@36
    92
    }
jtulach@36
    93
jtulach@38
    94
    @GET
jtulach@38
    95
    @Path("{id}")
jtulach@38
    96
    @Produces(MediaType.TEXT_PLAIN)
jtulach@38
    97
    public String getBoard(@PathParam("id") String id) {
jtulach@38
    98
        Game g = findGame(id);
jtulach@38
    99
        if (g == null) {
jtulach@38
   100
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@38
   101
        }
jtulach@38
   102
        return g.getBoard().toString();
jtulach@38
   103
    }
jtulach@38
   104
jtulach@36
   105
    @PUT
jtulach@36
   106
    @Path("{id}")
jtulach@38
   107
    @Produces(MediaType.APPLICATION_JSON)
jtulach@38
   108
    public Game applyMove(
jtulach@38
   109
        @PathParam("id") String id,
jtulach@38
   110
        @QueryParam("player") String player,
jtulach@38
   111
        @QueryParam("move") String move
jtulach@38
   112
    ) throws IllegalPositionException {
jtulach@36
   113
        Game g = findGame(id);
jtulach@36
   114
        if (g == null) {
jtulach@36
   115
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@36
   116
        }
jtulach@36
   117
        Move m = Move.valueOf(move);
jtulach@36
   118
        g.apply(player, m);
jtulach@37
   119
        try {
jtulach@37
   120
            storeGame(g);
jtulach@37
   121
        } catch (IOException ex) {
jtulach@37
   122
            LOG.log(Level.WARNING, "Cannot store game " + id, ex);
jtulach@37
   123
        }
jtulach@36
   124
        return g;
jtulach@36
   125
    }
jtulach@36
   126
jtulach@36
   127
    @GET
jtulach@36
   128
    @Produces(MediaType.APPLICATION_JSON)
jtulach@36
   129
    public List<Game> getGames() {
jtulach@36
   130
        return games;
jtulach@36
   131
    }
jtulach@36
   132
jtulach@36
   133
    private Game findGame(String id) {
jtulach@36
   134
        for (Game g : games) {
jtulach@36
   135
            if (g.getId().equals(id)) {
jtulach@36
   136
                return g;
jtulach@36
   137
            }
jtulach@36
   138
        }
jtulach@36
   139
        return null;
jtulach@36
   140
    }
jtulach@36
   141
jtulach@37
   142
    private Game readGame(File f) throws IOException {
jtulach@37
   143
        BufferedReader r = new BufferedReader(new FileReader(f));
jtulach@37
   144
        String white = null;
jtulach@37
   145
        String black = null;
jtulach@37
   146
        Game g = null;
jtulach@37
   147
        for (;;) {
jtulach@37
   148
            String line = r.readLine();
jtulach@37
   149
            if (line == null) {
jtulach@37
   150
                break;
jtulach@37
   151
            }
jtulach@39
   152
            line = line.trim();
jtulach@39
   153
            if (line.length() == 0) {
jtulach@39
   154
                continue;
jtulach@39
   155
            }
jtulach@37
   156
            if (line.startsWith("# white: ")) {
jtulach@37
   157
                white = line.substring(9);
jtulach@37
   158
                continue;
jtulach@37
   159
            }
jtulach@37
   160
            if (line.startsWith("# black: ")) {
jtulach@37
   161
                black = line.substring(9);
jtulach@37
   162
                continue;
jtulach@37
   163
            }
jtulach@37
   164
            if (line.startsWith("#")) {
jtulach@37
   165
                continue;
jtulach@37
   166
            }
jtulach@37
   167
            if (white == null || black == null) {
jtulach@37
   168
                throw new IOException("Missing white and black identification in " + f);
jtulach@37
   169
            }
jtulach@37
   170
            if (g == null) {
jtulach@37
   171
                g = new Game(f.getName(), white, black);
jtulach@37
   172
            }
jtulach@37
   173
            String[] moves = line.split(" ");
jtulach@37
   174
            if (moves.length == 0) {
jtulach@37
   175
                continue;
jtulach@37
   176
            }
jtulach@37
   177
            if (moves.length > 2) {
jtulach@37
   178
                throw new IOException("Too much moves on line: " + line);
jtulach@37
   179
            }
jtulach@37
   180
            try {
jtulach@37
   181
                g.apply(white, Move.valueOf(moves[0]));
jtulach@37
   182
                if (moves.length == 2) {
jtulach@37
   183
                    g.apply(black, Move.valueOf(moves[1]));
jtulach@37
   184
                }
jtulach@37
   185
            } catch (IllegalPositionException ex) {
jtulach@37
   186
                throw new IOException("Wrong move: " + ex.getMessage());
jtulach@37
   187
            }
jtulach@37
   188
        }
jtulach@37
   189
        if (g == null) {
jtulach@37
   190
            throw new IOException("No moves in " + f);
jtulach@37
   191
        }
jtulach@37
   192
        return g;
jtulach@36
   193
    }
jtulach@36
   194
jtulach@37
   195
    private void storeGame(Game g) throws IOException {
jtulach@37
   196
        dir.mkdirs();
jtulach@37
   197
        File f = new File(dir, g.getId());
jtulach@37
   198
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jtulach@37
   199
        pw.println("# white: " + g.getWhite());
jtulach@37
   200
        pw.println("# black: " + g.getBlack());
jtulach@37
   201
        int cnt = 0;
jtulach@37
   202
        for (Move m : g.getMoves()) {
jtulach@37
   203
            pw.print(m.toString());
jtulach@37
   204
            if (++cnt % 2 == 0) {
jtulach@37
   205
                pw.println();
jtulach@37
   206
            } else {
jtulach@37
   207
                pw.print(' ');
jtulach@37
   208
            }
jtulach@37
   209
        }
jtulach@37
   210
        pw.println();
jtulach@37
   211
        pw.println();
jtulach@37
   212
        pw.flush();
jtulach@37
   213
        pw.close();
jtulach@37
   214
    }
jtulach@36
   215
}