ManaSocial.cs 16 KB

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