Newer
Older
IrisServerWebSocket / src / main / java / com / ntlab / irisserver / resources / DrawingsRest.java
Kota on 8 Dec 2022 2 KB first commit
package com.ntlab.irisserver.resources;

import com.ntlab.irisserver.entities.Drawing;
import com.ntlab.irisserver.entities.Game;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.models.RoomManager;
import com.ntlab.irisserver.utils.Base64Decode;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

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

@Component
@Path("/rooms")

public class DrawingsRest implements ApplicationContextAware {

    private org.springframework.context.ApplicationContext applicationContext;

    public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    // お絵描きの終了判定
    @GET
    @Path("/{rid}/game/drawings")
    public String getDrawings(@PathParam("rid") String rid) {
        // 絵のデータ取得
        // 一旦必要のない方向性で行く
        return null;
    }

    // 絵のデータ取得(パスを渡せば良い)
    @GET
    @Path("/{rid}/game/drawings/{dno}")
    @Produces(MediaType.APPLICATION_JSON)
    public Drawing getDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno ) {

        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        Game game = room.getGame();
        Drawing drawing = game.getDrawing(dno);

        return drawing;
    }

    // 描いた絵を送る
    @PUT
    @Path("/{rid}/game/drawings/{dno}")
    @Produces(MediaType.APPLICATION_JSON)
    public String putDrawing(@PathParam("rid") String rid, @PathParam("dno") int dno, @FormParam("drawing") String drawingStr ) {
        String pathDirectory = "";
        String drawingPath = "";

        try {
            pathDirectory = applicationContext.getResource("file:").getFile().getAbsolutePath() + "/apache-tomcat-9.0.10/webapps/irisdata/image";
            // utilsのBase64Decodeにデコードしてもらって、そのついでに保存してもらう
            drawingPath = Base64Decode.saveAsFile(rid, dno, pathDirectory, drawingStr);

            System.out.println("kota0210/base64Decodeは成功。drawingPath完成→"+ drawingPath.toString());
            RoomManager rm = RoomManager.getInstance();
            Room room = rm.getRoom(rid);
            Game game = room.getGame();

            // 絵の情報を上書き
            Drawing drawing = game.getDrawing(dno);

            if (drawing == null) {
                drawing = new Drawing();
                System.out.println("kota0210/nullだったからdrawingをnewした");
            }
            drawing.setDrawing(drawingPath);
            game.putDrawing(dno, drawing);

        } catch (IOException e) {
            e.printStackTrace();
            throw new WebApplicationException(500);
        }

        return "画像保存完了";
    }

}