ManaSocial.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. if (!UpdateLock)
  259. {
  260. InputCommentData(IsPlayer, data);
  261. NextPage(IsPlayer, true);
  262. }
  263. else
  264. {
  265. PullLock = false;
  266. }
  267. }
  268. );
  269. }
  270. else
  271. {
  272. ManaServer.GetComment
  273. (
  274. ManaServer.SerialNumber,
  275. (VisiteeCommentPage + 1).ToString(),
  276. CommentType.Garden,
  277. data =>
  278. {
  279. if (!UpdateLock)
  280. {
  281. InputCommentData(IsPlayer, data);
  282. NextPage(IsPlayer, true);
  283. }
  284. else
  285. {
  286. PullLock = false;
  287. }
  288. }
  289. );
  290. }
  291. }
  292. public static void ClearComment()
  293. {
  294. int childAmt = ScrollRectPlus.content.childCount;
  295. for (int i = 0; i < childAmt; i++)
  296. {
  297. ManaReso.Save(ScrollRectPlus.content.GetChild(0));
  298. }
  299. ScrollRectPlus.verticalNormalizedPosition = 1;
  300. }
  301. public static void UpdatePage(bool isPlayer)
  302. {
  303. UpdateLock = true;
  304. ClearComment();
  305. ManaReso.Get("Q_Tip").TweenForCG();
  306. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "Loading"));
  307. if (isPlayer)
  308. {
  309. //Debug.Log("UpdatePlayer");
  310. PlayerFirstIndex = 0;
  311. PlayerLastIndex = -1;
  312. PlayerCommentPage = 0;
  313. PlayerCommentDatas = new List<CommentData>();
  314. ManaServer.GetComment
  315. (
  316. ManaServer.SerialNumber,
  317. "1",
  318. CommentType.Garden,
  319. data =>
  320. {
  321. if (!ManaVisit.InVisit)
  322. {
  323. DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
  324. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
  325. InputCommentData(true, data);
  326. NextPage(true, true);
  327. ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
  328. DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
  329. }
  330. }
  331. );
  332. }
  333. else
  334. {
  335. //Debug.Log("UpdateVisitee");
  336. VisiteeFirstIndex = 0;
  337. VisiteeLastIndex = -1;
  338. VisiteeCommentPage = 0;
  339. VisiteeCommentDatas = new List<CommentData>();
  340. ManaServer.GetComment
  341. (
  342. ManaVisit.VisiteeSerialNumber,
  343. "1",
  344. CommentType.Garden,
  345. data =>
  346. {
  347. if (ManaVisit.InVisit)
  348. {
  349. DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
  350. ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
  351. InputCommentData(false, data);
  352. NextPage(false, true);
  353. ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
  354. DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
  355. }
  356. }
  357. );
  358. }
  359. }
  360. public static void OnScroll(Vector2 position)
  361. {
  362. if (PullLock)
  363. {
  364. return;
  365. }
  366. if (ScrollRectPlus.content.childCount == 0)
  367. {
  368. return;
  369. }
  370. if (position.y >= 1)
  371. {
  372. PreviousPage(IsPlayer);
  373. }
  374. if (position.y <= 0)
  375. {
  376. if (IsPlayer)
  377. {
  378. if (PlayerLastIndex == PlayerCommentDatas.Count - 1)
  379. {
  380. PullPage(true);
  381. }
  382. else if (PlayerLastIndex < PlayerCommentDatas.Count - 1)
  383. {
  384. NextPage(true, false);
  385. }
  386. }
  387. else
  388. {
  389. if (VisiteeLastIndex == VisiteeCommentDatas.Count - 1)
  390. {
  391. PullPage(false);
  392. }
  393. else if (VisiteeLastIndex < VisiteeCommentDatas.Count - 1)
  394. {
  395. NextPage(false, false);
  396. }
  397. }
  398. }
  399. }
  400. public static void OpenRankPanel()
  401. {
  402. ManaAudio.PlayClip(Clip.BubbleClip);
  403. ManaReso.Get("S_RankBK").TweenForCG();
  404. }
  405. public static void CloseRankPanel()
  406. {
  407. ManaAudio.PlayClip(Clip.BtnClip);
  408. ManaReso.Get("S_RankBK").TweenBacCG();
  409. }
  410. public static void RecordRankPanel()
  411. {
  412. RankRecordFlag = true;
  413. }
  414. public static void RecoverRankPanel()
  415. {
  416. if (RankRecordFlag)
  417. {
  418. RankRecordFlag = false;
  419. OpenRankPanel();
  420. }
  421. }
  422. public static void InitializeRankPanel()
  423. {
  424. if (Initializer.Complete && ManaServer.RankReady)
  425. {
  426. for (int i = 0; i < ManaServer.RankDatas.Count; i++)
  427. {
  428. ManaReso.GetRanktem((i + 1).ToString(), ManaServer.RankDatas[i][1].ToString(), ManaServer.RankDatas[i][0].ToString());
  429. }
  430. }
  431. }
  432. public static void Comment()
  433. {
  434. if (string.IsNullOrEmpty(InputField.text))
  435. {
  436. Bubble.Show(Language.GetStr("UI", "Lb_Send2"));
  437. return;
  438. }
  439. if ((DateTime.Now - CommentTime).TotalSeconds > 30)
  440. {
  441. CommentTime = DateTime.Now;
  442. string content = StringFilter.GetFilteredString(InputField.text);
  443. ManaServer.AddComment(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber, content, CommentType.Garden);
  444. InputField.text = "";
  445. }
  446. else
  447. {
  448. Bubble.Show(Language.GetStr("UI", "Q_CommentTip"));
  449. }
  450. }
  451. public static void OpenCommentPanel()
  452. {
  453. IsPlayer = !ManaVisit.InVisit;
  454. if (IsPlayer)
  455. {
  456. UpdatePage(IsPlayer);
  457. }
  458. else
  459. {
  460. if (string.IsNullOrEmpty(ManaVisit.VisiteeSerialNumber))
  461. {
  462. Bubble.Show(null, Language.GetStr("UI", "C_CannotComment"));
  463. return;
  464. }
  465. else
  466. {
  467. UpdatePage(IsPlayer);
  468. }
  469. }
  470. IsPanelOpen = true;
  471. ManaReso.Get("Q_CommentBK").TweenForCG();
  472. ManaReso.SetActive("Q_InputBK", ManaVisit.InVisit);
  473. }
  474. public static void CloseCommentPanel()
  475. {
  476. ManaReso.Get("Q_CommentBK").TweenBacCG();
  477. IsPanelOpen = false;
  478. }
  479. public static void RecordCommentPanel()
  480. {
  481. if (IsPlayer)
  482. {
  483. CommentRecordFlag = true;
  484. CommentRecordLastIndex = PlayerLastIndex;
  485. CommentRecordFirstIndex = PlayerFirstIndex;
  486. CommentRecordPosition = ScrollRectPlus.content.transform.position;
  487. }
  488. }
  489. public static void RecoverCommentPanel()
  490. {
  491. if (CommentRecordFlag)
  492. {
  493. CommentRecordFlag = false;
  494. ClearComment();
  495. PlayerLastIndex = -1;
  496. PlayerFirstIndex = 0;
  497. int antiCrush = 0;
  498. while (PlayerFirstIndex != CommentRecordFirstIndex || PlayerLastIndex != CommentRecordLastIndex)
  499. {
  500. if (antiCrush > 10000)
  501. {
  502. throw new Exception("Crush");
  503. }
  504. if (ScrollRectPlus.content.childCount >= 20)
  505. {
  506. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  507. PlayerFirstIndex++;
  508. }
  509. PlayerLastIndex++;
  510. CommentData commentData = PlayerCommentDatas[PlayerLastIndex];
  511. ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
  512. }
  513. ScrollRectPlus.content.transform.position = CommentRecordPosition;
  514. OpenCommentPanel();
  515. }
  516. }
  517. }