From c1d1fa56b5706ac5d93f0eb2a1a25df6ae412870 Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Mon, 17 Mar 2025 21:05:22 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=B3=80=EA=B2=BD=EB=90=9C=20=EC=8A=A4?= =?UTF-8?q?=ED=82=AC=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EA=B0=80=20=EC=A0=95?= =?UTF-8?q?=EC=83=81=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EB=A7=A4=ED=95=91?= =?UTF-8?q?=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Backend/BackendSkillDataManager.cs | 133 ++++++++++++++++++ .../Backend/BackendSkillDataManager.cs.meta | 11 ++ .../Gameton/Scripts/GameData/UserSkillData.cs | 22 +++ .../Scripts/GameData/UserSkillData.cs.meta | 11 ++ .../Gameton/Scripts/Skill/SkillDataManager.cs | 95 +++++++++++-- .../Gameton/Scripts/UI/SkillSettingUI.cs | 18 +-- 6 files changed, 272 insertions(+), 18 deletions(-) create mode 100644 Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs create mode 100644 Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs.meta create mode 100644 Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs create mode 100644 Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs.meta diff --git a/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs new file mode 100644 index 00000000..d72683e1 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs @@ -0,0 +1,133 @@ +using System.Collections; +using System.Collections.Generic; +using BackEnd; +using UnityEngine; + +namespace TON +{ + /// + /// 뒤끝 서버 사용자 스킬 슬롯 데이터 관리 담당 클래스 + /// + public class BackendSkillDataManager + { + // 테이블 이름 상수 + private const string SKILL_TABLE = "USER_SKILL_DATA"; + + // 내 스킬 슬롯롯 데이터 조회 + public void LoadMySkillData(System.Action onComplete) + { + UserSkillData userSkillData = new UserSkillData(); + + Backend.PlayerData.GetMyData(SKILL_TABLE, callback => + { + if (callback.IsSuccess() == false) + { + Debug.Log("스킬 슬롯 정보 읽기 중에 문제가 발생했습니다 : " + callback.ToString()); + onComplete?.Invoke(userSkillData); + return; + } + + // 불러오기에는 성공했으나 데이터가 존재하지 않는 경우 + if (callback.IsSuccess() && callback.FlattenRows().Count <= 0) + { + Debug.Log("데이터가 존재하지 않습니다"); + InsertInitData(userSkillData, () => + { + // 초기 데이터 삽입 후 다시 데이터를 불러옴 + LoadDataAfterInsert(onComplete); + }); + return; + } + + if (callback.IsSuccess()) + { + // 데이터를 SkillData 객체로 변환합니다. + userSkillData.slot_1 = callback.FlattenRows()[0]["slot_1"].ToString() ?? string.Empty; + userSkillData.slot_2 = callback.FlattenRows()[0]["slot_2"].ToString() ?? string.Empty; + userSkillData.slot_3 = callback.FlattenRows()[0]["slot_3"].ToString() ?? string.Empty; + onComplete?.Invoke(userSkillData); + } + else + { + Debug.LogError("Failed to load skill data: " + callback.GetMessage()); + onComplete?.Invoke(null); + } + }); + + } + + // 데이터 삽입 후 다시 불러오는 메소드 + private void LoadDataAfterInsert(System.Action onComplete) + { + Backend.PlayerData.GetMyData(SKILL_TABLE, callback => + { + UserSkillData skillData = new UserSkillData(); + + if (callback.IsSuccess() && callback.FlattenRows().Count > 0) + { + skillData.slot_1 = callback.FlattenRows()[0]["slot_1"].ToString() ?? string.Empty; + skillData.slot_2 = callback.FlattenRows()[0]["slot_2"].ToString() ?? string.Empty; + skillData.slot_3 = callback.FlattenRows()[0]["slot_3"].ToString() ?? string.Empty; + } + + onComplete?.Invoke(skillData); + }); + } + + + /// + /// 스킬 슬롯 데이터 초기 row 삽입 + /// + private void InsertInitData(UserSkillData skillData, System.Action onComplete = null) + { + if (PlayerDataManager.Singleton.player == null) + { + return; + } + + Param param = new Param(); + param.Add("slot_1", skillData.slot_1); + param.Add("slot_2", skillData.slot_2); + param.Add("slot_3", skillData.slot_3); + + Backend.PlayerData.InsertData(SKILL_TABLE, param, callback => + { + if (callback.IsSuccess()) + { + Debug.Log("초기 스킬 데이터 삽입 성공"); + } + else + { + Debug.LogError("초기 스킬 데이터 삽입 실패: " + callback.ToString()); + } + + onComplete?.Invoke(); + }); + } + + /// + /// 스킬 슬롯 정보 업데이트 + /// + public void UpdateSkillData(UserSkillData skillData, System.Action onComplete = null) + { + Param param = new Param(); + param.Add("slot_1", skillData.slot_1 ?? string.Empty); + param.Add("slot_2", skillData.slot_2 ?? string.Empty); + param.Add("slot_3", skillData.slot_3 ?? string.Empty); + + Backend.PlayerData.UpdateMyLatestData(SKILL_TABLE, param, callback => + { + if (callback.IsSuccess()) + { + Debug.Log("스킬 정보 업데이트 성공"); + onComplete?.Invoke(); + } + else + { + Debug.LogError("스킬 정보 업데이트 실패: " + callback.ToString()); + } + + }); + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs.meta new file mode 100644 index 00000000..2197ed86 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Backend/BackendSkillDataManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04490730ace91bd4bb4659c90cebc8b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs b/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs new file mode 100644 index 00000000..c34084fe --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs @@ -0,0 +1,22 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TON +{ + [System.Serializable] + public class UserSkillData + { + + public string slot_1; + public string slot_2; + public string slot_3; + + public UserSkillData() + { + slot_1 = "K0001"; + slot_2 = string.Empty; + slot_3 = string.Empty; + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs.meta b/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs.meta new file mode 100644 index 00000000..fa1bb088 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/GameData/UserSkillData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f73cb0db91f6ba244869f551e8091e79 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Scripts/Skill/SkillDataManager.cs b/Gameton-06/Assets/Gameton/Scripts/Skill/SkillDataManager.cs index e55d2417..b6f471f3 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Skill/SkillDataManager.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Skill/SkillDataManager.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; using UnityEngine; using UnityEngine.Assertions; @@ -7,21 +8,48 @@ namespace TON { public class SkillDataManager : SingletonBase { + public List skillDatas { get; private set; } public SerializableDictionary skillInstances { get; private set; } private List equippedSkills = new List(); + private BackendSkillDataManager skillDataManager; + + // 각 단계별 이벤트 정의 + private event System.Action OnDataLoadComplete; + private event System.Action OnSetupComplete; + + + public void Initalize(System.Action onComplete = null) - public void Initalize() { + skillDataManager = new BackendSkillDataManager(); + + // 각 단계별 이벤트 등록 + OnDataLoadComplete = () => + { + SetSkillInstances(); + OnSetupComplete?.Invoke(); + }; + + OnSetupComplete = () => + { + GetActiveSkillInstance(); + // 장착된 스킬이 변경된 경우, UI 업데이트 실행할 수 있도록 onComplete 이벤트 등록 + onComplete?.Invoke(); + }; + + // 첫 단계 시작 LoadSkillData(); - SetSkillInstances(); - GetActiveSkillInstance(); } private void Update() { - // 씬이 인게임일때만 돌게 조건 추가 (stage 이름을 가지고 잇을대?) + SceneType activeScene = Main.Singleton.currentSceneType; + // 씬이 인게임일 때만 스킬 쿨타임 업데이트 되도록 적용 + if (activeScene != SceneType.Stage) + return; + foreach (var skill in equippedSkills) { UpdateSkillCoolDown(skill.SkillData.id); @@ -35,8 +63,9 @@ namespace TON skillDatas.Clear(); } - JSONLoader.SaveJsonToPersistentData("skill"); - skillDatas = JSONLoader.LoadJsonFromPersistentData>("skill"); + // gamedata 폴더에 있는 skill.json 파일을 불러옴 + skillDatas = JSONLoader.LoadFromResources>("skill"); + GetSkillDataList(); if (skillDatas == null) { @@ -44,8 +73,33 @@ namespace TON } } - public void UpdateSkillData(string skillId, int slotNumber) + private void GetSkillDataList() { + + // 서버에 저장된 사용자의 스킬 슬롯 데이터를 가져옴 + skillDataManager.LoadMySkillData(userSkillData => + { + // 스킬 슬롯 데이터가 없는 경우 + if (userSkillData == null) + { + Debug.LogError("스킬 슬롯 데이터가 없습니다"); + return; + } + + // 스킬 슬롯 데이터가 있는 경우 + UpdateSkillData(userSkillData.slot_1, 1); + UpdateSkillData(userSkillData.slot_2, 2); + UpdateSkillData(userSkillData.slot_3, 3); + + // 다음 단계로 진행 + OnDataLoadComplete?.Invoke(); + }); + + } + + public void UpdateSkillData(string skillId, int slotNumber, System.Action onComplete = null) + { + // 현재 슬롯에 스킬이 있는지 확인 foreach (var skill in skillDatas) { if (skill.id == skillId) @@ -58,8 +112,29 @@ namespace TON } } - Assert.IsTrue(JSONLoader.SaveUpdatedJsonToPersistentData(skillDatas, "skill")); - Initalize(); + // 스킬 슬롯 데이터를 서버에 저장 + if (onComplete != null) + { + UpdateSkillSlotDataToServer(onComplete); + } + + } + + private void UpdateSkillSlotDataToServer(System.Action onComplete) + { + UserSkillData userSkillData = new() + { + slot_1 = skillDatas.Find(skill => skill.slotNumber == 1)?.id, + slot_2 = skillDatas.Find(skill => skill.slotNumber == 2)?.id, + slot_3 = skillDatas.Find(skill => skill.slotNumber == 3)?.id + }; + + Debug.Log($"UpdateSkillSlotDataToServer() : {userSkillData.slot_1}, {userSkillData.slot_2}, {userSkillData.slot_3}"); + + skillDataManager.UpdateSkillData(userSkillData, () => + { + Initalize(onComplete); + }); } public void SetSkillInstances() @@ -72,7 +147,7 @@ namespace TON } } - // 스킬 슬롯에 배치할 수 있는 스킬 수 리턴하는 메소드 + // 스킬 슬롯에 배치할 수 있는 스킬 수 리턴하는 메소드 public int GetActiveSkillCount() { int characterLevel = PlayerDataManager.Singleton.player.level; diff --git a/Gameton-06/Assets/Gameton/Scripts/UI/SkillSettingUI.cs b/Gameton-06/Assets/Gameton/Scripts/UI/SkillSettingUI.cs index 5a3715b2..65b87952 100644 --- a/Gameton-06/Assets/Gameton/Scripts/UI/SkillSettingUI.cs +++ b/Gameton-06/Assets/Gameton/Scripts/UI/SkillSettingUI.cs @@ -174,17 +174,19 @@ namespace TON public void OnClickSettingButton() { // 스킬 데이터 업데이트 할때 selectedSlotIndex +1 해서 넘겨줘야함 - SkillDataManager.Singleton.UpdateSkillData(selectedSkillId, selectedSlotIndex + 1); + SkillDataManager.Singleton.UpdateSkillData(selectedSkillId, selectedSlotIndex + 1, () => + { + // 스킬 업데이트 후 UI 갱신 + var unselectedSkill = createSkillInfo.Find(skill => skill.skillId == selectedSkillId); + unselectedSkill?.UnselectedSkillInfo(); + createSkillSlots[selectedSlotIndex].UnselectedSlot(); - // 스킬 업데이트 후 UI 갱신 - var unselectedSkill = createSkillInfo.Find(skill => skill.skillId == selectedSkillId); - unselectedSkill?.UnselectedSkillInfo(); - createSkillSlots[selectedSlotIndex].UnselectedSlot(); + selectedSkillId = null; + selectedSlotIndex = -1; - selectedSkillId = null; - selectedSlotIndex = -1; + RefreshUI(); + }); - RefreshUI(); } private void RefreshUI()