SocialManager.cs 19 KB

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