using Gs2.Unity.Util; using System; using System.Threading.Tasks; using UnityEngine; using GS2Studio.Generated.Quest; using GS2Studio.Generated.Progress; using GS2Studio.Generated.Wallet; using GS2Studio.Generated.Energy; using GS2Studio.Generated.LoginRewardCollection; using GS2Studio.Generated.Mission; using GS2Studio.Generated.MissionCounter; public sealed class AdventureStudioView : MonoBehaviour { [SerializeField] private QuestListHandler quests; private bool busy; private string purchaseId; private bool unresolved; private string message = ""; [SerializeField] private ProgressHandler progress; [SerializeField] private WalletHandler wallet; [SerializeField] private EnergyHandler energy; [SerializeField] private LoginRewardCollectionHandler loginReward; [SerializeField] private MissionHandler mission; [SerializeField] private MissionCounterHandler counter; [SerializeField] private string missionGroup = "adventure"; [SerializeField] private string missionName = "clear3"; private void Awake() { loginReward.SetKeys("daily"); mission.SetKeys(missionName, missionGroup); mission.SetSdkKeys(missionGroup); counter.SetKeys("clear"); } private void OnEnable() { if (quests != null) quests.Failed += ShowLoadError; if (progress != null) progress.Failed += ShowLoadError; if (wallet != null) wallet.Failed += ShowLoadError; if (energy != null) energy.Failed += ShowLoadError; loginReward.Failed += ShowLoadError; mission.Failed += ShowLoadError; counter.Failed += ShowLoadError; } private void OnDisable() { if (quests != null) quests.Failed -= ShowLoadError; if (progress != null) progress.Failed -= ShowLoadError; if (wallet != null) wallet.Failed -= ShowLoadError; if (energy != null) energy.Failed -= ShowLoadError; loginReward.Failed -= ShowLoadError; mission.Failed -= ShowLoadError; counter.Failed -= ShowLoadError; } private void ShowLoadError(Exception error) { message = "불러오지 못했습니다: " + error.GetType().Name; } private void OnGUI() { var sessionHolder = Gs2GameSessionHolder.Instance; if (quests == null || sessionHolder == null || !sessionHolder.Initialized) { return; } GUILayout.BeginArea(new Rect(20, 100, 650, 650), GUI.skin.box); GUILayout.Label(message); if (quests.IsLoaded) { DrawQuestButtons(); DrawPlayerStatus(); DrawShop(); DrawDailyRewards(); } else { GUILayout.Label("모험 로딩 중"); } GUI.enabled = true; GUILayout.EndArea(); } private void DrawQuestButtons() { var canOperate = !busy && !unresolved && progress.HasValue; GUI.enabled = canOperate && !progress.Model.InProgress; foreach (var quest in quests.Binders) { GUILayout.Label(quest.DisplayName); if (GUILayout.Button("시작: " + quest.DisplayName)) { Run(() => quest.Start()); } } GUI.enabled = canOperate && progress.Model.InProgress; if (GUILayout.Button("클리어")) { Run(() => progress.Binder.Complete()); } GUI.enabled = true; } private void DrawPlayerStatus() { GUILayout.Label(wallet.HasValue ? "코인: " + wallet.Model.Free : "코인 로딩 중"); if (!progress.HasValue) { GUILayout.Label("진행 상황 로딩 중"); } else { GUILayout.Label(progress.Model.InProgress ? "모험 진행 중" : "모험을 선택하세요"); } GUILayout.Label(energy.HasValue ? "스태미나: " + energy.Model.CurrentValue : "스태미나 로딩 중"); } private void DrawShop() { GUI.enabled = !busy && (!unresolved || purchaseId != null); if (GUILayout.Button(purchaseId == null ? "20코인으로 스태미나 10 회복" : "같은 구매 재시도")) { Run(Buy, retryPurchase: purchaseId != null); } GUI.enabled = true; } private void DrawDailyRewards() { GUILayout.Space(12); GUILayout.Label("로그인 보너스와 미션"); if (!loginReward.HasValue || !mission.HasValue || !counter.HasValue) { GUILayout.Label("보상 정보 로딩 중"); return; } var steps = loginReward.Model.ReceivedSteps; for (var day = 0; day < 3; day++) { var received = steps != null && day < steps.Count && steps[day]; GUILayout.Label($"{day + 1}일 차: {(day + 1) * 10}코인 / " + (received ? "받음" : "아직 안 받음")); } GUI.enabled = !busy && !unresolved; if (GUILayout.Button("오늘의 로그인 보너스 받기")) Run(() => loginReward.Binder.Receive()); GUILayout.Label($"모험 누적 클리어 횟수: {counter.Model.TotalValue}"); GUILayout.Label(mission.Model.Received ? "받음" : mission.Model.Completed ? "미션 보상 30코인을 받을 수 있습니다" : "미션 도전 중"); GUI.enabled = !busy && !unresolved && mission.Model.Completed && !mission.Model.Received; if (GUILayout.Button("미션 보상 30코인 받기")) Run(() => mission.Binder.Receive()); GUI.enabled = true; } private async void Run(Func operation, bool retryPurchase = false) { if (busy || (unresolved && !retryPurchase)) return; busy = true; unresolved = true; message = "처리 중"; try { await operation(); unresolved = false; message = "작업이 완료되었습니다"; } catch (Exception error) { if (error is Gs2.Gs2Stamina.Exception.InsufficientException || error is Gs2.Gs2Money2.Exception.InsufficientException) { unresolved = false; purchaseId = null; message = error is Gs2.Gs2Stamina.Exception.InsufficientException ? "스태미나가 부족합니다. 회복한 뒤 다시 시도하세요" : "코인이 부족합니다. 모험에서 모은 뒤 다시 시도하세요"; } else if (error is Gs2.Core.Exception.BadRequestException) { unresolved = false; purchaseId = null; message = "작업을 실행하지 못했습니다: " + error.Message; } else message = purchaseId != null ? "연결을 확인하고 같은 구매를 재시도하세요: " + error.GetType().Name : "결과를 확인할 수 없어 새 작업을 중지했습니다: " + error.GetType().Name; } finally { busy = false; } } private async Task Buy() { purchaseId ??= Guid.NewGuid().ToString(); var request = Gs2ClientHolder.Instance.Gs2.Exchange.Namespace("EnergyProduct") .Me(Gs2GameSessionHolder.Instance.GameSession).Exchange(); var transaction = await request.ExchangeAsync( "refill", 1, speculativeExecute: false, duplicationAvoider: purchaseId); if (transaction != null) await transaction.WaitAsync(all: true); purchaseId = null; } }