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