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

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

@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 RoomManager(){
        Room r = new Room("test", "aaa");
        rooms.put("test", r);
        r.addMember("aaa");

        //メンバー設定()
        int length = 0;
        char[] rch;
        rch = new char[3];
        String rname;
        while(length < 10){
            for(int i = 0; i<3; i++){
                Random random = new Random();
                rch[i] = (char)(random.nextInt(26) + 'a');
            }
            rname = new String(rch);
            r.addMember(rname);
            List<Member> members = r.getMembers();
            length = members.size();
        }

        //チームシャッフル
        List<Member> m = r.getMembers();
        Collections.shuffle(m);
        for(int i=0; i < 10; i++){
            if(i >= m.size())break;
            Member info = m.get(i);
            if(i==0){
                info.setMaster(true);
                r.changeTeamAndMaster(info.getNickname(), "r", true);
            }else if(i==1){
                info.setMaster(true);
                r.changeTeamAndMaster(info.getNickname(), "b", true);
            }else{
                info.setMaster(false);
            }
            if(i % 2 == 0){
                info.setBelongs("r");
            }else{
                info.setBelongs("b");
            }
        }

        //gameインスタンス作成、取得
       

    }
    //-----------------------------------------------------------------


    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);
    }


}