Newer
Older
IrisServerWebSocket / src / main / java / com / ntlab / irisserver / models / RoomManager.java
Kota on 8 Dec 2022 1 KB first commit
package com.ntlab.irisserver.models;
import com.ntlab.irisserver.entities.Room;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import java.util.*;

@Component
public class RoomManager {
    private static RoomManager theInstance = null;
    private Map<String, Room> rooms = new HashMap<String, Room>();

    public static RoomManager getInstance() {
        if(theInstance == null) {
            theInstance = new RoomManager();
        }
        return theInstance;
    }



    public Room createRoom(String owner) {
        UUID uuid = UUID.randomUUID();
        String rid = uuid.toString(); //uuid型からstring型に変換
        Room r = new Room(rid, owner);
        rooms.put(rid, r);
        return r;
    }

    public Room getRoom(String rid) {
        Room r = rooms.get(rid);
        return r;
    }

    public List<Room> getRooms() {
        List<Room> roomlist = new ArrayList<>(rooms.values());
        return roomlist;
    }

    public void deleteRoom(String rid) {
        rooms.remove(rid);
    }

    //--------------------------------テスト用--------------------------------------

    public void createTestRoom() {

        //お絵描きテスト用
        Room dr = new Room("drawtest", "owner");
        rooms.put("drawtest", dr);
        dr.addMember("owner");

        //ゲームテスト用
        Room gr = new Room("gametest", "RedSpy(owner)");
        rooms.put("gametest", gr);
        gr.addMember("RedSpy(owner)");

    }


}