ServerGiftManager.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Prime31;
  7. public class ServerGiftManager : MonoBehaviour {
  8. public enum RegistrationStates
  9. {
  10. New,
  11. Old,
  12. UpdateDone,
  13. SantaNameNotUnique,
  14. IdentificatorNotIsset,
  15. ContactsIsNotEnough,
  16. Error
  17. }
  18. public class GiftInfo
  19. {
  20. public static GiftInfo GetInfo()
  21. {
  22. return new GiftInfo(PlayerPrefs.GetString("GiftTokenInfo",""));
  23. }
  24. public static void Save(GiftInfo info)
  25. {
  26. PlayerPrefs.SetString("GiftTokenInfo",info.ToString());
  27. }
  28. public static void Consume()
  29. {
  30. PlayerPrefs.SetString("GiftTokenInfo", "");
  31. }
  32. public bool HasToken
  33. {
  34. get { return _lastTokenDate.Date == DateTime.Today && _token > 0; }
  35. }
  36. private DateTime _lastTokenDate;
  37. private int _token;
  38. public int Token
  39. {
  40. get
  41. {
  42. if (HasToken)
  43. {
  44. return _token;
  45. }
  46. return 0;
  47. }
  48. }
  49. public GiftInfo(string source)
  50. {
  51. _lastTokenDate = DateTime.MinValue;
  52. if (!String.IsNullOrEmpty(source))
  53. {
  54. var arr = source.Split(':');
  55. _lastTokenDate = DateTime.FromBinary(Int64.Parse(arr[0]));
  56. _token = Int32.Parse(arr[1]);
  57. }
  58. }
  59. public GiftInfo(int token)
  60. {
  61. _token = token;
  62. _lastTokenDate = DateTime.Now;
  63. }
  64. public override string ToString()
  65. {
  66. if (HasToken)
  67. {
  68. return String.Format("{0}:{1}",_lastTokenDate.Date.ToBinary(),_token);
  69. }
  70. return "";
  71. }
  72. }
  73. internal void SetAge(object text, Func<object, object> p)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public static string ID;
  78. private static string DeviceID
  79. {
  80. get
  81. {
  82. return (SystemInfo.deviceUniqueIdentifier + ID);
  83. }
  84. }
  85. public bool PhoneProvided;// { get; private set; }
  86. public bool SantaNameProvided;// { get; private set; }
  87. public bool EmailProvided;// { get; private set; }
  88. public static ServerGiftManager Instance {
  89. get
  90. {
  91. if (!_instance)
  92. {
  93. _instance = new GameObject("_serverGiftManager").AddComponent<ServerGiftManager>();
  94. DontDestroyOnLoad(_instance.gameObject);
  95. }
  96. return _instance;
  97. }
  98. }
  99. private static ServerGiftManager _instance;
  100. public int LevelServer;
  101. public int GiftNum;
  102. public int ScheduleID;
  103. void Awake ()
  104. {
  105. //for (int i = 0; i < 20; i++)
  106. //{
  107. // Debug.LogError(Guid.NewGuid().ToString());
  108. //}
  109. Debug.Log(PlayerPrefs.GetInt("GetCoupon"));
  110. Debug.Log(PlayerPrefs.GetString("NumberPhone"));
  111. //PlayerPrefs.DeleteAll();
  112. // PlayerPrefs.SetInt("GetCoupon", 0);
  113. //PlayerPrefs.SetString("NumberPhone", "");
  114. PlayerPrefs.Save();
  115. if (PlayerPrefs.GetInt("Level") == 0)
  116. {
  117. PlayerPrefs.SetInt("Level", 1);
  118. }
  119. //PlayerPrefs.SetInt("FirstEnter", 0);
  120. ID = "f77";
  121. if (_instance && _instance != this)
  122. {
  123. Destroy(gameObject);
  124. return;
  125. }
  126. }
  127. private static void TESTCALL()
  128. {
  129. ServerGiftManager.Instance.RequestTreeUpdate(meals =>
  130. {
  131. if (meals == -1)
  132. {
  133. //todo: server error, or wrong number filled
  134. }
  135. else
  136. {
  137. //todo: handle meals amount (everything goes good)
  138. }
  139. });
  140. ServerGiftManager.Instance.BaseRegistration((state, santaName) =>
  141. {
  142. switch (state)
  143. {
  144. case RegistrationStates.New:
  145. //todo: handle - user was registered from scratch (everything goes good)
  146. break;
  147. case RegistrationStates.Old:
  148. //todo: handle - old user returns (everything goes good)
  149. break;
  150. case RegistrationStates.Error:
  151. //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
  152. break;
  153. }
  154. Debug.LogWarning(state + " " + santaName);
  155. });
  156. ServerGiftManager.Instance.FullRegistration("BestSanta", "+38 (063) 270-08-56", "m-snowy@mail.ru", state =>
  157. {
  158. switch (state)
  159. {
  160. case RegistrationStates.UpdateDone:
  161. //todo: handle - user info was updated (everything goes good)
  162. break;
  163. case RegistrationStates.Old:
  164. //todo: handle - old user returns (everything goes good)
  165. break;
  166. case RegistrationStates.SantaNameNotUnique:
  167. //todo: handle - user should guess another name for santa (no data was updated!)
  168. break;
  169. case RegistrationStates.IdentificatorNotIsset:
  170. //todo: handle - you must run base registration first! (no data was updated!)
  171. break;
  172. case RegistrationStates.ContactsIsNotEnough:
  173. //todo: handle - target field - santaName or phone or e-mail is empty (no data was updated!)
  174. break;
  175. case RegistrationStates.Error:
  176. //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
  177. break;
  178. }
  179. Debug.LogWarning(state);
  180. });
  181. ServerGiftManager.Instance.GetGiftToken(info =>
  182. {
  183. if (info.HasToken)
  184. {
  185. //todo: handle - user achieve gift token - we can give coupon
  186. }
  187. else
  188. {
  189. //todo: handle - no token!
  190. }
  191. Debug.LogWarning(info.HasToken + " " + info.Token);
  192. });
  193. ServerGiftManager.Instance.ConsumeToken();
  194. //ServerGiftManager.Instance.SendCoupon((done, message,errorCode) =>
  195. //{
  196. // if (done)
  197. // {
  198. // //TODO coupon sent
  199. // }
  200. // else
  201. // {
  202. // switch (errorCode)
  203. // {
  204. // case 56:
  205. // //TODO handle error: deviceID is not registered
  206. // break;
  207. // case 57:
  208. // //TODO handle error: no deviceID
  209. // break;
  210. // case 58:
  211. // //TODO handle error: phone number does not provided
  212. // break;
  213. // case 59:
  214. // //TODO handle error: request error
  215. // break;
  216. // }
  217. // }
  218. //});
  219. }
  220. public void ConsumeToken()
  221. {
  222. GiftInfo.Consume();
  223. KHD.FlurryAnalyticsTest.Instance.CouponWon();
  224. }
  225. public void GetGiftToken(Action<GiftInfo> onDone)
  226. {
  227. //Debug.Log("GetGiftTokenRoutine");
  228. var info = GiftInfo.GetInfo();
  229. if (info.HasToken)
  230. {
  231. onDone(info);
  232. StartCoroutine(GetGiftTokenRoutine(info, onDone));
  233. }
  234. else
  235. {
  236. StartCoroutine(GetGiftTokenRoutine(info,onDone));
  237. }
  238. }
  239. static string remSkob(string input)
  240. {
  241. char[] arr = input.ToCharArray();
  242. char[] arr2 = new char[input.ToCharArray().Length];
  243. for (int i = 0; i < arr.Length; i++)
  244. {
  245. if (arr[i] == '{')
  246. {
  247. int k = i + 1;
  248. arr2[i] = '{';
  249. while (arr[k] != '}')
  250. {
  251. //arr[k] = '\0';
  252. arr2[k] = arr[k];
  253. k++;
  254. }
  255. arr2[k] = arr[k];
  256. }
  257. }
  258. string result = "";
  259. for (int i = 0; i < arr2.Length; i++)
  260. {
  261. if (arr2[i] != '\0')
  262. {
  263. result += arr2[i];
  264. }
  265. }
  266. return result;
  267. }
  268. private IEnumerator GetGiftTokenRoutine(GiftInfo def,Action<GiftInfo> onDone)
  269. {
  270. WWW www = new WWW("");
  271. var linq = "";
  272. if (InGameScriptCS.Instance.Level > 0 && InGameScriptCS.Instance.Level <= 3)
  273. {
  274. linq = string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/GetGift.php?identificator={0}&lvl={1}", DeviceID, InGameScriptCS.Instance.Level);
  275. www = new WWW(linq);
  276. }
  277. else
  278. {
  279. linq = string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/GetGift.php?id={0}", DeviceID);
  280. www = new WWW(linq);
  281. }
  282. Debug.LogError(linq);
  283. yield return www;
  284. var result = remSkob(www.text);
  285. if (!string.IsNullOrEmpty(www.error))
  286. {
  287. onDone(def);
  288. }
  289. else
  290. {
  291. string res = "";
  292. var response = (JsonObject)Json.decode(www.text);
  293. if (response == null)
  294. {
  295. res = result;
  296. }
  297. else
  298. {
  299. if (response != null)
  300. {
  301. res = response["response"] == null ? "" : response["response"].ToString().ToLower();
  302. }
  303. }
  304. try
  305. {
  306. if (res.Contains("giftNum"))
  307. {
  308. res = "available";
  309. }
  310. }
  311. catch (Exception e)
  312. {
  313. Debug.LogError(e);
  314. }
  315. Debug.Log(res);
  316. switch (res)
  317. {
  318. case "available":
  319. if (PlayerPrefs.GetInt("GetCoupon") == 0)
  320. {
  321. ScheduleID = Int32.Parse(((JsonObject)Json.decode(result))["ScheduleID"].ToString());
  322. PlayerPrefs.SetInt("ScheduleID", ScheduleID);
  323. PlayerPrefs.Save();
  324. var token = Int32.Parse(((JsonObject)Json.decode(result))["giftNum"].ToString());
  325. GiftNum = token;
  326. PlayerPrefs.SetInt("giftNum", token);
  327. PlayerPrefs.Save();
  328. var newInfo = new GiftInfo(token);
  329. GiftInfo.Save(newInfo);
  330. onDone(newInfo);
  331. Debug.LogError("token");
  332. }
  333. yield return new WaitForSeconds(0.1f);
  334. break;
  335. case "day count error":
  336. var newInf = new GiftInfo(0);
  337. GiftInfo.Save(newInf);
  338. InGameScriptCS.Instance.GetComponent<ElementsGeneratorCS>().GetGift = false;
  339. onDone(newInf);
  340. yield return new WaitForSeconds(0.1f);
  341. break;
  342. default:
  343. onDone(def);
  344. yield return new WaitForSeconds(0.1f);
  345. break;
  346. }
  347. if (response==null)
  348. {
  349. yield return new WaitForSeconds(0.1f);
  350. }
  351. else
  352. {
  353. if (response.ContainsKey("lvl"))
  354. {
  355. string lvl = response["lvl"] == null ? "" : response["lvl"].ToString();
  356. LevelServer = int.Parse(lvl);
  357. }
  358. }
  359. }
  360. }
  361. public void RequestTreeUpdate(Action<int> onDone)
  362. {
  363. StartCoroutine(RequestTreeUpdateRoutine(onDone));
  364. }
  365. private IEnumerator RequestTreeUpdateRoutine(Action<int> onDone)
  366. {
  367. var www = new WWW("https://game.gamatic.com/app_runner_dk/meals.json");
  368. yield return www;
  369. if (!string.IsNullOrEmpty(www.error))
  370. {
  371. onDone(-1);
  372. }
  373. else
  374. {
  375. var response = ((JsonObject)Json.decode(www.text))["meals"].ToString().ToLower();
  376. int result = 0;
  377. if (int.TryParse(response, out result))
  378. {
  379. onDone(result);
  380. }
  381. else
  382. {
  383. onDone(-1);
  384. }
  385. }
  386. }
  387. public void RequestWeeksUpdate(Action<string> onDone)
  388. {
  389. StartCoroutine(RequestWeekUpdateRoutine(onDone));
  390. }
  391. private IEnumerator RequestWeekUpdateRoutine(Action<string> onDone)
  392. {
  393. var www = new WWW("https://game.gamatic.com/app_runner_dk/week.json");
  394. yield return www;
  395. if (!string.IsNullOrEmpty(www.error))
  396. {
  397. onDone(www.error);
  398. }
  399. else
  400. {
  401. var response = www.text;
  402. int result = 0;
  403. if (!string.IsNullOrEmpty(www.text))
  404. {
  405. onDone(www.text);
  406. }
  407. else
  408. {
  409. onDone("Empty");
  410. }
  411. }
  412. }
  413. public void BaseRegistration(Action<RegistrationStates, string> onDone)
  414. {
  415. StartCoroutine(BaseRegistrationRoutine(onDone));
  416. }
  417. public IEnumerator BaseRegistrationRoutine(Action<RegistrationStates, string> onDone)
  418. {
  419. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/Register.php?identificator={0}", WWW.EscapeURL(DeviceID)));
  420. yield return www;
  421. if (!string.IsNullOrEmpty(www.error))
  422. {
  423. onDone(RegistrationStates.Error,"");
  424. }
  425. else
  426. {
  427. var response = (JsonObject)Json.decode(www.text);
  428. var answer = response["response"].ToString().ToLower();
  429. string santaName = response["name"] == null ? "" : response["name"].ToString();
  430. string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  431. SantaNameProvided = !string.IsNullOrEmpty(santaName);
  432. PhoneProvided = setted.Contains("P");
  433. EmailProvided = setted.Contains("M");
  434. switch (answer)
  435. {
  436. case "isset":
  437. onDone(RegistrationStates.Old, santaName);
  438. break;
  439. case "added":
  440. onDone(RegistrationStates.New, santaName);
  441. break;
  442. default:
  443. onDone(RegistrationStates.Error,"");
  444. break;
  445. }
  446. }
  447. }
  448. public void RegisterPhone(string phone, Action<RegistrationStates> onDone)
  449. {
  450. if (string.IsNullOrEmpty(phone))
  451. {
  452. onDone(RegistrationStates.ContactsIsNotEnough);
  453. return;
  454. }
  455. StartCoroutine(FullRegistrationRoutine("", phone.Trim(), "", onDone));
  456. }
  457. public void RegisterMail(string mail, Action<RegistrationStates> onDone)
  458. {
  459. if (string.IsNullOrEmpty(mail))
  460. {
  461. onDone(RegistrationStates.ContactsIsNotEnough);
  462. return;
  463. }
  464. StartCoroutine(FullRegistrationRoutine("", "", mail.Trim(), onDone));
  465. }
  466. public void RegisterSantaName(string santaName, Action<RegistrationStates> onDone)
  467. {
  468. if (string.IsNullOrEmpty(santaName))
  469. {
  470. onDone(RegistrationStates.ContactsIsNotEnough);
  471. return;
  472. }
  473. StartCoroutine(FullRegistrationRoutine(santaName.Trim(), "", "", onDone));
  474. }
  475. public void FullRegistration(string santaName, string phone, string email,Action<RegistrationStates> onDone)
  476. {
  477. if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(santaName))
  478. {
  479. onDone(RegistrationStates.ContactsIsNotEnough);
  480. return;
  481. }
  482. StartCoroutine(FullRegistrationRoutine(santaName,phone,email,onDone));
  483. }
  484. protected IEnumerator FullRegistrationRoutine(string santaName, string phone, string email, Action<RegistrationStates> onDone)
  485. {
  486. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/Register.php?identificator={0}&name={1}&phone={2}&email={3}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(santaName), WWW.EscapeURL(phone), WWW.EscapeURL(email)));
  487. yield return www;
  488. //Debug.Log(www.url);
  489. if (!string.IsNullOrEmpty(www.error))
  490. {
  491. onDone(RegistrationStates.Error);
  492. }
  493. else
  494. {
  495. var response = (JsonObject)Json.decode(www.text);
  496. var answer = response["response"].ToString().ToLower();
  497. if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  498. {
  499. SantaNameProvided = !string.IsNullOrEmpty(santaName);
  500. }
  501. string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  502. PhoneProvided = setted.Contains("P");
  503. EmailProvided = setted.Contains("M");
  504. switch (answer)
  505. {
  506. case "identificator not isset":
  507. onDone(RegistrationStates.IdentificatorNotIsset);
  508. break;
  509. case "update is succesfull":
  510. //Debug.Log((RegistrationManager.Instance == null) +" "+ santaName .ToString()+ " RegistrationManager.Instance is NULL!");
  511. if (RegistrationManager.Instance && !string.IsNullOrEmpty(santaName))
  512. {
  513. RegistrationManager.Instance.CurName = santaName;
  514. }
  515. else
  516. {
  517. Debug.Log("RegistrationManager.Instance is NULL!");
  518. }
  519. onDone(RegistrationStates.UpdateDone);
  520. break;
  521. case "isset":
  522. onDone(RegistrationStates.Old);
  523. break;
  524. case "namenotunique":
  525. onDone(RegistrationStates.SantaNameNotUnique);
  526. break;
  527. default:
  528. onDone(RegistrationStates.Error);
  529. break;
  530. }
  531. Debug.Log(answer);
  532. }
  533. }
  534. public void SetLevel(int lvl,Action<object> onDone)
  535. {
  536. if (lvl == 0)
  537. {
  538. return;
  539. }
  540. StartCoroutine(SendGetLevel(lvl, onDone));
  541. }
  542. private IEnumerator SendGetLevel(int lvl, Action<object> onDone)
  543. {
  544. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/GetGift.php?identificator={0}&lvl={1}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(lvl.ToString())));
  545. yield return www;
  546. Debug.Log(www.url);
  547. if (!string.IsNullOrEmpty(www.error))
  548. {
  549. onDone(RegistrationStates.Error);
  550. }
  551. else
  552. {
  553. //var answer = response["result"].ToString().ToLower();
  554. //Debug.Log(answer);
  555. //if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  556. //{
  557. // SantaNameProvided = !string.IsNullOrEmpty(santaName);
  558. //}
  559. //string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  560. //PhoneProvided = setted.Contains("P");
  561. //EmailProvided = setted.Contains("M");
  562. //switch (answer)
  563. //{
  564. //}
  565. //Debug.Log(answer);
  566. }
  567. }
  568. public void GetLevel( Action<object> onDone)
  569. {
  570. StartCoroutine(SendGetLevel(onDone));
  571. }
  572. private IEnumerator SendGetLevel(Action<object> onDone)
  573. {
  574. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/GetGift.php?id={0}", WWW.EscapeURL(DeviceID)));
  575. yield return www;
  576. //Debug.Log(www.url);
  577. if (!string.IsNullOrEmpty(www.error))
  578. {
  579. //onDone(RegistrationStates.Error);
  580. }
  581. else
  582. {
  583. var response = (JsonObject)Json.decode(www.text);
  584. if (response.ContainsKey("lvl"))
  585. {
  586. string lvl = response["lvl"] == null ? "" : response["lvl"].ToString();
  587. //Debug.Log(lvl);
  588. if (!string.IsNullOrEmpty(lvl))
  589. {
  590. switch (lvl)
  591. {
  592. case "null":
  593. Debug.Log("null");
  594. break;
  595. }
  596. LevelServer = int.Parse(lvl);
  597. }
  598. else
  599. {
  600. LevelServer = 0;
  601. }
  602. }
  603. else
  604. {
  605. LevelServer = 0;
  606. }
  607. onDone(LevelServer);
  608. //Debug.Log(response);
  609. //var answer = response["result"].ToString().ToLower();
  610. //Debug.Log(answer);
  611. //if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  612. //{
  613. // SantaNameProvided = !string.IsNullOrEmpty(santaName);
  614. //}
  615. //string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  616. //PhoneProvided = setted.Contains("P");
  617. //EmailProvided = setted.Contains("M");
  618. //switch (answer)
  619. //{
  620. //}
  621. //Debug.Log(answer);
  622. }
  623. }
  624. public void SendInviteMailToserv(string email, Action<object> onDone)
  625. {
  626. if (string.IsNullOrEmpty(email))
  627. {
  628. onDone(RegistrationStates.ContactsIsNotEnough);
  629. return;
  630. }
  631. StartCoroutine(SendInviteMail(email, onDone));
  632. }
  633. private IEnumerator SendInviteMail(string email, Action<object> onDone)
  634. {
  635. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RegScripts/SendInvite.php?uid={0}&Mail={1}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(email)));
  636. yield return www;
  637. //Debug.Log(www.url);
  638. if (!string.IsNullOrEmpty(www.error))
  639. {
  640. onDone(RegistrationStates.Error);
  641. }
  642. else
  643. {
  644. var response = SimpleJSON.JSON.Parse(www.text)["Result"].ToString();
  645. onDone(response);
  646. Debug.Log(response);
  647. //var answer = response["result"].ToString().ToLower();
  648. //Debug.Log(answer);
  649. //if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  650. //{
  651. // SantaNameProvided = !string.IsNullOrEmpty(santaName);
  652. //}
  653. //string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  654. //PhoneProvided = setted.Contains("P");
  655. //EmailProvided = setted.Contains("M");
  656. //switch (answer)
  657. //{
  658. //}
  659. //Debug.Log(answer);
  660. }
  661. }
  662. public void SendCoupon(string phone,Action<bool, string, int> onDone)
  663. {
  664. Debug.Log( " phone " + phone + " " + WWW.EscapeURL(DeviceID));
  665. StartCoroutine(SendCouponRoutine(phone,onDone));
  666. }
  667. public void GetGiftActive(string giftId, Action onDone)
  668. {
  669. StartCoroutine(SendGetGiftOnRoad(giftId, onDone));
  670. }
  671. private IEnumerator SendGetGiftOnRoad(string giftId, Action onDone)
  672. {
  673. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?gift_id={0}", giftId));
  674. yield return www;
  675. Debug.Log(www.text);
  676. if (!string.IsNullOrEmpty(www.error))
  677. {
  678. //onDone(RegistrationStates.Error);
  679. Debug.LogError("error");
  680. }
  681. else
  682. {
  683. var response = (JsonObject)Json.decode(www.text);
  684. if (response.ContainsKey("success"))
  685. {
  686. Debug.LogError("success");
  687. }
  688. else if (response.ContainsKey("success"))
  689. {
  690. Debug.LogError("success");
  691. }
  692. onDone();
  693. }
  694. }
  695. protected IEnumerator SendCouponRoutine(string phone,Action<bool, string, int> onDone)
  696. {
  697. var linq = string.Format(
  698. "https://game.gamatic.com/CokeCaps/MIX/couponToken_V3d.php?identificator={0}&phone={1}&lvl={2}&ScheduleID={3}",
  699. WWW.EscapeURL(DeviceID), phone, LevelServer, PlayerPrefs.GetInt("ScheduleID"));
  700. Debug.LogError(linq);
  701. var www = new WWW(linq);
  702. yield return www;
  703. if (!string.IsNullOrEmpty(www.error))
  704. {
  705. onDone(false, www.error,100);
  706. }
  707. else
  708. {
  709. bool done = false;
  710. var result = remSkob(www.text);
  711. var answer = ((JsonObject)Json.decode(result));
  712. if (answer.ToString().Contains("issued"))
  713. {
  714. LoginManager.Instance.BackMessageCoupone.SetActive(true);
  715. LoginManager.Instance.CouponNumberWronge.Show();
  716. }
  717. else if (answer.ToString().Contains("successful"))
  718. {
  719. done = true;
  720. }
  721. else
  722. {
  723. if (answer.ToString().Contains("successful"))
  724. {
  725. done = answer["status"].ToString().Equals("done");
  726. }
  727. }
  728. int errorCode = answer.ContainsKey("errorCode") ? int.Parse(answer["errorCode"].ToString()) : 0;
  729. string message = answer.ContainsKey("errorCode")? answer["errorDesc"].ToString():"";
  730. //bool done = answer["status"].ToString().Equals("done");
  731. onDone(done, message, errorCode);
  732. }
  733. }
  734. public void GetAgeAction(Action<object> onDone)
  735. {
  736. StartCoroutine(GetAge(onDone));
  737. }
  738. protected IEnumerator GetAge(Action<object> onDone)
  739. {
  740. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RequestCheck.php?GetAge={0}&uid={1}", 1, WWW.EscapeURL(DeviceID)));
  741. yield return www;
  742. if (!string.IsNullOrEmpty(www.error))
  743. {
  744. onDone(RegistrationStates.Error);
  745. }
  746. else
  747. {
  748. var response = www.text;
  749. if (response == 1.ToString())
  750. {
  751. LoginManager.Instance.Age = true;
  752. }
  753. onDone(response);
  754. }
  755. }
  756. public void SetAgeAction(Action<object> onDone)
  757. {
  758. Debug.Log(DeviceID);
  759. StartCoroutine(SetAge(onDone));
  760. }
  761. protected IEnumerator SetAge(Action<object> onDone)
  762. {
  763. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_dk/RequestCheck.php?SetAge={0}&uid={1}", 1, WWW.EscapeURL(DeviceID)));
  764. yield return www;
  765. if (!string.IsNullOrEmpty(www.error))
  766. {
  767. onDone(RegistrationStates.Error);
  768. }
  769. else
  770. {
  771. var response = www.text;
  772. onDone(response);
  773. }
  774. }
  775. }