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