webidor/src/main/java/cz/xelfi/quoridor/webidor/resources/Games.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:37:47 +0200
changeset 48 69e897fe8140
parent 46 71e4cf307c93
child 77 d574ac6e44cc
permissions -rw-r--r--
Spliting Game into GameId and Game with full info about the state of the game
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;
jaroslav@48
    40
import java.util.Date;
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 {
jaroslav@48
    58
    private final 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
jaroslav@48
    78
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
    79
    public GameId createGame(
jtulach@38
    80
        @QueryParam("white") String white,
jtulach@38
    81
        @QueryParam("black") String black
jaroslav@48
    82
    ) throws IOException {
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);
jaroslav@48
    90
        storeGame(g);
jtulach@36
    91
        games.add(g);
jaroslav@48
    92
        return g.getId();
jtulach@36
    93
    }
jtulach@36
    94
jtulach@38
    95
    @GET
jtulach@38
    96
    @Path("{id}")
jtulach@38
    97
    @Produces(MediaType.TEXT_PLAIN)
jtulach@38
    98
    public String getBoard(@PathParam("id") String id) {
jtulach@38
    99
        Game g = findGame(id);
jtulach@38
   100
        if (g == null) {
jtulach@38
   101
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@38
   102
        }
jtulach@38
   103
        return g.getBoard().toString();
jtulach@38
   104
    }
jtulach@38
   105
jaroslav@46
   106
    @GET
jaroslav@46
   107
    @Path("{id}")
jaroslav@48
   108
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
   109
    public Game getBoardInfo(@PathParam("id") String id) {
jaroslav@46
   110
        Game g = findGame(id);
jaroslav@46
   111
        if (g == null) {
jaroslav@46
   112
            throw new IllegalArgumentException("Unknown game " + id);
jaroslav@46
   113
        }
jaroslav@48
   114
        return g;
jaroslav@46
   115
    }
jaroslav@46
   116
jtulach@36
   117
    @PUT
jtulach@36
   118
    @Path("{id}")
jaroslav@48
   119
    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
   120
    public GameId applyMove(
jtulach@38
   121
        @PathParam("id") String id,
jtulach@38
   122
        @QueryParam("player") String player,
jtulach@38
   123
        @QueryParam("move") String move
jtulach@38
   124
    ) throws IllegalPositionException {
jtulach@36
   125
        Game g = findGame(id);
jtulach@36
   126
        if (g == null) {
jtulach@36
   127
            throw new IllegalArgumentException("Unknown game " + id);
jtulach@36
   128
        }
jtulach@36
   129
        Move m = Move.valueOf(move);
jtulach@36
   130
        g.apply(player, m);
jtulach@37
   131
        try {
jtulach@37
   132
            storeGame(g);
jtulach@37
   133
        } catch (IOException ex) {
jtulach@37
   134
            LOG.log(Level.WARNING, "Cannot store game " + id, ex);
jtulach@37
   135
        }
jaroslav@48
   136
        return g.getId();
jtulach@36
   137
    }
jtulach@36
   138
jtulach@36
   139
    @GET
jaroslav@48
   140
    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
jaroslav@48
   141
    public List<GameId> listGames() {
jaroslav@48
   142
        List<GameId> arr = new ArrayList<GameId>(games.size());
jaroslav@48
   143
        for (Game g : games) {
jaroslav@48
   144
            arr.add(g.getId());
jaroslav@48
   145
        }
jaroslav@48
   146
        return arr;
jaroslav@48
   147
    }
jaroslav@48
   148
jtulach@36
   149
    public List<Game> getGames() {
jtulach@36
   150
        return games;
jtulach@36
   151
    }
jtulach@36
   152
jtulach@36
   153
    private Game findGame(String id) {
jtulach@36
   154
        for (Game g : games) {
jaroslav@48
   155
            if (g.getId().getId().equals(id)) {
jtulach@36
   156
                return g;
jtulach@36
   157
            }
jtulach@36
   158
        }
jtulach@36
   159
        return null;
jtulach@36
   160
    }
jtulach@36
   161
jtulach@37
   162
    private Game readGame(File f) throws IOException {
jtulach@37
   163
        BufferedReader r = new BufferedReader(new FileReader(f));
jtulach@37
   164
        String white = null;
jtulach@37
   165
        String black = null;
jtulach@37
   166
        Game g = null;
jtulach@37
   167
        for (;;) {
jtulach@37
   168
            String line = r.readLine();
jtulach@37
   169
            if (line == null) {
jaroslav@48
   170
                line = "finish";
jtulach@37
   171
            }
jtulach@39
   172
            line = line.trim();
jtulach@39
   173
            if (line.length() == 0) {
jtulach@39
   174
                continue;
jtulach@39
   175
            }
jtulach@37
   176
            if (line.startsWith("# white: ")) {
jtulach@37
   177
                white = line.substring(9);
jtulach@37
   178
                continue;
jtulach@37
   179
            }
jtulach@37
   180
            if (line.startsWith("# black: ")) {
jtulach@37
   181
                black = line.substring(9);
jtulach@37
   182
                continue;
jtulach@37
   183
            }
jtulach@37
   184
            if (line.startsWith("#")) {
jtulach@37
   185
                continue;
jtulach@37
   186
            }
jtulach@37
   187
            if (white == null || black == null) {
jtulach@37
   188
                throw new IOException("Missing white and black identification in " + f);
jtulach@37
   189
            }
jtulach@37
   190
            if (g == null) {
jaroslav@48
   191
                GameId id = new GameId(f.getName(), white, black, new Date(f.lastModified()), GameResult.IN_PROGRESS);
jaroslav@48
   192
                g = new Game(id);
jaroslav@48
   193
            }
jaroslav@48
   194
            if (line.equals("finish")) {
jaroslav@48
   195
                break;
jtulach@37
   196
            }
jtulach@37
   197
            String[] moves = line.split(" ");
jtulach@37
   198
            if (moves.length == 0) {
jtulach@37
   199
                continue;
jtulach@37
   200
            }
jtulach@37
   201
            if (moves.length > 2) {
jtulach@37
   202
                throw new IOException("Too much moves on line: " + line);
jtulach@37
   203
            }
jtulach@37
   204
            try {
jtulach@37
   205
                g.apply(white, Move.valueOf(moves[0]));
jtulach@37
   206
                if (moves.length == 2) {
jtulach@37
   207
                    g.apply(black, Move.valueOf(moves[1]));
jtulach@37
   208
                }
jtulach@37
   209
            } catch (IllegalPositionException ex) {
jtulach@37
   210
                throw new IOException("Wrong move: " + ex.getMessage());
jtulach@37
   211
            }
jtulach@37
   212
        }
jtulach@37
   213
        if (g == null) {
jtulach@37
   214
            throw new IOException("No moves in " + f);
jtulach@37
   215
        }
jtulach@37
   216
        return g;
jtulach@36
   217
    }
jtulach@36
   218
jtulach@37
   219
    private void storeGame(Game g) throws IOException {
jtulach@37
   220
        dir.mkdirs();
jaroslav@48
   221
        File f = new File(dir, g.getId().getId());
jtulach@37
   222
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jaroslav@48
   223
        pw.println("# white: " + g.getId().getWhite());
jaroslav@48
   224
        pw.println("# black: " + g.getId().getBlack());
jaroslav@48
   225
        pw.println("# status: " + g.getId().getResult());
jtulach@37
   226
        int cnt = 0;
jtulach@37
   227
        for (Move m : g.getMoves()) {
jtulach@37
   228
            pw.print(m.toString());
jtulach@37
   229
            if (++cnt % 2 == 0) {
jtulach@37
   230
                pw.println();
jtulach@37
   231
            } else {
jtulach@37
   232
                pw.print(' ');
jtulach@37
   233
            }
jtulach@37
   234
        }
jtulach@37
   235
        pw.println();
jtulach@37
   236
        pw.println();
jtulach@37
   237
        pw.flush();
jtulach@37
   238
        pw.close();
jtulach@37
   239
    }
jtulach@36
   240
}