ServerGiftManager.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. [System.Serializable]
  19. public class CouponsList
  20. {
  21. [SerializeField]
  22. public CouponInfo[] Coupons;
  23. }
  24. public class GiftInfo
  25. {
  26. public static GiftInfo GetInfo()
  27. {
  28. return new GiftInfo(PlayerPrefs.GetString("GiftTokenInfo",""));
  29. }
  30. public static void Save(GiftInfo info)
  31. {
  32. PlayerPrefs.SetString("GiftTokenInfo",info.ToString());
  33. }
  34. public static void Consume()
  35. {
  36. PlayerPrefs.SetString("GiftTokenInfo", "");
  37. }
  38. public bool HasToken
  39. {
  40. get { return _lastTokenDate.Date == DateTime.Today && _token > 0; }
  41. }
  42. private DateTime _lastTokenDate;
  43. private int _token;
  44. public int Token
  45. {
  46. get
  47. {
  48. if (HasToken)
  49. {
  50. return _token;
  51. }
  52. return 0;
  53. }
  54. }
  55. public GiftInfo(string source)
  56. {
  57. _lastTokenDate = DateTime.MinValue;
  58. if (!String.IsNullOrEmpty(source))
  59. {
  60. var arr = source.Split(':');
  61. _lastTokenDate = DateTime.FromBinary(Int64.Parse(arr[0]));
  62. _token = Int32.Parse(arr[1]);
  63. }
  64. }
  65. public GiftInfo(int token)
  66. {
  67. _token = token;
  68. _lastTokenDate = DateTime.Now;
  69. }
  70. public override string ToString()
  71. {
  72. if (HasToken)
  73. {
  74. return String.Format("{0}:{1}",_lastTokenDate.Date.ToBinary(),_token);
  75. }
  76. return "";
  77. }
  78. }
  79. internal void SetAge(object text, Func<object, object> p)
  80. {
  81. throw new NotImplementedException();
  82. }
  83. private void Consume(string code, Action<bool> onDone)
  84. {
  85. StartCoroutine(ConsumeRoutine(code, onDone));
  86. }
  87. public IEnumerator ConsumeRoutine(string couponCode, Action<bool> onDone)
  88. {
  89. var www = new WWW(string.Format("{0}Coupon/ConsumeCoupon.php?identificator={1}&code={2}", GameConstants.SERVER_BASE_URL, DeviceID, couponCode));
  90. yield return www;
  91. if (string.IsNullOrEmpty(www.error) && www.text.Contains("consumed"))
  92. {
  93. //FlurryLogger.Instance.CouponConsumed();
  94. GetCouponsList(() =>
  95. {
  96. onDone(true);
  97. });
  98. }
  99. else
  100. {
  101. onDone(false);
  102. }
  103. }
  104. public void GetCouponsList(Action onDone = null)
  105. {
  106. StartCoroutine(GetCouponsListRoutine(onDone));
  107. }
  108. private IEnumerator GetCouponsListRoutine(Action onDone = null)
  109. {
  110. var www = new WWW(string.Format("{0}Coupon/GetCoupons.php?identificator={1}", GameConstants.SERVER_BASE_URL, DeviceID));
  111. yield return www; ;
  112. //Debug.LogError(www.text);
  113. if (string.IsNullOrEmpty(www.error) && !string.IsNullOrEmpty(www.text.Trim()))
  114. {
  115. try
  116. {
  117. var data = UnityEngine.JsonUtility.FromJson<CouponsList>(www.text.Trim());
  118. Coupons.Clear();
  119. Coupons.AddRange(data.Coupons);
  120. foreach (var info in Coupons)
  121. {
  122. info.GetTexture(null);
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. Debug.LogException(ex);
  128. }
  129. }
  130. else
  131. {
  132. Coupons.Clear();
  133. }
  134. if (onDone != null)
  135. {
  136. onDone();
  137. }
  138. }
  139. public class CouponInfo
  140. {
  141. private static Dictionary<string, Texture2D> _texturesCache = new Dictionary<string, Texture2D>();
  142. [SerializeField]
  143. public string name;
  144. [SerializeField]
  145. public string imageUrl;
  146. [SerializeField]
  147. public string code;
  148. [SerializeField]
  149. public string expire;
  150. public void Consume(Action<bool> onDone)
  151. {
  152. ServerGiftManager.Instance.Consume(code, onDone);
  153. }
  154. public void GetTexture(Action<Texture2D> onDone)
  155. {
  156. if (_texturesCache.ContainsKey(imageUrl))
  157. {
  158. if (onDone != null)
  159. {
  160. onDone(_texturesCache[imageUrl]);
  161. }
  162. }
  163. else
  164. {
  165. Instance.StartCoroutine(Instance.LoadTexture(imageUrl, texture =>
  166. {
  167. if (texture)
  168. {
  169. if (!_texturesCache.ContainsKey(imageUrl))
  170. {
  171. _texturesCache.Add(imageUrl, texture);
  172. //Debug.LogError("TEXTURE LOADED: " + imageUrl);
  173. }
  174. }
  175. if (onDone != null)
  176. {
  177. onDone(texture);
  178. }
  179. }));
  180. }
  181. }
  182. }
  183. private IEnumerator LoadTexture(string url, Action<Texture2D> onDone)
  184. {
  185. var www = new WWW(url);
  186. yield return www;
  187. if (string.IsNullOrEmpty(www.error))
  188. {
  189. onDone(www.texture);
  190. }
  191. else
  192. {
  193. onDone(null);
  194. Debug.LogError(www.error + ":" + www.url);
  195. }
  196. }
  197. public static string ID;
  198. private static string DeviceID
  199. {
  200. get
  201. {
  202. return (SystemInfo.deviceUniqueIdentifier + ID);
  203. }
  204. }
  205. public bool PhoneProvided;// { get; private set; }
  206. public bool SantaNameProvided;// { get; private set; }
  207. public bool EmailProvided;// { get; private set; }
  208. [SerializeField]
  209. public List<CouponInfo> Coupons;
  210. public static ServerGiftManager Instance {
  211. get
  212. {
  213. if (!_instance)
  214. {
  215. _instance = new GameObject("_serverGiftManager").AddComponent<ServerGiftManager>();
  216. DontDestroyOnLoad(_instance.gameObject);
  217. }
  218. return _instance;
  219. }
  220. }
  221. private static ServerGiftManager _instance;
  222. public int LevelServer;
  223. public int GiftNum;
  224. void Awake ()
  225. {
  226. //PlayerPrefs.DeleteAll();
  227. //PlayerPrefs.SetInt("GetCoupon", 0);
  228. //PlayerPrefs.Save();
  229. if (PlayerPrefs.GetInt("Level") == 0)
  230. {
  231. PlayerPrefs.SetInt("Level", 1);
  232. }
  233. //PlayerPrefs.SetInt("FirstEnter", 0);
  234. ID = "v1_25";
  235. if (_instance && _instance != this)
  236. {
  237. Destroy(gameObject);
  238. return;
  239. }
  240. //ServerGiftManager.Instance.RegisterSantaName(Label.text, state =>
  241. //{
  242. // switch (state)
  243. // {
  244. // case ServerGiftManager.RegistrationStates.UpdateDone:
  245. // NGUIMenuScript.Instance.ShowMenu(NGUIMenuScript.NGUIMenus.Menu);
  246. // NGUITools.SetActive(this.transform.parent.gameObject, false);
  247. // Debug.Log("UpdateDone");
  248. // Label.text = "Nytt namm...";
  249. // //todo: handle - user info was updated (everything goes good)
  250. // break;
  251. // case ServerGiftManager.RegistrationStates.Old:
  252. // Debug.Log("Old");
  253. // //todo: handle - old user returns (everything goes good)
  254. // break;
  255. // case ServerGiftManager.RegistrationStates.SantaNameNotUnique:
  256. // UIMessage.LabelMessage.text = "Tyvärr är namnet redan upptaget";
  257. // UIMessage.GetComponent<ScreenBase>().Show();
  258. // Label.text = "Nytt namm...";
  259. // Debug.Log("SantaNameNotUnique");
  260. // //todo: handle - user should guess another name for santa (no data was updated!)
  261. // break;
  262. // case ServerGiftManager.RegistrationStates.IdentificatorNotIsset:
  263. // Debug.Log("IdentificatorNotIsset");
  264. // Label.text = "Nytt namm...";
  265. // //todo: handle - you must run base registration first! (no data was updated!)
  266. // break;
  267. // case ServerGiftManager.RegistrationStates.ContactsIsNotEnough:
  268. // Debug.Log("ContactsIsNotEnough");
  269. // Label.text = "Nytt namm...";
  270. // //todo: handle - santaName or phone or e-mail is empty (no data was updated!)
  271. // break;
  272. // case ServerGiftManager.RegistrationStates.Error:
  273. // UIMessage.LabelMessage.text = "Okänt fel";
  274. // UIMessage.GetComponent<ScreenBase>().Show();
  275. // Label.text = "Nytt namm...";
  276. // //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
  277. // break;
  278. // }
  279. // Debug.LogWarning(state);
  280. //});
  281. }
  282. private static void TESTCALL()
  283. {
  284. ServerGiftManager.Instance.RequestTreeUpdate(meals =>
  285. {
  286. if (meals == -1)
  287. {
  288. //todo: server error, or wrong number filled
  289. }
  290. else
  291. {
  292. //todo: handle meals amount (everything goes good)
  293. }
  294. });
  295. ServerGiftManager.Instance.BaseRegistration((state, santaName) =>
  296. {
  297. switch (state)
  298. {
  299. case RegistrationStates.New:
  300. //todo: handle - user was registered from scratch (everything goes good)
  301. break;
  302. case RegistrationStates.Old:
  303. //todo: handle - old user returns (everything goes good)
  304. break;
  305. case RegistrationStates.Error:
  306. //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
  307. break;
  308. }
  309. Debug.LogWarning(state + " " + santaName);
  310. });
  311. ServerGiftManager.Instance.FullRegistration("BestSanta", "+38 (063) 270-08-56", "m-snowy@mail.ru", state =>
  312. {
  313. switch (state)
  314. {
  315. case RegistrationStates.UpdateDone:
  316. //todo: handle - user info was updated (everything goes good)
  317. break;
  318. case RegistrationStates.Old:
  319. //todo: handle - old user returns (everything goes good)
  320. break;
  321. case RegistrationStates.SantaNameNotUnique:
  322. //todo: handle - user should guess another name for santa (no data was updated!)
  323. break;
  324. case RegistrationStates.IdentificatorNotIsset:
  325. //todo: handle - you must run base registration first! (no data was updated!)
  326. break;
  327. case RegistrationStates.ContactsIsNotEnough:
  328. //todo: handle - target field - santaName or phone or e-mail is empty (no data was updated!)
  329. break;
  330. case RegistrationStates.Error:
  331. //todo: handle - unknown error happens - maybe internet connection errors (no data was updated!)
  332. break;
  333. }
  334. Debug.LogWarning(state);
  335. });
  336. ServerGiftManager.Instance.GetGiftToken(info =>
  337. {
  338. if (info.HasToken)
  339. {
  340. //todo: handle - user achieve gift token - we can give coupon
  341. }
  342. else
  343. {
  344. //todo: handle - no token!
  345. }
  346. Debug.LogWarning(info.HasToken + " " + info.Token);
  347. });
  348. ServerGiftManager.Instance.ConsumeToken();
  349. //ServerGiftManager.Instance.SendCoupon((done, message,errorCode) =>
  350. //{
  351. // if (done)
  352. // {
  353. // //TODO coupon sent
  354. // }
  355. // else
  356. // {
  357. // switch (errorCode)
  358. // {
  359. // case 56:
  360. // //TODO handle error: deviceID is not registered
  361. // break;
  362. // case 57:
  363. // //TODO handle error: no deviceID
  364. // break;
  365. // case 58:
  366. // //TODO handle error: phone number does not provided
  367. // break;
  368. // case 59:
  369. // //TODO handle error: request error
  370. // break;
  371. // }
  372. // }
  373. //});
  374. }
  375. public void ConsumeToken()
  376. {
  377. GiftInfo.Consume();
  378. //FlurryLogger.Instance.CouponWon();
  379. }
  380. public void GetGiftToken(Action<GiftInfo> onDone)
  381. {
  382. //Debug.Log("GetGiftTokenRoutine");
  383. var info = GiftInfo.GetInfo();
  384. if (info.HasToken)
  385. {
  386. onDone(info);
  387. StartCoroutine(GetGiftTokenRoutine(info, onDone));
  388. }
  389. else
  390. {
  391. StartCoroutine(GetGiftTokenRoutine(info,onDone));
  392. }
  393. }
  394. static string remSkob(string input)
  395. {
  396. char[] arr = input.ToCharArray();
  397. char[] arr2 = new char[input.ToCharArray().Length];
  398. for (int i = 0; i < arr.Length; i++)
  399. {
  400. if (arr[i] == '{')
  401. {
  402. int k = i + 1;
  403. arr2[i] = '{';
  404. while (arr[k] != '}')
  405. {
  406. //arr[k] = '\0';
  407. arr2[k] = arr[k];
  408. k++;
  409. }
  410. arr2[k] = arr[k];
  411. }
  412. }
  413. string result = "";
  414. for (int i = 0; i < arr2.Length; i++)
  415. {
  416. if (arr2[i] != '\0')
  417. {
  418. result += arr2[i];
  419. }
  420. }
  421. return result;
  422. }
  423. public void GetCouponeInfo(Action onDone)
  424. {
  425. GiftNum = PlayerPrefs.GetInt("giftNum");
  426. if(GiftNum == 0)
  427. {
  428. return;
  429. }
  430. StartCoroutine(GetCouponeInfoRoutine(GiftNum, onDone));
  431. }
  432. private IEnumerator GetCouponeInfoRoutine(int giftNum, Action onDone)
  433. {
  434. WWW www = null;
  435. www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?giftId={0}", giftNum));
  436. yield return www;
  437. var result = remSkob(www.text);
  438. if (!string.IsNullOrEmpty(www.error))
  439. {
  440. onDone();
  441. }
  442. else
  443. {
  444. string res = "";
  445. var response = (JsonObject)Json.decode(www.text);
  446. //if (response == null)
  447. //{
  448. // res = result;
  449. //}
  450. //else
  451. //{
  452. // if (response != null)
  453. // {
  454. // res = response["response"] == null ? "" : response["response"].ToString().ToLower();
  455. // }
  456. //}
  457. var text = ((JsonObject)Json.decode(www.text))["text"].ToString();
  458. var url = ((JsonObject)Json.decode(www.text))["img"].ToString();
  459. NGUIMenuScript.Instance.LabelCoupone.text = text;
  460. WWW wwwTex = null;
  461. wwwTex = new WWW(url);
  462. yield return wwwTex;
  463. NGUIMenuScript.Instance.TextureCoupone.mainTexture = wwwTex.texture;
  464. onDone();
  465. Debug.LogError("INFO " + text + " " + url);
  466. yield return new WaitForSeconds(0.1f);
  467. }
  468. }
  469. private IEnumerator GetGiftTokenRoutine(GiftInfo def,Action<GiftInfo> onDone)
  470. {
  471. WWW www = null;
  472. if (InGameScriptCS.Instance.Level > 0 && InGameScriptCS.Instance.Level <= 2)
  473. {
  474. www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?identificator={0}&lvl={1}", DeviceID, InGameScriptCS.Instance.Level));
  475. }
  476. else
  477. {
  478. www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?id={0}", DeviceID));
  479. }
  480. yield return www;
  481. var result = remSkob(www.text);
  482. if (!string.IsNullOrEmpty(www.error))
  483. {
  484. StartCoroutine(GetGiftTokenRoutine(def, onDone));
  485. onDone(def);
  486. }
  487. else
  488. {
  489. string res = "";
  490. var response = (JsonObject)Json.decode(www.text);
  491. res = result;
  492. //if (response == null)
  493. //{
  494. // res = result;
  495. //}
  496. //else
  497. //{
  498. //if (response != null)
  499. //{
  500. // res = response["response"] == null ? "" : response["response"].ToString().ToLower();
  501. //}
  502. //}
  503. //Debug.Log(res);
  504. //Debug.Log(PlayerPrefs.GetInt("GetCoupon"));
  505. //Debug.Log(giftNum);
  506. //if (giftNum > 0)
  507. //{
  508. // Debug.Log(giftNum);
  509. // var newInfo = new GiftInfo(giftNum);
  510. // GiftInfo.Save(newInfo);
  511. // onDone(newInfo);
  512. //}
  513. //Debug.Log(res);
  514. try
  515. {
  516. if (res.Contains("giftNum"))
  517. {
  518. var respo = (JsonObject)Json.decode(res);
  519. res = "available";
  520. }
  521. }
  522. catch (Exception e)
  523. {
  524. Debug.LogError(e);
  525. }
  526. switch (res)
  527. {
  528. case "available":
  529. if (PlayerPrefs.GetInt("GetCoupon") == 0)
  530. {
  531. var token = Int32.Parse(((JsonObject)Json.decode(result))["giftNum"].ToString());
  532. GiftNum = token;
  533. PlayerPrefs.SetInt("giftNum", token);
  534. PlayerPrefs.Save();
  535. var newInfo = new GiftInfo(token);
  536. GiftInfo.Save(newInfo);
  537. onDone(newInfo);
  538. Debug.LogError("token");
  539. }
  540. yield return new WaitForSeconds(0.1f);
  541. break;
  542. case "day count error":
  543. //Debug.Log("Day Count error");
  544. var newInf = new GiftInfo(0);
  545. GiftInfo.Save(newInf);
  546. InGameScriptCS.Instance.GetComponent<ElementsGeneratorCS>().GetGift = false;
  547. onDone(newInf);
  548. yield return new WaitForSeconds(0.1f);
  549. break;
  550. default:
  551. //StartCoroutine(GetGiftTokenRoutine(def, onDone));
  552. yield return new WaitForSeconds(0.1f);
  553. break;
  554. }
  555. if (response==null)
  556. {
  557. yield return new WaitForSeconds(0.1f);
  558. }
  559. else
  560. {
  561. if (response.ContainsKey("lvl"))
  562. {
  563. StartCoroutine(GetGiftTokenRoutine(def, onDone));
  564. string lvl = response["lvl"] == null ? "" : response["lvl"].ToString();
  565. LevelServer = int.Parse(lvl);
  566. }
  567. }
  568. }
  569. }
  570. public void RequestTreeUpdate(Action<int> onDone)
  571. {
  572. StartCoroutine(RequestTreeUpdateRoutine(onDone));
  573. }
  574. private IEnumerator RequestTreeUpdateRoutine(Action<int> onDone)
  575. {
  576. var www = new WWW("https://game.gamatic.com/app_runner_is/meals.json");
  577. yield return www;
  578. if (!string.IsNullOrEmpty(www.error))
  579. {
  580. onDone(-1);
  581. }
  582. else
  583. {
  584. var response = ((JsonObject)Json.decode(www.text))["meals"].ToString().ToLower();
  585. int result = 0;
  586. if (int.TryParse(response, out result))
  587. {
  588. onDone(result);
  589. }
  590. else
  591. {
  592. onDone(-1);
  593. }
  594. }
  595. }
  596. public void RequestWeeksUpdate(Action<string> onDone)
  597. {
  598. StartCoroutine(RequestWeekUpdateRoutine(onDone));
  599. }
  600. private IEnumerator RequestWeekUpdateRoutine(Action<string> onDone)
  601. {
  602. var www = new WWW("https://game.gamatic.com/app_runner_is/week.json");
  603. yield return www;
  604. if (!string.IsNullOrEmpty(www.error))
  605. {
  606. onDone(www.error);
  607. }
  608. else
  609. {
  610. var response = www.text;
  611. int result = 0;
  612. if (!string.IsNullOrEmpty(www.text))
  613. {
  614. onDone(www.text);
  615. }
  616. else
  617. {
  618. onDone("Empty");
  619. }
  620. }
  621. }
  622. public void BaseRegistration(Action<RegistrationStates, string> onDone)
  623. {
  624. StartCoroutine(BaseRegistrationRoutine(onDone));
  625. }
  626. public IEnumerator BaseRegistrationRoutine(Action<RegistrationStates, string> onDone)
  627. {
  628. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/Register.php?identificator={0}", WWW.EscapeURL(DeviceID)));
  629. yield return www;
  630. if (!string.IsNullOrEmpty(www.error))
  631. {
  632. onDone(RegistrationStates.Error,"");
  633. }
  634. else
  635. {
  636. var response = (JsonObject)Json.decode(www.text);
  637. var answer = response["response"].ToString().ToLower();
  638. string santaName = response["name"] == null ? "" : response["name"].ToString();
  639. string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  640. SantaNameProvided = !string.IsNullOrEmpty(santaName);
  641. PhoneProvided = setted.Contains("P");
  642. EmailProvided = setted.Contains("M");
  643. switch (answer)
  644. {
  645. case "isset":
  646. onDone(RegistrationStates.Old, santaName);
  647. break;
  648. case "added":
  649. onDone(RegistrationStates.New, santaName);
  650. break;
  651. default:
  652. onDone(RegistrationStates.Error,"");
  653. break;
  654. }
  655. }
  656. }
  657. public void RegisterPhone(string phone, Action<RegistrationStates> onDone)
  658. {
  659. if (string.IsNullOrEmpty(phone))
  660. {
  661. onDone(RegistrationStates.ContactsIsNotEnough);
  662. return;
  663. }
  664. StartCoroutine(FullRegistrationRoutine("", phone.Trim(), "", onDone));
  665. }
  666. public void RegisterMail(string mail, Action<RegistrationStates> onDone)
  667. {
  668. if (string.IsNullOrEmpty(mail))
  669. {
  670. onDone(RegistrationStates.ContactsIsNotEnough);
  671. return;
  672. }
  673. StartCoroutine(FullRegistrationRoutine("", "", mail.Trim(), onDone));
  674. }
  675. public void RegisterSantaName(string santaName, Action<RegistrationStates> onDone)
  676. {
  677. if (string.IsNullOrEmpty(santaName))
  678. {
  679. onDone(RegistrationStates.ContactsIsNotEnough);
  680. return;
  681. }
  682. StartCoroutine(FullRegistrationRoutine(santaName.Trim(), "", "", onDone));
  683. }
  684. public void FullRegistration(string santaName, string phone, string email,Action<RegistrationStates> onDone)
  685. {
  686. if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(santaName))
  687. {
  688. onDone(RegistrationStates.ContactsIsNotEnough);
  689. return;
  690. }
  691. StartCoroutine(FullRegistrationRoutine(santaName,phone,email,onDone));
  692. }
  693. protected IEnumerator FullRegistrationRoutine(string santaName, string phone, string email, Action<RegistrationStates> onDone)
  694. {
  695. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/Register.php?identificator={0}&name={1}&phone={2}&email={3}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(santaName), WWW.EscapeURL(phone), WWW.EscapeURL(email)));
  696. yield return www;
  697. //Debug.Log(www.url);
  698. if (!string.IsNullOrEmpty(www.error))
  699. {
  700. onDone(RegistrationStates.Error);
  701. }
  702. else
  703. {
  704. var response = (JsonObject)Json.decode(www.text);
  705. var answer = response["response"].ToString().ToLower();
  706. if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  707. {
  708. SantaNameProvided = !string.IsNullOrEmpty(santaName);
  709. }
  710. string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  711. PhoneProvided = setted.Contains("P");
  712. EmailProvided = setted.Contains("M");
  713. switch (answer)
  714. {
  715. case "identificator not isset":
  716. onDone(RegistrationStates.IdentificatorNotIsset);
  717. break;
  718. case "update is succesfull":
  719. //Debug.Log((RegistrationManager.Instance == null) +" "+ santaName .ToString()+ " RegistrationManager.Instance is NULL!");
  720. if (RegistrationManager.Instance && !string.IsNullOrEmpty(santaName))
  721. {
  722. RegistrationManager.Instance.CurName = santaName;
  723. }
  724. else
  725. {
  726. Debug.Log("RegistrationManager.Instance is NULL!");
  727. }
  728. onDone(RegistrationStates.UpdateDone);
  729. break;
  730. case "isset":
  731. onDone(RegistrationStates.Old);
  732. break;
  733. case "namenotunique":
  734. onDone(RegistrationStates.SantaNameNotUnique);
  735. break;
  736. default:
  737. onDone(RegistrationStates.Error);
  738. break;
  739. }
  740. Debug.Log(answer);
  741. }
  742. }
  743. public void SetLevel(int lvl,Action<object> onDone)
  744. {
  745. if (lvl == 0)
  746. {
  747. return;
  748. }
  749. StartCoroutine(SendGetLevel(lvl, onDone));
  750. }
  751. private IEnumerator SendGetLevel(int lvl, Action<object> onDone)
  752. {
  753. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?identificator={0}&lvl={1}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(lvl.ToString())));
  754. yield return www;
  755. Debug.Log(www.url);
  756. if (!string.IsNullOrEmpty(www.error))
  757. {
  758. onDone(RegistrationStates.Error);
  759. }
  760. else
  761. {
  762. //var answer = response["result"].ToString().ToLower();
  763. //Debug.Log(answer);
  764. //if (string.IsNullOrEmpty(RegistrationManager.Instance.CurName))
  765. //{
  766. // SantaNameProvided = !string.IsNullOrEmpty(santaName);
  767. //}
  768. //string setted = response["Isset"] == null ? "" : response["Isset"].ToString();
  769. //PhoneProvided = setted.Contains("P");
  770. //EmailProvided = setted.Contains("M");
  771. //switch (answer)
  772. //{
  773. //}
  774. //Debug.Log(answer);
  775. }
  776. }
  777. public void GetLevel( Action<object> onDone)
  778. {
  779. StartCoroutine(SendGetLevel(onDone));
  780. }
  781. public void GetGiftActive(string giftId, Action onDone)
  782. {
  783. StartCoroutine(SendGetGiftOnRoad(giftId, onDone));
  784. }
  785. private IEnumerator SendGetGiftOnRoad(string giftId, Action onDone)
  786. {
  787. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?gift_id={0}", giftId));
  788. yield return www;
  789. Debug.Log(www.text);
  790. if (!string.IsNullOrEmpty(www.error))
  791. {
  792. //onDone(RegistrationStates.Error);
  793. Debug.LogError("error");
  794. }
  795. else
  796. {
  797. var response = (JsonObject)Json.decode(www.text);
  798. if (response.ContainsKey("success"))
  799. {
  800. Debug.LogError("success");
  801. }
  802. else if (response.ContainsKey("success"))
  803. {
  804. Debug.LogError("success");
  805. }
  806. onDone();
  807. }
  808. }
  809. private IEnumerator SendGetLevel(Action<object> onDone)
  810. {
  811. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/GetGift.php?id={0}", WWW.EscapeURL(DeviceID)));
  812. yield return www;
  813. //Debug.Log(www.url);
  814. if (!string.IsNullOrEmpty(www.error))
  815. {
  816. //onDone(RegistrationStates.Error);
  817. }
  818. else
  819. {
  820. var response = (JsonObject)Json.decode(www.text);
  821. if (response.ContainsKey("lvl"))
  822. {
  823. string lvl = response["lvl"] == null ? "" : response["lvl"].ToString();
  824. //Debug.Log(lvl);
  825. if (!string.IsNullOrEmpty(lvl))
  826. {
  827. switch (lvl)
  828. {
  829. case "null":
  830. Debug.Log("null");
  831. break;
  832. }
  833. LevelServer = int.Parse(lvl);
  834. }
  835. else
  836. {
  837. LevelServer = 0;
  838. }
  839. }
  840. else
  841. {
  842. LevelServer = 0;
  843. }
  844. onDone(LevelServer);
  845. }
  846. }
  847. public void SendInviteMailToserv(string email, Action<object> onDone)
  848. {
  849. if (string.IsNullOrEmpty(email))
  850. {
  851. onDone(RegistrationStates.ContactsIsNotEnough);
  852. return;
  853. }
  854. StartCoroutine(SendInviteMail(email, onDone));
  855. }
  856. private IEnumerator SendInviteMail(string email, Action<object> onDone)
  857. {
  858. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RegScripts/SendInvite.php?uid={0}&Mail={1}", WWW.EscapeURL(DeviceID), WWW.EscapeURL(email)));
  859. yield return www;
  860. if (!string.IsNullOrEmpty(www.error))
  861. {
  862. onDone(RegistrationStates.Error);
  863. }
  864. else
  865. {
  866. var response = SimpleJSON.JSON.Parse(www.text)["Result"].ToString();
  867. onDone(response);
  868. Debug.Log(response);
  869. }
  870. }
  871. public void SendCoupon(string phone,Action<bool, string, int> onDone)
  872. {
  873. Debug.Log( " phone " + phone + " " + WWW.EscapeURL(DeviceID));
  874. StartCoroutine(SendCouponRoutine(phone,onDone));
  875. }
  876. protected IEnumerator SendCouponRoutine(string phone,Action<bool, string, int> onDone)
  877. {
  878. var linq = string.Format(
  879. "https://game.gamatic.com/app_runner_is/RegScripts/SendPhone.php?&phone={1}&identificator={0}",
  880. WWW.EscapeURL(DeviceID), phone);
  881. var www = new WWW(linq);
  882. yield return www;
  883. if (!string.IsNullOrEmpty(www.error))
  884. {
  885. onDone(false, www.error,100);
  886. }
  887. else
  888. {
  889. bool done = false;
  890. var answer = ((JsonObject)Json.decode(www.text));
  891. if (answer.ToString().Contains("issued"))
  892. {
  893. LoginManager.Instance.BackMessageCoupone.SetActive(true);
  894. LoginManager.Instance.CouponNumberWronge.Show();
  895. }
  896. else if (answer.ToString().Contains("succesfull"))
  897. {
  898. done = true;
  899. }
  900. else
  901. {
  902. if (answer.ToString().Contains("succesfull"))
  903. {
  904. done = answer["status"].ToString().Equals("done");
  905. }
  906. }
  907. int errorCode = answer.ContainsKey("errorCode") ? int.Parse(answer["errorCode"].ToString()) : 0;
  908. string message = answer.ContainsKey("errorCode")? answer["errorDesc"].ToString():"";
  909. //bool done = answer["status"].ToString().Equals("done");
  910. onDone(done, message, errorCode);
  911. }
  912. }
  913. public void GetAgeAction(Action<object> onDone)
  914. {
  915. StartCoroutine(GetAge(onDone));
  916. }
  917. protected IEnumerator GetAge(Action<object> onDone)
  918. {
  919. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RequestCheck.php?GetAge={0}&uid={1}", 1, WWW.EscapeURL(DeviceID)));
  920. yield return www;
  921. if (!string.IsNullOrEmpty(www.error))
  922. {
  923. onDone(RegistrationStates.Error);
  924. }
  925. else
  926. {
  927. var response = www.text;
  928. if (response == 1.ToString())
  929. {
  930. LoginManager.Instance.Age = true;
  931. }
  932. onDone(response);
  933. }
  934. }
  935. public void SetAgeAction(Action<object> onDone)
  936. {
  937. Debug.Log(DeviceID);
  938. StartCoroutine(SetAge(onDone));
  939. }
  940. protected IEnumerator SetAge(Action<object> onDone)
  941. {
  942. var www = new WWW(string.Format("https://game.gamatic.com/app_runner_is/RequestCheck.php?SetAge={0}&uid={1}",1, WWW.EscapeURL(DeviceID)));
  943. yield return www;
  944. if (!string.IsNullOrEmpty(www.error))
  945. {
  946. onDone(RegistrationStates.Error);
  947. }
  948. else
  949. {
  950. var response = www.text;
  951. onDone(response);
  952. }
  953. }
  954. }