statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningNodeView.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 178 4b78d4f028b3
child 266 15fcdfc4cd4a
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Martin Rexa
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 package cz.xelfi.quoridor.statistics;
    19 
    20 import cz.xelfi.quoridor.webidor.GameId;
    21 import cz.xelfi.quoridor.webidor.GameStatus;
    22 import cz.xelfi.quoridor.Move;
    23 import java.util.Set;
    24 import java.util.HashSet;
    25 import java.util.Map;
    26 import java.util.HashMap;
    27 import java.util.List;
    28 import java.util.ArrayList;
    29 import javax.xml.bind.annotation.XmlElement;
    30 import javax.xml.bind.annotation.XmlElementWrapper;
    31 import javax.xml.bind.annotation.XmlRootElement;
    32 //import javax.xml.bind.annotation.XmlAccessType;
    33 //import javax.xml.bind.annotation.XmlAccessorType;
    34 import javax.xml.bind.annotation.XmlAttribute;
    35 
    36 /**
    37  *
    38  * @author Martin Rexa
    39  */
    40 
    41 @XmlRootElement
    42 //@XmlAccessorType(XmlAccessType.FIELD)
    43 public class OpeningNodeView {
    44     @XmlAttribute
    45     String code;
    46     @XmlElementWrapper(name="whiteGames")
    47     @XmlElement(name="gameId")
    48     List<GameId> whiteGames;
    49     @XmlElementWrapper(name="blackGames")
    50     @XmlElement(name="gameId")
    51     List<GameId> blackGames;
    52     Map<Move, OpeningNodeViewEntry> entries;
    53     boolean empty;
    54 
    55     public OpeningNodeView(){
    56     }
    57 
    58     public OpeningNodeView(String code){
    59         this.code = code;
    60         entries = new HashMap<Move, OpeningNodeViewEntry>();
    61         whiteGames = new ArrayList<GameId>();
    62         blackGames = new ArrayList<GameId>();
    63         empty = true;
    64     }
    65 
    66     @XmlElement
    67     public String getNodeCode(){
    68         return code;
    69     }
    70 
    71     public void processGame(Move m, String code, GameId gId){
    72         OpeningNodeViewEntry e = entries.get(m);
    73         if(e == null){
    74             e = new OpeningNodeViewEntry(m,code);
    75             entries.put(m, e);
    76         }
    77         e.processGame(gId);
    78         empty = false;
    79     }
    80 
    81     @XmlElementWrapper(name="children")
    82     @XmlElement(name="item")
    83     public Set<OpeningNodeViewEntry> getChildren(){
    84         Set<OpeningNodeViewEntry> result = new HashSet<OpeningNodeViewEntry>();
    85         for(Map.Entry<Move, OpeningNodeViewEntry> e: entries.entrySet()){
    86             result.add(e.getValue());
    87         }
    88         return result;
    89     }
    90 
    91 
    92     public void addFinishedGame(GameId gId){
    93         if(gId.getStatus().equals(GameStatus.whiteWon))
    94             whiteGames.add(gId);
    95         if(gId.getStatus().equals(GameStatus.blackWon))
    96             blackGames.add(gId);
    97         empty = false;
    98     }
    99 
   100     @XmlAttribute
   101     public int getWhiteCount(){
   102         return whiteGames.size();
   103     }
   104 
   105     @XmlAttribute
   106     public int getBlackCount(){
   107         return blackGames.size();
   108     }
   109 
   110 
   111     @XmlAttribute
   112     public boolean getChildrenNotEmpty(){
   113         return !entries.isEmpty();
   114     }
   115 
   116     public boolean isEmpty(){
   117         return this.empty;
   118     }
   119 
   120 }