chess/src/main/java/com/oracle/chess/client/htmljava/Communication.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Sep 2013 22:20:24 +0200
branchchess
changeset 49 945fbfff28f3
permissions -rw-r--r--
Advanced version of the chess game
jaroslav@49
     1
/**
jaroslav@49
     2
 * The MIT License (MIT)
jaroslav@49
     3
 *
jaroslav@49
     4
 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@49
     5
 *
jaroslav@49
     6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
jaroslav@49
     7
 * of this software and associated documentation files (the "Software"), to deal
jaroslav@49
     8
 * in the Software without restriction, including without limitation the rights
jaroslav@49
     9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jaroslav@49
    10
 * copies of the Software, and to permit persons to whom the Software is
jaroslav@49
    11
 * furnished to do so, subject to the following conditions:
jaroslav@49
    12
 *
jaroslav@49
    13
 * The above copyright notice and this permission notice shall be included in
jaroslav@49
    14
 * all copies or substantial portions of the Software.
jaroslav@49
    15
 *
jaroslav@49
    16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jaroslav@49
    17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jaroslav@49
    18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jaroslav@49
    19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jaroslav@49
    20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jaroslav@49
    21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jaroslav@49
    22
 * THE SOFTWARE.
jaroslav@49
    23
 */
jaroslav@49
    24
package com.oracle.chess.client.htmljava;
jaroslav@49
    25
jaroslav@49
    26
import net.java.html.json.ComputedProperty;
jaroslav@49
    27
import net.java.html.json.Model;
jaroslav@49
    28
import net.java.html.json.Property;
jaroslav@49
    29
jaroslav@49
    30
jaroslav@49
    31
/** Communication protocols to talk to the server.
jaroslav@49
    32
 *
jaroslav@49
    33
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@49
    34
 */
jaroslav@49
    35
final class Communication {
jaroslav@49
    36
    @Model(className = "Request", properties = {
jaroslav@49
    37
        @Property(name = "msg", type = MsgType.class),
jaroslav@49
    38
        @Property(name = "username", type = String.class),
jaroslav@49
    39
        @Property(name = "password", type = String.class),
jaroslav@49
    40
        @Property(name = "gameId", type = String.class),
jaroslav@49
    41
        @Property(name = "color", type = Color.class),
jaroslav@49
    42
        @Property(name = "from", type = String.class),
jaroslav@49
    43
        @Property(name = "to", type = String.class),
jaroslav@49
    44
        @Property(name = "observer", type = boolean.class),
jaroslav@49
    45
    })
jaroslav@49
    46
    static class RequestModel {
jaroslav@49
    47
    }
jaroslav@49
    48
jaroslav@49
    49
    @Model(className = "BoardRep", properties = {
jaroslav@49
    50
        @Property(name = "whites", type = String.class, array = true),
jaroslav@49
    51
        @Property(name = "blacks", type = String.class, array = true),
jaroslav@49
    52
    })
jaroslav@49
    53
    static class BoardRepModel {
jaroslav@49
    54
    }
jaroslav@49
    55
jaroslav@49
    56
    @Model(className = "Response", properties = {
jaroslav@49
    57
        @Property(name = "msg", type = String.class),
jaroslav@49
    58
        @Property(name = "games", array = true, type = Game.class),
jaroslav@49
    59
        @Property(name = "board", type = BoardRep.class),
jaroslav@49
    60
        @Property(name = "turn", type = Color.class),
jaroslav@49
    61
        @Property(name = "color", type = Color.class),
jaroslav@49
    62
        @Property(name = "gameId", type = String.class),
jaroslav@49
    63
        @Property(name = "error", type = Err.class),
jaroslav@49
    64
        @Property(name = "alert", type = Alert.class),
jaroslav@49
    65
        @Property(name = "status", type = String.class),
jaroslav@49
    66
        @Property(name = "summary", type = String.class),
jaroslav@49
    67
        @Property(name = "moves", type = String.class, array = true),
jaroslav@49
    68
        @Property(name = "from", type = String.class),
jaroslav@49
    69
        @Property(name = "to", type = String.class),
jaroslav@49
    70
        @Property(name = "check", type = CheckResult.class),
jaroslav@49
    71
        @Property(name = "whitePlayer", type = String.class),
jaroslav@49
    72
        @Property(name = "blackPlayer", type = String.class),
jaroslav@49
    73
    })
jaroslav@49
    74
    static class ResponseModel {
jaroslav@49
    75
        @ComputedProperty static Color nextTurn(Color turn, Alert alert) {
jaroslav@49
    76
            if (alert == null || !alert.getType().isTerminal()) {
jaroslav@49
    77
                return turn;
jaroslav@49
    78
            } else {
jaroslav@49
    79
                return null;
jaroslav@49
    80
            }
jaroslav@49
    81
        }
jaroslav@49
    82
    }
jaroslav@49
    83
jaroslav@49
    84
    static enum CheckResult {
jaroslav@49
    85
        VALID, INVALID, NOT_REGISTERED
jaroslav@49
    86
    };
jaroslav@49
    87
    
jaroslav@49
    88
    @Model(className = "Err", properties = {
jaroslav@49
    89
        @Property(name = "code", type = int.class),
jaroslav@49
    90
        @Property(name = "message", type = String.class),
jaroslav@49
    91
    })
jaroslav@49
    92
    static class ErrorModel {
jaroslav@49
    93
    }
jaroslav@49
    94
jaroslav@49
    95
    @Model(className = "Alert", properties = {
jaroslav@49
    96
        @Property(name = "type", type = AlertType.class),
jaroslav@49
    97
        @Property(name = "message", type = String.class),
jaroslav@49
    98
    })
jaroslav@49
    99
    static class AlertModel {
jaroslav@49
   100
    }
jaroslav@49
   101
jaroslav@49
   102
    static enum AlertType {
jaroslav@49
   103
        CHECKMATE, CHECK, DRAW;
jaroslav@49
   104
jaroslav@49
   105
        public boolean isTerminal() {
jaroslav@49
   106
            return this == CHECKMATE || this == DRAW;
jaroslav@49
   107
        }
jaroslav@49
   108
    };
jaroslav@49
   109
    
jaroslav@49
   110
}