using asap.core; using cfg; using GameCore; using System.Collections.Generic; using UnityEngine; public class InfiniteBuildingData { public int Lvl, Step, StepLvl = 1, LastHangingBlock = -1; public List Record = new List(); // private readonly TbConstructionUnits BlocksTable = GContext.container.Resolve().TbConstructionUnits; public int ConstructPrice => (int) (GContext.container.Resolve().TbMapData.DataList[^1].ConstrutionCost * GContext.container.Resolve().TbConstructionInfiniteConfig.CashMagList[Step]); public struct BlockRecord { public readonly int ID; public Vector2 Pos; public readonly float Height => GContext.container.Resolve().TbConstructionUnits[ID].Height; public readonly string PrefabID => GContext.container.Resolve().TbConstructionUnits[ID].Prefab; public BlockRecord(int id = 0, Vector2 pos = default) { this.ID = id; this.Pos = pos; } public BlockRecord(int id = 0, float x = 0, float y = 0) { this.ID = id; Pos = new Vector2(x, y); } public readonly override string ToString() => $"{ID},{Pos.x},{Pos.y}"; } private string Serialize() { var sb = new System.Text.StringBuilder(); sb.Append($"{Lvl},{Step},{StepLvl},{LastHangingBlock},"); foreach (BlockRecord node in Record) sb.Append($"{node.ToString()},"); return sb.ToString().Trim(','); } public void Deserialize(string s) { string[] tokens = s.Split(','); if (tokens == null || tokens.Length < 4 || tokens.Length % 3 != 1) { Debug.LogWarning($"string \"{s}\" cannot be deserialized."); return; } Lvl = int.Parse(tokens[0]); Step = int.Parse(tokens[1]); StepLvl = int.Parse(tokens[2]); LastHangingBlock = int.Parse(tokens[3]); Record = new List(); for (int i = 4; i < tokens.Length; i+=3) { Record.Add(new BlockRecord(int.Parse(tokens[i]), float.Parse(tokens[i + 1]), float.Parse(tokens[i + 2]))); //Debug.Log($"{int.Parse(tokens[i])}, {float.Parse(tokens[i + 1])}, {float.Parse(tokens[i + 2])}"); } } public void UploadData() { string s = Serialize(); //Debug.Log("Upload!!!!!!!!!!!!!!"); //Debug.Log(s); GContext.container.Resolve().InfiniteBuildingData = s; PlayFabMgr.Instance.UpdateUserDataValue("InfiniteBuildingData", s); } public void DownloadData(string s) { Deserialize(s); } #if UNITY_EDITOR public void PrintSelf() { Debug.Log($"Lvl: {Lvl}"); Debug.Log($"Step: {Step}"); Debug.Log($"StepLvl: {StepLvl}"); foreach (BlockRecord node in Record) { Debug.Log($"node: {node.PrefabID}, {node.Pos}"); } } #endif }