/* * 初始化 */ bool LoginLayer::init() { if (!CCLayer::init()) { return false; } NetConnection* pNetConnection = NetConnection::getInstance(); pNetConnection->setHandleNetDataDelegate(this); unsigned int server_id = CCUserDefault::sharedUserDefault()->getIntegerForKey(LOGIN_SERVER_KEY, 0); /* 更改服务器名 */ if (0 == server_id) { pNetConnection->setUUU(HTTP_SERVER_URL); /* 默认新浪服务器 */ m_pGameState->setServerId(0); } else { pNetConnection->setUUU(HTTP_SERVER_URL1); /* 研发服务器 */ m_pGameState->setServerId(1); } /* 初始化UI组件 */ _initComponent(); return true; }
/* * 处理 web 服务器响应 get 请求回应数据 */ void LoginLayer::handleWSResponseData(const char *szData, const unsigned int dataSize, char* szTag) { CCAssert(szData && dataSize > 0 && szTag, "invalid data"); bool isSuccess = false; if (!strcmp(szTag, HTTP_REQUEST_LOGIN_TAG)) /*登录请求*/ { isSuccess = ProcessData::processLoginResponseFromServer(szData, dataSize); if (isSuccess) { // 登陆成功,请求玩家武将信息 NetConnection* pNetConnection = NetConnection::getInstance(); char szPostBuffer[MAX_POSTTO_SERVER_BUFFER_SIZE]; memset(szPostBuffer, '\0', MAX_POSTTO_SERVER_BUFFER_SIZE); sprintf(szPostBuffer, "c=hero&a=hero_list&uid=%s", GamePlayer::getInstance()->getUid().c_str()); char szTag[] = HTTP_REQUEST_GETHERO_TAG; pNetConnection->commitPostRequestData(szPostBuffer, szTag); } } else if (!strcmp(szTag, HTTP_REQUEST_GETHERO_TAG)) /*武将数据*/ { NetConnection* pNetConnection = NetConnection::getInstance(); do { CC_BREAK_IF(!ProcessData::processHerosDataFromServer(szData, dataSize)); // 还有 “未决” 请求 CC_BREAK_IF(pNetConnection->isPending()); // 武将管理 HeroManager* pHeroManager = HeroManager::getInstance(); // 恢复战队位置 pHeroManager->restoreBattleTeamPosIndex(); if (m_pGameState->hasEndlessFight()) /* 有未完成战斗 */ { Battle* pBattle = Battle::getInstance(); // 恢复副本id m_pGameState->setCopyId(m_pGameState->getEndlessFightCopyId()); // 恢复关卡id m_pGameState->setLevId(m_pGameState->getEndlessFightLevelId()); // 恢复战斗波数 pBattle->setRoundIndex(m_pGameState->getEndlessFightRoundIndex() - 1); // 往战场添加本方上阵武将 unsigned int countOfGotoBattleHeros = pHeroManager->getGoIntoBattleHerosOfPlayerCount(); for (unsigned int index = 0; index < countOfGotoBattleHeros; index++) { HeroOfPlayerItem* pGotoBattleHeroItem = pHeroManager->getGoIntoBattleHeroDataByIndex(index); CCAssert(pGotoBattleHeroItem, "invalid data"); unsigned int posIndexInBattle = m_pGameState->getUfTeamPos(pGotoBattleHeroItem->getUniId()); CCAssert(posIndexInBattle > 0, "invalid posIndexInBattle"); pGotoBattleHeroItem->setPosInBattle(posIndexInBattle); pBattle->appendInBattleHero(pGotoBattleHeroItem); } /*for*/ pHeroManager->saveBattleTeamPosIndex(); // 提交战斗请求 pBattle->commitPveFightRequest(); } else { pNetConnection->setHandleNetDataDelegate(NULL); /* 设置login调用了主场景 */ m_pGameState->setTagWhoCallMainScene(1); CCDirector::sharedDirector()->replaceScene(MainScene::create()); CCNotificationCenter::sharedNotificationCenter()->postNotification(ON_MESSAGE_SHUTDOWN_LOADING); } } while (0); } else if (!strcmp(szTag, HTTP_REQUEST_FIGHT_PVE_TAG)) /*战斗请求*/{ NetConnection* pNetConnection = NetConnection::getInstance(); bool isSuccess = ProcessData::parseBufferFromSvr(szData, dataSize, szTag); if (isSuccess) { pNetConnection->setHandleNetDataDelegate(NULL); CCDirector::sharedDirector()->replaceScene(BattleScene::create()); } } }