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
jaroslav@178
     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@178
    18
package cz.xelfi.quoridor.statistics;
jaroslav@178
    19
jaroslav@178
    20
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@178
    21
import cz.xelfi.quoridor.webidor.GameStatus;
jaroslav@178
    22
import cz.xelfi.quoridor.Move;
jaroslav@178
    23
import java.util.Set;
jaroslav@178
    24
import java.util.HashSet;
jaroslav@178
    25
import java.util.Map;
jaroslav@178
    26
import java.util.HashMap;
jaroslav@178
    27
import java.util.List;
jaroslav@178
    28
import java.util.ArrayList;
jaroslav@178
    29
import javax.xml.bind.annotation.XmlElement;
jaroslav@178
    30
import javax.xml.bind.annotation.XmlElementWrapper;
jaroslav@178
    31
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@178
    32
//import javax.xml.bind.annotation.XmlAccessType;
jaroslav@178
    33
//import javax.xml.bind.annotation.XmlAccessorType;
jaroslav@178
    34
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@178
    35
jaroslav@178
    36
/**
jaroslav@178
    37
 *
jaroslav@178
    38
 * @author Martin Rexa
jaroslav@178
    39
 */
jaroslav@178
    40
jaroslav@178
    41
@XmlRootElement
jaroslav@178
    42
//@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@178
    43
public class OpeningNodeView {
jaroslav@178
    44
    @XmlAttribute
jaroslav@178
    45
    String code;
jaroslav@178
    46
    @XmlElementWrapper(name="whiteGames")
jaroslav@178
    47
    @XmlElement(name="gameId")
jaroslav@178
    48
    List<GameId> whiteGames;
jaroslav@178
    49
    @XmlElementWrapper(name="blackGames")
jaroslav@178
    50
    @XmlElement(name="gameId")
jaroslav@178
    51
    List<GameId> blackGames;
jaroslav@178
    52
    Map<Move, OpeningNodeViewEntry> entries;
jaroslav@178
    53
    boolean empty;
jaroslav@178
    54
jaroslav@178
    55
    public OpeningNodeView(){
jaroslav@178
    56
    }
jaroslav@178
    57
jaroslav@178
    58
    public OpeningNodeView(String code){
jaroslav@178
    59
        this.code = code;
jaroslav@178
    60
        entries = new HashMap<Move, OpeningNodeViewEntry>();
jaroslav@178
    61
        whiteGames = new ArrayList<GameId>();
jaroslav@178
    62
        blackGames = new ArrayList<GameId>();
jaroslav@178
    63
        empty = true;
jaroslav@178
    64
    }
jaroslav@178
    65
jaroslav@178
    66
    @XmlElement
jaroslav@178
    67
    public String getNodeCode(){
jaroslav@178
    68
        return code;
jaroslav@178
    69
    }
jaroslav@178
    70
jaroslav@178
    71
    public void processGame(Move m, String code, GameId gId){
jaroslav@178
    72
        OpeningNodeViewEntry e = entries.get(m);
jaroslav@178
    73
        if(e == null){
jaroslav@178
    74
            e = new OpeningNodeViewEntry(m,code);
jaroslav@178
    75
            entries.put(m, e);
jaroslav@178
    76
        }
jaroslav@178
    77
        e.processGame(gId);
jaroslav@178
    78
        empty = false;
jaroslav@178
    79
    }
jaroslav@178
    80
jaroslav@178
    81
    @XmlElementWrapper(name="children")
jaroslav@178
    82
    @XmlElement(name="item")
jaroslav@178
    83
    public Set<OpeningNodeViewEntry> getChildren(){
jaroslav@178
    84
        Set<OpeningNodeViewEntry> result = new HashSet<OpeningNodeViewEntry>();
jaroslav@178
    85
        for(Map.Entry<Move, OpeningNodeViewEntry> e: entries.entrySet()){
jaroslav@178
    86
            result.add(e.getValue());
jaroslav@178
    87
        }
jaroslav@178
    88
        return result;
jaroslav@178
    89
    }
jaroslav@178
    90
jaroslav@178
    91
jaroslav@178
    92
    public void addFinishedGame(GameId gId){
jaroslav@178
    93
        if(gId.getStatus().equals(GameStatus.whiteWon))
jaroslav@178
    94
            whiteGames.add(gId);
jaroslav@178
    95
        if(gId.getStatus().equals(GameStatus.blackWon))
jaroslav@178
    96
            blackGames.add(gId);
jaroslav@178
    97
        empty = false;
jaroslav@178
    98
    }
jaroslav@178
    99
jaroslav@178
   100
    @XmlAttribute
jaroslav@178
   101
    public int getWhiteCount(){
jaroslav@178
   102
        return whiteGames.size();
jaroslav@178
   103
    }
jaroslav@178
   104
jaroslav@178
   105
    @XmlAttribute
jaroslav@178
   106
    public int getBlackCount(){
jaroslav@178
   107
        return blackGames.size();
jaroslav@178
   108
    }
jaroslav@178
   109
jaroslav@178
   110
jaroslav@178
   111
    @XmlAttribute
jaroslav@178
   112
    public boolean getChildrenNotEmpty(){
jaroslav@178
   113
        return !entries.isEmpty();
jaroslav@178
   114
    }
jaroslav@178
   115
jaroslav@178
   116
    public boolean isEmpty(){
jaroslav@178
   117
        return this.empty;
jaroslav@178
   118
    }
jaroslav@178
   119
jaroslav@178
   120
}