123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class ManaSocial : Regist
- {
- #region Variable
- public static int RecordLastIndex;
- public static int RecordFirstIndex;
- public static bool RecordFlag;
- public static Vector3 RecordPosition;
- public static bool IsPlayer;
- public static bool PullLock;
- public static bool UpdateLock;
- public static bool IsPanelOpen;
- public static int PlayerLastIndex=-1;
- public static int VisiteeLastIndex=-1;
- public static int PlayerFirstIndex;
- public static int VisiteeFirstIndex;
- public static int PlayerCommentPage;
- public static int VisiteeCommentPage;
- public static Text PraiseText;
- public static Button PraiseBtn;
- public static DateTime CommentTime;
- public static InputField InputField;
- public static ScrollRectPlus ScrollRectPlus;
- public static float PraiseTimer = 5;
- public static float CommentTimer = 0;
- public static List<List<string>> PlayerCommentDatas = new List<List<string>>();
- public static List<List<string>> VisiteeCommentDatas = new List<List<string>>();
- #endregion
- public void Update()
- {
- if (ManaVisit.InVisit)
- {
- return;
- }
- CommentTimer += Time.deltaTime;
- if (CommentTimer >= 10)
- {
- CommentTimer = 0;
- if (!IsPanelOpen)
- {
- UpdatePage(true);
- }
- }
- PraiseTimer += Time.deltaTime;
- if (PraiseTimer >= 5)
- {
- //index += 1;
- //Debug.Log(index);
- //ManaServer.AddComment(ManaServer.SerialNumber, ManaServer.SerialNumber, index.ToString(), CommentType.Garden);
- PraiseTimer = 0;
- PraiseText.text = ManaServer.PraiseAmt.ToString();
- }
- }
- public override void RegistValueA()
- {
- PraiseText = ManaReso.Get<Text>("C_PraiseText");
- PraiseBtn = ManaReso.Get<Button>("C_Praise");
- InputField = ManaReso.Get<InputField>("Q_InputField");
- ScrollRectPlus = ManaReso.Get<ScrollRectPlus>("Q_ScrollRect");
- ScrollRectPlus.onValueChanged.AddListener(OnScroll);
- }
- public static void Praise()
- {
- if (ManaVisit.InVisit)
- {
- PraiseText.text = (int.Parse(PraiseText.text) + 1).ToString();
- ManaServer.Praise(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber);
- DisablePraise();
- }
- else
- {
- DisablePraise();
- }
- }
- public static void EnablePraise()
- {
- PraiseBtn.image.color = Color.white;
- PraiseBtn.interactable = true;
- }
- public static void DisablePraise()
- {
- PraiseBtn.image.color = Lib.Pink;
- PraiseBtn.interactable = false;
- }
- public static void InputCommentData(bool isPlayer, JsonData jsonData)
- {
- Auxiliary.Instance.DelayCall
- (
- () =>
- {
- PullLock = false;
- },
- 1
- );
- if (!jsonData.Inst_Object.ContainsKey("l"))
- {
- return;
- }
- if (jsonData["l"].Count == 0)
- {
- return;
- }
- if (isPlayer)
- {
- PlayerCommentPage++;
- foreach (JsonData commentData in jsonData["l"])
- {
- PlayerCommentDatas.Add(new List<string>() { commentData["s"].ToString(), commentData["c"].ToString() });
- }
- }
- else
- {
- VisiteeCommentPage++;
- foreach (JsonData commentData in jsonData["l"])
- {
- VisiteeCommentDatas.Add(new List<string>() { commentData["s"].ToString(), commentData["c"].ToString() });
- }
- }
- }
- public static void NextPage(bool isPlayer, bool isDownload)
- {
- Debug.Log("NextPage");
- if (isPlayer)
- {
- int saveAmt = 0;
- int updateAmt = Mathf.Min(15, PlayerCommentDatas.Count - PlayerLastIndex - 1);
- for (int i = 0; i < updateAmt; i++)
- {
- if (ScrollRectPlus.content.childCount >= 20)
- {
- saveAmt++;
- ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
- PlayerFirstIndex++;
- }
- PlayerLastIndex++;
- ManaReso.GetCommentItem(PlayerCommentDatas[PlayerLastIndex][0], PlayerCommentDatas[PlayerLastIndex][1]);
- }
- int offset = isDownload ? 1 : 0;
- float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
- if (saveAmt > 0)
- {
- ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0);
- ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0));
- }
- }
- else
- {
- int saveAmt = 0;
- int updateAmt = Mathf.Min(15, VisiteeCommentDatas.Count - VisiteeLastIndex - 1);
- for (int i = 0; i < updateAmt; i++)
- {
- if (ScrollRectPlus.content.childCount >= 20)
- {
- saveAmt++;
- ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
- VisiteeFirstIndex++;
- }
- VisiteeLastIndex++;
- ManaReso.GetCommentItem(VisiteeCommentDatas[VisiteeLastIndex][0], VisiteeCommentDatas[VisiteeLastIndex][1]);
- }
- int offset = isDownload ? 1 : 0;
- float scaleFactor = isDownload ? ScrollRectPlus.GetComponent<Image>().canvas.scaleFactor : 1;
- if (saveAmt > 0)
- {
- ScrollRectPlus.content.position += new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0);
- ScrollRectPlus.AddContentOffset(new Vector3(0, -(saveAmt - offset) * 120 * scaleFactor, 0));
- }
- }
- }
- public static void PreviousPage(bool isPlayer)
- {
- if (isPlayer)
- {
- if (PlayerCommentDatas.Count > 20 && PlayerFirstIndex > 0)
- {
- int revertAmt = Mathf.Min(15, PlayerFirstIndex);
- Debug.Log("PreviousPage");
- for (int i = 0; i < revertAmt; i++)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(19));
- CommentItem commentItem = ManaReso.GetCommentItem(PlayerCommentDatas[PlayerFirstIndex - 1][0], PlayerCommentDatas[PlayerFirstIndex - 1][1]);
- commentItem.transform.SetAsFirstSibling();
- PlayerLastIndex--;
- PlayerFirstIndex--;
- }
- if (revertAmt > 0)
- {
- ScrollRectPlus.content.position += new Vector3(0, revertAmt*120, 0);
- ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt*120, 0));
- }
- }
- }
- else
- {
- if (VisiteeCommentDatas.Count > 20 && VisiteeFirstIndex > 0)
- {
- int revertAmt = Mathf.Min(15, VisiteeFirstIndex);
- Debug.Log("Revertpage");
- for (int i = 0; i < revertAmt; i++)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(19));
- CommentItem commentItem = ManaReso.GetCommentItem(VisiteeCommentDatas[VisiteeFirstIndex - 1][0], VisiteeCommentDatas[VisiteeFirstIndex - 1][1]);
- commentItem.transform.SetAsFirstSibling();
- VisiteeLastIndex--;
- VisiteeFirstIndex--;
- }
- if (revertAmt > 0)
- {
- ScrollRectPlus.content.position += new Vector3(0, revertAmt * 120, 0);
- ScrollRectPlus.AddContentOffset(new Vector3(0, revertAmt * 120, 0));
- }
- }
- }
- }
- public static void PullPage(bool isPlayer)
- {
- PullLock = true;
- UpdateLock = false;
- Debug.Log("PullPage");
- if (isPlayer)
- {
- ManaServer.GetComment
- (
- ManaServer.SerialNumber,
- (PlayerCommentPage + 1).ToString(),
- CommentType.Garden,
- data =>
- {
- if (!UpdateLock)
- {
- InputCommentData(IsPlayer, data);
- NextPage(IsPlayer, true);
- }
- else
- {
- PullLock = false;
- }
- }
- );
- }
- else
- {
- ManaServer.GetComment
- (
- ManaServer.SerialNumber,
- (VisiteeCommentPage + 1).ToString(),
- CommentType.Garden,
- data =>
- {
- if (!UpdateLock)
- {
- InputCommentData(IsPlayer, data);
- NextPage(IsPlayer, true);
- }
- else
- {
- PullLock = false;
- }
- }
- );
- }
- }
- public static void ClearComment()
- {
- int childAmt = ScrollRectPlus.content.childCount;
- for (int i = 0; i < childAmt; i++)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(0));
- }
- ScrollRectPlus.verticalNormalizedPosition = 1;
- }
- public static void UpdatePage(bool isPlayer)
- {
- UpdateLock = true;
- ClearComment();
- if (isPlayer)
- {
- Debug.Log("UpdatePlayer");
- PlayerFirstIndex = 0;
- PlayerLastIndex = -1;
- PlayerCommentPage = 0;
- PlayerCommentDatas = new List<List<string>>();
- ManaServer.GetComment(ManaServer.SerialNumber, "1", CommentType.Garden, data => { InputCommentData(true, data); NextPage(true, true); });
- }
- else
- {
- Debug.Log("UpdateVisitee");
- VisiteeFirstIndex = 0;
- VisiteeLastIndex = -1;
- VisiteeCommentPage = 0;
- VisiteeCommentDatas = new List<List<string>>();
- ManaServer.GetComment(ManaVisit.VisiteeSerialNumber, "1", CommentType.Garden, data => { InputCommentData(false, data); NextPage(false, true); });
- }
- }
- public static void OnScroll(Vector2 position)
- {
- if (PullLock)
- {
- return;
- }
- if (position.y >= 1)
- {
- PreviousPage(IsPlayer);
- }
- if (position.y <= 0)
- {
- if (IsPlayer)
- {
- if (PlayerLastIndex == PlayerCommentDatas.Count - 1)
- {
- PullPage(true);
- }
- else if (PlayerLastIndex < PlayerCommentDatas.Count - 1)
- {
- NextPage(true, false);
- }
- }
- else
- {
- if (VisiteeLastIndex == VisiteeCommentDatas.Count - 1)
- {
- PullPage(false);
- }
- else if (VisiteeLastIndex < VisiteeCommentDatas.Count - 1)
- {
- NextPage(false, false);
- }
- }
- }
- }
- public static void Comment()
- {
- if ((DateTime.Now - CommentTime).TotalSeconds > 30)
- {
- CommentTime = DateTime.Now;
- ManaServer.AddComment(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber, InputField.text, CommentType.Garden);
- }
- else
- {
- Bubble.Show(Language.GetStr("UI", "Q_CommentTip"));
- }
- }
- public static void OpenCommentPanel()
- {
- ManaReso.Get("Q_CommentBK").TweenForCG();
- ManaReso.SetActive("Q_InputBK", ManaVisit.InVisit);
- IsPlayer = !ManaVisit.InVisit;
- IsPanelOpen = true;
- }
- public static void CloseCommentPanel()
- {
- ManaReso.Get("Q_CommentBK").TweenBacCG();
- IsPanelOpen = false;
- }
- public static void Record()
- {
- if (IsPlayer)
- {
- RecordFlag = true;
- RecordLastIndex = PlayerLastIndex;
- RecordFirstIndex = PlayerFirstIndex;
- RecordPosition = ScrollRectPlus.content.position;
- }
- }
- public static void Recover()
- {
- if (RecordFlag)
- {
- RecordFlag = false;
- OpenCommentPanel();
- ClearComment();
- PlayerLastIndex = -1;
- PlayerFirstIndex = 0;
- int antiCrush = 0;
- while (PlayerFirstIndex != RecordFirstIndex || PlayerLastIndex != RecordLastIndex)
- {
- if (antiCrush > 10000)
- {
- throw new Exception("Crush");
- }
- if (ScrollRectPlus.content.childCount >= 20)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
- PlayerFirstIndex++;
- }
- PlayerLastIndex++;
- ManaReso.GetCommentItem(PlayerCommentDatas[PlayerLastIndex][0], PlayerCommentDatas[PlayerLastIndex][1]);
- }
- ScrollRectPlus.content.position = RecordPosition;
- }
- }
- }
|