1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chess/src/main/java/com/oracle/chess/client/htmljava/Communication.java Tue Sep 24 22:20:24 2013 +0200
1.3 @@ -0,0 +1,110 @@
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 net.java.html.json.ComputedProperty;
1.30 +import net.java.html.json.Model;
1.31 +import net.java.html.json.Property;
1.32 +
1.33 +
1.34 +/** Communication protocols to talk to the server.
1.35 + *
1.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.37 + */
1.38 +final class Communication {
1.39 + @Model(className = "Request", properties = {
1.40 + @Property(name = "msg", type = MsgType.class),
1.41 + @Property(name = "username", type = String.class),
1.42 + @Property(name = "password", type = String.class),
1.43 + @Property(name = "gameId", type = String.class),
1.44 + @Property(name = "color", type = Color.class),
1.45 + @Property(name = "from", type = String.class),
1.46 + @Property(name = "to", type = String.class),
1.47 + @Property(name = "observer", type = boolean.class),
1.48 + })
1.49 + static class RequestModel {
1.50 + }
1.51 +
1.52 + @Model(className = "BoardRep", properties = {
1.53 + @Property(name = "whites", type = String.class, array = true),
1.54 + @Property(name = "blacks", type = String.class, array = true),
1.55 + })
1.56 + static class BoardRepModel {
1.57 + }
1.58 +
1.59 + @Model(className = "Response", properties = {
1.60 + @Property(name = "msg", type = String.class),
1.61 + @Property(name = "games", array = true, type = Game.class),
1.62 + @Property(name = "board", type = BoardRep.class),
1.63 + @Property(name = "turn", type = Color.class),
1.64 + @Property(name = "color", type = Color.class),
1.65 + @Property(name = "gameId", type = String.class),
1.66 + @Property(name = "error", type = Err.class),
1.67 + @Property(name = "alert", type = Alert.class),
1.68 + @Property(name = "status", type = String.class),
1.69 + @Property(name = "summary", type = String.class),
1.70 + @Property(name = "moves", type = String.class, array = true),
1.71 + @Property(name = "from", type = String.class),
1.72 + @Property(name = "to", type = String.class),
1.73 + @Property(name = "check", type = CheckResult.class),
1.74 + @Property(name = "whitePlayer", type = String.class),
1.75 + @Property(name = "blackPlayer", type = String.class),
1.76 + })
1.77 + static class ResponseModel {
1.78 + @ComputedProperty static Color nextTurn(Color turn, Alert alert) {
1.79 + if (alert == null || !alert.getType().isTerminal()) {
1.80 + return turn;
1.81 + } else {
1.82 + return null;
1.83 + }
1.84 + }
1.85 + }
1.86 +
1.87 + static enum CheckResult {
1.88 + VALID, INVALID, NOT_REGISTERED
1.89 + };
1.90 +
1.91 + @Model(className = "Err", properties = {
1.92 + @Property(name = "code", type = int.class),
1.93 + @Property(name = "message", type = String.class),
1.94 + })
1.95 + static class ErrorModel {
1.96 + }
1.97 +
1.98 + @Model(className = "Alert", properties = {
1.99 + @Property(name = "type", type = AlertType.class),
1.100 + @Property(name = "message", type = String.class),
1.101 + })
1.102 + static class AlertModel {
1.103 + }
1.104 +
1.105 + static enum AlertType {
1.106 + CHECKMATE, CHECK, DRAW;
1.107 +
1.108 + public boolean isTerminal() {
1.109 + return this == CHECKMATE || this == DRAW;
1.110 + }
1.111 + };
1.112 +
1.113 +}