// // TimerActionParser.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 28/12/2019. // #include #include "TimerActionParser.h" #include "JSONParseUtils.h" #include "ValueStorage.h" // main parse function cocos2d::Action* TimerActionParser::parseJSONAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished) { cocos2d::Action* parsedAction = NULL; if(JSONParseUtils::hasMemberString(jsonActionObject, "actionType")){ std::string actionType = jsonActionObject["actionType"].GetString(); if(actionType == "startTimer"){ parsedAction = this->parseStartTimerAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished); } else if(actionType == "stopTimer"){ parsedAction = this->parseStopTimerAction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished); } } return parsedAction; } // functions for parsing differenct actions /* "actionType" = "startTimer" "seconds" [float] "onProgressActions" : [], "onTimeUpActions" : [], "finishScenarioAfterTimeUp" : true, "timeIndicatorObject" : "timeSlider" //needs to conform to the TimeIndicatorInterface */ cocos2d::Action* TimerActionParser::parseStartTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished) { if(JSONParseUtils::hasMemberFloat(jsonActionObject, "seconds")){ float seconds = jsonActionObject["seconds"].GetFloat(); if(notifyDelegateWhenFinished){ auto storedValueKey = ValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getValueStorageContainerName()); auto actionFunction = [](float pSeconds, std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){ auto storedValue = ValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName()); ScenarioObject* sliderObject = ActionParser::getInstance().parseObject("timeIndicatorObject", pParseDelegate); if(sliderObject != nullptr){ //TODO } static int keyModifier = 0; static int modulus = 100; static std::string keyBase = "startTimerAction"; std::string key = keyBase + std::to_string((++keyModifier)%modulus); // auto action = ActionParser::getInstance().parseJSONAction(array[i], parseDelegate, false); pParseDelegate->scheduleOnce(std::bind([](float, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){ p2ParseDelegate->actionFinished(*ValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getValueStorageContainerName())); ValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getValueStorageContainerName()); // if the callback is unscheduled, this will not be called. However, when the scene changes, the ValueStorage is always cleared. }, std::placeholders::_1, pStoredValueKey, pParseDelegate), pSeconds, key); }; return cocos2d::CallFunc::create(std::bind(actionFunction, seconds, storedValueKey, parseDelegate)); } return cocos2d::DelayTime::create(seconds); } return nullptr; } /* "actionType" = "stopTimer" */ cocos2d::Action* TimerActionParser::parseStopTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished) { // auto storedValueKey = ValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getValueStorageContainerName()); // // auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){ // // auto storedJsonActionObject = ValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName()); // // if(ActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){ // // if(JSONParseUtils::hasMemberString(*storedJsonActionObject, "objectClass") && JSONParseUtils::hasMemberString(*storedJsonActionObject, "objectName")){ // DynamicObjectMapper mapper; // auto newObject = mapper.createObjectFromClassName((*storedJsonActionObject)["objectClass"].GetString(), *storedJsonActionObject, pParseDelegate); // pParseDelegate->addNewObject((*storedJsonActionObject)["objectName"].GetString(), newObject); // } // } // // ValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getValueStorageContainerName()); // }; //TODO !!!!!! ---> // return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate)); }