wsdor/src/main/java/cz/xelfi/quoridor/webidor/Game.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Sep 2010 17:04:51 +0200
changeset 256 1758a7727278
parent 178 webidor/src/main/java/cz/xelfi/quoridor/webidor/Game.java@4b78d4f028b3
child 264 d60370059c3c
permissions -rw-r--r--
Splitting classes representing data types on the server into own module
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import cz.xelfi.quoridor.Board;
    30 import cz.xelfi.quoridor.IllegalPositionException;
    31 import cz.xelfi.quoridor.Move;
    32 import cz.xelfi.quoridor.Player;
    33 import java.util.ArrayList;
    34 import java.util.Date;
    35 import java.util.List;
    36 import javax.xml.bind.annotation.XmlAccessType;
    37 import javax.xml.bind.annotation.XmlAccessorType;
    38 import javax.xml.bind.annotation.XmlAttribute;
    39 import javax.xml.bind.annotation.XmlElement;
    40 import javax.xml.bind.annotation.XmlElementWrapper;
    41 import javax.xml.bind.annotation.XmlRootElement;
    42 import javax.xml.bind.annotation.adapters.XmlAdapter;
    43 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    44 
    45 /**
    46  *
    47  * @author Jaroslav Tulach <jtulach@netbeans.org>
    48  */
    49 @XmlRootElement
    50 @XmlAccessorType(XmlAccessType.FIELD)
    51 public final class Game extends Object {
    52     @XmlElement
    53     private GameId id;
    54     @XmlElement
    55     @XmlJavaTypeAdapter(BoardAdapter.class)
    56     private Board board;
    57     @XmlElementWrapper(name="moves")
    58     @XmlElement(name="item")
    59     private List<CommentedMove> moves;
    60     private Integer move;
    61 
    62     Game() {
    63     }
    64 
    65     public Game(String first, String second) {
    66         this.id = new GameId(first, second);
    67         this.board = Board.empty();
    68     }
    69 
    70     public Game(GameId id) {
    71         this.id = id;
    72         this.board = Board.empty();
    73     }
    74 
    75     public GameId getId() {
    76         return id;
    77     }
    78     
    79     public Board getBoard() {
    80         return board;
    81     }
    82 
    83     @XmlAttribute
    84     final String getCurrentPlayer() {
    85         Player w = board.getWinner();
    86         if(w==null){
    87             if (board.getCurrentPlayer() == board.getPlayers().get(0)) {
    88                 return id.getWhite();
    89             } else {
    90                 return id.getBlack();
    91             }
    92         }else{
    93             if (w == board.getPlayers().get(0)) {
    94                 return id.getWhite();
    95             } else {
    96                 return id.getBlack();
    97             }
    98         }
    99     }
   100 
   101     public void apply(String player, Move m, Date when) throws IllegalPositionException {
   102         Player p = null;
   103         if (id.getWhite().equals(player)) {
   104             p = getBoard().getPlayers().get(0);
   105         } else {
   106             if (id.getBlack().equals(player)) {
   107                 p = getBoard().getPlayers().get(1);
   108             }
   109         }
   110         if (p != getBoard().getCurrentPlayer()) {
   111             throw new IllegalArgumentException("Wrong player: " + player);
   112         }
   113 
   114         board = getBoard().apply(m);
   115         if (when == null) {
   116             when = new Date(id.getModified());
   117         }
   118         final GameStatus status = GameStatus.valueOf(board);
   119         id = new GameId(
   120             id.getId(), id.getWhite(), id.getBlack(),
   121             new Date(id.getStarted()), when, status,
   122             id.getComments(), !status.isInProgress());
   123         getMoves().add(new CommentedMove(m, getMoves().size() + 1));
   124     }
   125 
   126     public void comment(String player, String comment, Date date) {
   127         Note n = new Note(comment, date, player);
   128         id = new GameId(
   129             id.getId(), id.getWhite(), id.getBlack(),
   130             new Date(id.getStarted()), new Date(id.getModified()),
   131             GameStatus.valueOf(board), id.getComments() + 1, id.isFinished()
   132         );
   133         getMoves().get(getMoves().size() - 1).addNote(n);
   134     }
   135 
   136 
   137     public List<CommentedMove> getMoves() {
   138         if (moves == null) {
   139             moves = new ArrayList<CommentedMove>();
   140         }
   141         return moves;
   142     }
   143 
   144     @XmlAttribute
   145     public int getCurrentMove() {
   146         return move == null ? getMoves().size() : move;
   147     }
   148 
   149     public Game snapshot(int move) throws IllegalPositionException {
   150         Board b = Board.empty();
   151         int cnt = 0;
   152         for (CommentedMove m : getMoves()) {
   153             if (move-- <= 0) {
   154                 break;
   155             }
   156             b = b.apply(m.getMove());
   157             cnt++;
   158         }
   159 
   160         Game g = new Game(
   161             new GameId(
   162                 id.getId(), id.getWhite(), id.getBlack(),
   163                 new Date(id.getStarted()), new Date(id.getModified()),
   164                 GameStatus.history, id.getComments(), id.isFinished()
   165             )
   166         );
   167         g.board = b;
   168         g.move = cnt;
   169         g.moves = new ArrayList<CommentedMove>(getMoves());
   170         return g;
   171     }
   172 /*
   173     private static final class MoveAdapter extends XmlAdapter<CommentedMove[],List<Move>> {
   174         @Override
   175         public List<Move> unmarshal(CommentedMove[] arr) throws Exception {
   176             List<Move> res = new ArrayList<Move>();
   177             if (arr != null) {
   178                 for (CommentedMove v : arr) {
   179                     res.add(Move.valueOf(v.move));
   180                 }
   181             }
   182             return res;
   183         }
   184 
   185         @Override
   186         public CommentedMove[] marshal(List<Move> arr) throws Exception {
   187             List<CommentedMove> res = new ArrayList<CommentedMove>();
   188             if (arr != null) {
   189                 int index = 0;
   190                 for (Move m : arr) {
   191                     res.add(new CommentedMove(m.toString(), ++index));
   192                 }
   193             }
   194             return res.toArray(new CommentedMove[0]);
   195         }
   196     } // end of MoveAdapter
   197 */
   198     private static final class BoardAdapter extends XmlAdapter<String,Board> {
   199 
   200         @Override
   201         public Board unmarshal(String v) throws Exception {
   202             return v == null ? null : Board.valueOf(v);
   203 //            return v == null ? null : new Board(v);
   204         }
   205 
   206         @Override
   207         public String marshal(Board v) throws Exception {
   208             return v == null ? null : v.toString();
   209 //            return v == null ? null : Board.board2HashCode(v);
   210         }
   211 
   212     }
   213 }