statistics/src/main/java/cz/xelfi/quoridor/statistics/resources/Openings.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
     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 
    19 package cz.xelfi.quoridor.statistics.resources;
    20 
    21 import cz.xelfi.quoridor.IllegalPositionException;
    22 import cz.xelfi.quoridor.webidor.Game;
    23 import cz.xelfi.quoridor.webidor.GameId;
    24 import cz.xelfi.quoridor.statistics.OpeningTree;
    25 import cz.xelfi.quoridor.statistics.OpeningTreeNode;
    26 import cz.xelfi.quoridor.statistics.OpeningNodeView;
    27 import cz.xelfi.quoridor.webidor.GameStatus;
    28 import java.util.List;
    29 import javax.ws.rs.GET;
    30 import javax.ws.rs.Path;
    31 import javax.ws.rs.PathParam;
    32 import javax.ws.rs.Produces;
    33 import javax.ws.rs.QueryParam;
    34 import javax.ws.rs.DefaultValue;
    35 import javax.ws.rs.core.MediaType;
    36 
    37 /**
    38  *
    39  * @author Martin Rexa
    40  */
    41 public class Openings {
    42     OpeningTree tree;
    43 
    44     public Openings(){
    45         tree = new OpeningTree();
    46     }
    47 
    48     public void processGame(Game game) {
    49         try{
    50             tree.processGame(game);
    51         }catch (IllegalPositionException e){
    52             e.printStackTrace();
    53         }
    54     }
    55 
    56     @GET
    57     @Path("{nodeId}")
    58     @Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    59     public OpeningNodeView getNode(@QueryParam("loginID") @DefaultValue("") String loginId,
    60                          @PathParam("nodeId") String nodeId){
    61         OpeningTreeNode node = tree.getNode(nodeId);
    62         if(node == null)
    63             node = tree.getRoot();
    64         return node.getView(loginId);
    65     }
    66 
    67     @GET
    68     @Path("{nodeId}.check")
    69     @Produces({ MediaType.TEXT_PLAIN })
    70     public String checkNode(@QueryParam("loginID") @DefaultValue("") String loginId,
    71                          @PathParam("nodeId") String nodeId){
    72         OpeningTreeNode node = tree.getNode(nodeId);
    73         if(node == null)
    74             return "";
    75         if(node.getView(loginId).isEmpty())
    76             return "";
    77         else
    78             return nodeId;
    79     }
    80 
    81     @GET
    82     @Path("{nodeId}/{status}")
    83     @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML })
    84     public List<GameId> listGames(
    85         @DefaultValue("") @QueryParam("loginID") String loginId,
    86         @PathParam("nodeId") String nodeId,
    87         @DefaultValue("") @PathParam("status") String status
    88     ) {
    89         OpeningTreeNode node = tree.getNode(nodeId);
    90         if(node == null)
    91             return null;
    92         if("white".equals(status))
    93             return node.filterGames(loginId, GameStatus.whiteWon);
    94         else if("black".equals(status))
    95             return node.filterGames(loginId, GameStatus.blackWon);
    96         else
    97             return null;
    98     }
    99 }