#ifndef _C_ROOM_H_INCLUDE_
#define _C_ROOM_H_INCLUDE_

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

#include "../../include/molnet/netallocator/NedAllocatedObject.h"

#include "../videoPlayback/CVideoPlaybackManager.h"
#include "CRoom.h"
#include "CPlayer.h"

class Player;
class Room;

class CRoom : public Room , public NedAllocatedObject
{
public:
	CRoom(RoomType type=ROOMTYPE_NULL);
	virtual ~CRoom();

	/// ÷ID
	virtual void SetID(int id) { m_Id = id; }
	/// õID
	virtual int GetID(void) { return m_Id; }
	/// ÷Ϸ
	virtual void SetGameType(int type) { m_GameType = type; }
	/// õϷ
	virtual int GetGameType(void) { return m_GameType; }
	/// ÷Ϸ
	virtual void SetGameId(int gameid) { m_gameid = gameid; }
	/// õϷ
	virtual int GetGameId(void) { return m_gameid; }
	/// ÷ʶ
	virtual void SetRoomMarking(int marking) { m_RoomMarking = marking; }
	/// õʶ
	virtual int GetRoomMarking(void) { return m_RoomMarking; }
	/// Ϸ߼ӿ
	virtual void SetServerLogicFrame(ServerLogicFrame *frame);
	/// õϷ߼ӿ
	virtual ServerLogicFrame* GetServerLogicFrame(void) { return m_ServerLogicFrame; }
	/// ÷
	virtual void SetRoomType(RoomType type) { m_RoomType = type; }
	/// õ
	virtual RoomType GetRoomType(void) { return m_RoomType; }
	/// ÷
	virtual void SetMaxPlayer(int max)
	{
		if(max > ROOM_MAX_PLAYERS)
			m_MaxCount = ROOM_MAX_PLAYERS;
		else
			m_MaxCount = max;
	}
	/// õ
	virtual int GetMaxPlayer(void) { return m_MaxCount; }
	/// ÷״̬
	virtual void SetRoomState(RoomState state) { m_RoomState = state; }
	/// õ״̬
	virtual RoomState GetRoomState(void) { return m_RoomState; }
	/// ÷Ϸ
	virtual void SetRoomGameType(enStartMode type) { m_RoomGameType = type; }
	/// õϷ
	virtual enStartMode GetRoomGameType(void) { return m_RoomGameType; }
	/// ÷
	virtual void SetName(std::string name) { m_Name = name; }
	/// õ
	virtual std::string GetName(void) { return m_Name; }
	/// Ϸˮֵ
	virtual void SetChouShui(float cs) { m_RoomRevenue = cs; }
	/// õϷˮֵ
	virtual float GetChouShui(void) { return m_RoomRevenue; }
	/// ϷԪ
	virtual void SetGamePielement(int pielement) { m_Pielement = pielement; }
	/// õϷԪ
	virtual int GetGamePielement(void) { return m_Pielement; }
	/// ϷҪٽ
	virtual void SetLastMoney(unsigned int money) { m_RoomLastMoney = money; }
	/// õϷҪٽ
	virtual unsigned int GetLastMoney(void) { return m_RoomLastMoney; }
	/// ǰǷΪ
	virtual bool IsEmpty(void);
	/// õָ
	virtual Player* GetPlayer(int index);
	/// õʾʱָ(ͻʹ)
	virtual int SwitchViewChairID(int chairIndex) { return 0; }
	/// õǰ
	virtual int GetPlayerCount(void);
	/// õ׼״̬ҵ
	virtual int GetReadyPlayerCount(void);
	/// õǰķ
	virtual void SetMaster(int playerId);
	/// õǰķΪǰһ׼ʼϷ
	virtual inline int GetMaster(void) { return m_masterId; }
	/// õеǰ
	virtual inline int GetCurrentPlayer(void) { return m_curPlayerId; }
	/// ûIDõûڷеID
	virtual int GetPlayerRoomId(uint32 connId);
	/// õǰָ״̬Ҹ
	virtual int GetPlayerCount(PlayerState state);
	/// õǰҵ״̬
	virtual void SetAllPlayerState(PlayerState state);
	/// ָҵ״̬
	virtual void SetPlayerState(int index,PlayerState state);
	/// ÷еǰ
	virtual void SetCurrentPlayer(int playerId) { m_curPlayerId = playerId; }
	/// õǰһ
	virtual int GetCurNextPlayer(void);
	/// õǰһ
	virtual int GetCurNextWorkPlayer(void);
	/// ÷
	virtual void SetEnterPassword(std::string pwd) { m_roomenterpwd = pwd; }
	/// õ
	virtual std::string getEnterPassword(void) { return m_roomenterpwd; }
	/// ÷ҷΧ
	virtual void SetEnterMoneyRect(int64 first,int64 second) { m_roomentermoneyfirst = first; m_roomentermoneysecond = second; }
	/// õҷΧ
	virtual void GetEnterMoneyRect(int64 *first,int64 *second) { *first = m_roomentermoneyfirst; *second = m_roomentermoneysecond; }

	/// е
	void ClearAllPlayers(void);
	/// һҵǰ
	int AddPlayer(CPlayer *pPlayer,int index=-1);
	/// һԹҵǰ
	int AddLookOnPlayer(CPlayer *pPlayer,int index);
	/// ӵǰָ
	void ClearPlayer(CPlayer *pPlayer);
	/// ӵǰɾָ
	void DeletePlayer(CPlayer *pPlayer);
	/// ӵǰָID
	void ClearPlayerById(int Id);
	/// ָǷڵǰ
	bool IsExist(CPlayer *pPlayer);
	/// ָԹǷڵǰ
	bool IsExistLookOn(CPlayer *pPlayer);
	/// ⷿǷѾԱ
	virtual bool IsFull(void);
	/// ⷿǷȫ׼
	virtual bool IsReadyed(void);
	/// idõڵǰе
	virtual int GetPlayerIndex(uint32 connId);
	/// õǰе
	virtual inline int GetLostPlayerCount(void) { return (int)m_lostPlayerList.size(); }
	/// б
	void ClearLostPlayerById(int id);

	/// յǰ
	virtual void Clear(void);
	/// ޳ָңϷʱ޳
	virtual bool EliminatePlayer(Player *pPlayer);
	/// дû
	virtual bool WriteUserScore(int wChairID, int64 lScore, int64 lRevenue, enScoreKind ScoreKind,int64 pAgentmoney=0,bool isCumulativeResult=true,int64 pcurJetton=0,const char* pgametip="");
	/// Ϸʱ
	virtual void GameEnd(bool isupdateuserdata=true);
	/// Ϸʼǵ
	virtual void GameStart(void);
    /// ָҵ
    virtual void UpdateUserScore(Player *pPlayer);

	/// ⵱ǰӮ(2:ʾӮˣ1ʾˣ0ʾ䲻Ӯ
	virtual int IsUserWin(int64 *result = NULL, int64 *sysMax = NULL, int64 *sysMin = NULL, bool isUpdate = false);
	/// ָҵӮ(1:ʾˣ2ʾӮˣ0ʾ䲻Ӯ-1ƣ
	virtual int IsSuperPlayerWin(Player *pPlayer,int64 *curresult=NULL,int64 *decResult=NULL);

	/// ָҷ
	virtual void SendTableMsg(int index,Json::Value &msg);
	/// ָҷԹ
	virtual void SendLookOnMes(int index,Json::Value &msg);

	/// ʼһʱ
	virtual bool StartTimer(int timerId,int space);
	/// رһʱ
	virtual void StopTimer(int id);
	/// رеĶʱ
	virtual void StopAllTimer(void);

	/// ʼ¼
	virtual void StartVideoTape(Player *pPlayer,Room *pRoom);
	/// ¼
	virtual void EndVideoTape(void);
	//¼ϷϢ
	virtual void OnTapeGameMessage(const void * pBuffer, WORD wDataSize);

	/// ӡ־
	virtual void Room_Log(enLogLevel loglevel,std::string log);
	/// õò
	virtual std::string GetRoomParameters(void);

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

	/// ڴյϢ
	virtual void OnProcessNetMes(uint32 connId,Json::Value &mes);

	/// ڴû׼Ϣ
	void OnProcessPlayerReadyMes(int playerId);
	/// ڴûʼϷʼϢ
	void OnProcessPlayerGameStartMes(void);
	/// ڴûϷϢ
	void OnProcessPlayerRoomMes(int playerId,Json::Value &mes);
	/// û뷿Ϣ
	void OnProcessEnterRoomMsg(int playerId);
	/// û뿪Ϣ
	void OnProcessLeaveRoomMsg(int playerId);
	/// ûϢ
	void OnProcessOfflineRoomMes(int playerId);
	/// ûϢ
	void OnProcessReEnterRoomMes(int playerId);

	/// ǷϷ
	void OnProcessContinueGaming(void);

private:
	/// ҷ
	void SendAllPlayerMsg(Json::Value &sg);
	/// ǰҷû뿪Ϣ
	void AllPlayerLeveaServer(void);
	/// Թҷ
	void SendAllLookOnPlayerMsg(Json::Value &msg);
	/// ָǷڵб
	bool IsExistLostPlayer(int index);
	/// ɾеĶʱ
	void DeleteAllTimer(void);

	//ʱ
protected:
	CTimerItemArray					m_TimerItemActive;					//
	Mutex                           m_TimerLock;                        /**< ڱеĶʱ */

private:
	int m_Id;                   /**< ID */
	int m_GameType;             /**< Ϸ */
	int m_gameid;               /**< Ϸ */
	int m_RoomMarking;          /**< ʶһĶ˿ںűʾ */
	std::string m_Name;         /**<  */
	int m_MaxCount;             /**< Ҹ */
	RoomType m_RoomType;        /**<  */
	RoomState m_RoomState;      /**< 䵱ǰ״̬ */
	enStartMode m_RoomGameType; /**< Ϸ */
	float m_RoomRevenue;        /**< ˰ */
	int m_RoomLastMoney;        /**< С */
	int m_Pielement;            /**< 䵥Ԫ */
	//int64 m_JackPot;            /**<  */

	std::string m_roomenterpwd; /**<  */
	int64 m_roomentermoneyfirst,m_roomentermoneysecond;  /**< ҷΧ */

	int m_masterId;             /**< ǰID */
	int m_curPlayerId;          /**< ǰID */

	CPlayer *m_PlayerList[ROOM_MAX_PLAYERS];       /**< Ϸб */
	std::map<int,std::vector<CPlayer*> > m_LookonPlayerList;       /**< Թб */
	std::vector<int> m_lostPlayerList;            /**< еб */

	Mutex m_playersLock;                          /**< Ҳ */

	ServerLogicFrame* m_ServerLogicFrame;               /**< ڴϷ߼ */

private:
	CVideoPlaybackManager          m_VideoPlaybackManager;               /**< Ϸ¼ƻط */
	uint32                         m_bvideotapeTime;                     //¼
};

#endif
