#ifndef _PLAYER_MANAGER_H_INCLUDE_
#define _PLAYER_MANAGER_H_INCLUDE_

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

#include <map>

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

	/// һ˵ϵͳ
	bool AddRobot(CPlayer *pPlayer);
	/// õеĻ
	CPlayer* GetFreeRobot(void);
	/// õл˸
	int GetFreeRobotCount(void);
	/// õܵӮ
	int64 GetRobotTotalResult(void);

	/// е
	void ClearAllPlayers(void);
	/// һҵϵͳ
	bool AddPlayer(CPlayer *pPlayer);
	/// ҵID
	void Reset(CPlayer *pPlayer);
	/// ϵͳɾָ
	void ClearPlayer(CPlayer *pPlayer);
	/// ϵͳժָ
	void DeletePlayer(CPlayer *pPlayer);
	///// ϵͳɾָID
	//void ClearPlayerByconnId(uint32 connId);
	/// ͨͻIDõͻ
	CPlayer* GetPlayer(uint32 connId);
	/// ͨͻIDõյ½Ŀͻ
	CPlayer* GetNewPlayer(uint32 connId);
	/// ͨIDõͻ
	CPlayer* GetPlayerById(uint32 id);
	/// õܵӮ
	int64 GetPlayerTotalResult(void);
	/// õǰϵͳҸ
	inline int GetPlayerCount(void) { return (int)m_PlayerList.size(); }
	/// õǰϵͳ˸
	inline int GetRobotCount(void) { return (int)m_RobotList.size(); }
	/// õǰϵͳܵ
	inline int GetTotalCount(void) { return GetPlayerCount() + GetRobotCount() + GetLostPlayerCount(); }
	/// ǰ
	inline void LockPlayerList(void) { m_PlayersLock.Acquire(); }
	/// ǰ
	inline void UnlockPlayerList(void) { m_PlayersLock.Release(); }
	/// õб
	inline std::map<uint32,CPlayer*>& GetPlayerList(void) { return m_PlayerList; }
	/// õ߻б
	inline std::map<uint32,CPlayer*>& GetRobotList(void) { return m_RobotList; }
	/// õб
	inline std::map<uint32,CPlayer*>& GetLostPlayerList(void) { return m_LostPlayerList; }
	/// ʾλϢ
	void UpdatePlayersTableInfo(void);

	/// Ϣǰߵ
	void SendMsgToEveryone(Json::Value &msg);
	/// Ϣָҵ
	void SendMsgToOtherPlayer(uint32 connId,Json::Value &msg);
	/// ϢָID
	void SendMsgToPlayer(uint32 UserID,Json::Value &msg);

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

	/// һҵб
	void AddLostPlayer(CPlayer *pPlayer);
	/// IDõ
	CPlayer* GetLostPlayer(uint32 userId);
	/// IDɾ
	void DeleteLostPlayer(uint32 userId);
	/// õ
	inline int GetLostPlayerCount(void) { return (int)m_LostPlayerList.size(); }
	/// õ⵽Ѿߵб
	void GetRealLostPlayerList(void);

private:
	std::map<uint32,CPlayer*> m_PlayerList;                 /**< б */
	std::map<uint32,CPlayer*> m_RobotList;                  /**< б */
	std::map<uint32,CPlayer*> m_LostPlayerList;             /**< б */

	Mutex m_PlayersLock;                                    /**< ǰ */
};

#define ServerPlayerManager PlayerManager::getSingleton()

#endif
