cocos2dx游戏任务系统开发之服务端数据库

前端之家收集整理的这篇文章主要介绍了cocos2dx游戏任务系统开发之服务端数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前面我给小伙伴们讲了任务系统客户端,服务端写了前搬部分的消息接受处理,现在我在借大家讲解一下当我在服务端这边接受到了客户端的消息,我要知道是不是满足了条件,现在你就要用到数据库了,不管是这样还有其他的比如像道具系统里面道具的查询,出售,人物上线的属性什么都是存到数据库的,这里数据库虽然存有大量的信息,但是在数据库里面做的逻辑操作没多少数据库里面的操作主要是增删改查存这些,其他基本都是在数据库之外操作,各位牛逼的大神都不要喷我这是个人学习来的,当然我才毕业没一年做起来有很多不完善的,我将在以后慢慢的完善改进,只求不喷。

#ifndef __BRUCE_DBPROXY_QUEST_HANDLER_HEADER__
#define __BRUCE_DBPROXY_QUEST_HANDLER_HEADER__

#include "typedef.hpp"
#include <Network.hpp>
#include "DBProxyApp.hpp"
#include "MysqL.hpp"
#include <DBProxyServer/Quest.hpp>
#include "QuestCommon.hpp"

DBPROXY_NS_BEGIN
 class QuestHandler { public: QuestHandler(); ~QuestHandler(); void initialize(); private: // 正在进行时的任务; void createTable(); // 创建完成任务以后储的任务表;Achieve void createAchieveTable(); void registerMessageHandler(); void handleGameServerRequestQuestInfo(Message::Ptr &message); void handleGameServerRequestQuestInfoCallBack(Result& result,SessionID sid,GS2DS_QuestsqlReq gQuestReqsql); //任务向关卡数据库查询提交的条件是否匹配; void handleGameToMissonsqlQuestInfo(Message::Ptr &message); //取出来; void handleGameServerRequestMissonQuestInfoCallBack(Result& result,GS2DS_QuestGetMissonCountInfoReq questGetMissonCountInfoReq); //删除当前数据表中所在的任务信息; void handleGameDeleteQuestIDInof(Message::Ptr &message); //删除后的数据; void handleDeleteDBProxyToGameQuestIDinfo(Result& result,GS2DS_QuesDeletesqlInfoReq mQuesDeletesqlInfoReq); //将删除的所有任务数据保存到下来; void handleDeleteQuestSeave(Message::Ptr &message); //将删除的所有任务数据保存到下来; void handleDeleteQuestIDinfo(Result& result,GS2DS_QuestDeltAndSeaveInfoReq sQuestDeltAndSeaveInfoReq); }; APP_NS_END #endif // !__BRUCE_DBPROXY_QUEST_HANDLER_HEADER__
#include "QuestHandler.hpp"

DBPROXY_NS_BEGIN

QuestHandler::QuestHandler()
{
}


QuestHandler::~QuestHandler()
{
}

void QuestHandler::registerMessageHandler()
{
    //DEFREG_MESSAGE(ID_GS2DS_QuestReqsql,QuestHandler,handleGameServerRequestQuestInfo)
    //接收Game过来的查询消息;
    DEFREG_MESSAGE(ID_GS2DS_QuestsqlReq,handleGameServerRequestQuestInfo)

    //接收任务向关卡数据库查请求提交的条件是否满足;
    DEFREG_MESSAGE(ID_GS2DS_QuestGetMissonCountInfoReq,handleGameToMissonsqlQuestInfo)

    //删除数据库中当前任务数据表示中的所在任务的信息;
    DEFREG_MESSAGE(ID_GS2DS_QuesDeletesqlInfoReq,handleGameDeleteQuestIDInof)

    //保存当前删除的信息;
    DEFREG_MESSAGE(ID_GS2DS_QuestDeltAndSeaveInfoReq,handleDeleteQuestSeave)

}

void QuestHandler::initialize()
{
    createTable();
    createAchieveTable();
    registerMessageHandler();
}

//正在进行的任务表;
void QuestHandler::createTable()
{

    //正在进行的任务表;
    static Colume QuestTable[] =
    {
        { "id","bigint","0" },//任务的ID;
        { "type",//任务类型; 
        { "groups",//解锁等级;
        { " count",//次数;
    };

    g_MysqL.executeNoRs("CREATE TABLE `chr_quest_rec` (`char_id` int NOT NULL)");
    g_MysqL.addColume(QUEST_INFO_TABLE,QuestTable,sizeof(QuestTable) / sizeof(Colume));
}


void QuestHandler::handleGameServerRequestQuestInfo(Message::Ptr &message)
{


    GS2DS_QuestsqlReq questReqsql;
    message->unpack(questReqsql);

    char szsql[1024] = { 0 };

    //从数据库中找出该玩家ID中的所任务信息;
    sprintf_s(szsql,sizeof(szsql),"select id,type,groups,count from chr_quest_rec where char_id = %d AND id = %d AND type = %d AND count = %d ",questReqsql.userID,questReqsql.nQuestid,questReqsql.nQuestType,questReqsql.nQuestCount);
    Query* pQuery = g_connectionPool.createQuery(szsql);
    pQuery->setCallBack(makeCallBack(&QuestHandler::handleGameServerRequestQuestInfoCallBack,this,message->getSessionID(),questReqsql));

    g_connectionPool.pushQuery(pQuery);


}

void QuestHandler::handleGameServerRequestQuestInfoCallBack(Result& result,SessionID sid,GS2DS_QuestsqlReq cquestReqsql)
{
    if (result.errno())
    {
        BRUCE_LOG(BruCe::logger::Log_Error,"%d",cquestReqsql.userID);
        BRUCE_LOG(BruCe::logger::Log_Debug,"%s:%d handleGameServerRequestQuestInfoCallBack erroe: %s",__FILE__,__LINE__,result.error().c_str());

    } 

    DS2GS_QuestsqlAck qusetAcksql;

    Row* pRow = nullptr;
    for (Row* pRow = result.fetch(); pRow != NULL; pRow = result.fetch())
    {
        i32 sqlQuestInfo = 0;
        *pRow >> sqlQuestInfo;
        qusetAcksql.nQuestfistInfo.push_back(sqlQuestInfo);

    }


    // 将服务查询的数据发送给GameSever接收;

    qusetAcksql.userID = cquestReqsql.userID;
    qusetAcksql.seq = cquestReqsql.seq;
    qusetAcksql.nQuestCount = cquestReqsql.nQuestCount;
    qusetAcksql.nQuestType = cquestReqsql.nQuestType;
    qusetAcksql.Result = 1;

    Message::Ptr sendMsg(new Message());
    sendMsg->pack(qusetAcksql);

    g_sessionManager.send(sid,sendMsg);

}

    //任务向关卡数据库查询提交的条件是否匹配;
void QuestHandler::handleGameToMissonsqlQuestInfo(Message::Ptr &message)
{
    GS2DS_QuestGetMissonCountInfoReq questGetMissonCountInfoReq;
    message->unpack(questGetMissonCountInfoReq);

    char szsql[1024] = { 0 };

    //从数据库中找出该玩家ID中的所任务信息   如果当传进来的参数是你要选择的这个那么 (mission_count = mission_count);
    //
    sprintf_s(szsql,"select mission_count from mission_info where char_id = %d AND mission_type = %d ",questGetMissonCountInfoReq.userID,questGetMissonCountInfoReq.mQuestType);
    Query* pQuery = g_connectionPool.createQuery(szsql);
    pQuery->setCallBack(makeCallBack(&QuestHandler::handleGameServerRequestMissonQuestInfoCallBack,questGetMissonCountInfoReq));

    g_connectionPool.pushQuery(pQuery);

}

void QuestHandler::handleGameServerRequestMissonQuestInfoCallBack(Result& result,GS2DS_QuestGetMissonCountInfoReq questGetMissonCountInfoReq)
{
    if (result.errno())
    {
        BRUCE_LOG(BruCe::logger::Log_Error,questGetMissonCountInfoReq.mQuestType);
        BRUCE_LOG(BruCe::logger::Log_Debug,"%s:%d handleGameServerRequestMissonQuestInfoCallBack erroe: %s",result.error().c_str());

    }
    DS2GS_QuestGetMissonCountInfoAck questGetMissonCountInfoAck;

    for (Row* pRow = result.fetch(); pRow != NULL; pRow = result.fetch()) 
    {
        //*pRow >> questGetMissonCountInfoAck.mQuestType; *pRow >> questGetMissonCountInfoAck.mQuestCount; } // 将服务查询的数据发送给GameSever接收; questGetMissonCountInfoAck.userID = questGetMissonCountInfoReq.userID; questGetMissonCountInfoAck.seq = questGetMissonCountInfoReq.seq; questGetMissonCountInfoAck.mQuestID = questGetMissonCountInfoReq.mQuestID; questGetMissonCountInfoAck.Result = 1; Message::Ptr sendMsg(new Message()); sendMsg->pack(questGetMissonCountInfoAck); g_sessionManager.send(sid,sendMsg); } /***********创建第二张保存表提交完成的任务***************************************************/

// 创建完成任务以后储的任务表;Achieve
void QuestHandler::createAchieveTable()
{
    // 创建完成任务以后储存的任务表;
    static Colume QuestAchieveTable[] =
    {
        { "id",//次数;
    };

    g_MysqL.executeNoRs("CREATE TABLE `chr_quest_achieve` (`char_id` int NOT NULL)");
    g_MysqL.addColume(QUEST_ACHIEVE_TABLE,QuestAchieveTable,sizeof(QuestAchieveTable) / sizeof(Colume));
}


//删除当前数据表中所在的任务信息;
void QuestHandler::handleGameDeleteQuestIDInof(Message::Ptr &message)
{


    GS2DS_QuesDeletesqlInfoReq mQuesDeletesqlInfoReq;
    message->unpack(mQuesDeletesqlInfoReq);

    char szsql[1024] = { 0 };
    //sprintf_s(szsql,"select chr_ques from mission_info where char_id ",questGetMissonCountInfoReq.userID);
    sprintf_s(szsql,"delete from chr_quest_rec where char_id = %d AND id = %d",mQuesDeletesqlInfoReq.userID,mQuesDeletesqlInfoReq.dQuestID);
    Query* pQuery = g_connectionPool.createQuery(szsql);
    pQuery->setCallBack(makeCallBack(&QuestHandler::handleDeleteDBProxyToGameQuestIDinfo,mQuesDeletesqlInfoReq));
    g_connectionPool.pushQuery(pQuery);

}

//删除后的数据;
void QuestHandler::handleDeleteDBProxyToGameQuestIDinfo(Result& result,GS2DS_QuesDeletesqlInfoReq mQuesDeletesqlInfoReq)
{

    if (result.errno())
    {
        BRUCE_LOG(BruCe::logger::Log_Error,mQuesDeletesqlInfoReq.dQuestID);
        BRUCE_LOG(BruCe::logger::Log_Debug,"%s:%d handleDeleteDBProxyToGameQuestIDinfo erroe: %s",result.error().c_str());
    }

    DS2GS_QuesDeletesqlInfoAck mQuesDeletesqlInfoAck;

    mQuesDeletesqlInfoAck.userID = mQuesDeletesqlInfoReq.userID;
    mQuesDeletesqlInfoAck.seq = mQuesDeletesqlInfoReq.seq;
    mQuesDeletesqlInfoAck.dQuestID = mQuesDeletesqlInfoReq.dQuestID;

    Message::Ptr sendMsg(new Message());
    sendMsg->pack(mQuesDeletesqlInfoAck);
    g_sessionManager.send(sid,sendMsg);

}


//将删除的所有任务数据保存到下来;
void QuestHandler::handleDeleteQuestSeave(Message::Ptr &message)
{

    GS2DS_QuestDeltAndSeaveInfoReq sQuestDeltAndSeaveInfoReq;
    message->unpack(sQuestDeltAndSeaveInfoReq);

    char szsql[1024] = { 0 };
    sprintf_s(szsql,"insert into chr_quest_achieve(char_id,id,count) value(%d,%d,%d)",sQuestDeltAndSeaveInfoReq.userID,sQuestDeltAndSeaveInfoReq.QuestSeaveDleID,sQuestDeltAndSeaveInfoReq.QuestSeaveDleType,sQuestDeltAndSeaveInfoReq.QuestSeaveDleGroups,sQuestDeltAndSeaveInfoReq.QuestSeaveDleCount);
    Query* pQuery = g_connectionPool.createQuery(szsql);
    pQuery->setCallBack(makeCallBack(&QuestHandler::handleDeleteQuestIDinfo,sQuestDeltAndSeaveInfoReq));
    g_connectionPool.pushQuery(pQuery);
}

//将删除的所有任务数据保存下来;
void QuestHandler::handleDeleteQuestIDinfo(Result& result,GS2DS_QuestDeltAndSeaveInfoReq sQuestDeltAndSeaveInfoReq)
{
    if (result.errno())
    {
        BRUCE_LOG(BruCe::logger::Log_Error,sQuestDeltAndSeaveInfoReq.userID);
        BRUCE_LOG(BruCe::logger::Log_Debug,"%s:%d handleDeleteQuestIDinfo erroe: %s",result.error().c_str());
    }

    DS2GS_QuestDeltAndSeaveInfoAck sQuestDeltAndSeaveInfoAck;

    sQuestDeltAndSeaveInfoAck.userID = sQuestDeltAndSeaveInfoReq.userID;
    sQuestDeltAndSeaveInfoAck.seq = sQuestDeltAndSeaveInfoReq.seq;
    sQuestDeltAndSeaveInfoAck.QuestSeaveDleID = sQuestDeltAndSeaveInfoReq.QuestSeaveDleID;

    Message::Ptr sendMsg(new Message());
    sendMsg->pack(sQuestDeltAndSeaveInfoAck);
    g_sessionManager.send(sid,sendMsg);

}





APP_NS_END
原文链接:/cocos2dx/343561.html

猜你在找的Cocos2d-x相关文章