Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / StateRest.java
Kota on 5 Jan 2023 2 KB 復元
package com.ntlab.irisserver.resources;

import com.ntlab.irisserver.entities.Game;
import com.ntlab.irisserver.models.KeywordManager;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.models.RoomManager;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;


@Component
@Path("/rooms")

public class StateRest implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    @GET//部屋の情報を入手

    @Path("/{rid}/state")

    public int getState(@PathParam("rid") String rid) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);

        if(room==null){
            var response = Response.status(Response.Status.NO_CONTENT);
            response.status(404).entity("部屋が存在しません");
            throw new WebApplicationException(response.build());
        }

        return room.getState(); //部屋の情報を送る
    }
//----------------------------------------------------------------------------------------------------------------------------------
    @PUT//部屋の情報を変更する

    @Path("/{rid}/state")

    //rid,nickname,stateを引数として持ってくる
    public void putState(@PathParam("rid") String rid,@FormParam("nickname") String nickname,@FormParam("state") Integer state) throws IOException {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);


        if(nickname.equals(room.ownerName)){ //nicknameがownerだった場合
            room.setState(state); //部屋の情報を変更
            if (state == 2) {
                String path;
                path = applicationContext.getResource("file:").getFile().getAbsolutePath()+"/apache-tomcat-9.0.10/webapps/irisdata/keywords.txt";
                //path = "C:\\Users\\student\\IdeaProjects\\IrisServer\\src\\main\\java\\com\\ntlab\\irisserver\\tmp\\keywords.txt";
                KeywordManager km = KeywordManager.getInstance();
                String[] keywords = km.getKeywords(path);
                Game game = new Game(room, keywords);
            }
        }
    }


}