1.1 --- a/chess/src/main/java/org/apidesign/html/demo/chess/UIModel.java Tue Sep 24 22:37:17 2013 +0200
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/UIModel.java Tue Sep 24 23:52:32 2013 +0200
1.3 @@ -25,17 +25,11 @@
1.4
1.5 import java.io.IOException;
1.6 import java.net.URISyntaxException;
1.7 -import java.util.HashSet;
1.8 import java.util.List;
1.9 -import java.util.Set;
1.10 -import java.util.logging.Level;
1.11 import java.util.logging.Logger;
1.12 import net.java.html.json.ComputedProperty;
1.13 import net.java.html.json.Function;
1.14 import net.java.html.json.Model;
1.15 -import net.java.html.json.ModelOperation;
1.16 -import net.java.html.json.OnPropertyChange;
1.17 -import net.java.html.json.OnReceive;
1.18 import net.java.html.json.Property;
1.19
1.20 /**
1.21 @@ -47,21 +41,14 @@
1.22 @Property(name = "selectedGameId", type = String.class),
1.23 @Property(name = "boards", type = Board.class, array = true),
1.24 @Property(name = "viewGames", type = Games.class),
1.25 - @Property(name = "settings", type = Settings.class),
1.26 - @Property(name = "connected", type = boolean.class),
1.27 - @Property(name = "disconnected", type = boolean.class),
1.28 })
1.29 public class UIModel {
1.30 private static final Logger LOG = Logger.getLogger(UIModel.class.getName());
1.31
1.32 - @ComputedProperty static boolean settingsActive(String selectedGameId) {
1.33 + @ComputedProperty static boolean viewGamesActive(String selectedGameId) {
1.34 return selectedGameId == null;
1.35 }
1.36
1.37 - @ComputedProperty static boolean viewGamesActive(String selectedGameId, boolean connected) {
1.38 - return selectedGameId == null && connected;
1.39 - }
1.40 -
1.41 @ComputedProperty static Board selectedBoard(String selectedGameId, List<Board> boards) {
1.42 Board active = null;
1.43 for (Board b : boards) {
1.44 @@ -80,149 +67,26 @@
1.45
1.46 @Function static void activateSettings(UI m) {
1.47 m.setSelectedGameId(null);
1.48 - refreshGames(m);
1.49 }
1.50
1.51 + private static int boardCnt;
1.52 +
1.53 @Function static void createGame(UI u) {
1.54 - u.setStatus("Creating new game...");
1.55 - Request r = new Request();
1.56 - r.setMsg(MsgType.CreateGame);
1.57 - r.setColor(Color.valueOf(u.getViewGames().getSelectedColor()));
1.58 - u.sendMsg(r);
1.59 + Board b = Rules.createBoard();
1.60 + b.setGameId("id" + ++boardCnt);
1.61 + final String wp = u.getViewGames().getWhitePlayer();
1.62 + b.setWhitePlayer(wp);
1.63 + final String bp = u.getViewGames().getBlackPlayer();
1.64 + b.setBlackPlayer(bp);
1.65 + b.setStatus(wp + " vs. " + bp);
1.66 + u.getBoards().add(b);
1.67 + u.setSelectedGameId(b.getGameId());
1.68 +
1.69 + u.getViewGames().setWhitePlayer(null);
1.70 + u.getViewGames().setBlackPlayer(null);
1.71 }
1.72
1.73 - @ModelOperation @Function static void refreshGames(UI u) {
1.74 - u.setStatus("Refreshing games...");
1.75 - Request r = new Request();
1.76 - r.setMsg(MsgType.QueryGames);
1.77 - u.sendMsg(r);
1.78 - }
1.79
1.80 - @ModelOperation static void sendMsg(UI ui, Request r) {
1.81 - final Settings sttngs = ui.getSettings();
1.82 -
1.83 - String url = sttngs.getUrl();
1.84 - r.setUsername(sttngs.getUsername());
1.85 - r.setPassword(sttngs.getPassword());
1.86 -
1.87 - LOG.log(Level.INFO, "Sending {0} to {1}", new Object[]{r, url});
1.88 - ui.queryServer(url, r);
1.89 - }
1.90 -
1.91 -
1.92 - @OnReceive(data = Request.class, url = "{url}", method = "WebSocket", onError = "wasAnError")
1.93 - static void queryServer(UI ui, Response r) {
1.94 - LOG.log(Level.INFO, "Received {0}", r);
1.95 - if (r == null) {
1.96 - ui.setDisconnected(false);
1.97 - verifyLogin(ui);
1.98 - return;
1.99 - }
1.100 - SWITCH: switch (MsgType.forResponse(r.getMsg())) {
1.101 - case CheckCredentials: {
1.102 - switch (r.getCheck()) {
1.103 - case NOT_REGISTERED:
1.104 - case VALID:
1.105 - ui.setConnected(true);
1.106 - ui.refreshGames();
1.107 - ui.setStatus("Connected.");
1.108 - return;
1.109 - case INVALID:
1.110 - ui.setStatus("Password is invalid");
1.111 - break;
1.112 - }
1.113 - disconnect(ui);
1.114 - }
1.115 -
1.116 - case QueryGames:
1.117 - ui.getViewGames().listGames(r.getGames(), ui.getSettings().getUsername());
1.118 - ui.setStatus("");
1.119 - break;
1.120 - case CreateGame: {
1.121 - ui.setStatus("Game " + r + " created");
1.122 - final Board b = Rules.createBoard();
1.123 - Rules.initBoard(
1.124 - b, r.getBoard().getWhites(),
1.125 - r.getBoard().getBlacks(),
1.126 - r.getTurn()
1.127 - );
1.128 - b.setGameId(r.getGameId());
1.129 - b.setWhitePlayer(r.getWhitePlayer());
1.130 - b.setBlackPlayer(r.getBlackPlayer());
1.131 - b.setPlayer(ui.getSettings().getUsername());
1.132 - b.updateSummary(r.getSummary());
1.133 - ui.getBoards().add(b);
1.134 - ui.setSelectedGameId(r.getGameId());
1.135 - UIModel.refreshGames(ui);
1.136 - break;
1.137 - }
1.138 - case SendMove: {
1.139 - if (r.getBoard() == null) {
1.140 - throw new NullPointerException("No board in " + r);
1.141 - }
1.142 - final Board b = findBoard(ui, r.getGameId());
1.143 - if (b == null) {
1.144 - break;
1.145 - }
1.146 - b.updateSummary(r.getSummary());
1.147 - final String errMsg = r.getError() == null ? null : r.getError().getMessage();
1.148 - final List<String> whites = r.getBoard().getWhites();
1.149 - final List<String> blacks = r.getBoard().getBlacks();
1.150 - final Color turn = r.getNextTurn();
1.151 - BoardModel.moveResponse(b, errMsg, whites, blacks, turn, r.getAlert());
1.152 - break;
1.153 - }
1.154 - case JoinGame: {
1.155 - Board b;
1.156 - if ((b = findBoard(ui, r.getGameId())) != null) {
1.157 - b.setWhitePlayer(r.getWhitePlayer());
1.158 - b.setBlackPlayer(r.getBlackPlayer());
1.159 - b.setPlayer(ui.getSettings().getUsername());
1.160 - b.updateSummary(r.getSummary());
1.161 - ui.setSelectedGameId(r.getGameId());
1.162 - break SWITCH;
1.163 - }
1.164 - ui.setStatus("Joining " + r.getGameId() + " as " + r.getColor());
1.165 - b = Rules.createBoard();
1.166 - Rules.initBoard(
1.167 - b, r.getBoard().getWhites(),
1.168 - r.getBoard().getBlacks(),
1.169 - r.getTurn()
1.170 - );
1.171 - b.setGameId(r.getGameId());
1.172 - b.setWhitePlayer(r.getWhitePlayer());
1.173 - b.setBlackPlayer(r.getBlackPlayer());
1.174 - b.setPlayer(ui.getSettings().getUsername());
1.175 - b.updateSummary(r.getSummary());
1.176 - if (r.getColor() == Color.B) {
1.177 - BoardModel.rotateBoard(b);
1.178 - }
1.179 - if (r.getMoves() != null) {
1.180 - for (String move : r.getMoves()) {
1.181 - Move m = BoardModel.MoveImpl.valueOf(move);
1.182 - b.getMoves().add(m);
1.183 - }
1.184 - } else {
1.185 - b.getMoves().clear();
1.186 - }
1.187 - ui.setSelectedGameId(b.getGameId());
1.188 - ui.getBoards().add(b);
1.189 - break;
1.190 - }
1.191 - case UpdateGame: {
1.192 - final Board b = findBoard(ui, r.getGameId());
1.193 - if (b == null) {
1.194 - break;
1.195 - }
1.196 - b.updateSummary(r.getSummary());
1.197 - final Move move = BoardModel.MoveImpl.valueOf(r.getFrom() + r.getTo());
1.198 - final List<String> whites = r.getBoard().getWhites();
1.199 - final List<String> blacks = r.getBoard().getBlacks();
1.200 - final Color turn = r.getNextTurn();
1.201 - BoardModel.moveUpdate(b, move, whites, blacks, turn, null);
1.202 - }
1.203 - }
1.204 - }
1.205
1.206
1.207 private static Board findBoard(UI ui, String gameId) {
1.208 @@ -235,80 +99,17 @@
1.209 }
1.210
1.211
1.212 - static void wasAnError(UI ui, Exception t) {
1.213 - if (t == null) {
1.214 - ui.setConnected(false);
1.215 - ui.setDisconnected(true);
1.216 - ui.setStatus("Disconnected.");
1.217 - } else {
1.218 - ui.setStatus("Error: " + t.getLocalizedMessage());
1.219 - }
1.220 - }
1.221 -
1.222 - @Function static void joinGame(UI u, Game data) {
1.223 - Request r = new Request();
1.224 - r.setMsg(MsgType.JoinGame);
1.225 - r.setObserver(false);
1.226 - r.setGameId(data.getGameId());
1.227 - u.sendMsg(r);
1.228 + @Function static void joinGame(UI u) {
1.229 }
1.230
1.231 - @Function static void observeGame(UI u, Game data) {
1.232 - Request r = new Request();
1.233 - r.setMsg(MsgType.JoinGame);
1.234 - r.setObserver(true);
1.235 - r.setGameId(data.getGameId());
1.236 - u.sendMsg(r);
1.237 - }
1.238 -
1.239 - private static final Set<UI> ACTIVE = new HashSet<>();
1.240 - @OnPropertyChange("connected") static void clearGames(UI m) {
1.241 - if (m.isDisconnected()) {
1.242 - m.getBoards().clear();
1.243 - ACTIVE.remove(m);
1.244 - } else {
1.245 - ACTIVE.add(m);
1.246 - }
1.247 - }
1.248 -
1.249 - static UI findUI(Board b) {
1.250 - for (UI ui : ACTIVE) {
1.251 - if (ui.getBoards().contains(b)) {
1.252 - return ui;
1.253 - }
1.254 - }
1.255 - return null;
1.256 - }
1.257 -
1.258 @Function static void leave(UI ui, Board data) {
1.259 ui.getBoards().remove(data);
1.260 ui.setSelectedGameId(null);
1.261 }
1.262
1.263 - @Function static void disconnect(UI ui) {
1.264 - ui.queryServer(ui.getSettings().getUrl(), null);
1.265 - ui.setStatus("Disconnecting...");
1.266 - }
1.267 -
1.268 - private static void verifyLogin(UI ui) {
1.269 - ui.setStatus("Verifying user credentials...");
1.270 - Request r = new Request();
1.271 - r.setMsg(MsgType.CheckCredentials);
1.272 - ui.sendMsg(r);
1.273 - }
1.274 -
1.275 - @Function static void reconnect(UI ui) {
1.276 - final Settings s = ui.getSettings();
1.277 - ui.setStatus("Connecting to the server...");
1.278 - ui.queryServer(s.getUrl(), null);
1.279 - s.write(ui);
1.280 - }
1.281 -
1.282 public static void initialize(String... args) throws URISyntaxException, IOException {
1.283 UI ui = new UI();
1.284 - ui.getSettings().read();
1.285 ui.setStatus("Ready.");
1.286 - ui.setDisconnected(true);
1.287 ui.applyBindings();
1.288 }
1.289