1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/chess/src/main/java/org/apidesign/html/demo/chess/GamesModel.java Tue Sep 24 22:37:17 2013 +0200
1.3 @@ -0,0 +1,86 @@
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 org.apidesign.html.demo.chess;
1.28 +
1.29 +import java.util.List;
1.30 +import net.java.html.json.ComputedProperty;
1.31 +import net.java.html.json.Model;
1.32 +import net.java.html.json.ModelOperation;
1.33 +import net.java.html.json.Property;
1.34 +
1.35 +/**
1.36 + *
1.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.38 + */
1.39 +@Model(className = "Games", properties = {
1.40 + @Property(name = "own", array = true, type = Game.class),
1.41 + @Property(name = "other", array = true, type = Game.class),
1.42 + @Property(name = "selectedColor", type = String.class),
1.43 +})
1.44 +class GamesModel {
1.45 + @Model(className = "Game", properties = {
1.46 + @Property(name = "gameId", type=String.class),
1.47 + @Property(name = "summary", type=String.class),
1.48 + @Property(name = "open", type=boolean.class),
1.49 + @Property(name = "whitePlayer", type=String.class),
1.50 + @Property(name = "blackPlayer", type=String.class)
1.51 + })
1.52 + static class GameImpl {
1.53 + @ComputedProperty static String description(String gameId, String summary) {
1.54 + return summary != null ? summary : gameId;
1.55 + }
1.56 +
1.57 + @ComputedProperty static boolean pendingPlayer(String whitePlayer, String blackPlayer) {
1.58 + return whitePlayer == null || blackPlayer == null;
1.59 + }
1.60 + }
1.61 +
1.62 +
1.63 + @ComputedProperty static boolean anyOwnGame(List<Game> own) {
1.64 + return !own.isEmpty();
1.65 + }
1.66 +
1.67 + @ComputedProperty static boolean anyOtherGame(List<Game> other) {
1.68 + return !other.isEmpty();
1.69 + }
1.70 +
1.71 + @ModelOperation static void listGames(Games g, List<Game> games, String username) {
1.72 + g.getOwn().clear();
1.73 + g.getOther().clear();
1.74 + int cnt = 0;
1.75 + final int maxGames = 30;
1.76 + for (Game game : games) {
1.77 + if (
1.78 + username.equals(game.getWhitePlayer()) ||
1.79 + username.equals(game.getBlackPlayer())
1.80 + ) {
1.81 + g.getOwn().add(game);
1.82 + } else {
1.83 + if (cnt++ < maxGames) {
1.84 + g.getOther().add(game);
1.85 + }
1.86 + }
1.87 + }
1.88 + }
1.89 +}