⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robotdeal.cpp

📁 java程序五子棋源代码。 java程序五子棋源代码。
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// RobotDeal.cpp: implementation of the RobotDeal class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GobangRobot.h"
#include "RobotDeal.h"
#include "FiveStoneProcess.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CShareMemory<NODE_STRUCT> *svr_recv_queue;		//接收队列
extern CShareMemory<NODE_STRUCT> *svr_send_queue;		//发送队列
char* split(char* &text,const char *seps);
RobotDeal::RobotDeal(CListBox *pListBox,CString configfile)
{
	this->configfile = configfile;
	m_pMsgList = pListBox;
	ReadFromConfig();
}

RobotDeal::~RobotDeal()
{

}



void RobotDeal::SetThreadNo(int no)
{
	threadno = no;
}

int RobotDeal::GetThreadNo()
{
	return threadno;
}

void RobotDeal::GetDataBasepara(CString &sdatabase,CString &sdbusername,CString &sdbpassword,CString &slogdir)
{
	sdatabase = this->database;
	sdbpassword = this->dbpassword;
	sdbusername = this->dbusername;
	slogdir = this->logdir;
}

//读配置文件
void RobotDeal::ReadFromConfig()
{	
	char buf[1024];
	threadnum = ::GetPrivateProfileInt("THREAD","gobang",10,configfile);
	::GetPrivateProfileString("DB","database","",buf,sizeof(buf),configfile);
	database =buf;
	::GetPrivateProfileString("DB","username","",buf,sizeof(buf),configfile);
	dbusername = buf;
	::GetPrivateProfileString("DB","password","",buf,sizeof(buf),configfile);
	dbpassword = buf;
	::GetPrivateProfileString("LOG","gobanglogdir","",buf,sizeof(buf),configfile);
	logdir = buf;
	logfile = logdir + "gobang.txt";
}


BOOL RobotDeal::Init()
{

//	DbpInit();
	//实例化一个共享内存对象
	svr_recv_queue = new CShareMemory<NODE_STRUCT> (GOBANG_ROBOT_KEY);
	if(!svr_recv_queue->Init(QUEUE_LENGTH)){
		AddMsg("初始化接收共享内存时产生错误!","系统错误",MB_OK|MB_ICONSTOP);
		exit(-1);
	}
	else{
		AddMsg("初始化接收共享内存时成功");
	}

	svr_send_queue = new CShareMemory<NODE_STRUCT> (GOBANG_RECV_KEY);
	if(!svr_send_queue->Init(QUEUE_LENGTH)){
			AddMsg("初始化发送共享内存时产生错误!","系统错误",MB_OK|MB_ICONSTOP);
		exit(-1);
	}
	else{
		AddMsg("初始化发送共享内存时成功");
	}
	RobotInit();
	return true;

}




UINT RobotDeal::Run()
{
	Init();
	for(int i = 0; i < threadnum; i++)
	{
		SetThreadNo(i+1);
		pRecvThread[i] = AfxBeginThread(RecvThreadFunc,this,THREAD_PRIORITY_NORMAL);
		Sleep(100);
	}
	AddMsg("五子棋服务程序初始化完成!");
	return 0;
}


//功能:向监视窗口添加一条信息
//参数:csMsg是要显示的信息
void RobotDeal::AddMsg(LPCTSTR fmt , ...)
{
	va_list args;
	CTime ctTime = CTime::GetCurrentTime();
	CString csTime = ctTime.Format("[%Y-%m-%d %H:%M:%S]");
	
	char szBuf[1024];
	va_start(args, fmt);
	vsprintf(szBuf,fmt,args);
	va_end(args);

	csTime = csTime + "  " + szBuf;
	
	if(m_pMsgList->GetCount() > MAXSHOWLINES-1){
		m_pMsgList->DeleteString(0);
	}
	m_pMsgList->AddString(csTime);
	WriteLog(logfile,csTime);
}


//功能:写LOG
//参数:sLogFile为Log文件名,sFileName指名哪个文件写LOG
//参数:nLine表明在第X行写LOG,fmt见printf...
BOOL RobotDeal::WriteLog(const char *sLogFile,const char *fmt,...)
{
	va_list args;
	FILE *fp;
	time_t t;
	struct tm *ptm;
	return TRUE;
}


void RobotDeal::RobotInit(){
	char sqlbuf[1024]={0};
	CString userid,roomid,tableid,tableseq;
	CString bodytemp;
	NODE_STRUCT recvNode;
	TDBPEx * dbp;
	memset(&recvNode,0,sizeof recvNode);
	strcpy(recvNode.err_code,"000");
	strcpy(recvNode.service_id,"001");
	strcpy(recvNode.ctrlmsg,"0");
	//创建数据库连接对象
	dbp = new TDBPEx("GobangRobot",database,dbusername,dbpassword,logdir);
	if (dbp && dbp->IsReady())
	{
		AddMsg(" 创建机器人初始化dbp对象成功!");
	}
	else
	{
		AddMsg("创建机器人初始化dbp对象失败!");
	}
	dbp->Run();
	if (dbp->Connect() == PBL_Error_FAIL)
	{
		AddMsg("[RecvThreadFunc]:线程 dbp连接数据库失败!");
	}
	else
	{
		AddMsg("[RecvThreadFunc]:线程 dbp连接数据库成功!");
	}
	strcpy(sqlbuf,"select userid,roomid,tableid,tableseq from gobang_robotinfo where status = '0'");
	dbp->SQLExecDirect(sqlbuf,FALSE);
	while(dbp->SQLFetch() == PBL_Error_OK){
		dbp->SQLGetDataStr(1,userid);
		dbp->SQLGetDataStr(2,roomid);
		dbp->SQLGetDataStr(3,tableid);
		dbp->SQLGetDataStr(4,tableseq);
		recvNode.queuenum = 0 - atoi(userid); 
		strcpy(recvNode.operate_id,"000");   //获取房间列表
		strcpy(recvNode.body,userid);
		if(svr_send_queue->Insert(&recvNode) == FALSE){
		}
		strcpy(recvNode.operate_id,"002");   //获取大厅信息
		bodytemp = userid + ";" + roomid;
		strcpy(recvNode.body,bodytemp);
		if(svr_send_queue->Insert(&recvNode) == FALSE){
		}
		strcpy(recvNode.operate_id,"004");   //用户坐下
		bodytemp = userid + ";" + roomid+ ";" + tableid+ ";" + tableseq;
		strcpy(recvNode.body,bodytemp);
		if(svr_send_queue->Insert(&recvNode) == FALSE){
		}
		strcpy(recvNode.operate_id,"007");   //用户准备好
		if(svr_send_queue->Insert(&recvNode) == FALSE){
		}

	}
	dbp->SQLFreeStmt();
	dbp->DisConnect();
}


/************************************************************************
函数功能:获得了房间列表响应

************************************************************************/
void  RobotDeal::GetRoomListRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '01' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '101' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
	
}

/************************************************************************
函数功能:获得大厅信息请求响应

************************************************************************/
void  RobotDeal::GetHallInfoRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag;
	CString userid,roomnum,userinfo="";
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '02' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '102' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
	

}

void  RobotDeal::ToExitRoomRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '01' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '103' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
}

void  RobotDeal::UserSitRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid,status,oppuserid,oppstatus;
	CString roomid,tableid,tableseq;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr = split(body,SUBDIVISION);
	succflag = tempstr;
	tempstr = split(body,SUBDIVISION);
	oppuserid = tempstr;
	tempstr = split(body,SUBDIVISION);  //nickname
	tempstr = split(body,SUBDIVISION);  //picture
	tempstr = split(body,SUBDIVISION);  //sex
	tempstr = split(body,SUBDIVISION);  //Age
	tempstr = split(body,SUBDIVISION);  //gamemoney
	tempstr = split(body,SUBDIVISION);  //oppstatus
	oppstatus = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '03',oppuserid = '%s',\
			oppstatus = '%s' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '104' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
}

void  RobotDeal::UserUpRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '02',oppstatus ='00',\
			oppuserid='0' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '105' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
}

//机器人准备好响应
void  RobotDeal::UserReadyRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;
	int tempid = 0 - queuenum;
	userid.Format("%d",tempid);
	if(strcmp(succflag,"0")==0){ //成功
		sprintf(sqlbuf, "update gobang_robotinfo set status = '04',oppstatus ='00',\
			oppuserid='0' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);

	}else{
		sprintf(sqlbuf, "update gobang_robotinfo set status = '106' where userid = '%s'",userid);
		dbp->SQLExecDirect(sqlbuf,TRUE,TRUE);
		
	}
}

//机器人坐下响应
void  RobotDeal::UserStartRespond(TDBPEx *dbp,CString recvbody,int queuenum){
	char sqlbuf[1024]={0};
	char *tempstr;
	CString succflag,userid;
	char *body;
	char tempbody[1024] = {0};
	strcpy(tempbody,recvbody);
	body = tempbody;
	tempstr=split(body,SUBDIVISION);
	succflag = tempstr;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -