Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / TurnsRest.java
package com.ntlab.irisserver.resources;
import com.ntlab.irisserver.entities.*;
import org.springframework.stereotype.Component;
import com.ntlab.irisserver.models.RoomManager;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Iterator;

@Component
@Path("/rooms")


public class TurnsRest {
    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns:

    @GET //現在のターンの取得
    @Path("/{rid}/game/turns")
    public Integer getTurnNumber(@PathParam("rid") String rid) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Integer tn = game.getNowTurn();

        return tn;
    }

    @POST //新しいターンの作成
    @Path("/{rid}/game/turns")
    @Produces(MediaType.APPLICATION_JSON)
    public void addTurns(@PathParam("rid") String rid) {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        game.createTurn();

    }

    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}:

    @GET //Turnインスタンスの取得
    @Path("/{rid}/game/turns/{tno}")
    @Produces(MediaType.APPLICATION_JSON)
    public TurnJson getTurns(@PathParam("rid") String rid, @PathParam("tno") int tno) {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        TurnJson tj = new TurnJson(t);

        if (t == null) {
            //部屋がなければエラー
            var response = Response.status(Response.Status.NO_CONTENT);
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }

        return tj;
    }
    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/hint:

    @GET //ヒントを取得
    @Path("/{rid}/game/turns/{tno}/hint")
    public String getHint(@PathParam("rid") String rid, @PathParam("tno") int tno){
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getHint();
    }

    @PUT //ヒントと最大数を入力することで諜報員のターンに遷移する
    @Path("/{rid}/game/turns/{tno}/hint")
    public void putHint(@PathParam("rid") String rid, @PathParam("tno") int tno, @FormParam("hint") String hint, @FormParam("max") int max){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        t.setHint(hint);
        t.setMax(max);
        t.setTurnstate(1);

    }

    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/max:
    @GET //Maxを取得
    @Path("/{rid}/game/turns/{tno}/max")
    public Integer getMax(@PathParam("rid") String rid, @PathParam("tno") int tno){
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getMax();
    }

/*
    @PUT //putHintに吸収合併
    @Path("/{rid}/game/turns/{tno}/max")
    public void putMax(@PathParam("rid") String rid, @PathParam("tno") int tno, @FormParam("max") int max){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        t.setMax(max);

    }
*/
    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/questions:

    @GET //カードごとの疑っている人のリストを取得...は動いてんのかな?
    @Path("/{rid}/game/turns/{tno}//questions")
    @Produces(MediaType.APPLICATION_JSON)
    public Boolean[] getQ(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getQuestionsList();
    }

    @POST //怪しいの切り替え
    @Path("/{rid}/game/turns/{tno}/questions")
    public void changeQ(@PathParam("rid") String rid, @PathParam("tno") int tno, @FormParam("cno") int cno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        t.setQuestions(cno);

    }

    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/openlist:

    @GET
    @Path("/{rid}/game/turns/{tno}/openlist")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Integer> getOpenList(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getOpenListAll();
    }

    @PUT //オープンリストの追加 カードをめくったときの色ごとの処理()
    @Path("/{rid}/game/turns/{tno}/openlist")
    public void setOpenList(@PathParam("rid") String rid, @PathParam("tno") Integer tno, @FormParam("cno")Integer cno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        Cell card = game.getCell(cno);

        t.addOpenList(cno);
        card.setIsOpen(true);

        //turnが続くかの判定
        int i;
        int r = 0;
        int b = 0;
        for(i = 0; i < 16; i++){
            Cell cell = game.getCell(i);
            boolean isOpen = cell.getIsOpen();
            if(isOpen == true){
                if(cell.getColor() == "r")r++;
                if(cell.getColor() == "b")b++;
        }
        }
        if(card.color == "d"){
            t.setEndstate(1);
        }else if(r >= 6||(b >= 5)){
            t.setEndstate(1);
        }else if(t.getMax() <= t.getOpenListAll().size()){
            game.createTurn();
        }else if(card.color == "g"||(card.color != t.getTeam())){
            game.createTurn();
        }

    }

    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/turnstate:

    @GET
    @Path("/{rid}/game/turns/{tno}/turnstate")
    @Produces(MediaType.APPLICATION_JSON)
    public Integer getTurnstate(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getTurnstate();
    }

    @PUT //Turnstateを1に変更
    @Path("/{rid}/game/turns/{tno}/turnstate")
    public void setTurnstate(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        t.setTurnstate(1);

    }

    //------------------------------------------------------------------------
    // rooms/{rid}/game/turns/{tno}/endstate:

    @GET
    @Path("/{rid}/game/turns/{tno}/endstate")
    @Produces(MediaType.APPLICATION_JSON)
    public Integer getEndstate(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);

        return t.getEndstate();
    }

    @PUT //Endstateを1に変更
    @Path("/{rid}/game/turns/{tno}/endstate")
    public void setEndstate(@PathParam("rid") String rid, @PathParam("tno") int tno){

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        t.setEndstate(1);

    }

}