SocialManager.cs 18 KB

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