1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 22:37:17 2013 +0200
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/BoardModel.java Tue Sep 24 23:52:32 2013 +0200
1.3 @@ -35,14 +35,12 @@
1.4 import net.java.html.json.OnPropertyChange;
1.5 import net.java.html.json.Property;
1.6 import net.java.html.sound.AudioClip;
1.7 -import org.apidesign.html.demo.chess.Communication.AlertType;
1.8
1.9 @Model(className="Board", properties={
1.10 @Property(name = "player", type = String.class),
1.11 @Property(name = "gameId", type = String.class),
1.12 @Property(name = "whitePlayer", type = String.class),
1.13 @Property(name = "blackPlayer", type = String.class),
1.14 - @Property(name = "alert", type = Alert.class),
1.15 @Property(name = "alertMessage", type = String.class),
1.16 @Property(name = "status", type = String.class),
1.17 @Property(name = "rows", type = Row.class, array = true),
1.18 @@ -85,17 +83,6 @@
1.19
1.20 private static final AudioClip CHECK = AudioClip.create("sounds/check.mp3");
1.21 private static final AudioClip CHECKMATE = AudioClip.create("sounds/checkmate.mp3");
1.22 - @OnPropertyChange("alert") static void warnCheckAndMate(Board b) {
1.23 - if (b.getAlert() == null) {
1.24 - return;
1.25 - }
1.26 - if (b.getAlert().getType() == AlertType.CHECK) {
1.27 - CHECK.play();
1.28 - }
1.29 - if (b.getAlert().getType() == AlertType.CHECKMATE) {
1.30 - CHECKMATE.play();
1.31 - }
1.32 - }
1.33
1.34 @Function static void selected(Board b, Square data) {
1.35 if (!b.isMyTurn()) {
1.36 @@ -141,17 +128,6 @@
1.37 previoslySelected.setPiece(null);
1.38 previoslySelected.setPieceColor(null);
1.39 Rules.computeAccessible(b, null);
1.40 -
1.41 - Request sm = new Request();
1.42 - sm.setMsg(MsgType.SendMove);
1.43 - sm.setGameId(b.getGameId());
1.44 - sm.setFrom(newMove.getFrom().getLocation());
1.45 - sm.setTo(newMove.getTo().getLocation());
1.46 - sm.setColor(newMove.getTurn());
1.47 - final UI ui = UIModel.findUI(b);
1.48 - if (ui != null) {
1.49 - ui.sendMsg(sm);
1.50 - }
1.51 }
1.52 }
1.53 }
1.54 @@ -259,8 +235,10 @@
1.55 return null;
1.56 }
1.57
1.58 - static void moveResponse(final Board b, final String errMsg, final List<String> whites, final List<String> blacks, final Color turn,
1.59 - Alert alert
1.60 + static void moveResponse(
1.61 + final Board b, final String errMsg,
1.62 + final List<String> whites, final List<String> blacks,
1.63 + final Color turn, Object alert
1.64 ) {
1.65 if (errMsg != null) {
1.66 b.getMoves().remove(b.getPendingMove());
1.67 @@ -270,11 +248,14 @@
1.68 b.setTurn(turn);
1.69 b.setAlertMessage(null);
1.70 }
1.71 - b.setAlert(alert);
1.72 Rules.initBoard(b, whites, blacks, turn);
1.73 }
1.74
1.75 - static void moveUpdate(final Board b, final Move move, final List<String> whites, final List<String> blacks, final Color turn, Alert alert) {
1.76 + static void moveUpdate(
1.77 + final Board b, final Move move,
1.78 + final List<String> whites, final List<String> blacks,
1.79 + final Color turn, Object alert
1.80 + ) {
1.81 final Square from = BoardModel.findSquare(b, move.getFrom());
1.82 final Square to = BoardModel.findSquare(b, move.getTo());
1.83 move.setPiece(from.getPiece());
1.84 @@ -282,8 +263,6 @@
1.85 if (to.getPiece() != null) {
1.86 move.setTakes(true);
1.87 }
1.88 - b.setAlert(alert);
1.89 - b.setAlertMessage(alert == null ? null : alert.getMessage());
1.90 move.setRound(b.getMoves().size() / 2 + 1);
1.91 b.getMoves().add(move);
1.92 Rules.initBoard(b, whites, blacks, turn);
2.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/Communication.java Tue Sep 24 22:37:17 2013 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,110 +0,0 @@
2.4 -/**
2.5 - * The MIT License (MIT)
2.6 - *
2.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.8 - *
2.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
2.10 - * of this software and associated documentation files (the "Software"), to deal
2.11 - * in the Software without restriction, including without limitation the rights
2.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2.13 - * copies of the Software, and to permit persons to whom the Software is
2.14 - * furnished to do so, subject to the following conditions:
2.15 - *
2.16 - * The above copyright notice and this permission notice shall be included in
2.17 - * all copies or substantial portions of the Software.
2.18 - *
2.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2.25 - * THE SOFTWARE.
2.26 - */
2.27 -package org.apidesign.html.demo.chess;
2.28 -
2.29 -import net.java.html.json.ComputedProperty;
2.30 -import net.java.html.json.Model;
2.31 -import net.java.html.json.Property;
2.32 -
2.33 -
2.34 -/** Communication protocols to talk to the server.
2.35 - *
2.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
2.37 - */
2.38 -final class Communication {
2.39 - @Model(className = "Request", properties = {
2.40 - @Property(name = "msg", type = MsgType.class),
2.41 - @Property(name = "username", type = String.class),
2.42 - @Property(name = "password", type = String.class),
2.43 - @Property(name = "gameId", type = String.class),
2.44 - @Property(name = "color", type = Color.class),
2.45 - @Property(name = "from", type = String.class),
2.46 - @Property(name = "to", type = String.class),
2.47 - @Property(name = "observer", type = boolean.class),
2.48 - })
2.49 - static class RequestModel {
2.50 - }
2.51 -
2.52 - @Model(className = "BoardRep", properties = {
2.53 - @Property(name = "whites", type = String.class, array = true),
2.54 - @Property(name = "blacks", type = String.class, array = true),
2.55 - })
2.56 - static class BoardRepModel {
2.57 - }
2.58 -
2.59 - @Model(className = "Response", properties = {
2.60 - @Property(name = "msg", type = String.class),
2.61 - @Property(name = "games", array = true, type = Game.class),
2.62 - @Property(name = "board", type = BoardRep.class),
2.63 - @Property(name = "turn", type = Color.class),
2.64 - @Property(name = "color", type = Color.class),
2.65 - @Property(name = "gameId", type = String.class),
2.66 - @Property(name = "error", type = Err.class),
2.67 - @Property(name = "alert", type = Alert.class),
2.68 - @Property(name = "status", type = String.class),
2.69 - @Property(name = "summary", type = String.class),
2.70 - @Property(name = "moves", type = String.class, array = true),
2.71 - @Property(name = "from", type = String.class),
2.72 - @Property(name = "to", type = String.class),
2.73 - @Property(name = "check", type = CheckResult.class),
2.74 - @Property(name = "whitePlayer", type = String.class),
2.75 - @Property(name = "blackPlayer", type = String.class),
2.76 - })
2.77 - static class ResponseModel {
2.78 - @ComputedProperty static Color nextTurn(Color turn, Alert alert) {
2.79 - if (alert == null || !alert.getType().isTerminal()) {
2.80 - return turn;
2.81 - } else {
2.82 - return null;
2.83 - }
2.84 - }
2.85 - }
2.86 -
2.87 - static enum CheckResult {
2.88 - VALID, INVALID, NOT_REGISTERED
2.89 - };
2.90 -
2.91 - @Model(className = "Err", properties = {
2.92 - @Property(name = "code", type = int.class),
2.93 - @Property(name = "message", type = String.class),
2.94 - })
2.95 - static class ErrorModel {
2.96 - }
2.97 -
2.98 - @Model(className = "Alert", properties = {
2.99 - @Property(name = "type", type = AlertType.class),
2.100 - @Property(name = "message", type = String.class),
2.101 - })
2.102 - static class AlertModel {
2.103 - }
2.104 -
2.105 - static enum AlertType {
2.106 - CHECKMATE, CHECK, DRAW;
2.107 -
2.108 - public boolean isTerminal() {
2.109 - return this == CHECKMATE || this == DRAW;
2.110 - }
2.111 - };
2.112 -
2.113 -}
3.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/GamesModel.java Tue Sep 24 22:37:17 2013 +0200
3.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/GamesModel.java Tue Sep 24 23:52:32 2013 +0200
3.3 @@ -23,10 +23,8 @@
3.4 */
3.5 package org.apidesign.html.demo.chess;
3.6
3.7 -import java.util.List;
3.8 import net.java.html.json.ComputedProperty;
3.9 import net.java.html.json.Model;
3.10 -import net.java.html.json.ModelOperation;
3.11 import net.java.html.json.Property;
3.12
3.13 /**
3.14 @@ -34,53 +32,15 @@
3.15 * @author Jaroslav Tulach <jtulach@netbeans.org>
3.16 */
3.17 @Model(className = "Games", properties = {
3.18 - @Property(name = "own", array = true, type = Game.class),
3.19 - @Property(name = "other", array = true, type = Game.class),
3.20 + @Property(name = "whitePlayer", type = String.class),
3.21 + @Property(name = "blackPlayer", type = String.class),
3.22 @Property(name = "selectedColor", type = String.class),
3.23 })
3.24 class GamesModel {
3.25 - @Model(className = "Game", properties = {
3.26 - @Property(name = "gameId", type=String.class),
3.27 - @Property(name = "summary", type=String.class),
3.28 - @Property(name = "open", type=boolean.class),
3.29 - @Property(name = "whitePlayer", type=String.class),
3.30 - @Property(name = "blackPlayer", type=String.class)
3.31 - })
3.32 - static class GameImpl {
3.33 - @ComputedProperty static String description(String gameId, String summary) {
3.34 - return summary != null ? summary : gameId;
3.35 - }
3.36 -
3.37 - @ComputedProperty static boolean pendingPlayer(String whitePlayer, String blackPlayer) {
3.38 - return whitePlayer == null || blackPlayer == null;
3.39 - }
3.40 - }
3.41 -
3.42 -
3.43 - @ComputedProperty static boolean anyOwnGame(List<Game> own) {
3.44 - return !own.isEmpty();
3.45 - }
3.46 -
3.47 - @ComputedProperty static boolean anyOtherGame(List<Game> other) {
3.48 - return !other.isEmpty();
3.49 - }
3.50 -
3.51 - @ModelOperation static void listGames(Games g, List<Game> games, String username) {
3.52 - g.getOwn().clear();
3.53 - g.getOther().clear();
3.54 - int cnt = 0;
3.55 - final int maxGames = 30;
3.56 - for (Game game : games) {
3.57 - if (
3.58 - username.equals(game.getWhitePlayer()) ||
3.59 - username.equals(game.getBlackPlayer())
3.60 - ) {
3.61 - g.getOwn().add(game);
3.62 - } else {
3.63 - if (cnt++ < maxGames) {
3.64 - g.getOther().add(game);
3.65 - }
3.66 - }
3.67 - }
3.68 + @ComputedProperty static boolean correctNames(
3.69 + String whitePlayer, String blackPlayer
3.70 + ) {
3.71 + return whitePlayer != null && blackPlayer != null && !whitePlayer.isEmpty() &&
3.72 + !blackPlayer.isEmpty();
3.73 }
3.74 }
4.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/SettingsModel.java Tue Sep 24 22:37:17 2013 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,82 +0,0 @@
4.4 -/**
4.5 - * The MIT License (MIT)
4.6 - *
4.7 - * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.8 - *
4.9 - * Permission is hereby granted, free of charge, to any person obtaining a copy
4.10 - * of this software and associated documentation files (the "Software"), to deal
4.11 - * in the Software without restriction, including without limitation the rights
4.12 - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4.13 - * copies of the Software, and to permit persons to whom the Software is
4.14 - * furnished to do so, subject to the following conditions:
4.15 - *
4.16 - * The above copyright notice and this permission notice shall be included in
4.17 - * all copies or substantial portions of the Software.
4.18 - *
4.19 - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4.20 - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4.21 - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4.22 - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4.23 - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4.24 - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4.25 - * THE SOFTWARE.
4.26 - */
4.27 -package org.apidesign.html.demo.chess;
4.28 -
4.29 -import java.io.File;
4.30 -import java.io.FileInputStream;
4.31 -import java.io.FileOutputStream;
4.32 -import java.io.IOException;
4.33 -import java.util.Properties;
4.34 -import net.java.html.json.ComputedProperty;
4.35 -import net.java.html.json.Model;
4.36 -import net.java.html.json.ModelOperation;
4.37 -import net.java.html.json.Property;
4.38 -
4.39 -/**
4.40 - *
4.41 - * @author Jaroslav Tulach <jtulach@netbeans.org>
4.42 - */
4.43 -@Model(className = "Settings", properties = {
4.44 - @Property(name = "username", type = String.class),
4.45 - @Property(name = "password", type = String.class),
4.46 - @Property(name = "url", type = String.class),
4.47 -})
4.48 -class SettingsModel {
4.49 - private static final String SETTINGS = ".j1chess/html.properties";
4.50 - private static final String DEFAULT_URL = "ws://localhost:8080/chess/chessserver";
4.51 -
4.52 - @ComputedProperty static boolean validCredentials(String username, String password) {
4.53 - return username != null && password != null && !username.isEmpty() && !password.isEmpty();
4.54 - }
4.55 -
4.56 - @ModelOperation static void read(Settings s) {
4.57 - File prefs = new File(System.getProperty("user.home"), SETTINGS);
4.58 - try (FileInputStream is = new FileInputStream(prefs)) {
4.59 - Properties p = new Properties();
4.60 - p.load(is);
4.61 -
4.62 - s.setUsername(p.getProperty("username", ""));
4.63 - s.setPassword(p.getProperty("password", ""));
4.64 - s.setUrl(p.getProperty("url", DEFAULT_URL));
4.65 - } catch (Throwable ex) {
4.66 - s.setUsername("");
4.67 - s.setPassword("");
4.68 - s.setUrl(DEFAULT_URL);
4.69 - }
4.70 - }
4.71 -
4.72 - @ModelOperation static void write(Settings s, UI ui) {
4.73 - File prefs = new File(System.getProperty("user.home"), SETTINGS);
4.74 - prefs.getParentFile().mkdirs();
4.75 - try (FileOutputStream os = new FileOutputStream(prefs)) {
4.76 - Properties p = new Properties();
4.77 - p.setProperty("username", s.getUsername());
4.78 - p.setProperty("password", s.getPassword());
4.79 - p.setProperty("url", s.getUrl());
4.80 - p.store(os, "Java One Chess Preferences for HTML/Java Client");
4.81 - } catch (Throwable ex) {
4.82 - ui.setStatus("Cannot save preferences: " + ex.getMessage());
4.83 - }
4.84 - }
4.85 -}
5.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/UIModel.java Tue Sep 24 22:37:17 2013 +0200
5.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/UIModel.java Tue Sep 24 23:52:32 2013 +0200
5.3 @@ -25,17 +25,11 @@
5.4
5.5 import java.io.IOException;
5.6 import java.net.URISyntaxException;
5.7 -import java.util.HashSet;
5.8 import java.util.List;
5.9 -import java.util.Set;
5.10 -import java.util.logging.Level;
5.11 import java.util.logging.Logger;
5.12 import net.java.html.json.ComputedProperty;
5.13 import net.java.html.json.Function;
5.14 import net.java.html.json.Model;
5.15 -import net.java.html.json.ModelOperation;
5.16 -import net.java.html.json.OnPropertyChange;
5.17 -import net.java.html.json.OnReceive;
5.18 import net.java.html.json.Property;
5.19
5.20 /**
5.21 @@ -47,21 +41,14 @@
5.22 @Property(name = "selectedGameId", type = String.class),
5.23 @Property(name = "boards", type = Board.class, array = true),
5.24 @Property(name = "viewGames", type = Games.class),
5.25 - @Property(name = "settings", type = Settings.class),
5.26 - @Property(name = "connected", type = boolean.class),
5.27 - @Property(name = "disconnected", type = boolean.class),
5.28 })
5.29 public class UIModel {
5.30 private static final Logger LOG = Logger.getLogger(UIModel.class.getName());
5.31
5.32 - @ComputedProperty static boolean settingsActive(String selectedGameId) {
5.33 + @ComputedProperty static boolean viewGamesActive(String selectedGameId) {
5.34 return selectedGameId == null;
5.35 }
5.36
5.37 - @ComputedProperty static boolean viewGamesActive(String selectedGameId, boolean connected) {
5.38 - return selectedGameId == null && connected;
5.39 - }
5.40 -
5.41 @ComputedProperty static Board selectedBoard(String selectedGameId, List<Board> boards) {
5.42 Board active = null;
5.43 for (Board b : boards) {
5.44 @@ -80,149 +67,26 @@
5.45
5.46 @Function static void activateSettings(UI m) {
5.47 m.setSelectedGameId(null);
5.48 - refreshGames(m);
5.49 }
5.50
5.51 + private static int boardCnt;
5.52 +
5.53 @Function static void createGame(UI u) {
5.54 - u.setStatus("Creating new game...");
5.55 - Request r = new Request();
5.56 - r.setMsg(MsgType.CreateGame);
5.57 - r.setColor(Color.valueOf(u.getViewGames().getSelectedColor()));
5.58 - u.sendMsg(r);
5.59 + Board b = Rules.createBoard();
5.60 + b.setGameId("id" + ++boardCnt);
5.61 + final String wp = u.getViewGames().getWhitePlayer();
5.62 + b.setWhitePlayer(wp);
5.63 + final String bp = u.getViewGames().getBlackPlayer();
5.64 + b.setBlackPlayer(bp);
5.65 + b.setStatus(wp + " vs. " + bp);
5.66 + u.getBoards().add(b);
5.67 + u.setSelectedGameId(b.getGameId());
5.68 +
5.69 + u.getViewGames().setWhitePlayer(null);
5.70 + u.getViewGames().setBlackPlayer(null);
5.71 }
5.72
5.73 - @ModelOperation @Function static void refreshGames(UI u) {
5.74 - u.setStatus("Refreshing games...");
5.75 - Request r = new Request();
5.76 - r.setMsg(MsgType.QueryGames);
5.77 - u.sendMsg(r);
5.78 - }
5.79
5.80 - @ModelOperation static void sendMsg(UI ui, Request r) {
5.81 - final Settings sttngs = ui.getSettings();
5.82 -
5.83 - String url = sttngs.getUrl();
5.84 - r.setUsername(sttngs.getUsername());
5.85 - r.setPassword(sttngs.getPassword());
5.86 -
5.87 - LOG.log(Level.INFO, "Sending {0} to {1}", new Object[]{r, url});
5.88 - ui.queryServer(url, r);
5.89 - }
5.90 -
5.91 -
5.92 - @OnReceive(data = Request.class, url = "{url}", method = "WebSocket", onError = "wasAnError")
5.93 - static void queryServer(UI ui, Response r) {
5.94 - LOG.log(Level.INFO, "Received {0}", r);
5.95 - if (r == null) {
5.96 - ui.setDisconnected(false);
5.97 - verifyLogin(ui);
5.98 - return;
5.99 - }
5.100 - SWITCH: switch (MsgType.forResponse(r.getMsg())) {
5.101 - case CheckCredentials: {
5.102 - switch (r.getCheck()) {
5.103 - case NOT_REGISTERED:
5.104 - case VALID:
5.105 - ui.setConnected(true);
5.106 - ui.refreshGames();
5.107 - ui.setStatus("Connected.");
5.108 - return;
5.109 - case INVALID:
5.110 - ui.setStatus("Password is invalid");
5.111 - break;
5.112 - }
5.113 - disconnect(ui);
5.114 - }
5.115 -
5.116 - case QueryGames:
5.117 - ui.getViewGames().listGames(r.getGames(), ui.getSettings().getUsername());
5.118 - ui.setStatus("");
5.119 - break;
5.120 - case CreateGame: {
5.121 - ui.setStatus("Game " + r + " created");
5.122 - final Board b = Rules.createBoard();
5.123 - Rules.initBoard(
5.124 - b, r.getBoard().getWhites(),
5.125 - r.getBoard().getBlacks(),
5.126 - r.getTurn()
5.127 - );
5.128 - b.setGameId(r.getGameId());
5.129 - b.setWhitePlayer(r.getWhitePlayer());
5.130 - b.setBlackPlayer(r.getBlackPlayer());
5.131 - b.setPlayer(ui.getSettings().getUsername());
5.132 - b.updateSummary(r.getSummary());
5.133 - ui.getBoards().add(b);
5.134 - ui.setSelectedGameId(r.getGameId());
5.135 - UIModel.refreshGames(ui);
5.136 - break;
5.137 - }
5.138 - case SendMove: {
5.139 - if (r.getBoard() == null) {
5.140 - throw new NullPointerException("No board in " + r);
5.141 - }
5.142 - final Board b = findBoard(ui, r.getGameId());
5.143 - if (b == null) {
5.144 - break;
5.145 - }
5.146 - b.updateSummary(r.getSummary());
5.147 - final String errMsg = r.getError() == null ? null : r.getError().getMessage();
5.148 - final List<String> whites = r.getBoard().getWhites();
5.149 - final List<String> blacks = r.getBoard().getBlacks();
5.150 - final Color turn = r.getNextTurn();
5.151 - BoardModel.moveResponse(b, errMsg, whites, blacks, turn, r.getAlert());
5.152 - break;
5.153 - }
5.154 - case JoinGame: {
5.155 - Board b;
5.156 - if ((b = findBoard(ui, r.getGameId())) != null) {
5.157 - b.setWhitePlayer(r.getWhitePlayer());
5.158 - b.setBlackPlayer(r.getBlackPlayer());
5.159 - b.setPlayer(ui.getSettings().getUsername());
5.160 - b.updateSummary(r.getSummary());
5.161 - ui.setSelectedGameId(r.getGameId());
5.162 - break SWITCH;
5.163 - }
5.164 - ui.setStatus("Joining " + r.getGameId() + " as " + r.getColor());
5.165 - b = Rules.createBoard();
5.166 - Rules.initBoard(
5.167 - b, r.getBoard().getWhites(),
5.168 - r.getBoard().getBlacks(),
5.169 - r.getTurn()
5.170 - );
5.171 - b.setGameId(r.getGameId());
5.172 - b.setWhitePlayer(r.getWhitePlayer());
5.173 - b.setBlackPlayer(r.getBlackPlayer());
5.174 - b.setPlayer(ui.getSettings().getUsername());
5.175 - b.updateSummary(r.getSummary());
5.176 - if (r.getColor() == Color.B) {
5.177 - BoardModel.rotateBoard(b);
5.178 - }
5.179 - if (r.getMoves() != null) {
5.180 - for (String move : r.getMoves()) {
5.181 - Move m = BoardModel.MoveImpl.valueOf(move);
5.182 - b.getMoves().add(m);
5.183 - }
5.184 - } else {
5.185 - b.getMoves().clear();
5.186 - }
5.187 - ui.setSelectedGameId(b.getGameId());
5.188 - ui.getBoards().add(b);
5.189 - break;
5.190 - }
5.191 - case UpdateGame: {
5.192 - final Board b = findBoard(ui, r.getGameId());
5.193 - if (b == null) {
5.194 - break;
5.195 - }
5.196 - b.updateSummary(r.getSummary());
5.197 - final Move move = BoardModel.MoveImpl.valueOf(r.getFrom() + r.getTo());
5.198 - final List<String> whites = r.getBoard().getWhites();
5.199 - final List<String> blacks = r.getBoard().getBlacks();
5.200 - final Color turn = r.getNextTurn();
5.201 - BoardModel.moveUpdate(b, move, whites, blacks, turn, null);
5.202 - }
5.203 - }
5.204 - }
5.205
5.206
5.207 private static Board findBoard(UI ui, String gameId) {
5.208 @@ -235,80 +99,17 @@
5.209 }
5.210
5.211
5.212 - static void wasAnError(UI ui, Exception t) {
5.213 - if (t == null) {
5.214 - ui.setConnected(false);
5.215 - ui.setDisconnected(true);
5.216 - ui.setStatus("Disconnected.");
5.217 - } else {
5.218 - ui.setStatus("Error: " + t.getLocalizedMessage());
5.219 - }
5.220 - }
5.221 -
5.222 - @Function static void joinGame(UI u, Game data) {
5.223 - Request r = new Request();
5.224 - r.setMsg(MsgType.JoinGame);
5.225 - r.setObserver(false);
5.226 - r.setGameId(data.getGameId());
5.227 - u.sendMsg(r);
5.228 + @Function static void joinGame(UI u) {
5.229 }
5.230
5.231 - @Function static void observeGame(UI u, Game data) {
5.232 - Request r = new Request();
5.233 - r.setMsg(MsgType.JoinGame);
5.234 - r.setObserver(true);
5.235 - r.setGameId(data.getGameId());
5.236 - u.sendMsg(r);
5.237 - }
5.238 -
5.239 - private static final Set<UI> ACTIVE = new HashSet<>();
5.240 - @OnPropertyChange("connected") static void clearGames(UI m) {
5.241 - if (m.isDisconnected()) {
5.242 - m.getBoards().clear();
5.243 - ACTIVE.remove(m);
5.244 - } else {
5.245 - ACTIVE.add(m);
5.246 - }
5.247 - }
5.248 -
5.249 - static UI findUI(Board b) {
5.250 - for (UI ui : ACTIVE) {
5.251 - if (ui.getBoards().contains(b)) {
5.252 - return ui;
5.253 - }
5.254 - }
5.255 - return null;
5.256 - }
5.257 -
5.258 @Function static void leave(UI ui, Board data) {
5.259 ui.getBoards().remove(data);
5.260 ui.setSelectedGameId(null);
5.261 }
5.262
5.263 - @Function static void disconnect(UI ui) {
5.264 - ui.queryServer(ui.getSettings().getUrl(), null);
5.265 - ui.setStatus("Disconnecting...");
5.266 - }
5.267 -
5.268 - private static void verifyLogin(UI ui) {
5.269 - ui.setStatus("Verifying user credentials...");
5.270 - Request r = new Request();
5.271 - r.setMsg(MsgType.CheckCredentials);
5.272 - ui.sendMsg(r);
5.273 - }
5.274 -
5.275 - @Function static void reconnect(UI ui) {
5.276 - final Settings s = ui.getSettings();
5.277 - ui.setStatus("Connecting to the server...");
5.278 - ui.queryServer(s.getUrl(), null);
5.279 - s.write(ui);
5.280 - }
5.281 -
5.282 public static void initialize(String... args) throws URISyntaxException, IOException {
5.283 UI ui = new UI();
5.284 - ui.getSettings().read();
5.285 ui.setStatus("Ready.");
5.286 - ui.setDisconnected(true);
5.287 ui.applyBindings();
5.288 }
5.289
6.1 --- a/chess/src/main/webapp/pages/index.html Tue Sep 24 22:37:17 2013 +0200
6.2 +++ b/chess/src/main/webapp/pages/index.html Tue Sep 24 23:52:32 2013 +0200
6.3 @@ -47,7 +47,7 @@
6.4 <body>
6.5 <div class="navbar">
6.6 <div class="navbar-inner">
6.7 - <a id="apptitle" class="brand" data-bind="css: { active: settingsActive }, click: activateSettings" href="#">JavaOne Chess</a>
6.8 + <a id="apptitle" class="brand" data-bind="css: { active: viewGamesActive }, click: activateSettings" href="#">JavaOne Chess</a>
6.9 <ul class="nav" data-bind="foreach: boards">
6.10 <li data-bind="css: { active: active }, click: $root.activateGame">
6.11 <span data-bind="visible: myTurn" class="badge badge-warning myturn">↪</span>
6.12 @@ -57,8 +57,7 @@
6.13 </ul>
6.14 </div>
6.15 </div>
6.16 - <div data-bind="template: { name: 'render-games', data: viewGames }"></div>
6.17 - <div data-bind="template: { name: 'render-settings', data: settings, if : settingsActive }"></div>
6.18 + <div data-bind="template: { name: 'render-games', data: viewGames, ifnot: selectedBoard }"></div>
6.19 <div data-bind="template: { name: 'render-board', data: selectedBoard, if : selectedBoard }"></div>
6.20
6.21 <script type="text/html" id="render-board">
6.22 @@ -119,29 +118,34 @@
6.23 </div>
6.24 </script>
6.25
6.26 - <script type="text/html" id="render-settings">
6.27 + <script type="text/html" id="render-games">
6.28 <div class="container-fluid">
6.29 - <h3>Connection Settings</h3>
6.30 - <p>
6.31 - Status: <span data-bind="text: $root.status"></span>
6.32 - </p>
6.33 - <p>
6.34 - Name:
6.35 - <input type="text" data-bind="value: username, enable: $root.disconnected, valueUpdate: 'afterkeydown'"
6.36 - class="input-small" placeholder="Name"></input>
6.37 - Password:
6.38 - <input type="password" data-bind="value: password, enable: $root.disconnected, valueUpdate: 'afterkeydown'"
6.39 - class="input-small" placeholder="Password"></input>
6.40 - </p>
6.41 - <br/>
6.42 - <p>
6.43 - Chess server URL:
6.44 - <input data-bind="value: url, enable: $root.disconnected" class="inpu-xlarge"></input>
6.45 - </p>
6.46 - <p>
6.47 - <button data-bind="click: $root.reconnect, enable: ($root.disconnected() && validCredentials()) ">Connect</button>
6.48 - <button data-bind="click: $root.disconnect, enable: $root.connected">Disconnect</button>
6.49 - </p>
6.50 + <div data-bind="visible: $root.viewGamesActive">
6.51 + <h3>List of Games</h3>
6.52 + <p>
6.53 + Status: <span data-bind="text: $root.status"></span>
6.54 + </p>
6.55 + <div class="input-append" class="select">
6.56 + <input type="text" data-bind="value: whitePlayer, valueUpdate: 'afterkeydown'"
6.57 + class="input-small" placeholder="White Player"></input>
6.58 + vs.
6.59 + <input type="text" data-bind="value: blackPlayer, valueUpdate: 'afterkeydown'"
6.60 + class="input-small" placeholder="Black Player"></input>
6.61 + </div>
6.62 + <br/>
6.63 + <button data-bind="click: $root.createGame, enable: correctNames"
6.64 + class="btn btn-success" >Create Game!</button>
6.65 + <br/>
6.66 + <div data-bind="foreach: $root.boards">
6.67 + <div class="input-append">
6.68 + <input type="text" data-bind="value: status" disabled></input>
6.69 + <button class="btn" data-bind="click: $root.joinGame">
6.70 + Show
6.71 + </button>
6.72 + </div>
6.73 + <br/>
6.74 + </div>
6.75 + </div>
6.76 </div>
6.77 <div id="chesscube">
6.78 <div id="spinner">
6.79 @@ -154,49 +158,6 @@
6.80 </div>
6.81 </div>
6.82 </script>
6.83 -
6.84 - <script type="text/html" id="render-games">
6.85 - <div class="container-fluid">
6.86 - <div data-bind="visible: $root.viewGamesActive ">
6.87 - <h3>List of Games</h3>
6.88 - <p>
6.89 - <a href="#" data-bind="click: $root.refreshGames">Refresh</a>
6.90 - </p>
6.91 - <!-- ko if: anyOwnGame -->
6.92 - <h5>Own Games</h5>
6.93 - <div data-bind="foreach: own">
6.94 - <div class="input-append">
6.95 - <input type="text" data-bind="value: description" disabled></input>
6.96 - <button class="btn" data-bind="click: $root.joinGame">
6.97 - <span data-bind="ifnot: pendingPlayer">Show</span>
6.98 - <span data-bind="if: pendingPlayer">Challenge Yourself</span>
6.99 - </button>
6.100 - </div>
6.101 - <br/>
6.102 - </div>
6.103 - <!-- /ko -->
6.104 - <h5>Other Games</h5>
6.105 - <div data-bind="foreach: other">
6.106 - <div class="input-append">
6.107 - <input type="text" data-bind="value: description" disabled></input>
6.108 - <!-- ko if: open -->
6.109 - <button class="btn btn-success" data-bind="click: $root.joinGame">Join</button>
6.110 - <!-- /ko -->
6.111 - <button class="btn" data-bind="click: $root.observeGame">Observe</button>
6.112 - </div>
6.113 - <br/>
6.114 - </div>
6.115 - <h3>New game</h3>
6.116 - <div class="input-append" class="select">
6.117 - <select data-bind="value: selectedColor">
6.118 - <option value="W">White</option>
6.119 - <option value="B">Black</option>
6.120 - </select>
6.121 - <button data-bind="click: $root.createGame" class="btn btn-success" >Create Game!</button>
6.122 - </div>
6.123 - </div>
6.124 - </div>
6.125 - </script>
6.126 <center data-bind="visible: false">
6.127 <img src="DukeHTML.png" width="320" height="200" alt="Loading...">
6.128 <h3>Initializing the virtual machine...</h3>
6.129 @@ -204,7 +165,7 @@
6.130 <script type="text/javascript" src="bck2brwsr.js"></script>
6.131 <script>
6.132 var vm = bck2brwsr('${project.build.finalName}.jar');
6.133 - vm.loadClass('com.oracle.chess.client.htmljava.LoadMain');
6.134 + vm.loadClass('org.apidesign.html.demo.chess.LoadMain');
6.135 </script>
6.136 </body>
6.137 </html>