Newer
Older
SproutServerMicro / src / main / java / org / ntlab / SproutServer / rooms / Rooms.java
s-bekki on 30 Nov 2017 3 KB initial commit
package org.ntlab.SproutServer.rooms;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Singleton;
import javax.naming.event.NamespaceChangeListener;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.ntlab.SproutServer.accounts.Account;
import org.ntlab.SproutServer.battles.Battles;
import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;
import org.omg.PortableServer.ThreadPolicyOperations;

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


}