#include "LoadingScene.h"
#include "audio/include/AudioEngine.h"
#include "OtherCoursesMenuScene.h"
#include "MJDownloadCommonResourcesAPI.h"
#include "JsonHelper.h"
#include "MJCourseModel.h"
#include "DatabaseManager.h"
#include "MJDefault.h"
#include "MJDownloadLessonAPI.h"
#include "MJDownloadCategoryAPI.h"
#include "MJDownloadCommonResourcesAPI.h"
#include "MJAppinfoAPI.h"
#include "APDatabase.h"
#include "APProfilesListAPI.h"
#include "MJChooseProfile.h"
#include "APLoadUpdateAPI.h"
#include "MJPopupAlert.h"
#include "MJOnboardingLanguageSelection.h"
#include "AnimationManager.h"
#include "HelperManager.h"
#include "APLocationAPI.h"
#include "MkStickerInfoAPI.h"
#include "SpineWrapper.h"
#include "MJFirebaseFireStore.hpp"
#include "MkUnlockStickerAPI.h"
#include "APCollectNickName.h"
#include "EventManager.h"
#include "MJSubscriptionLandscape.h"
#include "MJDownloadDataGame.h"
#include "MJGetListWordByCourseAPI.h"
#include "ClevertapAdapter.h"
#include "MJGetLessonConfig.h"
#include "GAFWrapper.h"
#include "ErrorConnectionPopup.h"
#include "IAPManager.h"
std::once_flag loading_scene_reader;
USING_NS_CC;
using namespace cocos2d::experimental;
INITIALIZE_READER(LoadingScene);
#define LOADING_SCENE_VIEW_NAME "LoadingScene"
#define RETRY_API_MAX_NUM 3
#define MONKEY_POSITION_Y 32.F
Scene* LoadingScene::createScene(ActionType i_type, int i_courdID, bool isMJLoadingLayerUIEnable, MJLoadingLayer::UIType type) {
auto scene = Scene::create();
if (scene) {
auto view = LoadingScene::createView(i_type,i_courdID, isMJLoadingLayerUIEnable, type);
view->setName(LOADING_SCENE_VIEW_NAME);
scene->addChild(view);
} else {
CC_SAFE_DELETE(scene);
}
return scene;
}
LoadingScene * LoadingScene::createView(ActionType i_type, int i_courdID, bool isMJLoadingLayerUIEnable, MJLoadingLayer::UIType type) {
std::call_once(loading_scene_reader, [=]
{
REGISTER_CSB_READER(LoadingScene);
});
auto view = dynamic_cast<LoadingScene*>(CSLoader::createNodeWithVisibleSize("csb/LoadingScene.csb"));
if (view && view->didLoadFromCSB(i_type,i_courdID, isMJLoadingLayerUIEnable, type)) {
return view;
}
CC_SAFE_DELETE(view);
return view;
}
bool LoadingScene::init() {
if (!LayerBase::init()) {
return false;
}
return true;
}
std::string urlencode(const std::string& c) {
auto char2Hex = [](const unsigned char dec) {
auto dig_1 = char((dec & 0xF0u) >> 4u);
auto dig_2 = char((dec & 0x0Fu));
if (0 <= dig_1 && dig_1 <= 9) dig_1 += 48; //0,48 in ascii
if (10 <= dig_1 && dig_1 <= 15) dig_1 += 65 - 10; //A,65 in ascii
if (0 <= dig_2 && dig_2 <= 9) dig_2 += 48;
if (10 <= dig_2 && dig_2 <= 15) dig_2 += 65 - 10;
std::string r;
r.append(&dig_1, 1);
r.append(&dig_2, 1);
return r;
};
std::string escaped;
auto max = int(c.length());
for (auto i = 0; i < max; i++) {
if (('0' <= c[i] && c[i] <= '9') || //0-9
('A' <= c[i] && c[i] <= 'Z') || //ABC...XYZ
('a' <= c[i] && c[i] <= 'z') || //abc...xyz
(c[i] == '~' || c[i] == '-' || c[i] == '_' || c[i] == '.')) {
escaped.append(&c[i], 1);
}
else {
escaped.append("%");
escaped.append(char2Hex(c[i])); //converts char 255 to string "FF"
}
}
return escaped;
}
void giuLaiKhoanCachChoAplhaVaNumber(cocos2d::StringUtils::StringUTF8& txt)
{
std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < txt.length(); i++)
{
auto character = txt.getString().at(i);
if (txt.getString().at(i)._char == " ") {
if (i + 1 < txt.length() && s.find(txt.getString().at(i + 1)._char) != std::string::npos) {
txt.deleteChar(i);
txt.insert(i, "XXX");
giuLaiKhoanCachChoAplhaVaNumber(txt);
break;
}
if (i - 1 >= 0 && s.find(txt.getString().at(i - 1)._char) != std::string::npos) {
txt.deleteChar(i);
txt.insert(i, "XXX");
giuLaiKhoanCachChoAplhaVaNumber(txt);
break;
}
}
}
}
void writeConvertedLanguageToJson(std::vector<std::pair<std::string, std::string>> vectorDict) {
rapidjson::Document doc;
doc.SetObject();
typedef rapidjson::GenericValue<rapidjson::UTF8<> > JValue;
rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
for (auto& xx : vectorDict) {
JValue jKey, jValue;
jKey.SetString(xx.first.c_str(), xx.first.length(), allocator);
auto value = xx.second;
value = monkey_helper::string::replaceAllString(value, "XXX", " ");
jValue.SetString(value.c_str(), allocator);
doc.AddMember(jKey, jValue, allocator);
}
JSON_HELPER->writeJsonToFile(doc, FileUtils::getInstance()->getWritablePath() + "/ok.json");
}
void returnx() {
std::string json = FileUtils::getInstance()->getWritablePath() + "/ok.json";
auto json_str = FileUtils::getInstance()->getStringFromFile(json);
rapidjson::Document doc;
doc.Parse(json_str.c_str());
if (doc.HasParseError()) {
//safety check
CCLOG("LanguageManager::init|json language error: %s", rapidjson::GetParseError_En(doc.GetParseError()));
return;
}
rapidjson::Document doc2;
doc2.SetObject();
typedef rapidjson::GenericValue<rapidjson::UTF8<> > JValue;
rapidjson::Document::AllocatorType& allocator = doc2.GetAllocator();
for (auto i = doc.MemberBegin(); i != doc.MemberEnd(); ++i) {
auto key = i->name.GetString();
auto val = i->value.GetString();
std::pair<std::string, std::string> xx = { std::string(key),std::string(val) };
JValue jKey, jValue;
jKey.SetString(xx.first.c_str(), xx.first.length(), allocator);
auto value = xx.second;
value = monkey_helper::string::replaceAllString(value, "|", "");
jValue.SetString(value.c_str(), allocator);
doc2.AddMember(jKey, jValue, allocator);
}
JSON_HELPER->writeJsonToFile(doc2, FileUtils::getInstance()->getWritablePath() + "/json.json");
}
static std::vector<std::pair<std::string, std::string>> s_vectorDict;
static int s_index = 0;
void dequy()
{
if (s_index >= s_vectorDict.size()) {
writeConvertedLanguageToJson(s_vectorDict);
returnx();
return;
}
auto& pair = s_vectorDict.at(s_index);
auto &str = s_vectorDict.at(s_index).second;
if ((str.find("font") != std::string::npos) || (str.find("<font") != std::string::npos) || (str.find("</font>") != std::string::npos)) {
s_index++; dequy();
return;
}
if ((str.find('\n') != std::string::npos)) {
s_index++; dequy();
return;
}
std::string rawData = ("text=" + str);
cocos2d::network::HttpRequest* request = new cocos2d::network::HttpRequest();
request->setUrl("http://www.sansarn.com/lexto-engine/LexTo");
std::vector<std::string> headers;
headers.push_back("Connection: keep-alive");
headers.push_back("Accept: */*");
headers.push_back("X-Requested-With: XMLHttpRequest");
headers.push_back("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36");
headers.push_back("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
headers.push_back("Accept-Language: vi-VN,vi;q=0.9,en-US;q=0.8,en;q=0.7");
request->setHeaders(headers);
request->setRequestData(rawData.data(), rawData.size());
request->setRequestType(cocos2d::network::HttpRequest::Type::POST);
request->setResponseCallback([&](cocos2d::network::HttpClient* sender, cocos2d::network::HttpResponse* response) {
auto code = response->getResponseCode();
if (200 == response->getResponseCode()) // connect success
{
std::vector<char>* buffer = response->getResponseData();
std::string data = std::string(buffer->begin(), buffer->end());
if (!data.empty())
{
data = monkey_helper::string::replaceAllString(data, "<font class=type0>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type1>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type2>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type3>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type4>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type5>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type6>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type7>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type8>", "");
data = monkey_helper::string::replaceAllString(data, "<font class=type9>", "");
data = monkey_helper::string::replaceAllString(data, "</font>", "");
data.erase(std::remove_if(data.begin(), data.end(), [](const char& c) {
return c == ' ';
}), data.end());
data = monkey_helper::string::replaceAllString(data, "XXX", " ");
data = monkey_helper::string::replaceAllString(data, "SPACEXD", " X");
data = monkey_helper::string::replaceAllString(data, "PERCENT", "%%");
data = monkey_helper::string::replaceAllString(data, "xxd", "%d");
data = monkey_helper::string::replaceAllString(data, "xxs", "%s");
data = monkey_helper::string::replaceAllString(data, "xxf", "%f");
CCLOG("reponse data:|key:%s id:%d \n%s\n", pair.first.c_str(), s_index, data.c_str());
str = data;
Director::getInstance()->getRunningScene()->runAction(Sequence::createWithTwoActions(DelayTime::create(0.5f), CallFunc::create([]() {
s_index++; dequy();
})));
}
else
{
CCLOG("\nconnect failed|key:%s id:%d msg:%s\n", pair.first.c_str(), s_index, data.c_str());
s_index++; dequy();
}
}
else // connect failed
{
std::vector<char>* buffer = response->getResponseData();
std::string data = std::string(buffer->begin(), buffer->end());
CCLOG("\nconnect failed|key:%s id:%d msg:%s\n", pair.first.c_str(), s_index,data.c_str());
s_index++; dequy();
}
});
request->setTag("Get test");
cocos2d::network::HttpClient::getInstance()->send(request);
request->release();
}
bool LoadingScene::didLoadFromCSB(ActionType i_type, int i_courdID, bool isMJLoadingLayerUIEnable, MJLoadingLayer::UIType type)
{
//returnx();
//return true;
auto &dict = LANGUAGE_MANAGER->_thDict;
std::string richTextKey = "", endlineKey ="" ;
for (auto &x : dict) {
std::string &str = x.second;
CCLOG("truoc replace:%s", str.c_str());
if ((str.find("font") != std::string::npos)||(str.find("<font") != std::string::npos)|| (str.find("</font>") != std::string::npos)) {
richTextKey += x.first;
richTextKey += "\n";
continue;
}
if ((str.find('\n') != std::string::npos)) {
endlineKey += x.first;
endlineKey += "\n";
continue;
}
cocos2d::StringUtils::StringUTF8 txt = cocos2d::StringUtils::StringUTF8(str);
//giuLaiKhoanCachChoAplhaVaNumber(txt);
str = txt.getAsCharSequence();
str = monkey_helper::string::replaceAllString(str, "%%", "PERCENT");
str = monkey_helper::string::replaceAllString(str, " X", "SPACEXD");
str = monkey_helper::string::replaceAllString(str, " ", "XXX");
str = monkey_helper::string::replaceAllString(str, "%d", "xxd");
str = monkey_helper::string::replaceAllString(str, "%s", "xxs");
str = monkey_helper::string::replaceAllString(str, "%f", "xxf");
str.erase(std::remove_if(str.begin(), str.end(), [](const char& c) {
return c == ' ';
}), str.end());
CCLOG("sau replace:%s", str.c_str());
}
CCLOG("richTextKey:\n%s\n\n", richTextKey.c_str());
CCLOG("endlineKey:\n%s\n\n", endlineKey.c_str());
for (auto& x : dict) {
//if(x.first=="easy.des")
s_vectorDict.push_back({ x.first,x.second });
}
dequy();
return true;
}
void LoadingScene::onEnter() {
CCLOG("%s|%s",LOADING_SCENE_VIEW_NAME,__FUNCTION__);
LayerBase::onEnter();
}
void LoadingScene::onEnterTransitionDidFinish() {
CCLOG("%s|%s",LOADING_SCENE_VIEW_NAME,__FUNCTION__);
LayerBase::onEnterTransitionDidFinish();
switch (_actionType)
{
case LoadingScene::ActionType::DEFAULT: {
if (auto logo = utils::findChild<SpineWrapper*>(this, "logo")) {
logo->playSequence("action", false, [=](SkeletonAnimation* obj, std::string callback_name) {
updateLoadingPercent(0, "start...");
this->callLocationAPI();
});
}
break;
}
case LoadingScene::ActionType::WITH_COURSE_ID: {
updateLoadingPercent(0, "start...");
this->callListCategoryAPI(_courseId, [=]() {
callListLessonAPI(_courseId, [=]() {
callListWordAPI([=]() {
syncStickerInfo();
});
});
});
break;
}
case LoadingScene::ActionType::WITH_US_COURSE_ID: {
updateLoadingPercent(0, "start...");
callListLessonAPI(_courseId, [=]() {
syncStickerInfo();
});
break;
}
case LoadingScene::ActionType::WITH_COURSE_ID_CHOOSE_PROFILE: {
auto loadingLayer = MJLoadingLayer::show(MJLoadingLayer::UIType::SPINE);
updateLoadingPercent(0, "start...");
std::function<void()> callApiProfile = [=]() {
if (_courseId == config::id::course::us) {
callListLessonAPI(_courseId, [=]() {
syncStickerInfo();
});
}
else {
callListCategoryAPI(_courseId, [=]() {
callListLessonAPI(_courseId, [=]() {
callListWordAPI([=]() {
syncStickerInfo();
});
});
});
}
};
auto profileId = MJDEFAULT->getIntegerForKey("key_profile_choose", DATABASE_MANAGER->getProfiles().front().profile_id);
MJDEFAULT->deleteValueForKey("key_profile_choose");
std::function<void()> switchProfileLocal = [=]() {
auto beforeTime = monkey_helper::utils::getTime();
PROFILE_MANAGER.switchProfileAsync(profileId, [=](bool isSuccess) {
PROFILE_MANAGER.setCurrentProfileId(profileId);
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] {
auto afterTime = monkey_helper::utils::getTime();
auto deltaTime = afterTime - beforeTime;
auto animTime = loadingLayer->getDurationAnimation("in");
if (deltaTime >= animTime + 0.1f) {
callApiProfile();
}
else
{
CCLOG("xrunActionxxxx:%f", std::abs((float)deltaTime - animTime) + 0.1f);
this->runAction(Sequence::createWithTwoActions(DelayTime::create(std::abs((float)deltaTime - animTime) + 0.1f), CallFunc::create([=]() {
callApiProfile();
})));
}
});
});
};
bool isLoggedToMainScene = MJDEFAULT->getBoolForKey(KEY_LOGGED_TO_MAINSCENE, false);
if (MJDEFAULT->getStringForKey(KEY_CURRENT_DEVICE_BY_PROFILE_ID(profileId), "") == PlatformConfigManager::getInstance()->getDeviceId() && isLoggedToMainScene) {
switchProfileLocal();
}
else {
MJFIRESTORE->retrieveDataProfileById(profileId, [=](bool success) {
Director::getInstance()->getScheduler()->performFunctionInCocosThread([=] {
if (success) {
int course_id = PROFILE_MANAGER.getIntegerForKey(KEY_CURRENT_COURSE_ID, 1);
if (course_id == -1) course_id = config::id::course::us;
_courseId = course_id;
switchProfileLocal();
}
else {
appendLogMessage("get profile firestore");
autoRetryAPI("get profile firestore", [=]() {
MJDEFAULT->setIntegerForKey("key_profile_choose", profileId);
Director::getInstance()->replaceScene(LoadingScene::createScene(LoadingScene::ActionType::WITH_COURSE_ID_CHOOSE_PROFILE, _courseId, true, MJLoadingLayer::UIType::SPINE_WITHOUT_IN_ANIMATION));
}, switchProfileLocal);
}
});
});
}
break;
}
case LoadingScene::ActionType::AFTER_LOGIN_SUCCESS: {
MJDEFAULT->setBoolForKey(config::key::logout::is_user_logout, false);
ClevertapAdapter::init();
CLEVERTAP_MANAGER->pushProfile(CleverTapManager::PUSH_TYPE::LOGIN,nullptr);
updateLoadingPercent(0, "start...");
callLoadUpdateAPI([=]() {
callProfilesListAPI([=]() {
goToChooseProfileScene();
});
});
break;
}
case LoadingScene::ActionType::AFTER_REGISTER_SUCCESS: {
MJDEFAULT->setBoolForKey(config::key::logout::is_user_logout, false);
ClevertapAdapter::init();
CLEVERTAP_MANAGER->pushProfile(CleverTapManager::PUSH_TYPE::LOGIN, nullptr);
updateLoadingPercent(0, "start...");
callLoadUpdateAPI([=]() {
goToChooseProfileScene();
});
break;
}
default:
break;
}
}
-
Convert tiếng Thái lan => cho tự động xuống dòng
| last edited by phuocnguyen Reputation: 0 | Posts: 36