Newer
Older
SproutServerMicro / src / main / java / org / ntlab / SproutServerMicro / rooms / Rooms.java
s-bekki on 5 Dec 2017 3 KB テスト
package org.ntlab.SproutServerMicro.rooms;

import java.util.HashMap;

import javax.ws.rs.FormParam;

import net.arnx.jsonic.JSONHint;


public class Rooms {

	public static final int playerNumber = 2; //プレイヤー数(ルーム上限人数)
	private static Rooms theInstance = null;
	public HashMap<Integer,Room> roomList = new HashMap<Integer,Room>();
    public int roomId = 0; // ルームID
	private Room waitingRoom = null;  //待ち部屋
	private Room waitingRoomSecond = null; //待ち部屋2
	private RoomsConnection roomsConnection = null; //ルームコネクション


	private Rooms(){
		if (theInstance == null) {
			theInstance = this;
		}		
	}
	
	public static Rooms getInstance() {
		if (theInstance == null) {
			theInstance = new Rooms();
		}
		return theInstance;
	}
	


    /**
     * 部屋のメンバーの準備チェック
     */
    public int readyCheck(int roomId,boolean frag){
    	int checkCount = 0;
    	int battleCount = 0;
    	Room room = roomList.get(roomId);
    	
    	if(frag == true){
    		for(int i = 0;i<room.getMemberId();i++){
    			if(room.getMemberList().get(i) != null && room.getMemberList().get(i).isReady() == true){
    				checkCount++;
    			}
    			if(checkCount == playerNumber){
    				room.setStartFrag(true);
    				if(room.isStartFrag() == true && room.getMemberList().size() == playerNumber && this.waitingRoom == null){
    					this.waitingRoom = room;
    				}else if(room.isStartFrag() == true && room.getMemberList().size() == playerNumber && this.waitingRoom != null){
    					this.waitingRoomSecond = room;
    					battleCount = 1;
    				}
    			}
    		}
    	}else if(frag == false){
    		if(room == this.waitingRoom){
    			this.waitingRoom = this.waitingRoomSecond;
    			this.waitingRoomSecond = null;
    			room.setStartFrag(false);
    		}
    		if(room == this.waitingRoomSecond){
    			this.waitingRoomSecond = null;
    			room.setStartFrag(false);
    		}
    	}

    	return battleCount;
    }

    
	public Room makeRoomBattle(@FormParam("roomId") int roomId, @FormParam("userId") int userId, @FormParam("roomName") String roomName, @FormParam("key") String key) {
		
		Member member = new Member(userId);
		
		Room room = new Room(roomName,roomId,key,member);
		roomList.put(roomId,room);
	
		return room;
	}
  

	
	//ルームリスト
	@JSONHint(ignore=true)
	public HashMap<Integer,Room> getRoomList() {
		return roomList;
	}


//	public void setRoomList(HashMap<Integer,Room> roomList) {
//		Rooms.roomList = roomList;
//	}

	
	//待ち部屋1
	//@JSONHint(ignore=true)
	public Room getWaitingRoom() {
		return waitingRoom;
	}

	public void setWaitingRoom(Room waitingRoom) {
		this.waitingRoom = waitingRoom;
	}

	//待ち部屋2
	//@JSONHint(ignore=true)
	public Room getWaitingRoomSecond() {
		return waitingRoomSecond;
	}

	public void setWaitingRoomSecond(Room waitingRoomSecond) {
		this.waitingRoomSecond = waitingRoomSecond;
	}

	//人数
	@JSONHint(ignore=true)
	public int getPlayerNumber() {
		return playerNumber;
	}


	//ルームID
	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	@JSONHint(ignore = true)
    public RoomsConnection getRoomsConnection() {
        return roomsConnection;
    }

}