chess/src/main/java/com/oracle/chess/client/htmljava/BoardModel.java
branchchess
changeset 51 3f1866fdb2a1
parent 50 49011b4a2689
child 52 6bb4070d2c20
     1.1 --- a/chess/src/main/java/com/oracle/chess/client/htmljava/BoardModel.java	Tue Sep 24 22:28:31 2013 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,451 +0,0 @@
     1.4 -/**
     1.5 - * The MIT License (MIT)
     1.6 - *
     1.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.8 - *
     1.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
    1.10 - * of this software and associated documentation files (the "Software"), to deal
    1.11 - * in the Software without restriction, including without limitation the rights
    1.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    1.13 - * copies of the Software, and to permit persons to whom the Software is
    1.14 - * furnished to do so, subject to the following conditions:
    1.15 - *
    1.16 - * The above copyright notice and this permission notice shall be included in
    1.17 - * all copies or substantial portions of the Software.
    1.18 - *
    1.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    1.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    1.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    1.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    1.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    1.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    1.25 - * THE SOFTWARE.
    1.26 - */
    1.27 -package com.oracle.chess.client.htmljava;
    1.28 -
    1.29 -import com.oracle.chess.client.htmljava.Communication.AlertType;
    1.30 -import java.util.Arrays;
    1.31 -import java.util.Collections;
    1.32 -import java.util.List;
    1.33 -import java.util.Timer;
    1.34 -import java.util.TimerTask;
    1.35 -import net.java.html.json.ComputedProperty;
    1.36 -import net.java.html.json.Function;
    1.37 -import net.java.html.json.Model;
    1.38 -import net.java.html.json.ModelOperation;
    1.39 -import net.java.html.json.OnPropertyChange;
    1.40 -import net.java.html.json.Property;
    1.41 -import net.java.html.sound.AudioClip;
    1.42 -
    1.43 -@Model(className="Board", properties={
    1.44 -    @Property(name = "player", type = String.class),
    1.45 -    @Property(name = "gameId", type = String.class),
    1.46 -    @Property(name = "whitePlayer", type = String.class),
    1.47 -    @Property(name = "blackPlayer", type = String.class),
    1.48 -    @Property(name = "alert", type = Alert.class),
    1.49 -    @Property(name = "alertMessage", type = String.class),
    1.50 -    @Property(name = "status", type = String.class),
    1.51 -    @Property(name = "rows", type = Row.class, array = true),
    1.52 -    @Property(name = "turn", type = Color.class),
    1.53 -    @Property(name = "moves", type = Move.class, array = true),
    1.54 -    @Property(name = "pendingMove", type = Move.class),
    1.55 -    @Property(name = "active", type = boolean.class),
    1.56 -})
    1.57 -public class BoardModel {
    1.58 -    @ComputedProperty static String title(String status, String gameId) {
    1.59 -        String t = status != null ? status : gameId;
    1.60 -        if (t != null && t.length() > 10) {
    1.61 -            return t.substring(0, 10);
    1.62 -        }
    1.63 -        return t;
    1.64 -    }
    1.65 -
    1.66 -    @ModelOperation static void updateSummary(Board b, String summary) {
    1.67 -        if (summary != null) {
    1.68 -            b.setStatus(summary);
    1.69 -        }
    1.70 -    }
    1.71 -    
    1.72 -    @ComputedProperty static boolean myTurn(String player, String whitePlayer, String blackPlayer, Color turn) {
    1.73 -        if (turn != null && player != null) switch (turn) {
    1.74 -            case B: return player.equals(blackPlayer);
    1.75 -            case W: return player.equals(whitePlayer);
    1.76 -        }
    1.77 -        return false;
    1.78 -    }
    1.79 -    
    1.80 -    @ComputedProperty static boolean justObserving(String player) {
    1.81 -        return player == null;
    1.82 -    }
    1.83 -    
    1.84 -    private static final AudioClip MOVE = AudioClip.create("sounds/move.mp3");
    1.85 -    @OnPropertyChange("moves") static void playMove() {
    1.86 -        MOVE.play();
    1.87 -    }
    1.88 -
    1.89 -    private static final AudioClip CHECK = AudioClip.create("sounds/check.mp3");
    1.90 -    private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
    1.91 -    @OnPropertyChange("alert") static void warnCheckAndMate(Board b) {
    1.92 -        if (b.getAlert() == null) {
    1.93 -            return;
    1.94 -        }
    1.95 -        if (b.getAlert().getType() == AlertType.CHECK) {
    1.96 -            CHECK.play();
    1.97 -        }
    1.98 -        if (b.getAlert().getType() == AlertType.CHECKMATE) {
    1.99 -            CHECKMATE.play();
   1.100 -        }
   1.101 -    }
   1.102 -    
   1.103 -    @Function static void selected(Board b, Square data) {
   1.104 -        if (!b.isMyTurn()) {
   1.105 -            b.setAlertMessage("Not your turn!");
   1.106 -            return;
   1.107 -        }
   1.108 -        
   1.109 -        Square previoslySelected = findSelectedSquare(b);
   1.110 -        if (previoslySelected == null) {
   1.111 -            if (data.getPiece() != null && data.getPieceColor() == b.getTurn()) {
   1.112 -                data.setSelected(true);
   1.113 -                Rules.computeAccessible(b, data);
   1.114 -            }
   1.115 -        } else {
   1.116 -            if (previoslySelected == data) {
   1.117 -                data.setSelected(false);
   1.118 -                Rules.computeAccessible(b, null);
   1.119 -                return;
   1.120 -            }
   1.121 -            if (data.getPiece() != null && data.getPieceColor() == previoslySelected.getPieceColor()) {
   1.122 -                previoslySelected.setSelected(false);
   1.123 -                data.setSelected(true);
   1.124 -                Rules.computeAccessible(b, data);
   1.125 -                return;
   1.126 -            }
   1.127 -            if (data.isAccessible()) {
   1.128 -                previoslySelected.setSelected(false);
   1.129 -
   1.130 -                Move newMove = new Move();
   1.131 -                newMove.setFrom(previoslySelected.getPosition());
   1.132 -                newMove.setTo(data.getPosition());
   1.133 -                newMove.setRound(b.getMoves().size() / 2 + 1);
   1.134 -                newMove.setPiece(previoslySelected.getPiece());
   1.135 -                newMove.setTurn(previoslySelected.getPieceColor());
   1.136 -                newMove.setTakes(data.getPiece() != null);
   1.137 -                b.getMoves().add(newMove);
   1.138 -                b.setPendingMove(newMove);
   1.139 -
   1.140 -                data.setPending(true);
   1.141 -                data.setPieceColor(previoslySelected.getPieceColor());
   1.142 -                data.setPiece(previoslySelected.getPiece());
   1.143 -                b.setTurn(null);
   1.144 -                previoslySelected.setPiece(null);
   1.145 -                previoslySelected.setPieceColor(null);
   1.146 -                Rules.computeAccessible(b, null);
   1.147 -
   1.148 -                Request sm = new Request();
   1.149 -                sm.setMsg(MsgType.SendMove);
   1.150 -                sm.setGameId(b.getGameId());
   1.151 -                sm.setFrom(newMove.getFrom().getLocation());
   1.152 -                sm.setTo(newMove.getTo().getLocation());
   1.153 -                sm.setColor(newMove.getTurn());
   1.154 -                final UI ui = UIModel.findUI(b);
   1.155 -                if (ui != null) {
   1.156 -                    ui.sendMsg(sm);
   1.157 -                }
   1.158 -            }
   1.159 -        }
   1.160 -    }
   1.161 -    
   1.162 -    static class NextMove extends TimerTask {
   1.163 -        private static final Timer T = new Timer("Animate moves");
   1.164 -        private final Board b;
   1.165 -        private final Move m;
   1.166 -
   1.167 -        public NextMove(Board b, Move m) {
   1.168 -            this.b = b;
   1.169 -            this.m = m;
   1.170 -            T.schedule(this, 1000);
   1.171 -        }
   1.172 -        
   1.173 -        
   1.174 -        @Override
   1.175 -        public void run() {
   1.176 -            b.showPosition(m);
   1.177 -        }
   1.178 -    }
   1.179 -    
   1.180 -    @ModelOperation @Function static void showPosition(Board b, Move data) {
   1.181 -        Rules.initBoard(b);
   1.182 -        boolean found = false;
   1.183 -        for (Move m : b.getMoves()) {
   1.184 -            if (found) {
   1.185 -                b.setTurn(null);
   1.186 -                new NextMove(b, m);
   1.187 -                return;
   1.188 -            }
   1.189 -            Square from = findSquare(b, (char)m.getFrom().getX(), m.getFrom().getY());
   1.190 -            Square to = findSquare(b, (char)m.getTo().getX(), m.getTo().getY());
   1.191 -            to.setPiece(from.getPiece());
   1.192 -            to.setPieceColor(from.getPieceColor());
   1.193 -            from.setPiece(null);
   1.194 -            from.setPieceColor(null);
   1.195 -            if (m == data) {
   1.196 -                found = true;
   1.197 -            }
   1.198 -        }
   1.199 -        b.setTurn(b.getMoves().size() % 2 == 0 ? Color.W : Color.B);
   1.200 -    }
   1.201 -    
   1.202 -    
   1.203 -
   1.204 -    @Function static void rotateBoard(Board b) {
   1.205 -        Collections.reverse(b.getRows());
   1.206 -        for (Row r : b.getRows()) {
   1.207 -            Collections.reverse(r.getColumns());
   1.208 -        }
   1.209 -        Square sq = findSelectedSquare(b);
   1.210 -        if (sq != null) {
   1.211 -            sq.setSelected(false);
   1.212 -            Rules.computeAccessible(b, null);
   1.213 -        }
   1.214 -    }
   1.215 -    
   1.216 -    @ComputedProperty static boolean whiteTurn(Color turn) {
   1.217 -        return turn == Color.W;
   1.218 -    }
   1.219 -
   1.220 -    @ComputedProperty static boolean blackTurn(Color turn) {
   1.221 -        return turn == Color.B;
   1.222 -    }
   1.223 -    
   1.224 -    @ComputedProperty static List<String> columnNames(List<Row> rows) {
   1.225 -        boolean whiteDown = rows.isEmpty() || rows.get(0).getY() == 8;
   1.226 -        String[] arr = new String[8];
   1.227 -        for (int i = 0; i < 8; i++) {
   1.228 -            String s;
   1.229 -            if (whiteDown) {
   1.230 -                s = "" + (char)('A' + i);
   1.231 -            } else {
   1.232 -                s = "" + (char)('H' - i);
   1.233 -            }
   1.234 -            arr[i] = s;
   1.235 -        }
   1.236 -        return Arrays.asList(arr);
   1.237 -    }
   1.238 -    
   1.239 -    static Square findSquare(Board b, char column, int row) {
   1.240 -        for (Row r : b.getRows()) {
   1.241 -            for (Square square : r.getColumns()) {
   1.242 -                if (square.getPosition().getX() == column && square.getPosition().getY() == row) {
   1.243 -                    return square;
   1.244 -                }
   1.245 -            }
   1.246 -        }
   1.247 -        return null;
   1.248 -    }
   1.249 -
   1.250 -    private static Square findSquare(Board b, Position to) {
   1.251 -        return findSquare(b, (char)to.getX(), to.getY());
   1.252 -    }
   1.253 -    
   1.254 -    static Square findSelectedSquare(Board b) {
   1.255 -        for (Row row : b.getRows()) {
   1.256 -            for (Square square : row.getColumns()) {
   1.257 -                if (square.isSelected()) {
   1.258 -                    return square;
   1.259 -                }
   1.260 -            }
   1.261 -        }
   1.262 -        return null;
   1.263 -    }
   1.264 -
   1.265 -    static void moveResponse(final Board b, final String errMsg, final List<String> whites, final List<String> blacks, final Color turn,
   1.266 -        Alert alert
   1.267 -    ) {
   1.268 -        if (errMsg != null) {
   1.269 -            b.getMoves().remove(b.getPendingMove());
   1.270 -            b.setPendingMove(null);
   1.271 -            b.setAlertMessage(errMsg);
   1.272 -        } else {
   1.273 -            b.setTurn(turn);
   1.274 -            b.setAlertMessage(null);
   1.275 -        }
   1.276 -        b.setAlert(alert);
   1.277 -        Rules.initBoard(b, whites, blacks, turn);
   1.278 -    }
   1.279 -
   1.280 -    static void moveUpdate(final Board b, final Move move, final List<String> whites, final List<String> blacks, final Color turn, Alert alert) {
   1.281 -        final Square from = BoardModel.findSquare(b, move.getFrom());
   1.282 -        final Square to = BoardModel.findSquare(b, move.getTo());
   1.283 -        move.setPiece(from.getPiece());
   1.284 -        move.setTurn(from.getPieceColor());
   1.285 -        if (to.getPiece() != null) {
   1.286 -            move.setTakes(true);
   1.287 -        }
   1.288 -        b.setAlert(alert);
   1.289 -        b.setAlertMessage(alert == null ? null : alert.getMessage());
   1.290 -        move.setRound(b.getMoves().size() / 2 + 1);
   1.291 -        b.getMoves().add(move);
   1.292 -        Rules.initBoard(b, whites, blacks, turn);
   1.293 -    }
   1.294 -
   1.295 -    @Model(className="Row", properties = {
   1.296 -        @Property(name = "columns", type = Square.class, array = true)
   1.297 -    })
   1.298 -    static class RowsImpl {
   1.299 -        @ComputedProperty static int y(List<Square> columns) {
   1.300 -            return columns.isEmpty() ? 0 : columns.get(0).getY();
   1.301 -        }
   1.302 -    }
   1.303 -    
   1.304 -    enum PieceType {
   1.305 -        PAWN(5), ROCK(2), KNIGHT(4), BISHOP(3), QUEEN(1), KING(0);
   1.306 -
   1.307 -        final int entityIndex;
   1.308 -        
   1.309 -        PieceType(int ei) {
   1.310 -            this.entityIndex = ei;
   1.311 -        }
   1.312 -
   1.313 -        static PieceType fromNotation(char notation) {
   1.314 -            switch (notation) {
   1.315 -                case 'R': return ROCK;
   1.316 -                case 'N': return KNIGHT;
   1.317 -                case 'B': return BISHOP;
   1.318 -                case 'Q': return QUEEN;
   1.319 -                case 'K': return KING;
   1.320 -                case 'P': return PAWN;
   1.321 -            }
   1.322 -            throw new IllegalStateException("Unexpected: " + notation);
   1.323 -        }
   1.324 -        
   1.325 -        String computeEntity(Color color) {
   1.326 -            if (color == null) {
   1.327 -                color = Color.W;
   1.328 -            }
   1.329 -            int base;
   1.330 -            switch (color) {
   1.331 -                case W: base = 12; break;
   1.332 -                case B: base = 18; break;
   1.333 -                default:
   1.334 -                    throw new AssertionError();
   1.335 -            }
   1.336 -            return "&#98" + String.valueOf(base + entityIndex) + ";";
   1.337 -        }
   1.338 -    }
   1.339 -    
   1.340 -    @Model(className="Position", properties = {
   1.341 -        @Property(name = "x", type = char.class),
   1.342 -        @Property(name = "y", type = int.class),
   1.343 -    })
   1.344 -    static class PositionImpl {
   1.345 -        @ComputedProperty static String location(int x, int y) {
   1.346 -            return "" + (char)(x - 'A' + 'a') + y;
   1.347 -        }
   1.348 -    }
   1.349 -    
   1.350 -    @Model(className="Square", properties = {
   1.351 -        @Property(name = "position", type = Position.class),
   1.352 -        @Property(name = "color", type = Color.class),
   1.353 -        @Property(name = "piece", type = PieceType.class),
   1.354 -        @Property(name = "pieceColor", type = Color.class),
   1.355 -        @Property(name = "selected", type = boolean.class),
   1.356 -        @Property(name = "accessible", type = boolean.class),
   1.357 -        @Property(name = "pending", type = boolean.class),
   1.358 -    })
   1.359 -    static class SquareModel {
   1.360 -        @ComputedProperty static String pieceEntity(
   1.361 -            PieceType piece, Color pieceColor
   1.362 -        ) {
   1.363 -            if (piece == null) {
   1.364 -                return "";
   1.365 -            }
   1.366 -            return piece.computeEntity(pieceColor);
   1.367 -        }
   1.368 -        
   1.369 -        @ComputedProperty static String squareColor(
   1.370 -            Color color, boolean selected, boolean accessible, boolean pending
   1.371 -        ) {
   1.372 -            if (selected) {
   1.373 -                return "selected";
   1.374 -            }
   1.375 -            if (accessible) {
   1.376 -                return "accessible";
   1.377 -            }
   1.378 -            if (pending) {
   1.379 -                return "pending";
   1.380 -            }
   1.381 -            
   1.382 -            if (color == null) {
   1.383 -                return "";
   1.384 -            } else {
   1.385 -                if (color == Color.W) {
   1.386 -                    return "white";
   1.387 -                } else {
   1.388 -                    return "black";
   1.389 -                }
   1.390 -            }
   1.391 -        }
   1.392 -        
   1.393 -        @ComputedProperty static char x(Position position) {
   1.394 -            return position == null ? 'A' : position.getX();
   1.395 -        }
   1.396 -
   1.397 -        @ComputedProperty static int y(Position position) {
   1.398 -            return position == null ? 1 : position.getY();
   1.399 -        }
   1.400 -    }
   1.401 -    
   1.402 -    @Model(className = "Move", properties = {
   1.403 -        @Property(name = "round", type = int.class),
   1.404 -        @Property(name = "turn", type = Color.class),
   1.405 -        @Property(name = "piece", type = PieceType.class),
   1.406 -        @Property(name = "from", type = Position.class),
   1.407 -        @Property(name = "to", type = Position.class),
   1.408 -        @Property(name = "promoted", type = PieceType.class),
   1.409 -        @Property(name = "takes", type = boolean.class),
   1.410 -        @Property(name = "check", type = boolean.class),
   1.411 -    })
   1.412 -    static class MoveImpl {
   1.413 -        @ComputedProperty static boolean whiteMove(Color turn) {
   1.414 -            return turn == Color.W;
   1.415 -        }
   1.416 -        
   1.417 -        @ComputedProperty static String html(
   1.418 -            Position from, Position to, boolean takes, PieceType piece, Color turn
   1.419 -        ) {
   1.420 -            if (from == null || to == null) {
   1.421 -                return "";
   1.422 -            }
   1.423 -            StringBuilder sb = new StringBuilder();
   1.424 -            if (piece != null && piece != PieceType.PAWN) {
   1.425 -                sb.append(piece.computeEntity(turn));
   1.426 -            }
   1.427 -            
   1.428 -            sb.append(from.getLocation());
   1.429 -            if (takes) {
   1.430 -                sb.append("x");
   1.431 -            }
   1.432 -            sb.append(to.getLocation());
   1.433 -            return sb.toString();
   1.434 -        }
   1.435 -
   1.436 -        static Move valueOf(String move) {
   1.437 -            move = move.toUpperCase();
   1.438 -            Move m = new Move();
   1.439 -            {
   1.440 -                Position p = new Position();
   1.441 -                p.setX(move.charAt(0));
   1.442 -                p.setY(move.charAt(1) - '0');
   1.443 -                m.setFrom(p);
   1.444 -            }
   1.445 -            {
   1.446 -                Position p = new Position();
   1.447 -                p.setX(move.charAt(2));
   1.448 -                p.setY(move.charAt(3) - '0');
   1.449 -                m.setTo(p);
   1.450 -            }
   1.451 -            return m;
   1.452 -        }
   1.453 -    }
   1.454 -}