ManaSocial.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using System.Collections;
  6. using System.Diagnostics;
  7. using System.Collections.Generic;
  8. using LitJson;
  9. using Debug = UnityEngine.Debug;
  10. public class ManaSocial : Regist
  11. {
  12. #region Variable
  13. public static bool RankRecordFlag;
  14. public static int CommentRecordLastIndex;
  15. public static int CommentRecordFirstIndex;
  16. public static bool CommentRecordFlag;
  17. public static Vector3 CommentRecordPosition;
  18. public static bool IsPlayer;
  19. public static bool PullLock;
  20. public static bool UpdateLock;
  21. public static bool IsPanelOpen;
  22. public static int PlayerLastIndex = -1;
  23. public static int VisiteeLastIndex = -1;
  24. public static int PlayerFirstIndex;
  25. public static int VisiteeFirstIndex;
  26. public static int PlayerCommentPage;
  27. public static int VisiteeCommentPage;
  28. public static Text PraiseText;
  29. public static Button PraiseBtn;
  30. public static DateTime CommentTime;
  31. public static InputField InputField;
  32. public static ScrollRectPlus ScrollRectPlus;
  33. public static float RankTime = 5;
  34. public static float RankTimer = 0;
  35. public static float PraiseTime = 10;
  36. public static float PraiseTimer = 5;
  37. public static float CommentTimespan = 60;
  38. public static float CommentTimer = 0;
  39. public static List<CommentData> PlayerCommentDatas = new List<CommentData>();
  40. public static List<CommentData> VisiteeCommentDatas = new List<CommentData>();
  41. #endregion
  42. public void Update()
  43. {
  44. if (!ManaServer.RankReady)
  45. {
  46. RankTimer += Time.deltaTime;
  47. if (RankTimer >= RankTime)
  48. {
  49. RankTimer = 0;
  50. ManaServer.RankRequest();
  51. }
  52. }
  53. if (ManaVisit.InVisit)
  54. {
  55. return;
  56. }
  57. //CommentTimer += Time.deltaTime;
  58. //if (CommentTimer >= CommentTimespan)
  59. //{
  60. // CommentTimer = 0;
  61. // if (!IsPanelOpen)
  62. // {
  63. // UpdatePage(true);
  64. // }
  65. //}
  66. PraiseTimer += Time.deltaTime;
  67. if (PraiseTimer >= PraiseTime)
  68. {
  69. //index += 1;
  70. //Debug.Log(index);
  71. //ManaServer.AddComment(ManaServer.SerialNumber, ManaServer.SerialNumber, index.ToString(), CommentType.Garden);
  72. PraiseTimer = 0;
  73. PraiseText.text = ManaServer.PraiseAmt.ToString();
  74. }
  75. }
  76. public override void RegistValueA()
  77. {
  78. PraiseText = ManaReso.Get<Text>("C_PraiseText");
  79. PraiseBtn = ManaReso.Get<Button>("C_Praise");
  80. InputField = ManaReso.Get<InputField>("Q_InputField");
  81. ScrollRectPlus = ManaReso.Get<ScrollRectPlus>("Q_ScrollRect");
  82. ScrollRectPlus.onValueChanged.AddListener(OnScroll);
  83. }
  84. public static void Praise()
  85. {
  86. if (ManaVisit.InVisit)
  87. {
  88. PraiseText.text = (int.Parse(PraiseText.text) + 1).ToString();
  89. ManaServer.Praise(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber);
  90. DisablePraise();
  91. }
  92. else
  93. {
  94. throw new Exception();
  95. }
  96. }
  97. public static void EnablePraise()
  98. {
  99. PraiseBtn.image.color = Color.white;
  100. PraiseBtn.interactable = true;
  101. }
  102. public static void DisablePraise()
  103. {
  104. PraiseBtn.image.color = Lib.Pink;
  105. PraiseBtn.interactable = false;
  106. }
  107. public static void InputCommentData(bool isPlayer, JsonData jsonData)
  108. {
  109. Auxiliary.Instance.DelayCall
  110. (
  111. () =>
  112. {
  113. PullLock = false;
  114. },
  115. 1
  116. );
  117. if (!jsonData.Inst_Object.ContainsKey("l"))
  118. {
  119. return;
  120. }
  121. if (jsonData["l"].Count == 0)
  122. {
  123. return;
  124. }
  125. if (isPlayer)
  126. {
  127. PlayerCommentPage++;
  128. foreach (JsonData commentData in jsonData["l"])
  129. {
  130. PlayerCommentDatas.UniqueAdd(new CommentData(commentData));
  131. }
  132. }
  133. else
  134. {
  135. VisiteeCommentPage++;
  136. foreach (JsonData commentData in jsonData["l"])
  137. {
  138. VisiteeCommentDatas.UniqueAdd(new CommentData(commentData));
  139. }
  140. }
  141. }
  142. public static void NextPage(bool isPlayer, bool isDownload)
  143. {
  144. //Debug.Log("NextPage");
  145. if (isPlayer)
  146. {
  147. int saveAmt = 0;
  148. int updateAmt = Mathf.Min(15, PlayerCommentDatas.Count - PlayerLastIndex - 1);
  149. for (int i = 0; i < updateAmt; i++)
  150. {
  151. if (ScrollRectPlus.content.childCount >= 20)
  152. {
  153. saveAmt++;
  154. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  155. PlayerFirstIndex++;
  156. }
  157. PlayerLastIndex++;
  158. CommentData commentData = PlayerCommentDatas[PlayerLastIndex];
  159. ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  160. }
  161. int offset = isDownload ? 1 : 0;
  162. float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
  163. if (saveAmt > 0)
  164. {
  165. ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset)*120*scaleFactor, 0);
  166. ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset)*120*scaleFactor, 0));
  167. }
  168. }
  169. else
  170. {
  171. int saveAmt = 0;
  172. int updateAmt = Mathf.Min(15, VisiteeCommentDatas.Count - VisiteeLastIndex - 1);
  173. for (int i = 0; i < updateAmt; i++)
  174. {
  175. if (ScrollRectPlus.content.childCount >= 20)
  176. {
  177. saveAmt++;
  178. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  179. VisiteeFirstIndex++;
  180. }
  181. VisiteeLastIndex++;
  182. CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
  183. ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  184. }
  185. int offset = isDownload ? 1 : 0;
  186. float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
  187. if (saveAmt > 0)
  188. {
  189. ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset)*120*scaleFactor, 0);
  190. ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset)*120*scaleFactor, 0));
  191. }
  192. }
  193. }
  194. public static void PreviousPage(bool isPlayer)
  195. {
  196. if (ScrollRectPlus.content.childCount == 0)
  197. {
  198. return;
  199. }
  200. //Debug.Log("PreviousPage");
  201. if (isPlayer)
  202. {
  203. if (PlayerCommentDatas.Count > 20 && PlayerFirstIndex > 0)
  204. {
  205. int revertAmt = Mathf.Min(15, PlayerFirstIndex);
  206. for (int i = 0; i < revertAmt; i++)
  207. {
  208. ManaReso.Save(ScrollRectPlus.content.GetChild(ScrollRectPlus.content.childCount - 1));
  209. CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
  210. CommentItem commentItem = ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  211. commentItem.transform.SetAsFirstSibling();
  212. PlayerLastIndex--;
  213. PlayerFirstIndex--;
  214. }
  215. if (revertAmt > 0)
  216. {
  217. ScrollRectPlus.content.position += new Vector3(0, revertAmt*120, 0);
  218. ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt*120, 0));
  219. }
  220. }
  221. }
  222. else
  223. {
  224. if (VisiteeCommentDatas.Count > 20 && VisiteeFirstIndex > 0)
  225. {
  226. int revertAmt = Mathf.Min(15, VisiteeFirstIndex);
  227. for (int i = 0; i < revertAmt; i++)
  228. {
  229. ManaReso.Save(ScrollRectPlus.content.GetChild(ScrollRectPlus.content.childCount - 1));
  230. CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
  231. CommentItem commentItem = ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  232. commentItem.transform.SetAsFirstSibling();
  233. VisiteeLastIndex--;
  234. VisiteeFirstIndex--;
  235. }
  236. if (revertAmt > 0)
  237. {
  238. ScrollRectPlus.content.position += new Vector3(0, revertAmt*120, 0);
  239. ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt*120, 0));
  240. }
  241. }
  242. }
  243. }
  244. public static void PullPage(bool isPlayer)
  245. {
  246. PullLock = true;
  247. UpdateLock = false;
  248. //Debug.Log("PullPage");
  249. if (isPlayer)
  250. {
  251. ManaServer.GetComment
  252. (
  253. ManaServer.SerialNumber,
  254. (PlayerCommentPage + 1).ToString(),
  255. CommentType.Garden,
  256. data =>
  257. {
  258. Debug.LogWarning("callback");
  259. if (!UpdateLock)
  260. {
  261. InputCommentData(IsPlayer, data);
  262. NextPage(IsPlayer, true);
  263. }
  264. else
  265. {
  266. PullLock = false;
  267. }
  268. }
  269. );
  270. }
  271. else
  272. {
  273. ManaServer.GetComment
  274. (
  275. ManaServer.SerialNumber,
  276. (VisiteeCommentPage + 1).ToString(),
  277. CommentType.Garden,
  278. data =>
  279. {
  280. Debug.LogWarning("callback");
  281. if (!UpdateLock)
  282. {
  283. InputCommentData(IsPlayer, data);
  284. NextPage(IsPlayer, true);
  285. }
  286. else
  287. {
  288. PullLock = false;
  289. }
  290. }
  291. );
  292. }
  293. }
  294. public static void ClearComment()
  295. {
  296. int childAmt = ScrollRectPlus.content.childCount;
  297. for (int i = 0; i < childAmt; i++)
  298. {
  299. ManaReso.Save(ScrollRectPlus.content.GetChild(0));
  300. }
  301. ScrollRectPlus.verticalNormalizedPosition = 1;
  302. }
  303. public static void UpdatePage(bool isPlayer)
  304. {
  305. UpdateLock = true;
  306. ClearComment();
  307. ManaReso.Get("Q_Tip").TweenForCG();
  308. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "Loading"));
  309. if (isPlayer)
  310. {
  311. //Debug.Log("UpdatePlayer");
  312. PlayerFirstIndex = 0;
  313. PlayerLastIndex = -1;
  314. PlayerCommentPage = 0;
  315. PlayerCommentDatas = new List<CommentData>();
  316. ManaServer.GetComment
  317. (
  318. ManaServer.SerialNumber,
  319. "1",
  320. CommentType.Garden,
  321. data =>
  322. {
  323. Debug.LogWarning("callback");
  324. if (!ManaVisit.InVisit)
  325. {
  326. DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
  327. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
  328. InputCommentData(true, data);
  329. NextPage(true, true);
  330. ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
  331. DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
  332. }
  333. }
  334. );
  335. }
  336. else
  337. {
  338. //Debug.Log("UpdateVisitee");
  339. VisiteeFirstIndex = 0;
  340. VisiteeLastIndex = -1;
  341. VisiteeCommentPage = 0;
  342. VisiteeCommentDatas = new List<CommentData>();
  343. ManaServer.GetComment
  344. (
  345. ManaVisit.VisiteeSerialNumber,
  346. "1",
  347. CommentType.Garden,
  348. data =>
  349. {
  350. if (ManaVisit.InVisit)
  351. {
  352. DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
  353. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
  354. InputCommentData(false, data);
  355. NextPage(false, true);
  356. ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
  357. DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
  358. }
  359. }
  360. );
  361. }
  362. }
  363. public static void OnScroll(Vector2 position)
  364. {
  365. if (PullLock)
  366. {
  367. return;
  368. }
  369. if (ScrollRectPlus.content.childCount == 0)
  370. {
  371. return;
  372. }
  373. if (position.y >= 1)
  374. {
  375. PreviousPage(IsPlayer);
  376. }
  377. if (position.y <= 0)
  378. {
  379. if (IsPlayer)
  380. {
  381. if (PlayerLastIndex == PlayerCommentDatas.Count - 1)
  382. {
  383. PullPage(true);
  384. }
  385. else if (PlayerLastIndex < PlayerCommentDatas.Count - 1)
  386. {
  387. NextPage(true, false);
  388. }
  389. }
  390. else
  391. {
  392. if (VisiteeLastIndex == VisiteeCommentDatas.Count - 1)
  393. {
  394. PullPage(false);
  395. }
  396. else if (VisiteeLastIndex < VisiteeCommentDatas.Count - 1)
  397. {
  398. NextPage(false, false);
  399. }
  400. }
  401. }
  402. }
  403. public static void OpenRankPanel()
  404. {
  405. ManaAudio.PlayClip(Clip.BubbleClip);
  406. ManaReso.Get("S_RankBK").TweenForCG();
  407. }
  408. public static void CloseRankPanel()
  409. {
  410. ManaAudio.PlayClip(Clip.BtnClip);
  411. ManaReso.Get("S_RankBK").TweenBacCG();
  412. }
  413. public static void RecordRankPanel()
  414. {
  415. RankRecordFlag = true;
  416. }
  417. public static void RecoverRankPanel()
  418. {
  419. if (RankRecordFlag)
  420. {
  421. RankRecordFlag = false;
  422. OpenRankPanel();
  423. }
  424. }
  425. public static void InitializeRankPanel()
  426. {
  427. if (Initializer.Complete && ManaServer.RankReady)
  428. {
  429. for (int i = 0; i < ManaServer.RankDatas.Count; i++)
  430. {
  431. ManaReso.GetRanktem((i + 1).ToString(), ManaServer.RankDatas[i][1].ToString(), ManaServer.RankDatas[i][0].ToString());
  432. }
  433. }
  434. }
  435. public static void Comment()
  436. {
  437. if (string.IsNullOrEmpty(InputField.text))
  438. {
  439. Bubble.Show(Language.GetStr("UI", "Lb_Send2"));
  440. return;
  441. }
  442. if ((DateTime.Now - CommentTime).TotalSeconds > 30)
  443. {
  444. CommentTime = DateTime.Now;
  445. string content = StringFilter.GetFilteredString(InputField.text);
  446. ManaServer.AddComment(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber, content, CommentType.Garden);
  447. }
  448. else
  449. {
  450. Bubble.Show(Language.GetStr("UI", "Q_CommentTip"));
  451. }
  452. }
  453. public static void OpenCommentPanel()
  454. {
  455. IsPlayer = !ManaVisit.InVisit;
  456. if (IsPlayer)
  457. {
  458. UpdatePage(IsPlayer);
  459. }
  460. else
  461. {
  462. if (string.IsNullOrEmpty(ManaVisit.VisiteeSerialNumber))
  463. {
  464. Bubble.Show(null, Language.GetStr("UI", "C_CannotComment"));
  465. return;
  466. }
  467. else
  468. {
  469. UpdatePage(IsPlayer);
  470. }
  471. }
  472. IsPanelOpen = true;
  473. ManaReso.Get("Q_CommentBK").TweenForCG();
  474. ManaReso.SetActive("Q_InputBK", ManaVisit.InVisit);
  475. }
  476. public static void CloseCommentPanel()
  477. {
  478. ManaReso.Get("Q_CommentBK").TweenBacCG();
  479. IsPanelOpen = false;
  480. }
  481. public static void RecordCommentPanel()
  482. {
  483. if (IsPlayer)
  484. {
  485. CommentRecordFlag = true;
  486. CommentRecordLastIndex = PlayerLastIndex;
  487. CommentRecordFirstIndex = PlayerFirstIndex;
  488. CommentRecordPosition = ScrollRectPlus.content.transform.position;
  489. }
  490. }
  491. public static void RecoverCommentPanel()
  492. {
  493. if (CommentRecordFlag)
  494. {
  495. CommentRecordFlag = false;
  496. ClearComment();
  497. PlayerLastIndex = -1;
  498. PlayerFirstIndex = 0;
  499. int antiCrush = 0;
  500. while (PlayerFirstIndex != CommentRecordFirstIndex || PlayerLastIndex != CommentRecordLastIndex)
  501. {
  502. if (antiCrush > 10000)
  503. {
  504. throw new Exception("Crush");
  505. }
  506. if (ScrollRectPlus.content.childCount >= 20)
  507. {
  508. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  509. PlayerFirstIndex++;
  510. }
  511. PlayerLastIndex++;
  512. CommentData commentData = PlayerCommentDatas[PlayerLastIndex];
  513. ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  514. }
  515. ScrollRectPlus.content.transform.position = CommentRecordPosition;
  516. OpenCommentPanel();
  517. }
  518. }
  519. }