ToyTimerActionParser.cpp
5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// 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;
}