123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using System;
- using System.Collections;
- using System.Diagnostics;
- using System.Collections.Generic;
- using LitJson;
- using Debug = UnityEngine.Debug;
- public class ManaSocial : Regist
- {
- #region Variable
- public static bool RankRecordFlag;
- public static int CommentRecordLastIndex;
- public static int CommentRecordFirstIndex;
- public static bool CommentRecordFlag;
- public static Vector3 CommentRecordPosition;
- 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 RankTime = 5;
- public static float RankTimer = 0;
- public static float PraiseTime = 10;
- public static float PraiseTimer = 5;
- public static float CommentTimespan = 60;
- public static float CommentTimer = 0;
- public static List<CommentData> PlayerCommentDatas = new List<CommentData>();
- public static List<CommentData> VisiteeCommentDatas = new List<CommentData>();
- #endregion
- public void Update()
- {
- if (!ManaServer.RankReady)
- {
- RankTimer += Time.deltaTime;
- if (RankTimer >= RankTime)
- {
- RankTimer = 0;
- ManaServer.RankRequest();
- }
- }
- if (ManaVisit.InVisit)
- {
- return;
- }
- //CommentTimer += Time.deltaTime;
- //if (CommentTimer >= CommentTimespan)
- //{
- // CommentTimer = 0;
- // if (!IsPanelOpen)
- // {
- // UpdatePage(true);
- // }
- //}
- PraiseTimer += Time.deltaTime;
- if (PraiseTimer >= PraiseTime)
- {
- //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
- {
- throw new Exception();
- }
- }
- 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.UniqueAdd(new CommentData(commentData));
- }
- }
- else
- {
- VisiteeCommentPage++;
- foreach (JsonData commentData in jsonData["l"])
- {
- VisiteeCommentDatas.UniqueAdd(new CommentData(commentData));
- }
- }
- }
- 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++;
- CommentData commentData = PlayerCommentDatas[PlayerLastIndex];
- ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- }
- 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++;
- CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
- ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- }
- 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 (ScrollRectPlus.content.childCount == 0)
- {
- return;
- }
- //Debug.Log("PreviousPage");
- if (isPlayer)
- {
- if (PlayerCommentDatas.Count > 20 && PlayerFirstIndex > 0)
- {
- int revertAmt = Mathf.Min(15, PlayerFirstIndex);
- for (int i = 0; i < revertAmt; i++)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(ScrollRectPlus.content.childCount - 1));
- CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
- CommentItem commentItem = ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- 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);
- for (int i = 0; i < revertAmt; i++)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(ScrollRectPlus.content.childCount - 1));
- CommentData commentData = VisiteeCommentDatas[VisiteeLastIndex];
- CommentItem commentItem = ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- 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();
- ManaReso.Get("Q_Tip").TweenForCG();
- ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "Loading"));
- if (isPlayer)
- {
- //Debug.Log("UpdatePlayer");
- PlayerFirstIndex = 0;
- PlayerLastIndex = -1;
- PlayerCommentPage = 0;
- PlayerCommentDatas = new List<CommentData>();
- ManaServer.GetComment
- (
- ManaServer.SerialNumber,
- "1",
- CommentType.Garden,
- data =>
- {
- if (!ManaVisit.InVisit)
- {
- DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
- ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
- InputCommentData(true, data);
- NextPage(true, true);
- ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
- DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
- }
- }
- );
- }
- else
- {
- //Debug.Log("UpdateVisitee");
- VisiteeFirstIndex = 0;
- VisiteeLastIndex = -1;
- VisiteeCommentPage = 0;
- VisiteeCommentDatas = new List<CommentData>();
- ManaServer.GetComment
- (
- ManaVisit.VisiteeSerialNumber,
- "1",
- CommentType.Garden,
- data =>
- {
- if (ManaVisit.InVisit)
- {
- DelayCall.Call(0.5f, () => ManaReso.Get("Q_Tip").TweenBacCG());
- ManaLan.Add(ManaReso.Get<Text>("Q_Tip"), Language.GetStr("UI", "LoadSucceed"));
- InputCommentData(false, data);
- NextPage(false, true);
- ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = false;
- DelayCall.Call(1, () => ManaReso.Get<CanvasGroup>("Q_CommentBK").interactable = true);
- }
- }
- );
- }
- }
- public static void OnScroll(Vector2 position)
- {
- if (PullLock)
- {
- return;
- }
- if (ScrollRectPlus.content.childCount == 0)
- {
- 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 OpenRankPanel()
- {
- ManaAudio.PlayClip(Clip.BubbleClip);
- ManaReso.Get("S_RankBK").TweenForCG();
- }
- public static void CloseRankPanel()
- {
- ManaAudio.PlayClip(Clip.BtnClip);
- ManaReso.Get("S_RankBK").TweenBacCG();
- }
- public static void RecordRankPanel()
- {
- RankRecordFlag = true;
- }
- public static void RecoverRankPanel()
- {
- if (RankRecordFlag)
- {
- RankRecordFlag = false;
- OpenRankPanel();
- }
- }
- public static void InitializeRankPanel()
- {
- if (Initializer.Complete && ManaServer.RankReady)
- {
- for (int i = 0; i < ManaServer.RankDatas.Count; i++)
- {
- ManaReso.GetRanktem((i + 1).ToString(), ManaServer.RankDatas[i][1].ToString(), ManaServer.RankDatas[i][0].ToString());
- }
- }
- }
- public static void Comment()
- {
- if (string.IsNullOrEmpty(InputField.text))
- {
- Bubble.Show(Language.GetStr("UI", "Lb_Send2"));
- return;
- }
- if ((DateTime.Now - CommentTime).TotalSeconds > 30)
- {
- CommentTime = DateTime.Now;
- string content = StringFilter.GetFilteredString(InputField.text);
- ManaServer.AddComment(ManaServer.SerialNumber, ManaVisit.VisiteeSerialNumber, content, CommentType.Garden);
- InputField.text = "";
- }
- else
- {
- Bubble.Show(Language.GetStr("UI", "Q_CommentTip"));
- }
- }
- public static void OpenCommentPanel()
- {
- IsPlayer = !ManaVisit.InVisit;
- if (IsPlayer)
- {
- UpdatePage(IsPlayer);
- }
- else
- {
- if (string.IsNullOrEmpty(ManaVisit.VisiteeSerialNumber))
- {
- Bubble.Show(null, Language.GetStr("UI", "C_CannotComment"));
- return;
- }
- else
- {
- UpdatePage(IsPlayer);
- }
- }
- IsPanelOpen = true;
- ManaReso.Get("Q_CommentBK").TweenForCG();
- ManaReso.SetActive("Q_InputBK", ManaVisit.InVisit);
- }
- public static void CloseCommentPanel()
- {
- ManaReso.Get("Q_CommentBK").TweenBacCG();
- IsPanelOpen = false;
- }
- public static void RecordCommentPanel()
- {
- if (IsPlayer)
- {
- CommentRecordFlag = true;
- CommentRecordLastIndex = PlayerLastIndex;
- CommentRecordFirstIndex = PlayerFirstIndex;
- CommentRecordPosition = ScrollRectPlus.content.transform.position;
- }
- }
- public static void RecoverCommentPanel()
- {
- if (CommentRecordFlag)
- {
- CommentRecordFlag = false;
- ClearComment();
- PlayerLastIndex = -1;
- PlayerFirstIndex = 0;
- int antiCrush = 0;
- while (PlayerFirstIndex != CommentRecordFirstIndex || PlayerLastIndex != CommentRecordLastIndex)
- {
- if (antiCrush > 10000)
- {
- throw new Exception("Crush");
- }
- if (ScrollRectPlus.content.childCount >= 20)
- {
- ManaReso.Save(ScrollRectPlus.content.GetChild(0).gameObject);
- PlayerFirstIndex++;
- }
- PlayerLastIndex++;
- CommentData commentData = PlayerCommentDatas[PlayerLastIndex];
- ManaReso.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- }
- ScrollRectPlus.content.transform.position = CommentRecordPosition;
- OpenCommentPanel();
- }
- }
- }
|