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