123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Xml;
- using Sfs2X.Entities.Data;
- using UnityEngine;
- using UnityEngine.UI;
- using Random = UnityEngine.Random;
- public class ChestData
- {
- #region Config
- public int Value;
- public int Round;
- public int RemainValue;
- public int RemainRound;
- public int DatabaseRoomID;
- public long ID;
- public long Owner;
- public DateTime ActivatedTime;
- public ChestType ChestType;
- public Vector3 Position;
- public static int SystemRoomDatabaseID = 1;
- private static bool Initialized;
- private static Vector3 DefaultPosition;
- private static Vector3 ChestOffset = new Vector3(0, 0, 1.040916f);
- #endregion
- public ChestData(ISFSObject arg)
- {
- if (!Initialized)
- {
- Initialize();
- }
- ID = arg.GetLong(gd_chest.ID);
- Value = arg.GetInt(gd_chest.Value);
- Round = arg.GetInt(gd_chest.Round);
- RemainValue = arg.GetInt(gd_chest.RemainValue);
- RemainRound = arg.GetInt(gd_chest.RemainRound);
- DatabaseRoomID = arg.GetInt(gd_chest.DatabaseRoomID);
- Owner = arg.GetLong(gd_chest.Owner);
- ActivatedTime = DateUtil.GetTimeFromSeconds(arg.GetLong(gd_chest.ActivatedTime).ToString());
- ChestType = (ChestType) arg.GetInt(gd_chest.Type);
- Vector3 defaultPosition = DefaultPosition + ChestOffset;
- if (arg.ContainsKey(gd_chest.Position))
- {
- string vectorStr = arg.GetUtfString(gd_chest.Position);
- if (vectorStr == "null")
- {
- Position = defaultPosition;
- }
- else
- {
- Position = arg.GetUtfString(gd_chest.Position).ToVector() + ChestOffset;
- }
- }
- else
- {
- Position = defaultPosition;
- }
- }
- private static void Initialize()
- {
- Initialized = true;
- DefaultPosition = ResourceManager.Get(PlazaRoomLabel.PlazaRoomChestPos).position;
- }
- }
- public class ChestOperateData
- {
- public long ChestID;
- public bool Received;
- public int DatabaseRoomID;
- public int? RemainGuessAmt;
- public DateTime? LastActivatedTime; //只有系统宝箱才用
- public ChestOperateData(string str)
- {
- string[] strings = str.Split('|');
- ChestID = long.Parse(strings[0]);
- DatabaseRoomID = int.Parse(strings[1]);
- Received = int.Parse(strings[2]).ToBool();
- if (strings.Length > 3)
- {
- RemainGuessAmt = int.Parse(strings[3]);
- }
- if (strings.Length > 4)
- {
- LastActivatedTime = DateTime.Parse(strings[4]);
- }
- }
- public ChestOperateData(bool received, long chestID, int databaseRoomID)
- {
- Received = received;
- ChestID = chestID;
- DatabaseRoomID = databaseRoomID;
- }
- public override string ToString()
- {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.Append(ChestID);
- stringBuilder.Append("|" + DatabaseRoomID);
- stringBuilder.Append("|" + Received.ToInt());
- if (RemainGuessAmt != null)
- {
- stringBuilder.Append("|" + RemainGuessAmt);
- }
- if (LastActivatedTime != null)
- {
- stringBuilder.Append("|" + LastActivatedTime);
- }
- return stringBuilder.ToString();
- }
- }
- public class ChestManager : Regist
- {
- #region Config
- public static int PlayerMaxGuessAmt = 3;
- private static Text Create_DescriptionText;
- private static Text Create_ButtonText;
- private static Text Create_SliderValueText;
- private static Slider Create_RoundSlider;
- private static Button Create_Button;
- private static InputField Create_InputField;
- private static Button GuessButton;
- private static InputField GuessInputField;
- public static List<PlazaRoomChest> PlazaRoomChests = new List<PlazaRoomChest>();
- public static List<int> RefundRoomIDs = new List<int>();
- public static Dictionary<long, ChestOperateData> OperateDataDictionary = new Dictionary<long, ChestOperateData>();
- #endregion
- public override void FirstInit()
- {
- XmlNodeList RefundNodes = ConfigManager.ConfigRootNode.SelectNodes(PlayerConfigLabel.RefundNode);
- XmlNodeList ReceivedNodes = ConfigManager.ConfigRootNode.SelectNodes(PlayerConfigLabel.ReceivedNode);
- for (int i = 0; i < RefundNodes.Count; i++)
- {
- RefundRoomIDs.Add(int.Parse(RefundNodes[i].InnerText));
- }
- for (int i = 0; i < ReceivedNodes.Count; i++)
- {
- ChestOperateData chestOperateData = new ChestOperateData(ReceivedNodes[i].InnerText);
- OperateDataDictionary.Add(chestOperateData.ChestID, chestOperateData);
- }
- CheckRefund();
- GetChestExpireStatus();
- }
- public override void RegistReference()
- {
- Create_Button = ResourceManager.Get<Button>(CanvasLabel.Yb_Btn);
- GuessButton = ResourceManager.Get<Button>(CanvasLabel.Y_Btn);
- Create_DescriptionText = ResourceManager.Get<Text>(CanvasLabel.Yb_Desc);
- Create_RoundSlider = ResourceManager.Get<Slider>(CanvasLabel.Yb_Slider);
- Create_ButtonText = ResourceManager.Get<Text>(CanvasLabel.Yb_BtnLab);
- Create_InputField = ResourceManager.Get<InputField>(CanvasLabel.Yb_InputField);
- GuessInputField = ResourceManager.Get<InputField>(CanvasLabel.Y_InputField);
- Create_SliderValueText = ResourceManager.Get<Text>(CanvasLabel.Yb_SliderValueLab);
- GuessInputField.onValueChanged.AddListener(OnGuessInputValueChange);
- OnGuessInputValueChange(null);
- }
- public static void OnGuessInputValueChange(string value)
- {
- if (string.IsNullOrEmpty(value))
- {
- GuessButton.image.material = Lib.GrayMat;
- GuessButton.interactable = false;
- }
- else
- {
- GuessButton.image.material = null;
- GuessButton.interactable = true;
- }
- }
- public static void OpenCreateChestPanel()
- {
- ResourceManager.Get(CanvasLabel.Y_Chest).TweenForCG();
- ResourceManager.SetActive(CanvasLabel.Ya_GetAward, false);
- ResourceManager.SetActive(CanvasLabel.Y_Guess, false);
- ResourceManager.SetActive(CanvasLabel.Yb_CreateChest, true);
- OnSliderValueChange(Create_RoundSlider.value);
- OnInputValueChange(Create_InputField.text);
- Create_RoundSlider.value = 1;
- Create_RoundSlider.maxValue = SFSManager.GardenSmartFox.PlazaRoomController.CurrentRoomData.MaxPlayer;
- }
- public static void CloseCreateChestPanel()
- {
- ResourceManager.Get(CanvasLabel.Y_Chest).TweenBacCG();
- }
- public static void OnLuckyToggle(bool enable)
- {
- if (enable)
- {
- ChestType = ChestType.Lucky;
- LanguageManager.Add(Create_DescriptionText, new MulLanStr(LanguageLabel.UI__Yb_LuckyDesc));
- }
- }
- public static void OnColorToggle(bool enable)
- {
- if (enable)
- {
- ChestType = ChestType.GuessColor;
- LanguageManager.Add(Create_DescriptionText, new MulLanStr(LanguageLabel.UI__Yb_GuessColorDesc));
- }
- }
- public static void OnNumberToggle(bool enable)
- {
- if (enable)
- {
- ChestType = ChestType.GuessNumber;
- LanguageManager.Add(Create_DescriptionText, new MulLanStr(LanguageLabel.UI__Yb_GuessNumberDesc));
- }
- }
- public static void OnInputValueChange(string value)
- {
- if (string.IsNullOrEmpty(value) || int.Parse(value) < 20)
- {
- Create_Button.interactable = false;
- Create_Button.image.material = Lib.GrayMat;
- LanguageManager.Add(Create_ButtonText, new MulLanStr(LanguageLabel.UI__Yb_Minimum));
- return;
- }
- Cost = int.Parse(value);
- ResourceManager.SetText(CanvasLabel.Yb_CostLab, Language.GetStr(LanguageLabel.UI__AA_Cost) + TransferLabel.DiamondSprite + Cost);
- if (Manager.Diamond < Cost)
- {
- Create_Button.interactable = false;
- Create_Button.image.material = Lib.GrayMat;
- LanguageManager.Add(Create_ButtonText, new MulLanStr(LanguageLabel.Common__ShortDiamond));
- }
- else
- {
- Create_Button.interactable = true;
- Create_Button.image.material = null;
- LanguageManager.Add(Create_ButtonText, new MulLanStr(LanguageLabel.Common__Confirm));
- }
- }
- public static void OnSliderValueChange(float value)
- {
- Round = (int) value;
- Create_SliderValueText.text = Round + Language.GetStr(LanguageLabel.UI__Yb_SliderValueLab);
- }
- private static int Cost;
- private static int Round;
- private static float ChestXMinOffset = 1;
- private static float ChestXMaxOffset = 2;
- private static ChestType ChestType = ChestType.Lucky;
- public static void CreateChest()
- {
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Y_Tip), new MulLanStr(LanguageLabel.UI__AA_SendRequest));
- ResourceManager.Get(CanvasLabel.Y_Tip).TweenForCG();
- int databaseRoomID = SFSManager.GardenSmartFox.PlazaRoomController.CurrentRoomData.ID;
- long serialNumber = long.Parse(HttpManager.SerialNumber);
- Vector3 position = PlayerPosToChestPos(SFSManager.PlazaRoomController.SelfInstance.Player.transform.position);
- SFSManager.PlazaRoomEvent.CreateChest(Round, Cost, ChestType, databaseRoomID, serialNumber, position);
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Y_Chest).interactable = false;
- }
- public static Vector3 PlayerPosToChestPos(Vector3 playerPos)
- {
- return playerPos + Mathf.Sign(Random.Range(-1f, 1f))*new Vector3(Random.Range(ChestXMinOffset, ChestXMaxOffset), 0, 0);
- }
- public static void OnCreateChestError()
- {
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Y_Tip), new MulLanStr(LanguageLabel.UI__AA_CreateError));
- ResourceManager.Get(CanvasLabel.Y_Tip).TweenBacCG();
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Y_Chest).interactable = true;
- }
- public static void OnCreateChestSucceed()
- {
- Manager.CreateChestAmt++;
- if (ChestType == ChestType.Lucky)
- {
- Manager.CreateLuckyChestAmt++;
- }
- else if (ChestType == ChestType.GuessColor)
- {
- Manager.CreateGuessColorChestAmt++;
- }
- else if (ChestType == ChestType.GuessNumber)
- {
- Manager.CreateGuessNumberChestAmt++;
- }
- LanguageManager.Add(ResourceManager.Get<Text>(CanvasLabel.Y_Tip), new MulLanStr(LanguageLabel.UI__AA_CreateSucceed));
- ResourceManager.Get(CanvasLabel.Y_Tip).TweenBacCG();
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Y_Chest).interactable = true;
- Manager.AddDiamond(-Cost, StaticsManager.ItemID.创建宝箱, StaticsManager.ConsumeModule.Charge);
- CloseCreateChestPanel();
- }
- public static void OnReceiveNewChest(ChestData chestData)
- {
- if (!SFSManager.PlazaRoomController.JoinedPlazaRoom)
- {
- return;
- }
- AudioManager.PlayClip(AudioLabel.Bubble);
- InstantiateChest(chestData);
- if (chestData.Owner == long.Parse(HttpManager.SerialNumber))
- {
- if (SFSManager.PlazaRoomController.CurrentRoomData.ID != ChestData.SystemRoomDatabaseID)
- {
- RefundRoomIDs.UniqueAdd(SFSManager.PlazaRoomController.CurrentRoomData.ID);
- }
- }
- }
- private static void CheckRefund()
- {
- if (RefundRoomIDs.Count == 0)
- {
- return;
- }
- else
- {
- SFSManager.PlazaRoomEvent.CheckChestRefund(RefundRoomIDs);
- }
- }
- private static void GetChestExpireStatus()
- {
- if (OperateDataDictionary.Keys.Count == 0)
- {
- return;
- }
- SFSManager.PlazaRoomEvent.GetChestExpireStatus(OperateDataDictionary.Keys.ToList());
- }
- public static void OnReceiveChestExpireStatus(List<long> chestIDs)
- {
- foreach (var chestID in chestIDs)
- {
- //Debug.LogWarning("expire " + chestID);
- OperateDataDictionary.Remove(chestID);
- }
- }
- public static void RetrieveAllChest()
- {
- foreach (var plazaRoomChest in PlazaRoomChests)
- {
- plazaRoomChest.RetriveChest();
- }
- PlazaRoomChests = new List<PlazaRoomChest>();
- }
- private static void InstantiateChest(ChestData chestData)
- {
- if (chestData.ChestType != ChestType.System)
- {
- if (chestData.RemainRound <= 0 || chestData.RemainValue <= 0)
- {
- return;
- }
- ChestOperateData chestOperateData;
- OperateDataDictionary.TryGetValue(chestData.ID, out chestOperateData);
- if (chestOperateData == null || chestOperateData.Received == false)
- {
- PlazaRoomChest chest = ResourceManager.GetPlazaRoomChest(chestData.Position);
- chest.Init(chestData);
- PlazaRoomChests.Add(chest);
- if (chestOperateData == null || chestOperateData.RemainGuessAmt == null || chestOperateData.RemainGuessAmt > 0)
- {
- chest.Active();
- chest.TurnNormalColor();
- }
- else
- {
- chest.Deactive();
- chest.TurnGray();
- }
- }
- }
- else
- {
- PlazaRoomChest chest = ResourceManager.GetPlazaRoomChest(chestData.Position);
- chest.SystemChestInit(chestData);
- PlazaRoomChests.Add(chest);
- }
- }
- private static int SystemChestActivateTimespan = 600;
- public static void ReceiveAllChestData(List<ChestData> chestDatas)
- {
- AudioManager.PlayClip(AudioLabel.Bubble);
- foreach (var chestData in chestDatas)
- {
- InstantiateChest(chestData);
- }
- }
- public static void ActivateSystemChest(DateTime activatedTime)
- {
- if (!SFSManager.GardenSmartFox.PlazaRoomController.JoinedPlazaRoom)
- {
- return;
- }
- if (PlazaRoomChest.SystemChest == null)
- {
- return;
- }
- if (OperateDataDictionary.Keys.Contains(PlazaRoomChest.SystemChest.ChestData.ID))
- {
- double timeSinceLastActivated = activatedTime.Subtract(OperateDataDictionary[PlazaRoomChest.SystemChest.ChestData.ID].LastActivatedTime.Value).TotalSeconds;
- if (timeSinceLastActivated >= SystemChestActivateTimespan)
- {
- ReactivateSystemChest(activatedTime);
- }
- return;
- }
- PlazaRoomChest.SystemChest.Active();
- PlazaRoomChest.SystemChest.TurnNormalColor();
- PlazaRoomChest.SystemChest.ChestTimerTransform.TweenBacCG();
- }
- public static void ReactivateSystemChest(DateTime activatedTime)
- {
- if (!SFSManager.GardenSmartFox.PlazaRoomController.JoinedPlazaRoom)
- {
- return;
- }
- if (PlazaRoomChest.SystemChest == null)
- {
- return;
- }
- OperateDataDictionary.Remove(PlazaRoomChest.SystemChest.ChestData.ID);
- PlazaRoomChest.SystemChest.Active();
- PlazaRoomChest.SystemChest.TurnNormalColor();
- PlazaRoomChest.SystemChest.ChestTimerTransform.TweenBacCG();
- PlazaRoomChest.SystemChest.LastActivatedTime = activatedTime;
- }
- public static void DeactivateSystemChest(long pasttime)
- {
- if (!SFSManager.GardenSmartFox.PlazaRoomController.JoinedPlazaRoom)
- {
- return;
- }
- if (PlazaRoomChest.SystemChest == null)
- {
- return;
- }
- PlazaRoomChest.SystemChest.Deactive();
- PlazaRoomChest.SystemChest.TurnGray();
- PlazaRoomChest.SystemChest.ChestRefreshTimer = PlazaRoomChest.SystemChest.ChestRefreshTime - pasttime + 1;
- }
- public static void ReceiveChestAward(int award, long chestID)
- {
- ChestOperateData chestOperateData;
- if (OperateDataDictionary.TryGetValue(chestID, out chestOperateData))
- {
- chestOperateData.Received = true;
- }
- else
- {
- chestOperateData = new ChestOperateData(true, PlazaRoomChest.SelectedChest.ChestData.ID, PlazaRoomChest.SelectedChest.ChestData.DatabaseRoomID);
- OperateDataDictionary.Add(chestOperateData.ChestID, chestOperateData);
- }
- ResourceManager.Get<CanvasGroup>(CanvasLabel.Y_Chest).interactable = true;
- if (award <= 0)
- {
- ResourceManager.Get(CanvasLabel.Y_Chest).TweenBacCG();
- Bubble.Show(null, Language.GetStr(LanguageLabel.UI__Y_SoldOut));
- }
- else
- {
- AudioManager.PlayClip(AudioLabel.GetCurrent);
- ResourceManager.Get(CanvasLabel.Y_Chest).TweenForCG();
- ResourceManager.SetActive(CanvasLabel.Ya_GetAward, true);
- ResourceManager.SetActive(CanvasLabel.Y_Guess, false);
- ResourceManager.SetActive(CanvasLabel.Yb_CreateChest, false);
- ResourceManager.SetText(CanvasLabel.Ya_Desc, $"x{award}");
- Manager.AddDiamond(award, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.RoomChestAward);
- Manager.GetChestAwardAmt++;
- }
- for (int i = 0; i < PlazaRoomChests.Count; i++)
- {
- if (PlazaRoomChests[i].ChestData.ID == chestID)
- {
- if (PlazaRoomChests[i].ChestData.ChestType == ChestType.System)
- {
- PlazaRoomChests[i].ResetChestRefreshTimer();
- OperateDataDictionary[chestID].RemainGuessAmt = 0;
- OperateDataDictionary[chestID].LastActivatedTime = PlazaRoomChests[i].LastActivatedTime;
- }
- else
- {
- PlazaRoomChests[i].RetriveChest();
- PlazaRoomChests.RemoveAt(i--);
- }
- }
- }
- }
- public static void ReceiveChestRefund(int refund, List<int> databaseRoomIDs)
- {
- foreach (var databaseRoomID in databaseRoomIDs)
- {
- RefundRoomIDs.Remove(databaseRoomID);
- }
- if (refund > 0)
- {
- Manager.AddDiamond(refund, StaticsManager.ItemID.获得钻石, StaticsManager.ConsumeModule.RoomChestRefund);
- InfoBoxManager.GardenInfoBox.Display(Language.GetStr(LanguageLabel.UI__Y_Refund)+refund, 10, Color.white, ResourceManager.LoadSprite("Atlas", Folder.Atlas));
- }
- }
- public static void SaveToConfig()
- {
- ClearRefundAndOperateData(ConfigManager.ConfigRootNode);
- foreach (var id in RefundRoomIDs)
- {
- XmlNode node = ConfigManager.ConfigDocument.CreateNode(XmlNodeType.Element, PlayerConfigLabel.RefundNode, null);
- node.InnerText = id.ToString();
- ConfigManager.ConfigRootNode.AppendChild(node);
- }
- foreach (var kv in OperateDataDictionary)
- {
- XmlNode node = ConfigManager.ConfigDocument.CreateNode(XmlNodeType.Element, PlayerConfigLabel.ReceivedNode, null);
- node.InnerText = kv.Value.ToString();
- ConfigManager.ConfigRootNode.AppendChild(node);
- //Debug.LogWarning($"{kv.Key}|{kv.Value}");
- }
- }
- public static void ClearRefundAndOperateData(XmlNode playerConfigNode)
- {
- XmlNode node = playerConfigNode.SelectSingleNode(PlayerConfigLabel.RefundNode);
- while (node != null)
- {
- playerConfigNode.RemoveChild(node);
- node = playerConfigNode.SelectSingleNode(PlayerConfigLabel.RefundNode);
- }
- node = playerConfigNode.SelectSingleNode(PlayerConfigLabel.ReceivedNode);
- while (node != null)
- {
- playerConfigNode.RemoveChild(node);
- node = playerConfigNode.SelectSingleNode(PlayerConfigLabel.ReceivedNode);
- }
- }
- }
|