ManaSocial.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using LitJson;
  8. public class ManaSocial : Regist
  9. {
  10. #region Variable
  11. public static int RecordLastIndex;
  12. public static int RecordFirstIndex;
  13. public static bool RecordFlag;
  14. public static Vector3 RecordPosition;
  15. public static bool IsPlayer;
  16. public static bool PullLock;
  17. public static bool UpdateLock;
  18. public static bool IsPanelOpen;
  19. public static int PlayerLastIndex=-1;
  20. public static int VisiteeLastIndex=-1;
  21. public static int PlayerFirstIndex;
  22. public static int VisiteeFirstIndex;
  23. public static int PlayerCommentPage;
  24. public static int VisiteeCommentPage;
  25. public static Text PraiseText;
  26. public static Button PraiseBtn;
  27. public static DateTime CommentTime;
  28. public static InputField InputField;
  29. public static ScrollRectPlus ScrollRectPlus;
  30. public static float PraiseTimer = 5;
  31. public static float CommentTimer = 0;
  32. public static List<List<string>> PlayerCommentDatas = new List<List<string>>();
  33. public static List<List<string>> VisiteeCommentDatas = new List<List<string>>();
  34. #endregion
  35. public void Update()
  36. {
  37. if (ManaVisit.InVisit)
  38. {
  39. return;
  40. }
  41. CommentTimer += Time.deltaTime;
  42. if (CommentTimer >= 10)
  43. {
  44. CommentTimer = 0;
  45. if (!IsPanelOpen)
  46. {
  47. UpdatePage(true);
  48. }
  49. }
  50. PraiseTimer += Time.deltaTime;
  51. if (PraiseTimer >= 5)
  52. {
  53. //index += 1;
  54. //Debug.Log(index);
  55. //ManaServer.AddComment(ManaServer.SerialNumber, ManaServer.SerialNumber, index.ToString(), CommentType.Garden);
  56. PraiseTimer = 0;
  57. PraiseText.text = ManaServer.PraiseAmt.ToString();
  58. }
  59. }
  60. public override void RegistValueA()
  61. {
  62. PraiseText = ManaReso.Get<Text>("C_PraiseText");
  63. PraiseBtn = ManaReso.Get<Button>("C_Praise");
  64. InputField = ManaReso.Get<InputField>("Q_InputField");
  65. ScrollRectPlus = ManaReso.Get<ScrollRectPlus>("Q_ScrollRect");
  66. ScrollRectPlus.onValueChanged.AddListener(OnScroll);
  67. }
  68. public static void Praise()
  69. {
  70. if (ManaVisit.InVisit)
  71. {
  72. PraiseText.text = (int.Parse(PraiseText.text) + 1).ToString();
  73. ManaServer.Praise(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber);
  74. DisablePraise();
  75. }
  76. else
  77. {
  78. DisablePraise();
  79. }
  80. }
  81. public static void EnablePraise()
  82. {
  83. PraiseBtn.image.color = Color.white;
  84. PraiseBtn.interactable = true;
  85. }
  86. public static void DisablePraise()
  87. {
  88. PraiseBtn.image.color = Lib.Pink;
  89. PraiseBtn.interactable = false;
  90. }
  91. public static void InputCommentData(bool isPlayer, JsonData jsonData)
  92. {
  93. Auxiliary.Instance.DelayCall
  94. (
  95. () =>
  96. {
  97. PullLock = false;
  98. },
  99. 1
  100. );
  101. if (!jsonData.Inst_Object.ContainsKey("l"))
  102. {
  103. return;
  104. }
  105. if (jsonData["l"].Count == 0)
  106. {
  107. return;
  108. }
  109. if (isPlayer)
  110. {
  111. PlayerCommentPage++;
  112. foreach (JsonData commentData in jsonData["l"])
  113. {
  114. PlayerCommentDatas.Add(new List<string>() { commentData["s"].ToString(), commentData["c"].ToString() });
  115. }
  116. }
  117. else
  118. {
  119. VisiteeCommentPage++;
  120. foreach (JsonData commentData in jsonData["l"])
  121. {
  122. VisiteeCommentDatas.Add(new List<string>() { commentData["s"].ToString(), commentData["c"].ToString() });
  123. }
  124. }
  125. }
  126. public static void NextPage(bool isPlayer, bool isDownload)
  127. {
  128. Debug.Log("NextPage");
  129. if (isPlayer)
  130. {
  131. int saveAmt = 0;
  132. int updateAmt = Mathf.Min(15, PlayerCommentDatas.Count - PlayerLastIndex - 1);
  133. for (int i = 0; i < updateAmt; i++)
  134. {
  135. if (ScrollRectPlus.content.childCount >= 20)
  136. {
  137. saveAmt++;
  138. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  139. PlayerFirstIndex++;
  140. }
  141. PlayerLastIndex++;
  142. ManaReso.GetCommentItem(PlayerCommentDatas[PlayerLastIndex][0], PlayerCommentDatas[PlayerLastIndex][1]);
  143. }
  144. int offset = isDownload ? 1 : 0;
  145. float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
  146. if (saveAmt > 0)
  147. {
  148. ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0);
  149. ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0));
  150. }
  151. }
  152. else
  153. {
  154. int saveAmt = 0;
  155. int updateAmt = Mathf.Min(15, VisiteeCommentDatas.Count - VisiteeLastIndex - 1);
  156. for (int i = 0; i < updateAmt; i++)
  157. {
  158. if (ScrollRectPlus.content.childCount >= 20)
  159. {
  160. saveAmt++;
  161. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  162. VisiteeFirstIndex++;
  163. }
  164. VisiteeLastIndex++;
  165. ManaReso.GetCommentItem(VisiteeCommentDatas[VisiteeLastIndex][0], VisiteeCommentDatas[VisiteeLastIndex][1]);
  166. }
  167. int offset = isDownload ? 1 : 0;
  168. float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
  169. if (saveAmt > 0)
  170. {
  171. ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0);
  172. ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0));
  173. }
  174. }
  175. }
  176. public static void PreviousPage(bool isPlayer)
  177. {
  178. if (isPlayer)
  179. {
  180. if (PlayerCommentDatas.Count > 20 && PlayerFirstIndex > 0)
  181. {
  182. int revertAmt = Mathf.Min(15, PlayerFirstIndex);
  183. Debug.Log("PreviousPage");
  184. for (int i = 0; i < revertAmt; i++)
  185. {
  186. ManaReso.Save(ScrollRectPlus.content.GetChild(19));
  187. CommentItem commentItem = ManaReso.GetCommentItem(PlayerCommentDatas[PlayerFirstIndex - 1][0], PlayerCommentDatas[PlayerFirstIndex - 1][1]);
  188. commentItem.transform.SetAsFirstSibling();
  189. PlayerLastIndex--;
  190. PlayerFirstIndex--;
  191. }
  192. if (revertAmt > 0)
  193. {
  194. ScrollRectPlus.content.position += new Vector3(0, revertAmt*120, 0);
  195. ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt*120, 0));
  196. }
  197. }
  198. }
  199. else
  200. {
  201. if (VisiteeCommentDatas.Count > 20 && VisiteeFirstIndex > 0)
  202. {
  203. int revertAmt = Mathf.Min(15, VisiteeFirstIndex);
  204. Debug.Log("Revertpage");
  205. for (int i = 0; i < revertAmt; i++)
  206. {
  207. ManaReso.Save(ScrollRectPlus.content.GetChild(19));
  208. CommentItem commentItem = ManaReso.GetCommentItem(VisiteeCommentDatas[VisiteeFirstIndex - 1][0], VisiteeCommentDatas[VisiteeFirstIndex - 1][1]);
  209. commentItem.transform.SetAsFirstSibling();
  210. VisiteeLastIndex--;
  211. VisiteeFirstIndex--;
  212. }
  213. if (revertAmt > 0)
  214. {
  215. ScrollRectPlus.content.position += new Vector3(0, revertAmt * 120, 0);
  216. ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt * 120, 0));
  217. }
  218. }
  219. }
  220. }
  221. public static void PullPage(bool isPlayer)
  222. {
  223. PullLock = true;
  224. UpdateLock = false;
  225. Debug.Log("PullPage");
  226. if (isPlayer)
  227. {
  228. ManaServer.GetComment
  229. (
  230. ManaServer.SerialNumber,
  231. (PlayerCommentPage + 1).ToString(),
  232. CommentType.Garden,
  233. data =>
  234. {
  235. if (!UpdateLock)
  236. {
  237. InputCommentData(IsPlayer, data);
  238. NextPage(IsPlayer, true);
  239. }
  240. else
  241. {
  242. PullLock = false;
  243. }
  244. }
  245. );
  246. }
  247. else
  248. {
  249. ManaServer.GetComment
  250. (
  251. ManaServer.SerialNumber,
  252. (VisiteeCommentPage + 1).ToString(),
  253. CommentType.Garden,
  254. data =>
  255. {
  256. if (!UpdateLock)
  257. {
  258. InputCommentData(IsPlayer, data);
  259. NextPage(IsPlayer, true);
  260. }
  261. else
  262. {
  263. PullLock = false;
  264. }
  265. }
  266. );
  267. }
  268. }
  269. public static void ClearComment()
  270. {
  271. int childAmt = ScrollRectPlus.content.childCount;
  272. for (int i = 0; i < childAmt; i++)
  273. {
  274. ManaReso.Save(ScrollRectPlus.content.GetChild(0));
  275. }
  276. ScrollRectPlus.verticalNormalizedPosition = 1;
  277. }
  278. public static void UpdatePage(bool isPlayer)
  279. {
  280. UpdateLock = true;
  281. ClearComment();
  282. if (isPlayer)
  283. {
  284. Debug.Log("UpdatePlayer");
  285. PlayerFirstIndex = 0;
  286. PlayerLastIndex = -1;
  287. PlayerCommentPage = 0;
  288. PlayerCommentDatas = new List<List<string>>();
  289. ManaServer.GetComment(ManaServer.SerialNumber, "1", CommentType.Garden, data => { InputCommentData(true, data); NextPage(true, true); });
  290. }
  291. else
  292. {
  293. Debug.Log("UpdateVisitee");
  294. VisiteeFirstIndex = 0;
  295. VisiteeLastIndex = -1;
  296. VisiteeCommentPage = 0;
  297. VisiteeCommentDatas = new List<List<string>>();
  298. ManaServer.GetComment(ManaVisit.VisiteeSerialNumber, "1", CommentType.Garden, data => { InputCommentData(false, data); NextPage(false, true); });
  299. }
  300. }
  301. public static void OnScroll(Vector2 position)
  302. {
  303. if (PullLock)
  304. {
  305. return;
  306. }
  307. if (position.y >= 1)
  308. {
  309. PreviousPage(IsPlayer);
  310. }
  311. if (position.y <= 0)
  312. {
  313. if (IsPlayer)
  314. {
  315. if (PlayerLastIndex == PlayerCommentDatas.Count - 1)
  316. {
  317. PullPage(true);
  318. }
  319. else if (PlayerLastIndex < PlayerCommentDatas.Count - 1)
  320. {
  321. NextPage(true, false);
  322. }
  323. }
  324. else
  325. {
  326. if (VisiteeLastIndex == VisiteeCommentDatas.Count - 1)
  327. {
  328. PullPage(false);
  329. }
  330. else if (VisiteeLastIndex < VisiteeCommentDatas.Count - 1)
  331. {
  332. NextPage(false, false);
  333. }
  334. }
  335. }
  336. }
  337. public static void Comment()
  338. {
  339. if ((DateTime.Now - CommentTime).TotalSeconds > 30)
  340. {
  341. CommentTime = DateTime.Now;
  342. ManaServer.AddComment(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber, InputField.text, CommentType.Garden);
  343. }
  344. else
  345. {
  346. Bubble.Show(Language.GetStr("UI", "Q_CommentTip"));
  347. }
  348. }
  349. public static void OpenCommentPanel()
  350. {
  351. ManaReso.Get("Q_CommentBK").TweenForCG();
  352. ManaReso.SetActive("Q_InputBK", ManaVisit.InVisit);
  353. IsPlayer = !ManaVisit.InVisit;
  354. IsPanelOpen = true;
  355. }
  356. public static void CloseCommentPanel()
  357. {
  358. ManaReso.Get("Q_CommentBK").TweenBacCG();
  359. IsPanelOpen = false;
  360. }
  361. public static void Record()
  362. {
  363. if (IsPlayer)
  364. {
  365. RecordFlag = true;
  366. RecordLastIndex = PlayerLastIndex;
  367. RecordFirstIndex = PlayerFirstIndex;
  368. RecordPosition = ScrollRectPlus.content.position;
  369. }
  370. }
  371. public static void Recover()
  372. {
  373. if (RecordFlag)
  374. {
  375. RecordFlag = false;
  376. OpenCommentPanel();
  377. ClearComment();
  378. PlayerLastIndex = -1;
  379. PlayerFirstIndex = 0;
  380. int antiCrush = 0;
  381. while (PlayerFirstIndex != RecordFirstIndex || PlayerLastIndex != RecordLastIndex)
  382. {
  383. if (antiCrush > 10000)
  384. {
  385. throw new Exception("Crush");
  386. }
  387. if (ScrollRectPlus.content.childCount >= 20)
  388. {
  389. ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
  390. PlayerFirstIndex++;
  391. }
  392. PlayerLastIndex++;
  393. ManaReso.GetCommentItem(PlayerCommentDatas[PlayerLastIndex][0], PlayerCommentDatas[PlayerLastIndex][1]);
  394. }
  395. ScrollRectPlus.content.position = RecordPosition;
  396. }
  397. }
  398. }