Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / TurnsRest.java
package com.ntlab.irisserver.resources;
import org.springframework.stereotype.Component;
import com.ntlab.irisserver.models.RoomManager;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.entities.Game;
import com.ntlab.irisserver.entities.Turn;
import com.ntlab.irisserver.entities.Cell;
import com.ntlab.irisserver.entities.Member;

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 int getTurnNumber(@PathParam("rid") String rid) {

        int tn = 0;

        return tn;
    }

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

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

        return t;
    }

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

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

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

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

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

    @PUT
    @Path("/{rid}/game/turns/{tno}/hint")
    public void putHint(@PathParam("rid") String rid, @PathParam("tno") int tno, @FormParam("hint") String hint){

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

    }

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

    @PUT
    @Path("/{rid}/game/turns/{tno}/max")
    public void putMax(@PathParam("rid") String rid, @PathParam("tno") int tno, @FormParam("hint") 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:

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

        boolean on = true;

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Turn t = game.getTurn(tno);
        List<Member> list = t.getQuestions(cno);
        for(Iterator it = list.iterator(); it.hasNext();){
            Member m = (Member) it.next();
            if(m.getNickname() == nickname){
                on = false;
                break;
            }
        }
        Member member = room.getMember(nickname);
        if(on == true){
            t.addQuestions(cno, member);
        }else{
            t.delieteQuestions(cno, member);
        }

    }

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

    @GET
    @Path("/{rid}/game/turns/{tno}/openlist")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Cell> 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") int tno, @FormParam("cno")int cno){

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

    }

}