287 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			287 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using MMO;
 | |
| 
 | |
| public partial class MMOModule : MonoBehaviour
 | |
| {
 | |
|     public event Action OnExit;
 | |
|     public event Action<int> OnDiamondUpdate;
 | |
|     public event Action OnWatchAd;
 | |
|     public event Action OnOpenMoreGames;
 | |
| 
 | |
|     public event Action<string, string, string> OnEvengLog;
 | |
| 
 | |
|     private Action mDelInitOver;
 | |
|     private Action mDelCheckOver;
 | |
| 
 | |
|     private bool mIsOn = false;
 | |
| 
 | |
|     private int mStartDiamond = 0;
 | |
|     private int mStartLevel = 0;
 | |
| 
 | |
|     public bool IsGuideFinished => MMOUserData.Instance.IsGuideFinished;
 | |
| 
 | |
|     private static MMOModule mInstance = null;
 | |
|     public static MMOModule Instance
 | |
|     {
 | |
|         get
 | |
|         {
 | |
|             if (mInstance == null)
 | |
|             {
 | |
|                 GameObject tMMOGO = Instantiate(Resources.Load(nameof(MMOModule))) as GameObject;
 | |
|                 mInstance = tMMOGO.AddComponent<MMOModule>();
 | |
|                 tMMOGO.SetActive(false);
 | |
|                 DontDestroyOnLoad(tMMOGO);
 | |
|             }
 | |
| 
 | |
|             return mInstance;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private MMOUIManager mUIMgr;
 | |
|     public MMOUIManager UIMgr
 | |
|     {
 | |
|         get
 | |
|         {
 | |
|             if (mUIMgr == null)
 | |
|             {
 | |
|                 mUIMgr = GetComponentInChildren<MMOUIManager>(true);
 | |
|             }
 | |
| 
 | |
|             return mUIMgr;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private bool mIsMMOUser
 | |
|     {
 | |
|         get => PlayerPrefs.GetInt(nameof(mIsMMOUser), 0) == 1;
 | |
|         set => PlayerPrefs.SetInt(nameof(mIsMMOUser), value ? 1 : 0);
 | |
|     }
 | |
| 
 | |
|     public void Init(Action pDelInitDone)
 | |
|     {
 | |
|         if (MMOUserData.Instance.TaskRecordList.Count <= 0)
 | |
|         {
 | |
|             MMOUserData.Instance.GenerateTaskRecord(101);
 | |
|             MMOUserData.Instance.GenerateTaskRecord(201);
 | |
|         }
 | |
| 
 | |
|         MMOUserData.Instance.CheckTask();
 | |
| 
 | |
|         mDelInitOver = pDelInitDone;
 | |
| 
 | |
|         gameObject.SetActive(true);
 | |
|         MMOUILoading tUILoading = UIMgr.OpenUI<MMOUILoading>();
 | |
|         tUILoading.DelInitOver = mDelInitOver;
 | |
|         tUILoading.DelLoadOver = OnLoadOver;
 | |
|     }
 | |
| 
 | |
|     public void StartGame(Action pDelCheckOver, bool pIsMMOUser, int pDiamond, int pLevel)
 | |
|     {
 | |
|         mIsMMOUser = pIsMMOUser;
 | |
|         OnEvengLog?.Invoke("Robux_" + mIsMMOUser, "Robux", mIsMMOUser.ToString());
 | |
| 
 | |
|         mDelCheckOver = pDelCheckOver;
 | |
| 
 | |
|         mStartDiamond = pDiamond;
 | |
|         mStartLevel = pLevel;
 | |
| 
 | |
|         if (UIMgr.GetUI<MMOUILoading>().IsOpened)
 | |
|         {
 | |
|             UIMgr.GetUI<MMOUILoading>().FinishRest();
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             CheckGuide();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void OnLoadOver()
 | |
|     {
 | |
|         UIMgr.CloseUI<MMOUILoading>();
 | |
|         gameObject.SetActive(false);
 | |
|         MMOUserData.Instance.FinishLoading();
 | |
| 
 | |
|         CheckGuide();
 | |
|     }
 | |
| 
 | |
|     private void CheckGuide()
 | |
|     {
 | |
|         if (mIsMMOUser && !MMOUserData.Instance.IsGuideFinished)
 | |
|         {
 | |
|             Enter(mStartDiamond, mStartLevel);
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             mDelCheckOver?.Invoke();
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void OnGuideOver()
 | |
|     {
 | |
|         mDelCheckOver?.Invoke();
 | |
|         Exit();
 | |
|     }
 | |
| 
 | |
|     public void Enter(int pDiamond, int pGameLevel)
 | |
|     {
 | |
|         gameObject.SetActive(true);
 | |
| 
 | |
|         MMOUserData.Instance.DelUpdateDiamond = UpdateDiamond;
 | |
|         MMOUserData.Instance.Diamond = pDiamond;
 | |
|         MMOUserData.Instance.DelUpdateLevel = UpdateLevel;
 | |
|         MMOUserData.Instance.GameLevel = pGameLevel;
 | |
| 
 | |
|         UIMgr.OpenUI<MMOUITop>().DelOpenMoreGames = OpenMoreGames;
 | |
|         UIMgr.OpenUI<MMOUIMain>().DelWatchAd = WatchAd;
 | |
| 
 | |
|         if (!MMOUserData.Instance.IsGuideFinished)
 | |
|         {
 | |
|             UIMgr.OpenUI<MMOUIGuide>().DelGuideOver = OnGuideOver;
 | |
|         }
 | |
| 
 | |
|         mIsOn = true;
 | |
| 
 | |
|         LogEvent(MMOEvent.EnterMMO);
 | |
|     }
 | |
| 
 | |
|     public void Exit()
 | |
|     {
 | |
|         mIsOn = false;
 | |
| 
 | |
|         UIMgr.CloseAll();
 | |
|         gameObject.SetActive(false);
 | |
| 
 | |
|         OnExit?.Invoke();
 | |
|     }
 | |
| 
 | |
|     public void ShowTip(string pTipString)
 | |
|     {
 | |
|         if (mIsOn)
 | |
|         {
 | |
|             UIMgr.OpenUI<MMOUITip>().SetTip(pTipString);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void LogEvent(string pEvent)
 | |
|     {
 | |
|         OnEvengLog?.Invoke("Robux_Action", "Robux", pEvent);
 | |
|     }
 | |
| 
 | |
|     public void PassLevel()
 | |
|     {
 | |
|         for (int i = 0; i < MMOUserData.Instance.TaskRecordList.Count; i++)
 | |
|         {
 | |
|             MMOTaskRecord tRecord = MMOUserData.Instance.TaskRecordList[i];
 | |
|             if (tRecord.TaskType == MMOTaskType.DailyLevel)
 | |
|             {
 | |
|                 int tValue = MMOUserData.Instance.GetTaskValue(tRecord.RecordKey);
 | |
|                 MMOUserData.Instance.SetTaskValue(tRecord.RecordKey, tValue + 1);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public void WatchAdSuccessfully()
 | |
|     {
 | |
|         for (int i = 0; i < MMOUserData.Instance.TaskRecordList.Count; i++)
 | |
|         {
 | |
|             MMOTaskRecord tRecord = MMOUserData.Instance.TaskRecordList[i];
 | |
|             if (tRecord.TaskType == MMOTaskType.WatchAds || tRecord.TaskType == MMOTaskType.DailyAds)
 | |
|             {
 | |
|                 int tValue = MMOUserData.Instance.GetTaskValue(tRecord.RecordKey);
 | |
|                 MMOUserData.Instance.SetTaskValue(tRecord.RecordKey, tValue + 1);
 | |
|             }
 | |
|             else if (tRecord.TaskType == MMOTaskType.RiseRank)
 | |
|             {
 | |
|                 int tValue = MMOUserData.Instance.GetTaskValue(tRecord.RecordKey);
 | |
|                 MMOUserData.Instance.SetTaskValue(tRecord.RecordKey, tValue + 50);
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         UIMgr.GetUI<MMOUIMain>().RefreshTaskList();
 | |
|     }
 | |
| 
 | |
|     public int GetRewardDiamondOfLevel(int pDiamond, int pLevel)
 | |
|     {
 | |
|         int tTarget = 0;
 | |
|         for (int i = 0; i < MMOUserData.Instance.TaskRecordList.Count; i++)
 | |
|         {
 | |
|             if (MMOUserData.Instance.TaskRecordList[i].TaskType == MMOTaskType.GetDiamond)
 | |
|             {
 | |
|                 if (!MMOUserData.Instance.TaskRecordList[i].IsFinished)
 | |
|                 {
 | |
|                     tTarget = MMOUserData.Instance.TaskRecordList[i].TargetNum;
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         if (tTarget == 0)
 | |
|         {
 | |
|             return 1000;
 | |
|         }
 | |
| 
 | |
|         int tFinalDiamond = 1;
 | |
|         int tStuckDiamond = 500;
 | |
|         int tStuckLevel = 100;
 | |
| 
 | |
|         int tLeftDiamond = tTarget - pDiamond;
 | |
|         int tLeftLevel = Mathf.Max(tStuckLevel - pLevel, 1);
 | |
|         if (tLeftDiamond <= tStuckDiamond && pLevel <= tStuckLevel)
 | |
|         {
 | |
|             tFinalDiamond = Mathf.Max(tLeftDiamond / tLeftLevel / 3, 1);
 | |
|         }
 | |
|         else
 | |
|         {
 | |
|             MMODataLevelDiamond tData = MMOTableLevelDiamond.Instance.GetData(pLevel);
 | |
|             if (tData != null)
 | |
|             {
 | |
|                 tFinalDiamond = tData.Diamond;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return Mathf.Max(tFinalDiamond, 1);
 | |
|     }
 | |
| 
 | |
|     private void UpdateDiamond(int pDiamond)
 | |
|     {
 | |
|         OnDiamondUpdate?.Invoke(pDiamond);
 | |
| 
 | |
|         UIMgr.GetUI<MMOUITop>().RefreshDiomand();
 | |
|         for (int i = 0; i < MMOUserData.Instance.TaskRecordList.Count; i++)
 | |
|         {
 | |
|             MMOTaskRecord tRecord = MMOUserData.Instance.TaskRecordList[i];
 | |
|             if (tRecord.TaskType == MMOTaskType.GetDiamond)
 | |
|             {
 | |
|                 MMOUserData.Instance.SetTaskValue(tRecord.RecordKey, pDiamond);
 | |
|                 if (tRecord.IsFinished)
 | |
|                 {
 | |
|                     LogEvent(MMOEvent.TaskDiamondReach);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void UpdateLevel(int pLevel)
 | |
|     {
 | |
|         for (int i = 0; i < MMOUserData.Instance.TaskRecordList.Count; i++)
 | |
|         {
 | |
|             MMOTaskRecord tRecord = MMOUserData.Instance.TaskRecordList[i];
 | |
|             if (tRecord.TaskType == MMOTaskType.PassLevel)
 | |
|             {
 | |
|                 MMOUserData.Instance.SetTaskValue(tRecord.RecordKey, pLevel);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void OpenMoreGames()
 | |
|     {
 | |
|         OnOpenMoreGames?.Invoke();
 | |
|     }
 | |
| 
 | |
|     private void WatchAd()
 | |
|     {
 | |
|         OnWatchAd?.Invoke();
 | |
|     }
 | |
| } |