逻辑代码
接口
- 初始化角色信息
int32_t process_request_create_role(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);
int32_t process_response_create_role(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);
-
获取角色信息
int32_t process_request_get_role_info(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);
int32_t process_response_get_role_info(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);
int32_t process_notify_load_player_completed(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);
## 创建角色
1. 功能图
2. CPbMsgRequestCreateRole 结构体信息
//MSG_LOGIC_CREATE_ROLE 0x2020
message CPbMsgRequestCreateRole
{
required string m_szRoleName = 1;
required int32 m_iSirdarID = 2;
optional string m_szFBAccount = 3;
}
3. process_request_create_role接口实现
MSG_LOGIC_CREATE_ROLE
`int32_t CPlayer::process_request_create_role(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength)`
1. 未创建角色状态判断
2. 定时器判断
目的防止客户端还在创建过程中,没有返回给客户端,但是客户端又重新创建。
3. 填充玩家信息
4. 设置定时器
4. ProcessRequestCreateRole
SS_MSG_CREATE_ROLE
`int32_t CRoleProcessor::ProcessRequestCreateRole(int32_t iServerID, CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength)`
1. 向数据库写入角色信息
**注意:** 防止SQL非法字符注入,
```
char szEscapeName[max_role_name_length+max_role_name_length+1] = {0};
MakeRealEscapeString(szEscapeName, stReqBody.m_stInfo.m_stRoleBrief.m_szRoleName, strlen(stReqBody.m_stInfo.m_stRoleBrief.m_szRoleName));
```
2. 同步插入
```
int32_t CProcessor::FormatAndExecuteSQLSync(const char* szFormat,...)
{
va_list vaList;
va_start(vaList,szFormat);
int32_t iRet = FormatSQLString(szFormat, vaList);
va_end(vaList);
if (success != iRet)
{
return fail;
}
return m_stMySql.execute(&m_szSQL[0]);
}
```
因为在玩家创建角色过程中,我们关注玩家的返回结果。创建玩家账号,和玩家角色信息。创建角色信息失败,自然也要删除之前创建的账号信息。之后可以给新玩家,赠送道具什么的。整个创建角色过程中,判断是否有超时。
3. 响应
stRspBody.m_nResultID = result_id_success;
stRspBody.m_stInfo = stReqBody.m_stInfo;
return SendResponse(iServerID, rstMsgHead, stRspBody);
到此玩家创建角色结束。
5. process_response_create_role
SS_MSG_CREATE_ROLE
解除定时器
6. process_request_get_role_info
获取之前生成一个FlagKey
m_lRoleFlagKey = now_msec();
CRequstGetRoleInfo stReqBody;
stReqBody.m_lRoleFlagKey = m_lRoleFlagKey;
send_request_to_gamedb(&stReqBody, SS_MSG_GET_ROLE_INFO, rstMsgHead.m_iMessageSequence);
return success;
7.process_response_get_role_info
7. 获取gamedb中创建角色信息
SS_MSG_GET_ROLE_INFO
`irtual void Excuting(int32_t iServerID, CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength);`
接口实现
```
void CGetRoleCommand::Excuting(int32_t iServerID, CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength)
{
CRequestGetRoleInfo stMsgBody;
if (!stMsgBody.ParseFromArray(pszMsgBody, iBodyLength))
{
return;
}
CResponseGetRoleInfo stRspBody;
stRspBody.m_nResultID = GetProcessor()->LoadPlayerDataFromDB(GetPlayer());
if (result_id_success == stRspBody.m_nResultID)
{
stRspBody.m_stInfo = GetPlayer()->m_stRole;
}
SendResponse(iServerID, rstMsgHead, stRspBody);
if (result_id_success == stRspBody.m_nResultID)
{
GetProcessor()->SendPlaterDataToLogicServer(GetPlayer());
GetProcessor()->NotifyMessageToPlayer(GetPlayer()->get_group(), GetPalyer()->get_uin(), SS_MSG_NOTIFY_LOAD_PLAYER_COMPLETED, &stMsgBody);
}
}
- process_notify_load_player_completed
SS_MSG_NOTIFY_LOAD_PLAYER_COMPLETED
检查状态是否同步,检查和更新玩家数据信息。int32_t CPlayer::process_notify_load_player_completed(CMessageHead& rstMsgHead, char* pszMsgBody, int32_t iBodyLength) { CNotifyLoadPlayerComplete stMsgBody; if (!stMsgBody.ParseFromArray(pszMsgBody, iBodyLength)) { return fail; } if (stMsgBody.m_lRoleFlagKey != m_lRoleFlagKey) { return fail; } . . . google::protobuf::MessageLite* pstBody = NULL; return send_notify_to_client(pstBody, MSG_LOGIC_NOTIFY_LOAD_PLAYER_COMPLETED); }