123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- 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 SocialManager : Regist
- {
- #region Config
- public static bool OpenRankPanelFlag;
- public static int RecordCommentLastIndex;
- public static int RecordCommentFirstIndex;
- public static bool OpenCommentFlag;
- public static Vector3 RecordCommentPosition;
- public static bool IsPlayer;
- public static bool IsPanelOpen;
- public static bool PullCommentDataFlag;
- public static bool UpdateCommentDataFlag;
- public static int PlayerLastDisplayIndex = -1;
- public static int PlayerFirstDisplayIndex;
- public static int VisiteeLastDisplayIndex = -1;
- public static int VisiteeFirstDisplayIndex;
- public static int CurrentPlayerCommentPage;
- public static int CurrentVisiteeCommentPage;
- public static bool IsFriendFilterOn;
- public static Text PraiseText;
- public static Button PraiseButton;
- public static Toggle CommentFriendFilterToggle;
- public static DateTime CommentTime;
- public static InputField CommentInputField;
- public static ScrollRectPlus CommentScrollRect;
- public static float UpdateRankDataTime = 5;
- public static float UpdateRankDataTimer = 0;
- public static float UpdatePraiseDataTime = 10;
- public static float UpdatePraiseDataTimer = 5;
- public static float CommentTimeSpan = 30f;
- public static List<CommentData> PlayerCommentDatas = new List<CommentData>();
- public static List<CommentData> VisiteeCommentDatas = new List<CommentData>();
- public static List<CommentItem> CommentItems = new List<CommentItem>();
- private static int CommentPanelMaxRevertAmt = 15;
- private static int CommentPanelMaxChildAmt = 20;
- private static float CommentItemHeight = 120;
- #endregion
- public void Update()
- {
- if (!HttpManager.IsRankDataReady)
- {
- UpdateRankDataTimer += Time.deltaTime;
- if (UpdateRankDataTimer >= UpdateRankDataTime)
- {
- UpdateRankDataTimer = 0;
- HttpManager.GetRankData();
- }
- }
- if (VisitManager.InVisit)
- {
- return;
- }
- UpdatePraiseDataTimer += Time.deltaTime;
- if (UpdatePraiseDataTimer >= UpdatePraiseDataTime)
- {
- UpdatePraiseDataTimer = 0;
- PraiseText.text = HttpManager.PraiseAmt.ToString();
- }
- }
- public override void FirstInit()
- {
- PraiseText = ResourceManager.Get<Text>(CanvasLabel.C_PraiseText);
- PraiseButton = ResourceManager.Get<Button>(CanvasLabel.C_Praise);
- CommentInputField = ResourceManager.Get<InputField>(CanvasLabel.Q_InputField);
- CommentScrollRect = ResourceManager.Get<ScrollRectPlus>(CanvasLabel.Q_ScrollRect);
- CommentFriendFilterToggle = ResourceManager.Get<Toggle>(CanvasLabel.Q_FriendFilterButton);
- CommentFriendFilterToggle.onValueChanged.AddListener(OnCommentFriendFilterToggleClick);
- CommentScrollRect.onValueChanged.AddListener(OnScroll);
- FriendPanel.OnFriendListRefresh += OnFriendListRefresh;
- VisitManager.OnEnterVisteeGarden += OnEnterVisiteeGarden;
- VisitManager.OnExitVisteeGarden += OnExitVisiteeGarden;
- }
- public static void Praise()
- {
- if (VisitManager.InVisit)
- {
- PraiseText.text = (int.Parse(PraiseText.text) + 1).ToString();
- HttpManager.Praise(HttpManager.SerialNumber, VisitManager.VisiteeSerialNumber);
- DisablePraise();
- }
- else
- {
- throw new Exception();
- }
- }
- public static void EnablePraise()
- {
- PraiseText.color = Lib.PraiseTextBlue;
- PraiseButton.image.color = Color.white;
- PraiseButton.interactable = true;
- }
- public static void DisablePraise()
- {
- PraiseText.color = Lib.PraiseTextWhite;
- PraiseButton.image.color = Lib.Pink;
- PraiseButton.interactable = false;
- }
- public static void Comment()
- {
- if (string.IsNullOrEmpty(CommentInputField.text))
- {
- Bubble.Show(Language.GetStr(LanguageLabel.UI__Lb_Send2));
- return;
- }
- if ((DateTime.Now - CommentTime).TotalSeconds > CommentTimeSpan)
- {
- CommentTime = DateTime.Now;
- string content = StringFilter.GetFilteredString(CommentInputField.text);
- HttpManager.AddComment(HttpManager.SerialNumber, VisitManager.VisiteeSerialNumber, content, CommentType.Garden);
- CommentInputField.text = "";
- }
- else
- {
- Bubble.Show(Language.GetStr(LanguageLabel.UI__Q_CommentTip));
- }
- }
- public static void OpenCommentPanel()
- {
- IsPlayer = !VisitManager.InVisit;
- if (IsPlayer)
- {
- UpdateCommentPage(IsPlayer);
- }
- else
- {
- if (string.IsNullOrEmpty(VisitManager.VisiteeSerialNumber))
- {
- Bubble.Show(null, Language.GetStr(LanguageLabel.UI__C_CannotComment));
- return;
- }
- else
- {
- UpdateCommentPage(IsPlayer);
- }
- }
- IsPanelOpen = true;
- ResourceManager.Get(CanvasLabel.Q_CommentBK).TweenForCG();
- if (VisitManager.InVisit)
- {
- ResourceManager.SetActive(CanvasLabel.Q_InputBK, true);
- RectTransform rectTransform = ResourceManager.Get<RectTransform>(CanvasLabel.Q_ScrollRect);
- rectTransform.sizeDelta = new Vector2(rectTransform.rect.size.x, 206.1f);
- }
- else
- {
- ResourceManager.SetActive(CanvasLabel.Q_InputBK, false);
- RectTransform rectTransform = ResourceManager.Get<RectTransform>(CanvasLabel.Q_ScrollRect);
- rectTransform.sizeDelta = new Vector2(rectTransform.rect.size.x, 278.3f);
- }
- }
- public static void CloseCommentPanel()
- {
- ResourceManager.Get(CanvasLabel.Q_CommentBK).TweenBacCG();
- IsPanelOpen = false;
- }
- public static void RecordCommentPanel()
- {
- if (IsPlayer)
- {
- OpenCommentFlag = true;
- RecordCommentLastIndex = PlayerLastDisplayIndex;
- RecordCommentFirstIndex = PlayerFirstDisplayIndex;
- RecordCommentPosition = CommentScrollRect.content.transform.position;
- }
- }
- public static void RecoverCommentPanel()
- {
- if (OpenCommentFlag)
- {
- OpenCommentFlag = false;
- ClearCommentPanel();
- PlayerLastDisplayIndex = -1;
- PlayerFirstDisplayIndex = 0;
- int antiCrush = 0;
- while (PlayerFirstDisplayIndex != RecordCommentFirstIndex || PlayerLastDisplayIndex != RecordCommentLastIndex)
- {
- if (antiCrush > 10000)
- {
- throw new Exception("Crush");
- }
- if (CommentScrollRect.content.childCount >= CommentPanelMaxChildAmt)
- {
- CommentItem commentItem = CommentScrollRect.content.GetChild(0).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- PlayerFirstDisplayIndex++;
- }
- PlayerLastDisplayIndex++;
- CommentData commentData = PlayerCommentDatas[PlayerLastDisplayIndex];
- CommentItem newCommentItem = ResourceManager.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- CommentItems.Add(newCommentItem);
- }
- CommentScrollRect.content.transform.position = RecordCommentPosition;
- OpenCommentPanel();
- }
- }
- public static void PullCommentPage(bool isPlayer)
- {
- PullCommentDataFlag = true;
- UpdateCommentDataFlag = false;
- //Debug.Log("PullPage");
- if (isPlayer)
- {
- HttpManager.GetComment
- (
- HttpManager.SerialNumber,
- (CurrentPlayerCommentPage + 1).ToString(),
- CommentType.Garden,
- data =>
- {
- if (!UpdateCommentDataFlag)
- {
- ReceiveCommentDatas(IsPlayer, data);
- NextCommentPage(IsPlayer, true);
- }
- else
- {
- PullCommentDataFlag = false;
- }
- }
- );
- }
- else
- {
- HttpManager.GetComment
- (
- HttpManager.SerialNumber,
- (CurrentVisiteeCommentPage + 1).ToString(),
- CommentType.Garden,
- data =>
- {
- if (!UpdateCommentDataFlag)
- {
- ReceiveCommentDatas(IsPlayer, data);
- NextCommentPage(IsPlayer, true);
- }
- else
- {
- PullCommentDataFlag = false;
- }
- }
- );
- }
- }
- public static void UpdateCommentPage(bool isPlayer)
- {
- UpdateCommentDataFlag = true;
- ClearCommentPanel();
- ResourceManager.Get(CanvasLabel.Q_Tip).TweenForCG();
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Q_Tip), Language.GetStr(LanguageLabel.UI__Loading));
- if (isPlayer)
- {
- //Debug.Log("UpdatePlayer");
- PlayerFirstDisplayIndex = 0;
- PlayerLastDisplayIndex = -1;
- CurrentPlayerCommentPage = 0;
- PlayerCommentDatas = new List<CommentData>();
- HttpManager.GetComment
- (
- HttpManager.SerialNumber,
- "1",
- CommentType.Garden,
- data =>
- {
- if (!VisitManager.InVisit)
- {
- DelayCall.Call(0.5f, () => ResourceManager.Get(CanvasLabel.Q_Tip).TweenBacCG());
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Q_Tip), Language.GetStr(LanguageLabel.UI__LoadSucceed));
- ReceiveCommentDatas(true, data);
- NextCommentPage(true, true);
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Q_CommentBK).interactable = false;
- DelayCall.Call(1, () => ResourceManager.Get<CanvasGroup>(CanvasLabel.Q_CommentBK).interactable = true);
- }
- }
- );
- }
- else
- {
- //Debug.Log("UpdateVisitee");
- VisiteeFirstDisplayIndex = 0;
- VisiteeLastDisplayIndex = -1;
- CurrentVisiteeCommentPage = 0;
- VisiteeCommentDatas = new List<CommentData>();
- HttpManager.GetComment
- (
- VisitManager.VisiteeSerialNumber,
- "1",
- CommentType.Garden,
- data =>
- {
- if (VisitManager.InVisit)
- {
- DelayCall.Call(0.5f, () => ResourceManager.Get(CanvasLabel.Q_Tip).TweenBacCG());
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Q_Tip), Language.GetStr(LanguageLabel.UI__LoadSucceed));
- ReceiveCommentDatas(false, data);
- NextCommentPage(false, true);
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Q_CommentBK).interactable = false;
- DelayCall.Call(1, () => ResourceManager.Get<CanvasGroup>(CanvasLabel.Q_CommentBK).interactable = true);
- }
- }
- );
- }
- }
- public static void ReceiveCommentDatas(bool isPlayer, JsonData jsonData)
- {
- Auxiliary.Instance.DelayCall
- (
- () =>
- {
- PullCommentDataFlag = false;
- },
- 1
- );
- if (!jsonData.Inst_Object.ContainsKey("l"))
- {
- return;
- }
- if (jsonData["l"].Count == 0)
- {
- return;
- }
- if (isPlayer)
- {
- CurrentPlayerCommentPage++;
- foreach (JsonData commentData in jsonData["l"])
- {
- PlayerCommentDatas.UniqueAdd(new CommentData(commentData));
- }
- }
- else
- {
- CurrentVisiteeCommentPage++;
- foreach (JsonData commentData in jsonData["l"])
- {
- VisiteeCommentDatas.UniqueAdd(new CommentData(commentData));
- }
- }
- }
- public static void NextCommentPage(bool isPlayer, bool isDownload)
- {
- //Debug.Log("NextPage");
- if (isPlayer)
- {
- int saveAmt = 0;
- int updateAmt = Mathf.Min(15, PlayerCommentDatas.Count - PlayerLastDisplayIndex - 1);
- for (int i = 0; i < updateAmt; i++)
- {
- if (CommentScrollRect.content.childCount >= CommentPanelMaxChildAmt)
- {
- saveAmt++;
- CommentItem commentItem = CommentScrollRect.content.GetChild(0).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- PlayerFirstDisplayIndex++;
- }
- PlayerLastDisplayIndex++;
- CommentData commentData = PlayerCommentDatas[PlayerLastDisplayIndex];
- CommentItem newCommentItem = ResourceManager.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- CommentItems.Add(newCommentItem);
- }
- int offset = isDownload ? 1 : 0;
- float scaleFactor = isDownload ? CommentScrollRect.GetComponent<Image>().canvas.scaleFactor : 1;
- if (saveAmt > 0)
- {
- CommentScrollRect.content.position += new Vector3(0, -(saveAmt - offset)*CommentItemHeight*scaleFactor, 0);
- CommentScrollRect.AddContentOffset(new Vector3(0, -(saveAmt - offset)* CommentItemHeight * scaleFactor, 0));
- }
- }
- else
- {
- int saveAmt = 0;
- int updateAmt = Mathf.Min(15, VisiteeCommentDatas.Count - VisiteeLastDisplayIndex - 1);
- for (int i = 0; i < updateAmt; i++)
- {
- if (CommentScrollRect.content.childCount >= CommentPanelMaxChildAmt)
- {
- saveAmt++;
- CommentItem commentItem = CommentScrollRect.content.GetChild(0).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- VisiteeFirstDisplayIndex++;
- }
- VisiteeLastDisplayIndex++;
- CommentData commentData = VisiteeCommentDatas[VisiteeLastDisplayIndex];
- CommentItem newCommentItem = ResourceManager.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- CommentItems.Add(newCommentItem);
- }
- int offset = isDownload ? 1 : 0;
- float scaleFactor = isDownload ? CommentScrollRect.GetComponent<Image>().canvas.scaleFactor : 1;
- if (saveAmt > 0)
- {
- CommentScrollRect.content.position += new Vector3(0, -(saveAmt - offset)* CommentItemHeight * scaleFactor, 0);
- CommentScrollRect.AddContentOffset(new Vector3(0, -(saveAmt - offset)* CommentItemHeight * scaleFactor, 0));
- }
- }
- }
- public static void PreviousCommentPage(bool isPlayer)
- {
- if (CommentScrollRect.content.childCount == 0)
- {
- return;
- }
- //Debug.Log("PreviousPage");
- if (isPlayer)
- {
- if (PlayerCommentDatas.Count > CommentPanelMaxChildAmt && PlayerFirstDisplayIndex > 0)
- {
- int revertAmt = Mathf.Min(CommentPanelMaxRevertAmt, PlayerFirstDisplayIndex);
- for (int i = 0; i < revertAmt; i++)
- {
- CommentItem commentItem = CommentScrollRect.content.GetChild(CommentScrollRect.content.childCount - 1).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- CommentData commentData = VisiteeCommentDatas[VisiteeLastDisplayIndex];
- commentItem = ResourceManager.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- CommentItems.Add(commentItem);
- commentItem.transform.SetAsFirstSibling();
- PlayerLastDisplayIndex--;
- PlayerFirstDisplayIndex--;
- }
- if (revertAmt > 0)
- {
- CommentScrollRect.content.position += new Vector3(0, revertAmt* CommentItemHeight, 0);
- CommentScrollRect.AddContentOffset(new Vector3(0, revertAmt* CommentItemHeight, 0));
- }
- }
- }
- else
- {
- if (VisiteeCommentDatas.Count > CommentPanelMaxChildAmt && VisiteeFirstDisplayIndex > 0)
- {
- int revertAmt = Mathf.Min(CommentPanelMaxRevertAmt, VisiteeFirstDisplayIndex);
- for (int i = 0; i < revertAmt; i++)
- {
- CommentItem commentItem = CommentScrollRect.content.GetChild(CommentScrollRect.content.childCount - 1).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- CommentData commentData = VisiteeCommentDatas[VisiteeLastDisplayIndex];
- commentItem = ResourceManager.GetCommentItem(commentData.NickName, commentData.SerialNumber, commentData.Content);
- CommentItems.Add(commentItem);
- commentItem.transform.SetAsFirstSibling();
- VisiteeLastDisplayIndex--;
- VisiteeFirstDisplayIndex--;
- }
- if (revertAmt > 0)
- {
- CommentScrollRect.content.position += new Vector3(0, revertAmt* CommentItemHeight, 0);
- CommentScrollRect.AddContentOffset(new Vector3(0, revertAmt* CommentItemHeight, 0));
- }
- }
- }
- }
- public static void ClearCommentPanel()
- {
- int childAmt = CommentScrollRect.content.childCount;
- for (int i = 0; i < childAmt; i++)
- {
- CommentItem commentItem = CommentScrollRect.content.GetChild(0).GetComponent<CommentItem>();
- ResourceManager.Save(commentItem);
- CommentItems.Remove(commentItem);
- }
- CommentScrollRect.verticalNormalizedPosition = 1;
- }
- public static void OnScroll(Vector2 position)
- {
- if (PullCommentDataFlag)
- {
- return;
- }
- if (CommentScrollRect.content.childCount == 0)
- {
- return;
- }
- if (position.y >= 1)
- {
- PreviousCommentPage(IsPlayer);
- }
- if (position.y <= 0)
- {
- if (IsPlayer)
- {
- if (PlayerLastDisplayIndex == PlayerCommentDatas.Count - 1)
- {
- PullCommentPage(true);
- }
- else if (PlayerLastDisplayIndex < PlayerCommentDatas.Count - 1)
- {
- NextCommentPage(true, false);
- }
- }
- else
- {
- if (VisiteeLastDisplayIndex == VisiteeCommentDatas.Count - 1)
- {
- PullCommentPage(false);
- }
- else if (VisiteeLastDisplayIndex < VisiteeCommentDatas.Count - 1)
- {
- NextCommentPage(false, false);
- }
- }
- }
- }
- public static void OpenRankPanel()
- {
- AudioManager.PlayClip(AudioLabel.Bubble);
- ResourceManager.Get(CanvasLabel.S_RankBK).TweenForCG();
- }
- public static void CloseRankPanel()
- {
- AudioManager.PlayClip(AudioLabel.ClickButton);
- ResourceManager.Get(CanvasLabel.S_RankBK).TweenBacCG();
- }
- public static void RecordRankPanel()
- {
- OpenRankPanelFlag = true;
- }
- public static void RecoverRankPanel()
- {
- if (OpenRankPanelFlag)
- {
- OpenRankPanelFlag = false;
- OpenRankPanel();
- }
- }
- public static void InitRankPanel()
- {
- if (Initializer.Inited && HttpManager.IsRankDataReady)
- {
- for (int i = 0; i < HttpManager.RankDatas.Count; i++)
- {
- ResourceManager.GetRanktem((i + 1).ToString(), HttpManager.RankDatas[i][1].ToString(), HttpManager.RankDatas[i][0].ToString());
- }
- }
- }
- public static void OnCommentFriendFilterToggleClick(bool value)
- {
- AudioManager.PlayClip(ResourceLabel.BtnClip);
- if (value)
- {
- EnableCommentFriendFilter();
- }
- else
- {
- DisableCommentFriendFilter();
- }
- }
- public static void EnableCommentFriendFilter()
- {
- IsFriendFilterOn = true;
- foreach (var commentItem in CommentItems)
- {
- if (!commentItem.IsFriend(FriendPanel.FriendAccountDatas))
- {
- commentItem.SetActive(false);
- }
- }
- }
- public static void DisableCommentFriendFilter()
- {
- IsFriendFilterOn = false;
- foreach (var commentItem in CommentItems)
- {
- commentItem.SetActive(true);
- }
- }
- private static void OnFriendListRefresh(List<AccountData> accountDatas)
- {
- if (IsFriendFilterOn)
- {
- EnableCommentFriendFilter();
- }
- }
- public static void OnEnterVisiteeGarden()
- {
- ResourceManager.SetActive(CanvasLabel.C_NotifyParent, false);
- }
- public static void OnExitVisiteeGarden()
- {
- if (ResourceManager.Get(CanvasLabel.C_Notify).gameObject.activeSelf)
- {
- ResourceManager.SetActive(CanvasLabel.C_NotifyParent, true);
- }
- }
- }
|