Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / SettingsRest.java
s-michishita on 27 May 2022 2 KB SettingsRest
package com.ntlab.irisserver.resources;

import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.models.RoomManager;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Component
@Path("rooms")
public class SettingsRest {

    RoomManager rm = RoomManager.getInstance();

//------------------------------------------------------------------------------------
    //GET:設定情報の入手
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{rid}/settings")
    public Object GetSettings(@PathParam("rid") String rid) {

        Room r = rm.getRoom(rid);

        //部屋が存在しないとき
        var response = Response.status(Response.Status.NO_CONTENT);
        if(r == null) {
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }
        else return
                "drawingTime:" + r.isDrawingTimer()+ ",\n" +
                "drawingTimerTimes:" + r.getDrawingTimerTimes() + ",\n" +
                "gameTimer:" + r.isGameTimer() +",\n" +
                "gameTimerTimes:" + r.getGameTimerTimes() + ",\n" +
                "gameTimerInitializeTimes:" + r.getGameTimerFirstThinkingTimes();
    }

//----------------------------------------------------------------------------------------------------
    //PUT:設定値の変更
    @PUT
    @Path("/{rid}/settings")
    public void PutSettings(@PathParam("rid") String rid,
                              @FormParam("drawingTimer") boolean dTimer,
                              @FormParam("drawingTimerTimes") int dTimerTimes,
                              @FormParam("gameTimer") boolean gTimer,
                              @FormParam("gameTimerTimes") int gTimerTimes,
                              @FormParam("gameTimerFirstThinkingTimes") int gTimerFTTimes) {

        Room r = rm.getRoom(rid);

        //部屋がある時、値を変更
        if(r != null) {
            r.setDrawingTimer(dTimer);
            r.setDrawingTimerTimes(dTimerTimes);
            r.setGameTimer(gTimer);
            r.setGameTimerTimes(gTimerTimes);
            r.setGameTimerFirstThinkingTimes(gTimerFTTimes);
        }else{
            //部屋がなければエラー
            var response = Response.status(Response.Status.NO_CONTENT);
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }
    }

}