statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTreeNode.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 191 c8a0edd2a392
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
jaroslav@178
     1
/*
jaroslav@178
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@178
     3
 *
jaroslav@178
     4
 * The contents of this file are subject to the terms of either the GNU
jaroslav@178
     5
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@178
     6
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@178
     7
 * "License"). You may not use this file except in compliance with the
jaroslav@178
     8
 * License. You can obtain a copy of the License at
jaroslav@178
     9
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@178
    10
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@178
    11
 * specific language governing permissions and limitations under the
jaroslav@178
    12
 * License.  When distributing the software, include this License Header
jaroslav@178
    13
 * Notice in each file and include the License file at
jaroslav@178
    14
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jaroslav@178
    15
 * particular file as subject to the "Classpath" exception as provided
jaroslav@178
    16
 * by Sun in the GPL Version 2 section of the License file that
jaroslav@178
    17
 * accompanied this code. If applicable, add the following below the
jaroslav@178
    18
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@178
    19
 * your own identifying information:
jaroslav@178
    20
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@178
    21
 *
jaroslav@178
    22
 * Contributor(s):
jaroslav@178
    23
 *
jaroslav@178
    24
 * Portions Copyrighted 2010 Martin Rexa
jaroslav@178
    25
 */
jaroslav@178
    26
jaroslav@178
    27
package cz.xelfi.quoridor.statistics;
jaroslav@178
    28
jaroslav@178
    29
import cz.xelfi.quoridor.webidor.Game;
jaroslav@178
    30
import cz.xelfi.quoridor.webidor.GameId;
jaroslav@178
    31
import cz.xelfi.quoridor.webidor.GameStatus;
jaroslav@178
    32
import cz.xelfi.quoridor.webidor.User;
jaroslav@178
    33
import cz.xelfi.quoridor.Move;
jaroslav@178
    34
import java.util.HashMap;
jaroslav@178
    35
import java.util.Map;
jaroslav@178
    36
import java.util.List;
jaroslav@178
    37
import java.util.ArrayList;
jaroslav@178
    38
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@178
    39
jaroslav@178
    40
/**
jaroslav@178
    41
 *
jaroslav@178
    42
 * @author Martin Rexa
jaroslav@178
    43
 */
jaroslav@178
    44
public class OpeningTreeNode {
jaroslav@178
    45
    @XmlAttribute
jaroslav@178
    46
    String hashCode;
jaroslav@178
    47
    List<GameId> gameIds;
jaroslav@178
    48
    List<GameId> finishedGames;
jaroslav@178
    49
    Map<Move, OpeningTreeNode> children;
jaroslav@178
    50
jaroslav@178
    51
    public OpeningTreeNode(){
jaroslav@178
    52
    }
jaroslav@178
    53
jaroslav@178
    54
    public OpeningTreeNode(String hashCode){
jaroslav@178
    55
        this.hashCode = hashCode;
jaroslav@178
    56
        gameIds = new ArrayList<GameId>();
jaroslav@178
    57
        finishedGames = new ArrayList<GameId>();
jaroslav@178
    58
        children = new HashMap<Move,OpeningTreeNode>();
jaroslav@178
    59
    }
jaroslav@178
    60
jaroslav@178
    61
    public OpeningTreeNode addGame(Game game){
jaroslav@178
    62
        gameIds.add(game.getId());
jaroslav@178
    63
        return this;
jaroslav@178
    64
    }
jaroslav@178
    65
jaroslav@178
    66
    public OpeningTreeNode addFinishedGame(Game game){
jaroslav@178
    67
        finishedGames.add(game.getId());
jaroslav@178
    68
        return this;
jaroslav@178
    69
    }
jaroslav@178
    70
jaroslav@178
    71
    public OpeningTreeNode addGameId(GameId gameId){
jaroslav@178
    72
        gameIds.add(gameId);
jaroslav@178
    73
        return this;
jaroslav@178
    74
    }
jaroslav@178
    75
jaroslav@178
    76
    public OpeningTreeNode addChild(Move move, OpeningTreeNode child){
jaroslav@178
    77
        children.put(move, child);
jaroslav@178
    78
        return this;
jaroslav@178
    79
    }
jaroslav@178
    80
jaroslav@178
    81
    public OpeningNodeView getView(User user){
jaroslav@178
    82
        return getView(user.getId());
jaroslav@178
    83
    }
jaroslav@178
    84
jaroslav@178
    85
    public OpeningNodeView getView(String userId){
jaroslav@178
    86
        OpeningNodeView view = new OpeningNodeView(hashCode);
jaroslav@178
    87
        for(Map.Entry<Move, OpeningTreeNode> e: children.entrySet()){
jaroslav@178
    88
            for(GameId gId: e.getValue().gameIds){
jaroslav@178
    89
                if(User.canSee(gId, userId)){
jaroslav@178
    90
                    view.processGame(e.getKey(), e.getValue().hashCode, gId);
jaroslav@178
    91
                }
jaroslav@178
    92
            }
jaroslav@178
    93
        }
jaroslav@178
    94
        for(GameId gId: finishedGames){
jaroslav@178
    95
            view.addFinishedGame(gId);
jaroslav@178
    96
        }
jaroslav@178
    97
        return view;
jaroslav@178
    98
    }
jaroslav@178
    99
jaroslav@178
   100
    public List<GameId> filterGames(String userId, GameStatus status){
jaroslav@178
   101
        List<GameId> result = new ArrayList<GameId>();
jaroslav@178
   102
        for(GameId gId: gameIds){
jaroslav@178
   103
            if(User.canSee(gId, userId))
jaroslav@178
   104
                if(gId.getStatus().equals(status))
jaroslav@178
   105
                    result.add(gId);
jaroslav@178
   106
        }
jaroslav@178
   107
        return result;
jaroslav@178
   108
    }
jaroslav@178
   109
jaroslav@178
   110
    public List<GameId> getGameIds(){
jaroslav@178
   111
        return gameIds;
jaroslav@178
   112
    }
jaroslav@178
   113
jaroslav@178
   114
//    @XmlAttribute
jaroslav@178
   115
//    @XmlElement
jaroslav@178
   116
    public String getHashCode(){
jaroslav@178
   117
        return this.hashCode;
jaroslav@178
   118
    }
jaroslav@178
   119
jaroslav@178
   120
jaroslav@178
   121
    @Override
jaroslav@178
   122
    public boolean equals(Object obj) {
jaroslav@178
   123
        if (obj == null) {
jaroslav@178
   124
            return false;
jaroslav@178
   125
        }
jaroslav@178
   126
        if (getClass() != obj.getClass()) {
jaroslav@178
   127
            return false;
jaroslav@178
   128
        }
jaroslav@178
   129
        OpeningTreeNode other = (OpeningTreeNode)obj;
jaroslav@178
   130
        return hashCode.equals(other.hashCode);
jaroslav@178
   131
    }
jaroslav@178
   132
jaroslav@178
   133
    @Override
jaroslav@178
   134
    public int hashCode() {
jaroslav@178
   135
        return hashCode.hashCode();
jaroslav@178
   136
    }
jaroslav@178
   137
jaroslav@178
   138
}