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