ToyTimerActionParser.cpp 5.25 KB
//
//  ToyTimerActionParser.cpp
//  WattsenglishToyApp-mobile
//
//  Created by Katarzyna Kalinowska-Górska on 28/12/2019.
//

#include <stdio.h>
#include "ToyTimerActionParser.h"
#include "ToyJSONParseUtils.h"
#include "ToyValueStorage.h"

// main parse function

cocos2d::Action* ToyTimerActionParser::parseJSONAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
    cocos2d::Action* parsedAction = NULL;
    if(ToyJSONParseUtils::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 ToyTimeIndicatorInterface
*/
    
cocos2d::Action* ToyTimerActionParser::parseStartTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
    if(ToyJSONParseUtils::hasMemberFloat(jsonActionObject, "seconds")){
    
        float seconds = jsonActionObject["seconds"].GetFloat();
        
        if(notifyDelegateWhenFinished){
            
            auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
            
            auto actionFunction = [](float pSeconds, std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
            
                auto storedValue = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
                ToyScenarioObject* sliderObject = ToyActionParser::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 = ToyActionParser::getInstance().parseJSONAction(array[i], parseDelegate, false);
                
                pParseDelegate->scheduleOnce(std::bind([](float, std::string p2StoredValueKey, ActionParseDelegate* p2ParseDelegate){
                    
                    p2ParseDelegate->actionFinished(*ToyValueStorage::getInstance().getStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName()));
                    ToyValueStorage::getInstance().removeStoredValue(p2StoredValueKey, p2ParseDelegate->getToyValueStorageContainerName()); // if the callback is unscheduled, this will not be called. However, when the scene changes, the ToyValueStorage 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* ToyTimerActionParser::parseStopTimerAction(const rapidjson::Value& jsonActionObject, ActionParseDelegate* parseDelegate, bool notifyDelegateWhenFinished)
{
//    auto storedValueKey = ToyValueStorage::getInstance().storeValue(jsonActionObject, parseDelegate->getToyValueStorageContainerName());
//
//    auto actionFunction = [&](std::string pStoredValueKey, ActionParseDelegate* pParseDelegate){
//
//        auto storedJsonActionObject = ToyValueStorage::getInstance().getStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
//
//        if(ToyActionParser::getInstance().checkLateCondition(*storedJsonActionObject, pParseDelegate)){
//
//            if(ToyJSONParseUtils::hasMemberString(*storedJsonActionObject, "objectClass") && ToyJSONParseUtils::hasMemberString(*storedJsonActionObject, "objectName")){
//                DynamicObjectMapper mapper;
//                auto newObject = mapper.createObjectFromClassName((*storedJsonActionObject)["objectClass"].GetString(), *storedJsonActionObject, pParseDelegate);
//                pParseDelegate->addNewObject((*storedJsonActionObject)["objectName"].GetString(), newObject);
//            }
//       }
//
//       ToyValueStorage::getInstance().removeStoredValue(pStoredValueKey, pParseDelegate->getToyValueStorageContainerName());
//    };
    
    //TODO !!!!!! --->
//    return this->finalizeParseActionWithActionFunction(jsonActionObject, parseDelegate, notifyDelegateWhenFinished, std::bind(actionFunction, storedValueKey, parseDelegate));
    return nullptr;
}