#ifndef _C_GAME_LOGIC_FRAME_H_INCLUDE_
#define _C_GAME_LOGIC_FRAME_H_INCLUDE_

#include "cdefines.h"
#include "CmdHelper.h"
#include "GameLogic.h"
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>

class CServerLogicFrame : public ServerLogicFrame
{
public:
	/// 캯
	CServerLogicFrame();
	/// 
	~CServerLogicFrame();

	/// õǰӦ÷
	virtual void SetGameRoom(Room* pRoom) { m_g_GameRoom = pRoom; }
	/// ڴûʼϷʼϢ
	virtual void OnProcessPlayerGameStartMes();
	/// ڴûϷϢ
	virtual void OnProcessPlayerRoomMes(int playerId,Json::Value &mes);
	/// û뷿Ϣ
	virtual void OnProcessEnterRoomMsg(int playerId);
	/// û뿪Ϣ
	virtual void OnProcessLeaveRoomMsg(int playerId);
	/// ûϢ
	virtual void OnProcessReEnterRoomMes(int playerId);
	/// ûϢ
	virtual void OnProcessOfflineRoomMes(int playerId);
	/// ûʱϢ
	virtual void OnProcessTimerMsg(int timerId,int curTimer);

	//////////////////////////////////////////////////////////////////////////
	void Reset();
	//////////////////////////////////////////////////////////////////////////

	void OperOut(int iChairID, CMD_C_OutMJ &cmdOut, bool isTimeOut = false);

	void OperAUTO(int iChairID, bool isTimeOut);

	void OperTouch(int iChairID, int iTouchTpye = 0);

	void OperPeng(int playerId);

	void OperGang(int iChairID, CMD_C_GangMJ &cmdGang);

	void FanCount(int &iChairID, MJBYTE bMJ, int FanStyle[], enHuType enhuType, int64 &iHuPaiFan, int64 &iExtraFan);

	void LookHuaZhuDaJiao(CMD_S_GameOver &cmdsGameOver);

	void OperHu(int playerId, int iDianPaoPlayer, enHuType enhuType);

	void OperGameOver(enEndGameType endGameType);

	void OperGiveUp(int playId);

	void OperTrustee(int iChairID, bool isTimeOut = false);

	//iWalkType 0ʾ 1ʾ
	bool isHu(int &iChairID, MJBlock bSelfHUMJ[], int iSelfHUMJCount, MJBYTE bSelfHandMJ[], int iSelfHandMJCount, MJBYTE bMJ, bool iWalkType = false);

	void UpdateOperation(int &iChairID, MJBYTE &iOutMJ, MJBlock bHuMJ[], MJBYTE iHuMJCount, MJBYTE bHandMJ[], MJBYTE iHandMJCount, MJBYTE bOutMJ[], MJBYTE iOutMJCount, int &OperationType, bool iWalkType = false);
	int  UpdatePriority(int &iChairID, int &iOutPlayer);

	//ȡҽʱ״̬
	void getUserEndType(int iHuUser, enHuType enhuType, enUserEndType enUSEendType[]);

	void GoodMJToRobot(MJBYTE bAllMJ[], int iAllMJCount, int iChairID);

#ifdef ENABLE_DEBUG_TOOL
	BOOL ForTest();
#endif

//öٶ ö滻еĺ
private:

	enum Timer_Msg
	{
		//ʱʹ
		TIMER_Nomal	= 1,				//Զ
		TIMER_BUFFER,				//йܻƺӳ2ơȴͻ˵Ķ
		TIMER_CHANGETABLE,			//
		TIMER_USER_OFFLINE,			//ҵ

		TIMER_QUIT = 9,
		TIMER_STRATDROP,			//ҿֵ
		TIMER_START,
		TIMER_DELAY_READY,
		TIMER_DHU,
		TIMER_DHUNomal,
		TIMER_NewNomal,				//Զ
	};

	//غ궨
	enum Other_HongDefine
	{
		GuaFengMaxCount = 16,
	};

	//ηغ궨
	enum GuaFengRatio
	{
		GuaFeng_Null = 0,
		GuaFeng_Ming,
		GuaFeng_An_ZiMoMing,
	};
//ṹĶ
private:
	/*!< ҹηϢ */
	typedef struct tagGuaFengInfo
	{
		enOperationGangType			eGangType;										/*!< ܵ */
		//MJBYTE						eMJ;
		int							eGangPlayer;									/*!< ܵ */
		int							eYinGanger[GAME_PLAYER];						/*!< ܵ,-1Ϊûܣ0123ʾID */
		int64							eGangSorce[GAME_PLAYER];										/*!< ܵķΪoperhuṩ */
	}GuaFengInfo;

	/*!< ҿòϢ */
	typedef struct tagOperationInfo
	{
		MJFLAG						bOperation;										/*!< ҿò */
		enOperationGangType			eGangType;										/*!< иܣֻܵ͡һ */
		int32						iGangCount;										/*!< Ŀ */
		MJBYTE						bMJGang[MAX_SELECT_GANG];						/*!< ɸܵ */
	}OperationInfo;

	/*!< ҹˮϢ */
	typedef struct tagGuoShuiInfo
	{
		MJBYTE						bGuoShuiMJ;
		int							bGSStartUser;
		int							bWhoHuUser;
		int							bGangPao;
	}GuoShuiInfo;

	/*!< ҺϢ */
	typedef struct tagEnHuInfo
	{
		enHuType					iHuInfoType;
		int							iHuUser;
		int							iDianPaoUser;
		MJBYTE						bHuMJ;
		int64							iHuFan;
		int64							iExtraFan;
		int							iFanStyle[MAX_FAN_STYLE];
	}EnHuInfo;

private:
	Room *m_g_GameRoom;                                  /**< Ϸ */

	CMahJongLogic				*m_pGameLogic;

	GuaFengInfo					m_GuaFengInfo[GuaFengMaxCount];						/*!< 洢ηϢڷҹη÷ */
	OperationInfo				m_OperationInfo[GAME_PLAYER];						/*!< ҵϢ */
	GuoShuiInfo					m_GuoShuiInfo[GAME_PLAYER];							/*!< ˮ־ */
	EnHuInfo					m_HuInfo[GAME_PLAYER];								/*!< ҺϢ */
	int64							m_AllGangSorce[GAME_PLAYER];						/*!< Ҹܷ֣ܵܽͶʱʹ */
	int64							m_AllHuSorce[GAME_PLAYER];							/*!< Һܷ֣ܽʹ */
	int64							m_AllTotal[GAME_PLAYER];							/*!< ܷ֣ܽʹ */

	static const MJBYTE			bOriginalMJ[MAX_ORG_MJ];							/*!< ԭʼ齫 */
	int							m_iBisicCore;
	MJBYTE						m_bAllMJ[MAX_ORG_MJ];								/*!< 齫 */
	int							m_iTouchPos;										/*!< λ */
	int							m_iDiceNum01;										/*!< 1 */
	int							m_iDiceNum02;										/*!< 2 */
	int							m_iDiceNum11;										/*!< 2 */
	int							m_iDiceNum12;										/*!< 2 */
	int							m_iIsReRandomBanker;								/*!< 4һƵľ */

	int							m_OperPriority[GAME_PLAYER];						/*!< ҿòȼ */
	int							m_TruBuffer[GAME_PLAYER];
	int							m_iBankerUser;										/*!< ׯ */

	//////////////////////////////////////////////////////////////////////////
	// m_iOldOperateUer m_iOldCurrentUerܲ
	int							m_iOldOperateUer;									/*!<  ϴβ*/
	int							m_iOperateUer;										/*!<  ǰ*/
	int							m_iOldCurrentUer;									/*!<  һγƵĲ*/
	bool						m_StartDrop;

	MJBYTE						m_bTouchMJ;											/*!<  */
	MJBYTE						m_bGangKaiMJ[GAME_PLAYER];							/*!< ܿ */
	MJBYTE						m_bGangTouchMJ;										/*!<  */
	bool						m_isLiuLie;											/*!< Ƿ */
	int							m_isLiuLieUser[GAME_PLAYER];
	//bool						m_LastOutMJLose;									/*!< һγ齫Ƿʧ */
	MJBlock						m_bHUMJ[GAME_PLAYER][MAX_BLOCK_COUNT];				/*!< ƿ(ܵ) */
	int							m_iHUMJCount[GAME_PLAYER];							/*!< ƿ */
	MJBYTE						m_bHandMJ[GAME_PLAYER][MAX_HAND_MJ];				/*!< е */
	int							m_iHandMJCount[GAME_PLAYER];						/*!< еƵ */
	MJBYTE						m_bOutMJ[GAME_PLAYER][MAX_OUT_MJ];					/*!< Ѿ */
	int							m_iOutMJCount[GAME_PLAYER];							/*!< ѾƵ */
	int							m_iAUTO_OutCount[GAME_PLAYER];						/*!< ˴򳬹ξԶй */

	int							m_TouchPlayer;										//ǰ

	enPlayerStatu				enUserStatu[GAME_PLAYER];							/*!< ״̬ */
	enPlayerStatu				enUserStatuOld[GAME_PLAYER];						/*!< ҵǰ״̬ */

	int							m_operationGaveUp[GAME_PLAYER];						/*!< ,ʼΪoperation_huܺΪ0ø²ʱڼ */
	int							m_bQiangGangMJ[GAME_PLAYER];						/*!< ܺ齫ֻҪܺңÿ˶һ */
	int							m_isAlreadyHu;										/*!< ǷѾƣ´γʱ */

	//int							m_isTianDiHu;										/*!< 0ʲôûУ1 2غ */
	int							m_UserTouchFirst[GAME_PLAYER];						/*!< ҵһƣûΪ0Ϊ1Ϊغ׼ */
	int							m_isForceQuit[GAME_PLAYER];							/*!< Ƿǿ˳*/

	bool						m_StartQuit;										/*!< ֵʱǷǿ*/
	int64							m_QuitSorce[GAME_PLAYER];							/*!< ǿ˿۵ķ*/
	MJBYTE						m_huMJ;												/*!< 齫*/

	int							m_zuozhuang;										/*!< ׯ*/
	int							m_twice;
	int							m_DoubleHuPlayer;									/*!< һڶʱҪй*/
	int							m_iOldHandCount[GAME_PLAYER];
	int							m_HasHuTouch;
	int							m_QiangHuCount;										/*!<ܺ-1ûиֵ*/
	int							m_GiveQiangHuCount;									/*!<ܺ-1ûиֵ*/
	int							m_BeiQiangUser;
	int							m_ReadyCount;
	int64						m_UserMoney[GAME_PLAYER];
	CMD_S_GameOver				m_CmdsGameOver;

	int							m_StartUserID[GAME_PLAYER];
	int							m_ForceUserID[GAME_PLAYER];
	int							m_huRanderUser[GAME_PLAYER];
	int							m_HuPos;
#ifdef ENABLE_DEBUG_TOOL
	BOOL						m_IsDebug;											//
#endif
};

#endif
