-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCentralClient.cpp
More file actions
424 lines (388 loc) · 13.7 KB
/
CentralClient.cpp
File metadata and controls
424 lines (388 loc) · 13.7 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "platform/CCCommon.h"
#include "CentralClient.h"
#include "Env.h"
#include "EnvPrivate.h"
#include "RestClient.h"
#include "cocos/base/CCUserDefault.h"
#include "cocos/2d/CCSpriteFrameCache.h"
#include "cocos/base/CCDirector.h"
#include "CocosGUI.h"
#include <openssl/sha.h>
#include <iomanip>
#include <sstream>
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#if TARGET_IPHONE_SIMULATOR
const std::string CentralClient::CENTRAL_API_URL = "http://localhost:3000";
#elif COCOS2D_DEBUG
// const std::string CentralClient::CENTRAL_API_URL = "https://ocentral-api-stg.herokuapp.com/";
const std::string CentralClient::CENTRAL_API_URL = "http://192.168.0.2:3000";
#else
const std::string CentralClient::CENTRAL_API_URL = "https://ocentral-api.herokuapp.com/";
#endif
//const std::string CentralClient::CENTRAL_API_URL = "http://yalab.example.com:3000";
const char* PAYMENT_USER_TOKEN = "paymentUserToken";
const int CentralClient::ChargeNone = -1;
const int CentralClient::INVALID_ITEM_BOUGHT = -1;
using namespace cocos2d::ui;
CentralClient::CentralClient()
: _init(false)
, _currentCharge(CentralClient::ChargeNone)
, _userToken("")
{
}
CentralClient* CentralClient::getInstance()
{
static CentralClient s_instance;
return &s_instance;
}
void CentralClient::init()
{
if(_init){
return;
}
setUserToken(UserDefault::getInstance()->getStringForKey(PAYMENT_USER_TOKEN));
if(getUserToken().length() < 1){
createUser();
}
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
TIAPDeveloperInfo pGoogleInfo;
pGoogleInfo["GooglePlayAppKey"] = GOOGLE_APPKEY;
if(pGoogleInfo.empty()) {
char msg[256] = { 0 };
sprintf(msg, "Google App Key info is empty. PLZ fill your Google App Key info in %s(nearby line %d)", __FILE__, __LINE__);
MessageBox(msg, "Google IAP Warning");
}
s_protocolIAP = dynamic_cast<ProtocolIAP*>(PluginManager::getInstance()->loadPlugin("IAPGooglePlay"));
s_protocolIAP->configDeveloperInfo(pGoogleInfo);
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
TIAPDeveloperInfo info;
s_protocolIAP = dynamic_cast<ProtocolIAP*>(PluginManager::getInstance()->loadPlugin("IOSIAP"));
auto itr = SKU.begin();
std::string productKeys = (*itr).second;
itr ++;
for(; itr != SKU.end(); itr++) {
productKeys += "," + (*itr).second;
}
info["iapKeys"] = productKeys;
s_protocolIAP->configDeveloperInfo(info);
#endif
#if COCOS2D_DEBUG
s_protocolIAP->setDebugMode(true);
#endif
_init = true;
}
void CentralClient::buyCharge(const int price, ProtocolIAP::ProtocolIAPCallback callback)
{
TProductInfo info;
const std::string IAPId = SKU.at(price);
RestClient::PostData postData;
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
postData["platform"] = "googleplay";
info["IAPId"] = IAPId;
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
postData["platform"] = "appstore";
info["productId"] = IAPId;
#endif
auto userToken = getUserToken();
if(userToken.length() < 1){
MessageBox("API エラーにより現在課金が行えません", "もうしわけございません");
return;
}
postData["iap_id"] = IAPId;
postData["token"] = userToken;
postData["price"] = std::to_string(price);
RestClient::getInstance()->post(CENTRAL_API_URL + "/payments", postData, [&, callback, info](HttpClient* client, HttpResponse* response){
if(response->isSucceed()){
auto v = *response->getResponseData();
const std::string paymentId(v.begin(), v.end());
#if COCOS2D_DEBUG
std::string msg = "";
paymentSuccess(paymentId, callback, PayResultCode::kPaySuccess, msg);
return;
#endif
s_protocolIAP->payForProduct(info, [&, callback, paymentId, info](int status, std::string& msg){
log("Payment result %i %s", status, msg.c_str());
// s_protocolIAP->consumeForProduct(info);
if(status == PayResultCode::kPaySuccess){
paymentSuccess(paymentId, callback, status, msg);
}else{
callback(status, msg);
}
});
}else{
MessageBox("API エラーにより現在課金が行えません", "もうしわけございません");
}
});
}
void CentralClient::createUser()
{
RestClient::PostData postData;
postData["game_id"] = GAME_ID;
RestClient::getInstance()->post(CENTRAL_API_URL + "/players", postData, [&](HttpClient* client, HttpResponse* response){
if(response->isSucceed()){
auto v = *response->getResponseData();
const std::string body(v.begin(), v.end());
if(body.length() == 64){
UserDefault::getInstance()->setStringForKey(PAYMENT_USER_TOKEN, body);
setUserToken(body);
}
}
});
}
void CentralClient::paymentSuccess(const std::string& paymentId, ProtocolIAP::ProtocolIAPCallback callback, int status, std::string& msg)
{
RestClient::PostData postData;
postData["receipt_data"] = msg;
postData["token"] = getUserToken();
auto url = CENTRAL_API_URL + "/payments/" + paymentId;
RestClient::getInstance()->put(url, postData, wrapCallback(callback));
}
void CentralClient::getCurrentCharge(ProtocolIAP::ProtocolIAPCallback callback)
{
RestClient::getInstance()->get(CENTRAL_API_URL + "/payments?token=" + getUserToken(), [&, callback](HttpClient* client, HttpResponse* response){
auto status = response->getResponseCode();
auto v = *response->getResponseData();
std::string msg(v.begin(), v.end());
setCurrentCharge(std::stoi(msg));
callback(static_cast<int>(status), msg);
});
}
void CentralClient::buyItem(const int price, const int itemId, ProtocolIAP::ProtocolIAPCallback callback)
{
RestClient::PostData postData;
postData["token"] = getUserToken();
postData["amount"] = std::to_string(price);
auto url = "/charges/" + std::to_string(itemId);
const int currentCharge = getCurrentCharge();
RestClient::getInstance()->destroy(url, postData, [&, callback, currentCharge, price](HttpClient* client, HttpResponse* response){
auto status = response->getResponseCode();
auto v = *response->getResponseData();
if(status == 402){
std::string msg = "";
callback(static_cast<int>(status), msg);
return;
}
std::string msg(v.begin(), v.end());
const int charge = std::stoi(msg);
setCurrentCharge(charge);
if(currentCharge - price != charge){
status = CentralClient::INVALID_ITEM_BOUGHT;
}
callback(static_cast<int>(status), msg);
});
}
ccHttpRequestCallback CentralClient::wrapCallback(ProtocolIAP::ProtocolIAPCallback& callback)
{
return [callback](HttpClient* client, HttpResponse* response){
auto status = response->getResponseCode();
auto v = *response->getResponseData();
std::string msg(v.begin(), v.end());
callback(static_cast<int>(status), msg);
};
}
void CentralClient::postPlayLog(const std::string& data)
{
RestClient::PostData postData;
postData["token"] = getUserToken();
RestClient::getInstance()->post("/play_logs", postData, [](HttpClient* client, HttpResponse* response){});
}
void CentralClient::backup()
{
auto token = getUserToken();
const std::string fpath = token.substr(0, 3) + "/" + token + ".plist";
auto futils = FileUtils::getInstance();
const auto data = futils->getStringFromFile(getBackupFilePath());;
const auto sha256 = SHA256Hash(data);;
RestClient::PostData postData;
postData["hashed_payload"] = sha256;
postData["method"] = "PUT";
postData["filename"] = fpath;
const std::string url = CENTRAL_API_URL + "/players/" + token + "/s3_header";
RestClient::getInstance()->post(url, postData, [&, data, fpath](HttpClient* client, HttpResponse* response){
if(response->getResponseCode() != 200){
return;
}
auto v = *response->getResponseData();
const std::string header(v.begin(), v.end());
putToS3(fpath, data, header);
});
}
void CentralClient::putToS3(const std::string& fpath, const std::string& data, const std::string& header)
{
auto* request = new HttpRequest();
request->setUrl(("https://oke-ya-central.s3.amazonaws.com" + fpath).c_str());
request->setRequestType(HttpRequest::Type::PUT);
request->setRequestData(data.c_str(), data.length());
std::vector<std::string> headers;
std::istringstream iss(header);
std::string line;
while(std::getline(iss, line)){
headers.push_back(line);
}
request->setHeaders(headers);
request->setResponseCallback([](HttpClient* client, HttpResponse* response){
auto v = *response->getResponseData();
const std::string body(v.begin(), v.end());
log("PutToS3 finish %s", body.c_str());
});
HttpClient::getInstance()->send(request);
request->release();
}
const std::string CentralClient::SHA256Hash(const std::string& key)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, key.c_str(), key.size());
SHA256_Final(hash, &sha256);
std::stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
log("%s", ss.str().c_str());
return ss.str();
}
void CentralClient::downloadAssets(const std::function<void(EventAssetsManagerEx* event)>& callback)
{
std::stringstream ss;
ss << "Manifests/";
ss << GAME_ID;
ss << ".project.";
#if TARGET_IPHONE_SIMULATOR
ss << "l";
#elif CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
ss << "a";
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
ss << "i";
#endif
ss << ".manifest";
#if TARGET_IPHONE_SIMULATOR
ss << ".dev";
#elif COCOS2D_DEBUG
ss << ".stg";
#else
ss << ".prod";
#endif
const std::string manifest = ss.str();
log("manifest %s", manifest.c_str());
auto destDir = getAssetDir();
auto assetManager = AssetsManagerEx::create(manifest, destDir);
assetManager->retain();
auto eventListener = EventListenerAssetsManagerEx::create(assetManager, [=](EventAssetsManagerEx* event){
callback(event);
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(eventListener, 1);
assetManager->update();
}
#include <dirent.h>
void CentralClient::addSpriteCaches(Node* node, const std::string prefix)
{
auto layer = Layer::create();
node->addChild(layer);
auto mask = LayerColor::create(Color4B::BLACK);
node->addChild(mask);
auto assetDir = getAssetDir();
std::string cacheExt = ".plist";
struct dirent *ent;
DIR *dir;
if((dir = opendir(assetDir.c_str())) != NULL){
while((ent = readdir (dir)) != NULL){
auto plist = std::string(ent->d_name);
std::string::size_type index = plist.find_last_of(".");
if(index == std::string::npos){
continue;
}
auto extname = plist.substr(index);
if(extname != cacheExt){
continue;
}
if(prefix.size() > 0 && plist.substr(0, prefix.size()) != prefix){
continue;
}
auto plistPath = assetDir + "/" + plist;
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plistPath);
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(plistPath);
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(fullPath);
auto& frames = dict["frames"].asValueMap();
for(auto itr = frames.cbegin(); itr != frames.cend(); itr++){
auto img = ImageView::create(itr->first, ImageView::TextureResType::PLIST);
layer->addChild(img);
}
}
closedir(dir);
}
}
void CentralClient::savePlayData(ValueMap& dict)
{
FileUtils::getInstance()->writeToFile(dict, getBackupFilePath());
}
const ValueMap CentralClient::loadPlayData()
{
return FileUtils::getInstance()->getValueMapFromFile(getBackupFilePath());
}
void CentralClient::printValueMap(cocos2d::ValueMap& vmap)
{
auto v = Value(vmap);
log("%s", valueToStr(v).c_str());
}
const std::string CentralClient::valueToStr(Value& value)
{
std::stringstream ss;
switch(value.getType()){
case Value::Type::MAP :{
ss << "{";
auto& vmap = value.asValueMap();
auto size = vmap.size();
int i = 0;
for(auto& v: vmap){
ss << "\"" + v.first + "\"";
ss << ": ";
ss << valueToStr(v.second);
i++;
if(i < size){
ss << ", ";
}
}
ss << "}";
return ss.str();
break;
}
case Value::Type::VECTOR :{
ss << "[";
auto& vec = value.asValueVector();
auto size = vec.size();
int i = 0;
for(auto& v: vec){
ss << valueToStr(v);
i++;
if(i < size){
ss << ", ";
}
}
ss << "]";
return ss.str();
break;
}
case Value::Type::INTEGER :{
ss << value.asInt();
return ss.str();
break;
}
case Value::Type::STRING :{
ss << "\"";
ss << value.asString();
ss << "\"";
return ss.str();
break;
}
case Value::Type::NONE :{
return "null";
}
default: {
log("Not defined print type");
}
}
}