斗地主游戏实现8:代码之三(请求响应)

1. game_req.h

#ifndef GAMEREQ_H
#define GAMEREQ_H
unsigned char *gameCommIntCopy(unsigned char *, int,int,int *, int *);
unsigned int setInt8bitTo1(unsigned int);
DB_HANDER loginCompleteHander,endGameDbHander;
int checkLogin(int);
int playerSendDiscardResByfd(order_request_response *,int );
int sendCardReqbyfd(order_request_response *,int);
int callScoreReqbyfd(order_request_response *,int);
int callScoreStatusbyfd(order_request_response *,int );
order_request_response * setServerRequest(unsigned char *,int,int,int);
int errorOrderHander(order_request_response *,unsigned char);
void setCommonOrderFields(order_request_response *,unsigned char,unsigned char *,int,FREE *);
/* 定义不同的请求类型的响应处理函数 */
GAME_CPARSE_HAND keepalive,userLogin,commErrorHander,commonRequestHander,
				notLoginHander,selectGame,selectRoom,selectDesk,sendCardReq,callScoreReq,
				callScoreStatus,callScoreValue,createLandocratRes,nextGame,
				endGameRes,playerDiscardRes,playerDiscard,playerSendDiscardRes,
				nextGameRes,pauseGameReq,pauseGameRes,activeGameRes;

RSCLEAN_HANDER freeRS,freeRSAll;
#endif

 

2. game_req.c

#include "game.h"

/*
unsigned char *oderCommSetChar(unsigned char *order, unsigned char orderchar,int *resLen, int *resMaxLen) {
	if(*resLen + 1 > *resMaxLen) {
		order = (unsigned char *) memReallocBuf(order, (*resMaxLen) * 2, resMaxLen);
	}
	order[*resLen] = orderchar;
	(*resLen)++;
	return order;
}
*/
static int isGameEnd(oppose_landocrat_playing_info *playing_info);
static CWCB gameServerWriteComplete;


#define SET_STR_FIELD 1
#define SET_INT_FIELD 2
#define SET_LONG_FIELD 3
#define SET_FLOAT_FIELD 4
int isFitRule(unsigned char ppreOrder,unsigned char preOrder,unsigned char thisOrder) {
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	switch(preOrder) {
		case 0:
			if(thisOrder == LOGIN_R_I) return 1;
			return 0;
		case LOGIN_R_I:
			if(thisOrder == SELECT_GAME_R_I) return 1;
			return 0;
		case SELECT_GAME_R_I:
			if(thisOrder == SELECT_ROOM_R_I) return 1;
			return 0;
		case SELECT_ROOM_R_I:
			if(thisOrder == DESK_FIT_R_I) return 1;
			return 0;
		case DESK_FIT_R_I:
			if(thisOrder == STARTGAME_R) return 1;
			return 0;
		default : return 1;
	}
}
unsigned int setInt8bitTo1(unsigned int intv) {
	unsigned int returnV = (intv & 0x000000007F) | (((intv >> 7) & 0x000000007F) << 8) | (((intv >> 14) & 0x000000007F) << 16) | (((intv >> 21) & 0x000000007F) << 24);
	return returnV | 0x80808080;
}
unsigned char *gameCommIntCopy(unsigned char *dest, int intv,int size,int *offset, int *resMaxLen) {
	int i;
	int offsetV = 0;
	if(offset != NULL)
		offsetV = *offset;
	if(resMaxLen != NULL && (offsetV + size > *resMaxLen)) {
		dest = (unsigned char *) memReallocBuf(dest, (*resMaxLen) * 2, resMaxLen);
	}
	if(size > 0 && size <= sizeof(int)) {
		
		for(i = 0; i < size; i++) {
			dest[offsetV + i] = (unsigned char) intv;
			intv = intv >> 8;
		}
		if(offset != NULL)
			(*offset) += size;
	}
	return dest;
}

unsigned char *gameCommCharCopy(unsigned char *dest,unsigned char bitv,int *resLen, int *resMaxLen) {
	int size = 1;
	if(*resLen + size + 2 > *resMaxLen) {
		dest = (unsigned char *) memReallocBuf(dest, (*resMaxLen) * 2, resMaxLen);
	}
	dest[*resLen] = bitv;
	(*resLen) += size;
	return dest;
}
unsigned char *gameCommBufCopy(unsigned char *dest,unsigned char *buf,int size,int *resLen, int *resMaxLen) {
	if(*resLen + size + 2 > *resMaxLen) {
		dest = (unsigned char *) memReallocBuf(dest, (*resMaxLen) * 2, resMaxLen);
	}
	memcpy(dest+(*resLen),buf,size);
	(*resLen) += size;
	return dest;
}
unsigned char *gameCommStringCopy(unsigned char *dest,unsigned char *strbuf,int *resLen, int *resMaxLen) {
	int size = strlen(strbuf);
	return gameCommBufCopy(dest,strbuf,size,resLen, resMaxLen);
}
static void setPlayerStrfield(MYSQL_ROW row,void *dest,unsigned char *fname,int vtype) {
	unsigned char *colvalue;
	login_gamehash_link *loglink;
	loglink = (login_gamehash_link *)gamehash_lookup(login_fieldt,fname);
	colvalue = row[loglink->index];
	if(colvalue != NULL) {
		if(vtype == SET_STR_FIELD) {
			unsigned char **dests2 = dest;
			*dests2 = xstrdup(colvalue);
		}
		else if(vtype == SET_INT_FIELD) {
			int *desti = (int *)dest;
			*desti = atoi(colvalue);
		}
		else if(vtype == SET_LONG_FIELD) {
			long *destl = (long *)dest;
			*destl = atol(colvalue);
		}
		else if(vtype == SET_FLOAT_FIELD) {
			float *destl = (float *)dest;
			*destl = atof(colvalue);
		}
	}
	else {
		if(vtype == SET_STR_FIELD) {
			unsigned char **dests2 = dest;
			*dests2 = xstrdup("");
		}
		else if(vtype == SET_INT_FIELD) {
			int *desti = (int *)dest;
			*desti = 0;
		}
		else if(vtype == SET_LONG_FIELD) {
			long *destl = (long *)dest;
			*destl = 0L;
		}
		else if(vtype == SET_FLOAT_FIELD) {
			float *destl = (float *)dest;
			*destl = 0.0F;
		}
	}
}

static player *setPlayer(MYSQL_ROW row,int fd) {
	player_gamehash_link *plylink;
	player *P = NULL;
	P = &gamePlayers[fd];
	bzero(P,sizeof(player));
	/*P->isInit = 1;*/
	gamePlayers[fd].game_status = LOGIN_STATUS_HASLOGIN;
	setPlayerStrfield(row,&P->play_name,"play_name",SET_STR_FIELD);
	setPlayerStrfield(row,&P->password,"password",SET_STR_FIELD);
	setPlayerStrfield(row,&P->play_id,"play_id",SET_LONG_FIELD);
	setPlayerStrfield(row,&P->user_code,"user_code",SET_STR_FIELD);
	setPlayerStrfield(row,&P->open_date,"open_date",SET_STR_FIELD);
	setPlayerStrfield(row,&P->state,"state",SET_INT_FIELD);
	setPlayerStrfield(row,&P->score,"score",SET_FLOAT_FIELD);
	setPlayerStrfield(row,&P->comment,"comment",SET_STR_FIELD);
	setPlayerStrfield(row,&P->mobile,"mobile",SET_STR_FIELD);
	setPlayerStrfield(row,&P->telephone,"telephone",SET_STR_FIELD);
	setPlayerStrfield(row,&P->role_id,"role_id",SET_LONG_FIELD);
	setPlayerStrfield(row,&P->parent_id,"parent_id",SET_LONG_FIELD);
	setPlayerStrfield(row,&P->score_limited,"score_limited",SET_INT_FIELD);
	setPlayerStrfield(row,&P->win_count,"win_count",SET_INT_FIELD);
	setPlayerStrfield(row,&P->failed_count,"failed_count",SET_INT_FIELD);
	setPlayerStrfield(row,&P->abort_count,"abort_count",SET_INT_FIELD);
	setPlayerStrfield(row,&P->point,"point",SET_INT_FIELD);
	P->playing_info = NULL;
	P->player_pos = -1;
	P->game_type = -1;
	P->game_room = -1;
	plylink = (player_gamehash_link *)xmalloc(sizeof(player_gamehash_link));
	plylink->pindex = fd;
	plylink->key = xstrdup(P->play_name);
	gamehash_join(login_name,(gamehash_link *)plylink);
	printf(">>>>>>>>>>>>P->score=%.2f\n",P->score);
	return P;
}


void setLoginContent(unsigned char loginStatus,const player *P,order_request_response *RS) {
	if(loginStatus != LOGIN_CHECKFLAG_SECUSESS) {
		unsigned char lcontent[3];
		int offset = 1;
		lcontent[0] = loginStatus; 
		gameCommIntCopy(lcontent,setInt8bitTo1(0),2,&offset, NULL);	
		setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],lcontent,3,NULL);
	} 
	/* TODO */
	else {
		int maxLen = 512;
		int offset = 0;
		int numbLen = 0;
		unsigned char *lcontent = xmalloc(maxLen);	
		unsigned char numstr[MAX_NUMBER_TO_STRLEN];
		csequence[RS->fd] = 0;
		lcontent[offset++] = loginStatus;
		offset += 2;
		lcontent = gameCommStringCopy(lcontent,"user_code=",&offset,&maxLen);
		lcontent = gameCommStringCopy(lcontent,P->user_code,&offset,&maxLen);

		lcontent = gameCommStringCopy(lcontent,"&open_date=",&offset,&maxLen);
		lcontent = gameCommStringCopy(lcontent,P->open_date,&offset,&maxLen);

		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",P->state);
		lcontent = gameCommStringCopy(lcontent,"&state=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);

		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%.2f",P->score);
		lcontent = gameCommStringCopy(lcontent,"&score=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);

		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",P->score_limited);
		lcontent = gameCommStringCopy(lcontent,"&score_limited=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);

		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",P->win_count);
		lcontent = gameCommStringCopy(lcontent,"&win_count=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);

		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",P->failed_count);
		lcontent = gameCommStringCopy(lcontent,"&failed_count=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);
		
		numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",P->abort_count);
		lcontent = gameCommStringCopy(lcontent,"&abort_count=",&offset,&maxLen);
		lcontent = gameCommBufCopy(lcontent,numstr,numbLen,&offset,&maxLen);

		numbLen = 1;
		lcontent = gameCommIntCopy(lcontent,setInt8bitTo1(offset-3),2,&numbLen, &maxLen);
		setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],lcontent,offset,xfree);
	}
	RS->waitDbCallBack = 0;
}
#define RES_LOGIN_ODER 0x81

/* 用户登录 */
int userLogin(order_request_response *RS) {
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	int userlen = RS->client_rq[orderContentBegin] GAME_R_OP;
	int pwdlen = RS->client_rq[orderContentBegin+userlen+1] GAME_R_OP;
	int oderlen = getOrderLen(RS->client_rq,RS->currentOder);
	if(oderlen == (userlen+pwdlen+2)) {
		int resMaxLen = 256;
		int offset = 0;
		unsigned char *sql = NULL;
		player_gamehash_link *plylink;
		char username[MAX_USER_LEN];
		char password[MAX_PWD_LEN];	
		memcpy(username,RS->client_rq+orderContentBegin+1,userlen);
		username[userlen] = '\0';
		memcpy(password,RS->client_rq+orderContentBegin+userlen+1+1,pwdlen);
		password[pwdlen] = '\0';
		plylink = (player_gamehash_link *)gamehash_lookup(login_name,username);
		if(plylink) {
			setLoginContent(LOGIN_CHECKFLAG_HASLOGIN,NULL,RS);
		}
		else {
			if(sql_con[0] && 0)	{
				userlen = mysql_real_escape_string(sql_con[0],username,RS->client_rq+orderContentBegin+1,userlen);
				pwdlen =  mysql_real_escape_string(sql_con[0],password,RS->client_rq+orderContentBegin+userlen+1+1,pwdlen);
			}
			else {
				userlen = mysql_escape_string(username,RS->client_rq+orderContentBegin+1,userlen);
				pwdlen =  mysql_escape_string(password,RS->client_rq+orderContentBegin+userlen+1+1,pwdlen);
			}
			sql = xmalloc(resMaxLen);
			bzero(sql,resMaxLen);
			sql = gameCommStringCopy(sql,"select * from zsgame.t_player where play_name='",&offset,&resMaxLen);
			sql = gameCommBufCopy(sql,RS->client_rq+orderContentBegin+1,userlen,&offset,&resMaxLen);
			sql = gameCommStringCopy(sql,"' and password='",&offset,&resMaxLen);
			sql = gameCommBufCopy(sql,RS->client_rq+orderContentBegin+userlen+1+1,pwdlen,&offset,&resMaxLen);
			sql = gameCommStringCopy(sql,"'",&offset,&resMaxLen);
			sql[offset] = '\0';
			db_callback *callback=(db_callback*) xmalloc(sizeof(db_callback));
			bzero(callback,sizeof(db_callback));
			callback->handler = &loginCompleteHander;
			callback->RS = RS;
			addCallbackDBTask(SELECT,sql,offset,callback);
			debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
			return 0;
		}
	}
	else {
		commErrorHander(RS);
	}
	return 0;
}
int endGameDbHander(db_tasks *task) {
	return 0;
}
int loginCompleteHander(db_tasks *task) {
	int i;
	int count = task->row_count;
	order_request_response *callback = task->callback->RS;
	order_request_response *RS = task->callback->RS;



	int fd = callback->fd;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	assert(task);
	if(task->exec_status == 0) {		
		if(count > 0) {
			int fnum = mysql_num_fields(task->res_set);
			MYSQL_ROW row;
			login_gamehash_link *loglink;
			if(login_fieldt == NULL) {			
				MYSQL_FIELD *fields = mysql_fetch_fields(task->res_set);
				login_fieldt = gamehash_create((HASHCMP *) strcmp, fnum*2, gamehash_string);
				for(i = 0; i < fnum; i++) {
					loglink = xmalloc(sizeof(login_gamehash_link));
					loglink->key = xstrdup(fields[i].name);
					loglink->index = i;
					gamehash_join(login_fieldt,(gamehash_link *)loglink);
				}
			}
			
			row = mysql_fetch_row(task->res_set);
			preOrder[fd] = LOGIN_R_I;
			setLoginContent(LOGIN_CHECKFLAG_SECUSESS,setPlayer(row,fd),RS);
		}
		else {
			setLoginContent(LOGIN_CHECKFLAG_U_P_ERROR,NULL,RS);
		}
	}
	else setLoginContent(LOGIN_CHECKFLAG_SYSTEM_ERROR,NULL,RS);
	return 0;
}
void freeRSAll(order_request_response *RS) {
	freeRS(RS);
	xfree(RS);
}
void freeRS(order_request_response *RS) {
	if(RS == NULL)
		return;
	if(RS->client_rq != NULL) {
		xfree(RS->client_rq);
		RS->client_rq = NULL;
	}
	if(RS->response !=NULL) {
		xfree(RS->response);
		RS->response = NULL;
	}
	if(RS->orders !=NULL) {
		xfree(RS->orders);
		RS->orders = NULL;
	}
	RS->canUse = 1;
	/*xfree(RS);*/
}
int commonRequestHander(order_request_response *RS) {
	int i = 0;
	int fd = RS->fd;
	int orderc = RS->oderCount;
	if(RS->currentOder <= orderc && RS->currentOder > 0) {
		if(memcmp(getOderFlag(RS->client_rq,RS->currentOder),pFflag+GameFlagLen,OderFlagLen) != 0)
			commErrorHander(RS);
		else {
			unsigned char order = RS->orders[RS->currentOder-1];
			if(RS->type == PRO_TYPE_RESPONSE) {
				if(order != LOGIN_R_I && order != KEEP_ALIVE_I && !checkLogin(fd)) {
					notLoginHander(RS);
				}
				else {
					if(order != KEEP_ALIVE_I) {
						if(gamePlayers[fd].game_status > LOGIN_STATUS_NOTLOGIN)
							debug(82, 2) ("From User[%s] Order[0x%X]: %s\n",gamePlayers[fd].play_name,order,getGameOrderMessage(order));
						else
						debug(82, 2) ("From FD[%d] Order[0x%X]: %s\n",fd,order,getGameOrderMessage(order));
					}
					GAME_CPARSE_HAND *hdl = clientReqMthod[order];
					hdl(RS);
				}
			}
		}
	}
	else {
		unsigned char *responseBase64;
		activeNote(RS);
		responseBase64 = xmalloc(RS->resLen * 4 /3 + 16);
		reSetBodylen(RS->response,getRealBodylen(RS->response));
		if(RS->orders[0] != KEEP_ALIVE_I) {
			debug(82, 2)("%d B to FD %d:\n",RS->resLen,fd);		
			for(i = 0; i< RS->resLen; i++) {
				debug(82, 2)("%d,",RS->response[i]);
			}
			debug(82, 2)("\n");
		}
		RS->resLen = base64_encode1(responseBase64,RS->response,RS->resLen);
		RS->data = responseBase64;    
		responseBase64[(RS->resLen)++] = '\0';
		if(gamePlayers[fd].send_status == 0 ){						
		orderList * temp = xmalloc(sizeof(orderList));
		orderList ** header;

		temp->data = RS;
		temp->next = NULL;
		
		for (header = &gamePlayers[fd].orders; *header; header = &(*header)->next) ;
		*header = temp;			
		gamePlayers[fd].send_status = 1;
		if(gamePlayers[fd].game_net_status != GAME_NET_OFFLINE) {
			comm_write(fd, responseBase64, RS->resLen, gameServerWriteComplete, NULL, xfree);
		}
		else {
			xfree(responseBase64);
		}
		
	}else {
		// 加RS 到 orders中.
		orderList * temp = xmalloc(sizeof(orderList));
		orderList ** header;	
		temp->data = RS;
		temp->next = NULL;
		
		for (header = &gamePlayers[fd].orders; *header; header = &(*header)->next) ;
		*header = temp;	
		gamePlayers[fd].send_status =1;
		
	}
		//freeRS(RS);
	}
	return 0;
}

/******************************/
// added by wangpch 
static void
gameServerWriteComplete(int fd, char *bufnotused, size_t size, int errflag, void *data)
{
	  orderList * temp = NULL;
	  order_request_response * RS = NULL;
    //commSetTimeout(fd, 120, , NULL)
    // 
    //gamePlayers[fd]
    // 如果order 列表中
    gamePlayers[fd].send_status = 0;
      temp = gamePlayers[fd].orders;
    if( temp ){
		order_request_response *RS;
		RSCLEAN_HANDER *freeHdl;
    	gamePlayers[fd].orders = temp->next;
		RS = (order_request_response *)temp->data;
		freeHdl = RS->hd_free;
		RS->canUse = 1;
		if(freeHdl)
			freeHdl(RS);    	
    	xfree(temp);
    }
    
    if(gamePlayers[fd].orders ){
    	RS = (order_request_response *)gamePlayers[fd].orders->data;
    	gamePlayers[fd].send_status = 1;
    	comm_write(RS->fd, RS->data, RS->resLen, gameServerWriteComplete, NULL, xfree);
    }
    
    
}

int pauseGameReq(order_request_response *RS) {
	char unsigned content[3];
	int isSend = 0;
	int i;
	int fd = RS->fd;
	int status = gamePlayers[fd].game_status;
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	content[0] = 2;
	
	if(status == LOGIN_STATUS_GAMEING && gamePlayers[fd].isPlayPause == 0) {
		isSend = 1;
		content[0] = 1;
		gamePlayers[fd].isPlayPause = 1;
	}
	else if((status == LOGIN_STATUS_CALLSCORE || status == LOGIN_STATUS_STARTSCORE)&& gamePlayers[fd].isCallValuePause == 0) {
		isSend = 1;
		content[0] = 1;
		gamePlayers[fd].isCallValuePause = 1;
	}	
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
	for(i = 0; i < 4; i++) {
		content[i] = RS->client_rq[orderContentBegin+i];
	}
	if(isSend) {
			for(i = 0; i < 3; i++) {
				unsigned char *orders = xmalloc(1);
				int fdc = gamePlayers[fd].playing_info->fds[i];
				order_request_response *ors;
				orders[0] = GAME_PAUSE_I;
				ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
				setCommonOrderFields(ors,GAME_PAUSE_I,content,4,NULL);
			}
		}
	return 0;
}
int pauseGameRes(order_request_response *RS) {
	freeRS(RS);
	return 0;
}
int keepalive(order_request_response *RS) {
	char unsigned content[1];
	if(!isOffline(&gamePlayers[RS->fd])) {
		commSetTimeout(RS->fd, GAME_NET_TIMEOUT, requestTimeout, RS);
	}
	content[0] = 1;
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
	return 0;
}
int checkLogin(int fd) {
	if(gamePlayers[fd].play_name)
		return 1;
	return 0;
}
int notLoginHander(order_request_response *RS) {
	char unsigned content[2];
	content[0] = 'N';
	content[1] = 'L';
	setCommonOrderFields(RS,NOT_LOGIN_I,content,2,NULL);
	return 0;
}
static char checkGame(unsigned char type) {
	if(type > ERROR_SEQ_INV || type <=0 || !game_type_run[type]) {
		return 0x02;
	} 
	return 0x01;
}
int selectGame(order_request_response *RS) {
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	unsigned char type = RS->client_rq[orderContentBegin];
	unsigned char content[1];
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[RS->fd] != LOGIN_R_I) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}	
	gamePlayers[RS->fd].game_type = type;
	content[0] = checkGame(type);
	if(content[0] == 0x01) {
		ppreOrder[RS->fd] = preOrder[RS->fd];
		preOrder[RS->fd] = SELECT_GAME_R_I;
	}
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
	return 0;
}
order_request_response * setServerRequest(unsigned char *orders,int orderc,int fd,int type) {
	int resMaxLen = 512;
	int resLen = 0;
	unsigned char *response;
	order_request_response *order_rs;
	csequence[fd]++;
	if(csequence[fd] >= maxcSequence)
		csequence[fd] = 1;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	response = xmalloc(resMaxLen);
	response = setGameFlag(response,&resLen,&resMaxLen);
	response = setVesion(response,GM_VERSION,&resLen,&resMaxLen);
	response = setOrderType(response,type,&resLen,&resMaxLen);
	response = setSequence(response,csequence[fd],&resLen,&resMaxLen);
	response = setOrderCount(response,orderc,&resLen,&resMaxLen);
	response = setBodylen(response,0,&resLen,&resMaxLen);
	/*order_rs = xmalloc(sizeof(order_request_response));*/
	order_rs = addGameOrderTaskNotSyn();
	order_rs->fd = fd;
	order_rs->client_rq = NULL;
	order_rs->reqLen = 0;
	order_rs->response = response;
	order_rs->resLen = resLen;
	order_rs->resMaxLen = resMaxLen;
	order_rs->currentOder = 1;
	order_rs->oderCount = orderc;
	order_rs->type = type;
	order_rs->orders = orders;
	return order_rs;
	/*
	commonRequestHander(order_rs);
	*/
}

void setCommonOrderFields(order_request_response *RS,unsigned char order,unsigned char *orderContent,int orderLen,FREE * free_func) {
	RS->response = setOderFlag(RS->response,RS->currentOder,&RS->resLen,&RS->resMaxLen);
	RS->response = setOrder(RS->response,order,RS->currentOder,&RS->resLen,&RS->resMaxLen);
	RS->response = setOrderLen(RS->response,orderLen,RS->currentOder,&RS->resLen,&RS->resMaxLen);
	RS->response = setOrderContent(RS->response,orderContent,orderLen,RS->currentOder,&RS->resLen,&RS->resMaxLen);
	(RS->currentOder)++;
	if(free_func != NULL)
		free_func(orderContent);
	if(RS->type != PRO_TYPE_REQUEST && !RS->waitDbCallBack)
		commonRequestHander(RS);
	else {
		if(gamePlayers[RS->fd].game_status > LOGIN_STATUS_NOTLOGIN)
			debug(82, 2) ("User[%s] Order[0x%X]: %s\n",gamePlayers[RS->fd].play_name,order,getGameOrderMessage(order));
		else
			debug(82, 2) ("FD[%d] Order[0x%X]: %s\n",RS->fd,order,getGameOrderMessage(order));
	}
}
int commErrorHander(order_request_response *RS) {
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	unsigned char content[3];
	content[0] = 0x80;
	content[1] = 0x80;
	setCommonOrderFields(RS,0xFF,content,3,NULL);	
	return 0;
}

int selectRoom(order_request_response *RS) {
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	unsigned char content[1];
	int room = RS->client_rq[orderContentBegin];
	int isRseq = 1;
	isRseq = preOrder[RS->fd] == SELECT_GAME_R_I;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(!isRseq) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	if(room <= oppose_landocrat.room_counts && room > 0) {
		gamePlayers[RS->fd].game_room = room;
		content[0] = 0x01;
		ppreOrder[RS->fd] = preOrder[RS->fd];
		preOrder[RS->fd] = RS->orders[RS->currentOder-1];
		gamePlayers[RS->fd].game_status = LOGIN_STATUS_SELECTROOM;
	}
	else content[0] = 0x02;
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);	
	return 0;
}

int selectDesk(order_request_response *RS) {
	int i;
	/*int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);*/
	unsigned char content[4*2];
	int res_desk = 0xFFFFFFFD;
	int fd = RS->fd;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != SELECT_ROOM_R_I) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	ppreOrder[fd] = preOrder[fd];
	preOrder[fd] = RS->orders[RS->currentOder-1];
	gamePlayers[fd].game_status = LOGIN_STATUS_WAITDESK;

	gameCommIntCopy(content,res_desk,4,NULL, NULL);
	content[4] = 0xFF;
	
	
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,5,NULL);
	if(addPlayer(fd) == 3){
		oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
		playing_info->room = gamePlayers[fd].game_room;
		res_desk = addGame(playing_info);
		for(i = 0; i < 3; i++) {
			unsigned char *orders = xmalloc(1);
			int fdc = gamePlayers[fd].playing_info->fds[i];
			orders[0] = STARTGAME_R;
			order_request_response *ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			gameCommIntCopy(content,setInt8bitTo1(res_desk),4,NULL, NULL);
			content[4] = i+1;
			playing_info->players[i]->player_pos = i;
			setCommonOrderFields(ors,STARTGAME_R,content,5,NULL);
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = STARTGAME_R;
			gamePlayers[fdc].game_status = LOGIN_STATUS_SELECTDESK;
		}
	}
	return 0;
}
int sendCardReq(order_request_response *RS) {
	sendCardReqbyfd(RS,RS->fd);
	return 0;
}
int sendCardReqbyfd(order_request_response *RS,int fd) {
	int i;
	int isSend = 1;
	oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != STARTGAME_R) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[fd] = STARTGAME_S;
	gamePlayers[fd].game_status = LOGIN_STATUS_WAITCARD;
	for(i = 0; i < 3; i++) {
		if(!isOffline(playing_info->players[i]) && playing_info->players[i]->game_status != LOGIN_STATUS_WAITCARD) {
			isSend = 0;
			break;
		}
	}
	if(isSend) {
		oppose_landocrat_deal(playing_info);
		for(i = 0; i < 3; i++) {
			unsigned char *orders = xmalloc(1);
			unsigned char content[21];
			int fdc = playing_info->fds[i];
			orders[0] = SENDCARD_R;
			order_request_response *ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			content[0] = playing_info->next_player +1;
			content[1] = i + 1;
			content[2] = playing_info->card_landocrat_flag;
			content[3] = 17;
			memcpy(content+4,playing_info->player_cards[i],17);
			setCommonOrderFields(ors,SENDCARD_R,content,21,NULL);
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = SENDCARD_R;
			playing_info->players[i]->game_status = LOGIN_STATUS_GETCARD;
		}
	}
	freeRS(RS);
	return 0;
}

int callScoreReq(order_request_response *RS) {
	callScoreReqbyfd(RS,RS->fd);
	return 0;
}
int callScoreReqbyfd(order_request_response *RS,int fd) {
	int i;
	int isSend = 1;
	oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != SENDCARD_R) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	gamePlayers[fd].game_status = LOGIN_STATUS_CALLSCORE;
	for(i = 0; i < 3; i++) {
		if(!isOffline(playing_info->players[i]) && playing_info->players[i]->game_status != LOGIN_STATUS_CALLSCORE) {
			isSend = 0;
			break;
		}
	}
	if(isSend) {
		for(i = 0; i < 3; i++) {
			unsigned char *orders = xmalloc(1);
			unsigned char content[3];
			orders[0] = CALLSCORE_R;
			int fdc = gamePlayers[fd].playing_info->fds[i];
			order_request_response *ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			content[2] = playing_info->next_player + 1;
			content[1] = 0xFF;
			content[0] = 0xFF;
			if(!isOffline(&gamePlayers[fdc]))
					gamePlayers[fdc].nextPrifixOrder = CALLSCORE_S;
			setCommonOrderFields(ors,CALLSCORE_R,content,3,NULL);
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = CALLSCORE_R;
		}
	}
	freeRS(RS);
	return 0;
}
int callScoreStatus(order_request_response *RS) {
	callScoreStatusbyfd(RS,RS->fd);
	return 0;
}
int callScoreStatusbyfd(order_request_response *RS,int fd) {
	int i;
	int isSend = 1;
	oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != CALLSCORE_R || gamePlayers[fd].game_status != LOGIN_STATUS_CALLSCORE) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[fd] = CALLSCORE_S;
	gamePlayers[fd].game_status = LOGIN_STATUS_STARTSCORE;
	for(i = 0; i < 3; i++) {
		if(!isOffline(playing_info->players[i]) && playing_info->players[i]->game_status != LOGIN_STATUS_STARTSCORE) {
			isSend = 0;
			break;
		}
	}
	if(isSend) {
		for(i = 0; i < 3; i++) {
			int fdc = gamePlayers[fd].playing_info->fds[i];
			gamePlayers[fdc].game_status = LOGIN_STATUS_STARTSCORE;
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = CALLSCORE_S;
		}
	}
	freeRS(RS);
	return 0;
}
int checkScore(int score,int room,int pre_score) {
	int score_counts = oppose_landocrat.rooms[room].score_counts;
	if(score == 0xFF || pre_score == 0xFF)
		return 1;
	if(pre_score >= score)
		return 0;
	if(score_counts >= score && score > 0)
		return 1;
	return 0;
}
int endGameReq(oppose_landocrat_playing_info *playing_info) {
	int i;
	float score = 0;
	int isSomeOffline = 0;
	unsigned char content[128];
	int numbLen = 0;
	char numstr[MAX_NUMBER_TO_STRLEN];
	float thisgame_rate = game_rate;
	if(playing_info->landocrat == -1) {
		int offset = 0;
		for(i = 0; i < 3; i++) {
			order_request_response *ors;
			unsigned char *orders = xmalloc(1);
			int fdc = playing_info->fds[i];
			content[offset++] = i + 1;
			content[offset++] = 0xFF;
			gameCommIntCopy(content+offset,setInt8bitTo1((int)score),4,NULL, NULL);
			offset += 4;
			content[offset++] = playing_info->player_remain_count[i] | 0x80;
			if(playing_info->player_remain_count[i] > 0) {
				memcpy(content+offset,playing_info->player_remain_cards[i],playing_info->player_remain_count[i]);
				offset += playing_info->player_remain_count[i];
			}
			ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			setCommonOrderFields(ors,ENDGAME_R,content,offset,NULL);
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = ENDGAME_R;
			gamePlayers[fdc].game_status = LOGIN_STATUS_ENDGAME;
			gamePlayers[fdc].sendCardTimes = 0;
		}
	}
	else {
			int offset = 0;
			int offset2 = 0;
			int isSpring = 1;
			int maxLen = 512;
			char *sql = xmalloc(maxLen);
			float scores[3];
			float winrate = 0;
			int isWins[3];
			char abortV[3]={'0','0','0'};
			order_request_response *ors[3];
			for(i = 0; i < 3; i++) {
				int fdc = playing_info->fds[i];					
				isWins[i] = 0;
				isWins[i] = (playing_info->player_remain_count[playing_info->landocrat] > 0) &&  (i != playing_info->landocrat);
				isWins[i] = isWins[i] || (playing_info->player_remain_count[i] == 0);			
				if(isSpring && playing_info->landocrat != i && gamePlayers[fdc].sendCardTimes != 0) {
					isSpring = 0;
				}
				content[offset2++] = i + 1;
				if(isWins[i]) {
					content[offset2++] = 0x01;
				}
				else
					content[offset2++] = 0x02;			
			}
			if(gamePlayers[playing_info->landocrat].sendCardTimes == 1)
				isSpring = 1;
			if(isSpring)
				playing_info->bomb_counts++;
			
			sql = gameCommStringCopy(sql,"call zsgame_lairdly_procedure(",&offset,&maxLen);
			
			/*gameId       -  游戏Id.*/
			numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",oppose_landocrat.game_id);
			sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
			sql = gameCommStringCopy(sql,",",&offset,&maxLen);

			/*homeId       -  房间等级ID.*/
			numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",playing_info->room);
			sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
			sql = gameCommStringCopy(sql,",",&offset,&maxLen);

			/*activeId     -  活动时期当前活动的ID,如没有测为0.*/
			numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",gactive.active_id);
			sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
			sql = gameCommStringCopy(sql,",",&offset,&maxLen);

			/*bombV        -  当前这局玩家所打出来的炸*弹数.*/
			numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%d",playing_info->bomb_counts);
			sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
			sql = gameCommStringCopy(sql,",",&offset,&maxLen);
			
			/*abortV       -  三个玩家是否逃跑标记*/			
			sql = gameCommBufCopy(sql,abortV,3,&offset,&maxLen);
			sql = gameCommStringCopy(sql,",",&offset,&maxLen);
			 
			/*playerId_a(b,c)   -  玩家的ID */
			for(i = 0; i < 3; i++) {
				winrate = 0;
				score = oppose_landocrat.rooms[playing_info->room].score[playing_info->call_score];	
				if(playing_info->bomb_counts > 0)
					score = score * pow(2,playing_info->bomb_counts);
				if(i == playing_info->landocrat)
					score *= 2;
				scores[i] = score;
				numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%ld",gamePlayers[playing_info->fds[i]].play_id);
				sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
				sql = gameCommStringCopy(sql,",",&offset,&maxLen);
				if(!isWins[i]) {
					sql = gameCommStringCopy(sql,"-",&offset,&maxLen);
				}
				numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%.2f",scores[i]);
				sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
				sql = gameCommStringCopy(sql,",",&offset,&maxLen);
				
				if(isWins[i]) {
					score = scores[i] * (1 - thisgame_rate);
					winrate = scores[i] * thisgame_rate;
				}
				score *= 100;
				score = ROUND(score);
				gameCommIntCopy(content+offset2,setInt8bitTo1((int)score),4,NULL, NULL);
				offset2 += 4;
				content[offset2++] = playing_info->player_remain_count[i] | 0x80;
				if(playing_info->player_remain_count[i] > 0) {
					memcpy(content+offset2,playing_info->player_remain_cards[i],playing_info->player_remain_count[i]);
					offset2 += playing_info->player_remain_count[i];
				}
				numbLen = snprintf(numstr,MAX_NUMBER_TO_STRLEN-1,"%.2f",score);
				sql = gameCommBufCopy(sql,numstr,numbLen,&offset,&maxLen);
				sql = gameCommStringCopy(sql,",",&offset,&maxLen);
				
			}
			sql = gameCommStringCopy(sql,
			"@outPlayerScore_a,@outPlayerPrize_a,@outPlayerScore_b,@outPlayerPrize_b,\
			@outPlayerScore_c,@outPlayerPrize_c,@outCumulateScore,@outResult",&offset,&maxLen);						
			for(i = 2; i >= 0; i--) {
				int fdc = playing_info->fds[i];
				unsigned char *orders = xmalloc(1);
				orders[0] = ENDGAME_R;
				ors[i] = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
				ors[i]->waitDbCallBack = 1;
				setCommonOrderFields(ors[i],ENDGAME_R,content,offset2,NULL);
				if(i < 2)
					ors[i]->next = ors[i+1];
			}
			db_callback *callback=(db_callback*) xmalloc(sizeof(db_callback));
			bzero(callback,sizeof(db_callback));
			callback->handler = &endGameDbHander;
			callback->RS = ors[0];
			addCallbackDBTask(UPDATE,sql,offset,callback);
		}
		
	for(i = 0; i < 3; i++) {
			int fdc = playing_info->fds[i];
			gamePlayers[fdc].sendCardTimes = 0;
			if(gamePlayers[fdc].game_net_status == GAME_NET_OFFLINE) {
				isSomeOffline = 1;
				resetUser(&gamePlayers[fdc]);
				gamePlayers[fdc].game_net_status = GAME_NET_OFFLINE;
			}
	}
	if(isSomeOffline) {
		int isclean = 0;
		for(i = 0; i < 3; i++) {
			int fdc = playing_info->fds[i];
			gamePlayers[fdc].playing_info = NULL;
			if(!isOffline(playing_info->players[i]))
				continue;
			if(playing_info->players[i]->game_status == LOGIN_STATUS_FREE)
				isclean = 1;
			else playing_info->players[i]->game_status = LOGIN_STATUS_FREE;
		}
		if(!isclean) {
			
		removeGame(playing_info);
	}
	}
	resetPlayInfo(playing_info);
	return 0;
}
int callScoreValue(order_request_response *RS) {
	int i;
	int isSend = 1;
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	unsigned char callValue = RS->client_rq[orderContentBegin];
	unsigned char content[1];
	int fd = RS->fd;
	oppose_landocrat_playing_info *playing_info = gamePlayers[RS->fd].playing_info;
	int score_count = oppose_landocrat.rooms[playing_info->room].score_counts;
	char pos = gamePlayers[RS->fd].player_pos;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[RS->fd] != CALLSCORE_S || gamePlayers[RS->fd].game_status != LOGIN_STATUS_STARTSCORE) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[fd] = SCORE_VALUE_S;
	content[0] = 0x01;
	if(pos != playing_info->next_player || !checkScore(callValue,playing_info->room,playing_info->call_score+1)) {
		content[0] = 0x02;
		setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
		return 0;
	}
	(playing_info->pre_player)++;
	playing_info->next_player = (playing_info->next_player+1)%3;
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
	if(callValue != 0xFF) {
		playing_info->landocrat = pos;
		playing_info->call_score = callValue - 1;
	}
	/* callValue  == oppose_landocrat.rooms[playing_info->room].score[score_count-1] */
	if(callValue  == score_count || playing_info->pre_player >= 3) {
		unsigned char content[10];
		playing_info->pre_player = 0;
		playing_info->next_player = playing_info->landocrat;
		if(playing_info->landocrat == -1) {
			content[0] = 0xFF;			
		}
		else {
			content[0] = playing_info->landocrat + 1;
		}
		content[2] = playing_info->cards_deal_for_landocrat[0];
		content[3] = playing_info->cards_deal_for_landocrat[1];
		content[4] = playing_info->cards_deal_for_landocrat[2];
		for(i = 0; i < 3; i++) {
			int fdc = gamePlayers[fd].playing_info->fds[i];
			unsigned char *orders = xmalloc(1);
			order_request_response *ors;
			if(playing_info->landocrat == -1) {
				ppreOrder[fdc] = preOrder[fdc];
				preOrder[fdc] = SCORE_VALUE_R;
				gamePlayers[fdc].game_status = LOGIN_STATUS_ENDGAME;
			}
			else {
				ppreOrder[fdc] = preOrder[fdc];
				preOrder[fdc] = SCORE_VALUE_R;
				gamePlayers[fdc].game_status = LOGIN_STATUS_SETLAND;
			}
			orders[0] = LANDOCRAT_CREATE_R;
			content[1] = playing_info->call_score + 1;
			ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			setCommonOrderFields(ors,LANDOCRAT_CREATE_R,content,5,NULL);
		}
		playing_info->pre_player = -1;
		if(playing_info->landocrat == -1)
			endGameReq(playing_info);
		else oppose_landocrat_deal_remain(playing_info);
	}
	else {
		if(isSend) {
			for(i = 0; i < 3; i++) {
				unsigned char *orders = xmalloc(1);
				unsigned char content[3];
				int fdc = gamePlayers[fd].playing_info->fds[i];
				order_request_response *ors;
				orders[0] = CALLSCORE_R;
				ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
				content[2] = playing_info->next_player +1;
				content[1] = callValue;
				content[0] = pos + 1;
				if(!isOffline(&gamePlayers[fdc]))
					gamePlayers[fdc].nextPrifixOrder = CALLSCORE_S;
				setCommonOrderFields(ors,CALLSCORE_R,content,3,NULL);
				ppreOrder[fdc] = preOrder[fdc];
				preOrder[fdc] = CALLSCORE_R;
				gamePlayers[fdc].game_status = LOGIN_STATUS_CALLSCORE;
			}
		}
	}
	return 0;
}
int createLandocratRes(order_request_response *RS) {
	int i;
	int isSend = 1;
	int fd = RS->fd;
	oppose_landocrat_playing_info *playing_info = gamePlayers[RS->fd].playing_info;
	if(playing_info == NULL) {
		freeRS(RS);
		return 0;
	}
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] == SCORE_VALUE_R && gamePlayers[fd].game_status == LOGIN_STATUS_ENDGAME) {
		ppreOrder[fd] = preOrder[fd];
		preOrder[fd] = ENDGAME_S;
	}
	else if(preOrder[fd] == SCORE_VALUE_R && gamePlayers[fd].game_status == LOGIN_STATUS_SETLAND) {
		ppreOrder[fd] = preOrder[fd];
		preOrder[fd] = LANDOCRAT_CREATE_S;
	}
	for(i = 0; i < 3; i++) {
		int fdc = gamePlayers[fd].playing_info->fds[i];
		if(!isOffline(playing_info->players[i]) && (playing_info->players[i]->game_status != LOGIN_STATUS_SETLAND || preOrder[fdc] != LANDOCRAT_CREATE_S)) {
			isSend = 0;
			break;
		}
	}
	if(isSend) {
		for(i = 0; i < 3; i++) {
			int fdc = playing_info->fds[i];
			unsigned char content[3];
			order_request_response *ors;
			unsigned char *orders = xmalloc(1);
			orders[0] = PLAY_CARD_R_I;
			ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
			content[0] = playing_info->landocrat + 1;
			content[1] = 0x01;
			setCommonOrderFields(ors,PLAY_CARD_R_I,content,2,NULL);
			ppreOrder[fdc] = preOrder[fdc];
			preOrder[fdc] = PLAY_CARD_R_I;
			gamePlayers[fdc].game_status = LOGIN_STATUS_GAMEING;
			
		}
		playing_info->pre_player = -1;
		playing_info->next_player =  playing_info->landocrat;
	}
	freeRS(RS);
	return 0;
}
int nextGameRes(order_request_response *RS) {
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	freeRS(RS);
	return 0;
}
int nextGame(order_request_response *RS) {
	int fd = RS->fd;
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	int flag = 0;
	int room = -1;
	int *cWaitPlayer;
	order_request_response *ors;
	unsigned char *orders;
	unsigned char content[1];
	int pos;
	int i;
	oppose_landocrat_playing_info *playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if((preOrder[fd] != ENDGAME_S) && (preOrder[fd] != DESK_FIT_R_I)) {
		content[0] = 0x02;
		setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
		return 0;
	}
	flag = RS->client_rq[orderContentBegin];
	room = gamePlayers[fd].game_room;
	playing_info = gamePlayers[fd].playing_info;
	cWaitPlayer = &oppose_landocrat.rooms[room].currentWaitPlayer;
	pos = gamePlayers[fd].player_pos;
	if(flag == 1) {
		if(preOrder[fd] == DESK_FIT_R_I) {
			ppreOrder[fd] = preOrder[fd];
			preOrder[fd] = SELECT_GAME_R_I;
			if(*cWaitPlayer > 0)
				(*cWaitPlayer)--;
			if(pos != *cWaitPlayer) {
				playing_info->players[pos] = playing_info->players[*cWaitPlayer];
				playing_info->players[pos]->player_pos = pos;
				playing_info->fds[pos] = playing_info->fds[*cWaitPlayer];
			}
			gamePlayers[fd].player_pos = -1;
			content[0] = 0x01;
			setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
			return 0;
		}
		else if(preOrder[fd] == ENDGAME_S) {
			ppreOrder[fd] = preOrder[fd];
			preOrder[fd] = SELECT_GAME_R_I;	
			content[0] = 0x01;
			gamePlayers[fd].game_status = LOGIN_STATUS_SELECTROOM;
			setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
			if(pos >= 0 && playing_info) {
				removeGame(playing_info);
				for(i = 0; i < 3; i++) {
					int fdc = playing_info->fds[i];
					gamePlayers[fdc].playing_info = NULL;
					if(gamePlayers[fdc].game_status == LOGIN_STATUS_GAMECONTINUE) {
						orders = xmalloc(1);
						orders[0] = GAME_END_NEXT_R_I;
						ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
						gamePlayers[fdc].game_status = LOGIN_STATUS_SELECTROOM;
						content[0] = 0x02;
						ppreOrder[fdc] = preOrder[fd];
						preOrder[fdc] = SELECT_ROOM_R_I;
						setCommonOrderFields(ors,GAME_END_NEXT_R_I,content,1,NULL);
					}
					/*
					orders = xmalloc(1);
					orders[0] = GAME_END_NEXT_R_I;
					ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
					gamePlayers[fd].game_status = LOGIN_STATUS_SELECTROOM;
					content[0] = 0x01;
					ppreOrder[fdc] = preOrder[fd];
					preOrder[fdc] = SELECT_GAME_R_I;
					setCommonOrderFields(ors,GAME_END_NEXT_R_I,content,1,NULL);
					*/
				}				
			}			
		}
		return 0;
	}
	else if(preOrder[fd] != ENDGAME_S) {
			content[0] = 0x02;
			setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
			return 0;
	}
	else if(flag == 3) {
		int isSend = 0;
		int isclean = 0;
		int offlines = 0;
		if(preOrder[fd] == ENDGAME_S) {
			if(playing_info) {
			int deskSeq = gamePlayers[fd].playing_info->deskSeq;
			for(i = 0; i < 3; i++) {
				if(isOffline(playing_info->players[i])) {
					isSend = 1;
					if(pos != i)
						offlines++;
				}
				if(playing_info->players[i]->player_pos == -1)
					isSend = 1;
				if(playing_info->players[i]->game_status == LOGIN_STATUS_FREE)
					isclean = 1;
			}
			if(!isclean && offlines == 2) {
				gamePlayers[fd].game_status = LOGIN_STATUS_FREE;
				removeGame(playing_info);
			}
				if(isSend) {
					content[0] = 0x02;
					ppreOrder[fd] = preOrder[fd];
					preOrder[fd] = SELECT_ROOM_R_I;
					setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
					gamePlayers[fd].player_pos = -1;
					return 0;
				}
			isSend = 1;
			gamePlayers[fd].game_status = LOGIN_STATUS_GAMECONTINUE;
			for(i = 0; i < 3; i++) {
				int fdc = gamePlayers[fd].playing_info->fds[i];
				if(gamePlayers[fdc].game_status != LOGIN_STATUS_GAMECONTINUE) {
					isSend = 0;
					break;
				}
			}
			content[0] = 0x03;
			setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
			if(isSend) {
				int res_desk;
				for(i = 0; i < 3; i++) {
					int fdc = gamePlayers[fd].playing_info->fds[i];
					orders = xmalloc(1);
					orders[0] = GAME_END_NEXT_R_I;
					ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
					content[0] = 0x03;
					setCommonOrderFields(ors,GAME_END_NEXT_R_I,content,1,NULL);
					orders = xmalloc(1);
					orders[0] = STARTGAME_R;
					ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
					res_desk = deskSeq + 1;
					gameCommIntCopy(content,setInt8bitTo1(res_desk),4,NULL, NULL);
					content[4] = i+1;
					gamePlayers[fd].playing_info->players[i]->player_pos = i;
					setCommonOrderFields(ors,STARTGAME_R,content,5,NULL);
					ppreOrder[fdc] = preOrder[fdc];
					preOrder[fdc] = STARTGAME_R;
					gamePlayers[fdc].game_status = LOGIN_STATUS_SELECTDESK;
				}
			}
			}
			else {
				ppreOrder[fd] = preOrder[fd];
				preOrder[fd] = SELECT_ROOM_R_I;
				content[0] = 0x02;
				setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
			}
			return 0;
		}
	}
	/* 重新配桌 */
	else if(flag == 2) {
		content[0] = 0x01;
		ppreOrder[fd] = preOrder[fd];
		preOrder[fd] = SELECT_ROOM_R_I;	
		setCommonOrderFields(RS,GAME_END_NEXT_S_I,content,1,NULL);
		if(playing_info) {
			for(i = 0; i < 3; i++) {
				int fdc = playing_info->fds[i];
				gamePlayers[fdc].playing_info = NULL;
				if(gamePlayers[fd].player_pos == i || gamePlayers[fdc].game_status != LOGIN_STATUS_GAMECONTINUE)
					continue;
					playing_info->players[i]->player_pos = -1;
					orders = xmalloc(1);
					orders[0] = GAME_END_NEXT_R_I;
					ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
					gamePlayers[fdc].game_status = LOGIN_STATUS_SELECTROOM;
					content[0] = 0x02;
					ppreOrder[fdc] = preOrder[fdc];
					preOrder[fdc] = SELECT_ROOM_R_I;
					setCommonOrderFields(ors,GAME_END_NEXT_R_I,content,1,NULL);
			}
			removeGame(playing_info);
		}
		return 0;
	}
	freeRS(RS);
	return 0;
}
int endGameRes(order_request_response *RS) {
	int fd = RS->fd;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != ENDGAME_R || gamePlayers[fd].game_status != LOGIN_STATUS_ENDGAME) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	ppreOrder[fd] = preOrder[fd];
	preOrder[fd] = ENDGAME_S;
	freeRS(RS);
	return 0;
}

int errorOrderHander(order_request_response *RS,unsigned char flag) {
	char unsigned content[1];
	content[0] = flag;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	setCommonOrderFields(RS,ORDER_ERROR_I,content,1,NULL);	
	return 0;
}

/* 用户出牌 */
int playerDiscard(order_request_response *RS) {
	int fd = RS->fd;
	int i;
	oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
	int orderBegin = getOrderBegin(RS->client_rq,RS->currentOder);
	int orderContentBegin = getOrderContentBegin(orderBegin);
	int player_id = 0;
	unsigned char card_coount = 0;
	char unsigned content[512];
	unsigned char *cards;
	int  sendlen = 0;
	order_request_response *ors;
	unsigned char *orders;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(playing_info == NULL) {
		gamePlayers[fd].game_net_status = GAME_NET_OFFLINE;
		freeRS(RS);
		return 0;
	}
	if((!isOffline(&gamePlayers[RS->fd]) && preOrder[RS->fd] != PLAY_CARD_S_I) || gamePlayers[RS->fd].game_status != LOGIN_STATUS_GAMEING) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[fd] = PLAY_SEND_CARD_S_I;
	player_id = RS->client_rq[orderContentBegin] - 1;
	card_coount = RS->client_rq[orderContentBegin + 1];
	content[1] = card_coount;
	content[2] = RS->client_rq[orderContentBegin + 2];
	sendlen = 3;
	
	if(card_coount == 0xFF) {
		playing_info->next_player = (playing_info->next_player + 1) % oppose_landocrat.player_count;
		if(playing_info->pre_player == playing_info->next_player) {
			xfree(playing_info->pre_cards);
			playing_info->pre_player = -1;
		}
		card_coount = 0;
	}
	else {
		cards = xmalloc(sizeof(unsigned char)*card_coount);
		memcpy(cards,RS->client_rq+orderContentBegin+2,card_coount);
		memcpy(content + 2,RS->client_rq+orderContentBegin+2,card_coount+1);
		sendlen = 3 + card_coount;
		oppose_landocrat_sendcard(playing_info,cards,card_coount);
	}
	content[0] = 0x01;
	setCommonOrderFields(RS,responseMap[getOrder(RS->client_rq,RS->currentOder)],content,1,NULL);
	content[0] = player_id + 1;
	for(i = 0; i < 3; i++) {
		int fdc = gamePlayers[fd].playing_info->fds[i];
		orders = xmalloc(1);
		orders[0] = PLAY_SEND_CARD_R_I;
		ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
		ppreOrder[fdc] = preOrder[fdc];
		preOrder[fdc] = PLAY_SEND_CARD_R_I;
		gamePlayers[fdc].game_status = LOGIN_STATUS_GAMEING;
		if(!isOffline(&gamePlayers[fdc]))
			gamePlayers[fdc].nextPrifixOrder = PLAY_SEND_CARD_S_I;
		setCommonOrderFields(ors,PLAY_SEND_CARD_R_I,content,sendlen,NULL);
	}	
	return 0;
}
int playerDiscardRes(order_request_response *RS) {
	int i;
	/*
	int isSend = 1;
	*/
	int fd = RS->fd;
	oppose_landocrat_playing_info *playing_info = gamePlayers[RS->fd].playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != PLAY_CARD_R_I || gamePlayers[fd].game_status != LOGIN_STATUS_GAMEING) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[RS->fd] = PLAY_CARD_S_I;
	for(i = 0; i < 3; i++) {
		int fdc = gamePlayers[fd].playing_info->fds[i];
		if(preOrder[fdc] == PLAY_CARD_R_I && isOffline(playing_info->players[i])) {
			preOrder[fdc] = PLAY_CARD_S_I;
		}
	}
	freeRS(RS);
	return 0;
}
int playerSendDiscardRes(order_request_response *RS) {
	 playerSendDiscardResByfd(RS,RS->fd);
	 return 0;
}
int playerSendDiscardResByfd(order_request_response *RS,int fd) {
	int i;
	int isSend = 1;
	int isEnd = 0;
	oppose_landocrat_playing_info *playing_info = gamePlayers[fd].playing_info;
	debug(82, 2) ("%s(%s,%d)\n",__FUNCTION__,__FILE__,__LINE__);
	if(preOrder[fd] != PLAY_SEND_CARD_R_I || gamePlayers[fd].game_status != LOGIN_STATUS_GAMEING) {
		return errorOrderHander(RS,ERROR_SEQ_INV);
	}
	preOrder[fd] = PLAY_CARD_R_I;
	for(i = 0; i < 3; i++) {
		int fdc = gamePlayers[fd].playing_info->fds[i];
		if(!isOffline(playing_info->players[i]) && preOrder[fdc] != PLAY_CARD_R_I) {
			isSend = 0;
			break;
		}
	}
	if(isSend) {
		isEnd = isGameEnd(playing_info);
		if(!isEnd) {
			for(i = 0; i < 3; i++) {
				int fdc = playing_info->fds[i];
				unsigned char content[3];
				order_request_response *ors;
				unsigned char *orders = xmalloc(1);
				orders[0] = PLAY_CARD_R_I;
				ors = setServerRequest(orders,1,fdc,PRO_TYPE_REQUEST);
				content[0] = playing_info->next_player+1;
				if(playing_info->pre_player == -1)
					content[1] = 0x01;
				else 
					content[1] = 0x02;
				setCommonOrderFields(ors,PLAY_CARD_R_I,content,2,NULL);
				ppreOrder[fdc] = preOrder[fdc];
				preOrder[fdc] = PLAY_CARD_R_I;
				if(!isOffline(&gamePlayers[fdc]))
					gamePlayers[fdc].nextPrifixOrder = PLAY_CARD_S_I;
				gamePlayers[fdc].game_status = LOGIN_STATUS_GAMEING;
				
			}
			
		}
		else if(isEnd) {
				endGameReq(playing_info);
		}
	}
	if(RS != NULL)
	freeRS(RS);
	return 0;
}
static int isGameEnd(oppose_landocrat_playing_info *playing_info) {
	int i = 0;
	for(i = 0; i < 3; i++) {
		if(playing_info->player_remain_count[i] == 0)
			return i+1;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值