statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Openings.java
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 264 d60370059c3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Openings.java	Thu Jan 07 22:34:17 2010 +0100
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2010 Martin Rexa
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.statistics.resources;
    1.31 +
    1.32 +import cz.xelfi.quoridor.IllegalPositionException;
    1.33 +import cz.xelfi.quoridor.webidor.Game;
    1.34 +import cz.xelfi.quoridor.webidor.GameId;
    1.35 +import cz.xelfi.quoridor.statistics.OpeningTree;
    1.36 +import cz.xelfi.quoridor.statistics.OpeningTreeNode;
    1.37 +import cz.xelfi.quoridor.statistics.OpeningNodeView;
    1.38 +import cz.xelfi.quoridor.webidor.GameStatus;
    1.39 +import java.util.List;
    1.40 +import javax.ws.rs.GET;
    1.41 +import javax.ws.rs.Path;
    1.42 +import javax.ws.rs.PathParam;
    1.43 +import javax.ws.rs.Produces;
    1.44 +import javax.ws.rs.QueryParam;
    1.45 +import javax.ws.rs.DefaultValue;
    1.46 +import javax.ws.rs.core.MediaType;
    1.47 +
    1.48 +/**
    1.49 + *
    1.50 + * @author Martin Rexa
    1.51 + */
    1.52 +public class Openings {
    1.53 +    OpeningTree tree;
    1.54 +
    1.55 +    public Openings(){
    1.56 +        tree = new OpeningTree();
    1.57 +    }
    1.58 +
    1.59 +    public void processGame(Game game) {
    1.60 +        try{
    1.61 +            tree.processGame(game);
    1.62 +        }catch (IllegalPositionException e){
    1.63 +            e.printStackTrace();
    1.64 +        }
    1.65 +    }
    1.66 +
    1.67 +    @GET
    1.68 +    @Path("{nodeId}")
    1.69 +    @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.70 +    public OpeningNodeView getNode(@QueryParam("loginID") @DefaultValue("") String loginId,
    1.71 +                         @PathParam("nodeId") String nodeId){
    1.72 +        OpeningTreeNode node = tree.getNode(nodeId);
    1.73 +        if(node == null)
    1.74 +            node = tree.getRoot();
    1.75 +        return node.getView(loginId);
    1.76 +    }
    1.77 +
    1.78 +    @GET
    1.79 +    @Path("{nodeId}.check")
    1.80 +    @Produces({ MediaType.TEXT_PLAIN })
    1.81 +    public String checkNode(@QueryParam("loginID") @DefaultValue("") String loginId,
    1.82 +                         @PathParam("nodeId") String nodeId){
    1.83 +        OpeningTreeNode node = tree.getNode(nodeId);
    1.84 +        if(node == null)
    1.85 +            return "";
    1.86 +        if(node.getView(loginId).isEmpty())
    1.87 +            return "";
    1.88 +        else
    1.89 +            return nodeId;
    1.90 +    }
    1.91 +
    1.92 +    @GET
    1.93 +    @Path("{nodeId}/{status}")
    1.94 +    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    1.95 +    public List<GameId> listGames(
    1.96 +        @DefaultValue("") @QueryParam("loginID") String loginId,
    1.97 +        @PathParam("nodeId") String nodeId,
    1.98 +        @DefaultValue("") @PathParam("status") String status
    1.99 +    ) {
   1.100 +        OpeningTreeNode node = tree.getNode(nodeId);
   1.101 +        if(node == null)
   1.102 +            return null;
   1.103 +        if("white".equals(status))
   1.104 +            return node.filterGames(loginId, GameStatus.whiteWon);
   1.105 +        else if("black".equals(status))
   1.106 +            return node.filterGames(loginId, GameStatus.blackWon);
   1.107 +        else
   1.108 +            return null;
   1.109 +    }
   1.110 +}