#ifndef _ROOM_MANAGER_H_INCLUDE_
#define _ROOM_MANAGER_H_INCLUDE_

#include "../../include/molnet/Network.h"

#include "CRoom.h"
#include "CPlayer.h"

#include <vector>

class RoomManager : public Singleton<RoomManager>
{
public:
	RoomManager();
	~RoomManager();

	/// еķ
	void ClearAllRooms(void);

	/// һ䵽
	void AddRoom(CRoom *pRoom);
	/// һҵ
	bool AddPlayer(CPlayer *pPlayer,int roomIndex=-1,int chairIndex=-1,bool isQueue=true,bool isGaming=true);
	/// һԹҵ
	bool AddLookOnPlayer(CPlayer *pPlayer,int roomIndex,int chairIndex);
	/// ıһҵϷ
	bool ChangeGameRoom(CPlayer *pPlayer);
	/// ӷָ
	void ClearPlayer(CPlayer *pPlayer);
	/// ӵǰɾָ
	bool DeletePlayer(CPlayer *pPlayer);
	/// еϷ
	void ResetAllGameRooms(void);
	/// ݷIDŵõ
	CRoom* GetRoomById(int id);
	/// õһշ
	CRoom* GetEmptyRoom(void);
	/// õһδΪյķ
	CRoom* GetUnderFullRoom(void);
	/// õһеķ
	CRoom* GetFreeRoom(void);
	/// õǰϵͳеķб
	inline std::vector<CRoom*>& GetRoomList(void) { return m_RoomList; }
	/// õǰϵͳз
	inline int GetRoomCount(void) { return (int)m_RoomList.size(); }
	/// õǰлж
	int GetRoomPlayerCount(void);
	/// סǰ
	inline void LockRoomList(void) { m_RoomLock.Acquire(); }
	/// ǰ
	inline void UnlockRoomList(void) { m_RoomLock.Release(); }

	/// ڴյϢ
	void OnProcessNetMes(uint32 connId,Json::Value &mes);
	/// ڴڶϿϢ
	void OnProcessDisconnectNetMes(uint32 connId);
	/// ڴҸıϷ
	void OnProcessChangeGameRoomMes(uint32 connId);

	///ʱ¼
	bool OnEventTimer(uint32 dwTimerID, uint32 wBindParam);

private:
	std::vector<CRoom*> m_RoomList;           /**< б */
	Mutex m_RoomLock;                      /**< ڱǰеķ */
};

#define ServerRoomManager RoomManager::getSingleton()


#endif
