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