Support Base for Monkey Junior 5.0
Core Structure:
Support System: Management of all the services
We implemented multiple Services to Support Monkey Junior 5.0
How to use the Support System:
SupportSystem class is a Singleton class and DontDestroyOnScene Object.
Support System must attached to the GameObject and Execute before any class.
The services will automatically initialize.
To use the Service, follow the code
IDownloadService DownloadService = SupportSystem.Instance[SupportSystem.ServiceIDs.DownloadService] as IDownloadService;
Explanation:
IDownloadSerivce: Interface of Service
SupportSystem.ServiceIDs.DownloadService: Service type name
Services:
1. UpdateService: Manage the update method of the MonoBehaviour class
Based on the best practice: Update in every class is worse on the performance.
https://blog.unity.com/engine-platform/10000-update-calls
So Update Service is the solution for that:
1.1. Sample:
using Monkey.Support;
using UnityEngine;
public class TestUpdateServiceScript : MonoBehaviour, IUpdate
{
public void OnDisable()
{
this.RemoveMe();
}
public void OnEnable()
{
this.AddMe();
}
public void UpdateMe()
{
Debug.Log("Update Me: " + Time.realtimeSinceStartup);
}
}
1.2. Explanation:
- The class that uses UpdateService must inherit IUpdate interface
- The UpdateMe method will be called every frame.
- UpdateService will work for both the MonoBehaviour class and the normal class.
- Manage list classes active in Update Service:
2. UserDataService: Manage the User Data, Profile Data, and Profile Setting of Users. To use UserDataService, call from Interface IUserDataService.
2.1. Interface:
public interface IUserDataService
{
/// <summary>
/// Get Profile Data
/// </summary>
/// <returns>UserProfileModel</returns>
public UserProfileModel GetProfile();
/// <summary>
/// Get User Data
/// </summary>
/// <returns>UserModel</returns>
public UserModel GetUser();
/// <summary>
/// Get All Profile Settings
/// </summary>
/// <returns>List of ProfileSettingModel</returns>
public List<ProfileSettingModel> GetProfileSettings();
/// <summary>
/// Get Specific Profile Setting
/// </summary>
/// <param name="setting">Setting Name</param>
/// <returns>ProfileSettingModel</returns>
public ProfileSettingModel GetProfileSetting(string setting);
}
2.2. Sample:
var userService = SupportSystem.Instance[SupportSystem.ServiceIDs.UserDataService] as IUserDataService;
var user = userService.GetUser();
3. DataSyncService: Manage the Course Data, and Award Data (Pet Items, Sticker, Coin). To use UserDataService, call from Interface IDataSyncService.
3.1. Interface:
public interface IDataSyncService
{
#region Data Course
/// <summary>
/// Get Lesson Data
/// </summary>
/// <param name="course">Course ID</param>
/// <param name="lessonId">Lesson ID</param>
/// <returns></returns>
LessonReportDataSync GetLessonData(TYPE_COURSE course, string lessonId);
/// <summary>
/// Get Course Data
/// </summary>
/// <param name="course">Course ID</param>
/// <returns></returns>
DataCourseSyncModel GetCourseData(TYPE_COURSE course);
/// <summary>
/// Update Lesson Data
/// </summary>
/// <param name="course">Course ID</param>
/// <param name="lesson">New Lesson Data</param>
void UpdateLessonData(TYPE_COURSE course, LessonReportDataSync lesson);
#endregion
#region Pet&Award
/// <summary>
/// Get Current Coin
/// </summary>
/// <returns></returns>
int GetCoin();
/// <summary>
/// Set Coin Data
/// </summary>
/// <param name="coinChanged">Coin Changed</param>
void SetCoin(int coinChanged);
/// <summary>
/// Get Theme Data
/// </summary>
/// <param name="themeId">Theme ID</param>
/// <returns></returns>
DataThemesModel GetTheme(string themeId);
/// <summary>
/// Add new Sticker
/// </summary>
/// <param name="themeId">Theme ID</param>
/// <param name="stickerId">Sticker ID</param>
void AddSticker(string themeId, int stickerId);
/// <summary>
/// Get All Pet Items
/// </summary>
/// <returns></returns>
List<PetItem> GetPetItems();
/// <summary>
/// Get List Used Pet Item
/// </summary>
/// <returns></returns>
List<int> GetUsedItems();
/// <summary>
/// Check Pet Item is Used
/// </summary>
/// <param name="itemId">Item ID</param>
/// <returns></returns>
bool IsUsedItem(int itemId);
/// <summary>
/// Get List New Pet Items
/// </summary>
/// <returns></returns>
List<int> GetNewItems();
/// <summary>
/// Check Pet Item is New
/// </summary>
/// <param name="itemId"></param>
/// <returns></returns>
bool IsNewItem(int itemId);
/// <summary>
/// Add a Pet Item
/// </summary>
/// <param name="ItemId">Item ID</param>
/// <param name="isNew">Is tag New Item, for the default IsNew = false</param>
void AddItem(int ItemId, bool isNew = false);
/// <summary>
/// Change Pet Item State: Used, Unuse
/// </summary>
/// <param name="ItemId">Item ID</param>
/// <param name="isUsed">Is Item Used</param>
void ChangeItemState(int ItemId, bool isUsed);
#endregion
}
3.2. Sample:
var syncData = SupportSystem.Instance[SupportSystem.ServiceIDs.DataSyncService] as IDataSyncService;
syncData.GetCourseData(TYPE_COURSE.MJ5);
4. EventTrackingService: Send tracking, events, data to React Native. To use UserDataService, call from Interface IEventTrackingService.
4.1. Interface
public interface IEventTrackingService
{
/// <summary>
/// Push Event to React Native to Tracking, Sync Data
/// </summary>
/// <param name="nameEvent">Event name</param>
/// <param name="typeEvent">Event Type</param>
/// <param name="properties">Properties</param>
/// <param name="typeToPushEvent">Event Type: AWS, CLEVERTAP, AWS_AND_CLEVERTAP, AIRBRIDGE</param>
void PushEvent(string nameEvent, string typeEvent, Dictionary<string, object> properties, TYPE_EVENT typeToPushEvent = TYPE_EVENT.AWS);
}
4.2. Sample
var trackingService = SupportSystem.Instance[SupportSystem.ServiceIDs.EventTrackingService] as IEventTrackingService;
Dictionary<string, object> properties = new Dictionary<string, object>() {
{ "completed", complete},
{ "time_on_screeen", endTimeLesson - startTimeLesson},
{ "age", UserManager.instance.currentProfile.Age}
};
trackingService.PushEvent("mx_learn_AI_lesson", EnvironmentConfig.awsLearnLesson, properties, TYPE_EVENT.CLEVERTAP);
5. AddressableService: Download Game assets and course assets. To use UserDataService, call from Interface IAddressableService.
5.1. Interface
public interface IAddressableService
{
/// <summary>
/// Download Addressable Group
/// </summary>
/// <param name="groupName">Group Name</param>
/// <param name="callback">Callback when Download finished</param>
/// <param name="progress">Callback the progress</param>
/// <param name="total_retry">No need to change the value</param>
/// <returns></returns>
IEnumerator DownloadGroup(string groupName, Action<bool> callback, Action<float> progress = null, int total_retry = 0);
void LoadAssets<T>(string asset_name, Action<AsyncOperationHandle<T>> callback) where T : UnityEngine.Object;
/// <summary>
/// Check when Addressable service ready
/// </summary>
/// <returns></returns>
bool IsServiceActive();
}
5.2. Sample
var AddressableService = SupportSystem.Instance[SupportSystem.ServiceIDs.AddressableService] as IAddressableService;
yield return new WaitUntil(() => AddressableService.IsServiceActive());
yield return AddressableService.DownloadGroup("lrc", (success)=> {
Debug.Log("Download Success? " + success);
}, (progress)=> {
Debug.Log("Progress: " + progress);
});
6. DownloadService: Manage the Download process for Assetbundle, words, and lesson data. To use DownloadService, call from Interface IDownloadService.
6.1. Interface
public interface IDownloadService
{
/// <summary>
/// Download a file in the background.
/// </summary>
/// <param name="info"> Info item download </param>
public void DownLoadFileInBackGround(ItemDownload info);
/// <summary>
/// Download list files in the background.
/// </summary>
/// <param name="list_info"> List info item download </param>
/// <param name="sucessCallBack"> Call back when successful download </param>
/// <param name="progressCallBack"> Call back while downloading, this call back return percent </param>
/// <param name="errorCallBack"> Call back when download fail, this call back return a message error </param>
public void DownLoadListFileInBackGround(List<ItemDownload> list_info, Action<object> sucessCallBack, Action<object> progressCallBack, Action<object> errorCallBack);
/// <summary>
/// Download a file in the background by priority.
/// </summary>
/// <param name="info">Info item priority download</param>
public void DownLoadFilePriority(ItemDownload info);
/// <summary>
/// Download list files in the background by priority.
/// </summary>
/// <param name="list_info"> List info item priority download </param>
/// <param name="sucessCallBack"> Call back when successful download </param>
/// <param name="progressCallBack"> Call back while downloading, this call back return percent </param>
/// <param name="errorCallBack"> Call back when download fail, this call back return a message error </param>
public void DownLoadListFilePriority(List<ItemDownload> list_info, Action<object> sucessCallBack, Action<object> progressCallBack, Action<object> errorCallBack);
/// <summary>
/// Download all files bundle words in Lesson.
/// </summary>
/// <param name="idLesson"> Id of lesson </param>
/// <param name="sucessCallBack"> Call back when successful download </param>
/// <param name="progressCallBack"> Call back while downloading, this call back return percent </param>
/// <param name="errorCallBack"> Call back when download fail, this call back return a message error </param>
public void DownLoadWordsInLesson(int idLesson, Action<object> sucessCallBack, Action<object> progressCallBack, Action<object> errorCallBack);
/// <summary>
/// DownLoad file addressable by name addressable
/// </summary>
/// <param name="nameAddressable"> Name of addressable </param>
/// <param name="sucessCallBack"> Call back when successful download </param>
/// <param name="progressCallBack"> Call back while downloading, this call back return percent </param>
/// <param name="errorCallBack"> Call back when download fail, this call back return a message error </param>
public void DownLoadAddressable(string nameAddressable, Action<object> sucessCallBack, Action<object> progressCallBack, Action<object> errorCallBack);
/// <summary>
/// Get link host download file bundle word in course.
/// </summary>
/// <returns> Return link host link host download file bundle word </returns>
public string GetLinkDownloadWord();
/// <summary>
/// Get link host download file zip activity in course.
/// </summary>
/// <returns> Return link host link host download file zip activity </returns>
public string GetLinkDownloadAct();
}
6.2. Sample
var downloadService = SupportSystem.Instance[SupportSystem.ServiceIDs.DownloadService] as IDownloadService;
downloadService.GetLinkDownloadAct();
7. Popup Service: Manage the Popups in the game, Push and Pop the Popup on scene. To use PopupService, call from Interface IPopupService.
7.1. Interface
public interface IPopupService
{
/// <summary>
/// Show popup by name class
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="showType"></param>
/// <param name="actionShow"></param>
/// <returns> return popup </returns>
public T Show<T>(object data = null, ShowType showType = ShowType.DissmissCurrent, Action actionShow = null) where T : Panel;
/// <summary>
/// Close popup by name class
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <param name="actHide"></param>
public void Hide<T>(object data = null, Action actHide = null) where T : Panel;
/// <summary>
/// Close all popups
/// </summary>
/// <param name="actHideAll"></param>
public void HideAll(Action actHideAll = null);
}
7.2. Sample
public class PopupTestService : Panel
{
public override void Hide(object data = null, Action actionHide = null)
{
base.Hide(data, actionHide);
}
public override void Show(object data = null, bool duplicated = false, Action actionShow = null)
{
base.Show(data, duplicated, actionShow);
}
public void OnClickBtnClose()
{
Hide(null, () => { Debug.LogError("OnClickBtnClose"); });
}
public void OnClickBtnOpenPopup2()
{
var popup = SupportSystem.Instance[SupportSystem.ServiceIDs.PopupService] as IPopupService;
popup.Show<PopupTestService2>(null, PopupService.ShowType.NotHide);
}
}
8. Assetbundles Service: Manager the Asset Bundle and get data game. To use AssetbundlesService, call interface IAssetbundlesService..
8.1. Interface
public interface IAssetbundlesService
{
/// <summary>
/// Get Data Game Primitive
/// </summary>
/// <param name="dictWordInforDetail"> Dictionary info word detail with key is link down load word and value is WordInforDetail </param>
/// <param name="listWord"> List word model in file word.json </param>
/// <returns> Dictionary<int, DataGamePrimitive> </returns>
public Task<Dictionary<int, DataGamePrimitive>> GetDataGamePrimitive(Dictionary<string, WordInforDetail> dictWordInforDetail, List<ListWordModel> listWord);
/// <summary>
/// Get asset in bundle by type T
/// </summary>
/// <typeparam name="T"> Type asset </typeparam>
/// <param name="bundle_name"> Name bundle </param>
/// <param name="asset_name"> Name asset </param>
/// <param name="local_path_file"> Path bundle in storage device </param>
/// <param name="on_done"> Action invoke when get asset complete </param>
/// <param name="on_error"> Action invoke when get asset fail </param>
/// <returns> Asset with type T </returns>
public Task<T> ResourceGetAssetInBundle<T>(string bundle_name, string asset_name, string local_path_file, string link_download, Action<object> on_done = null, Action<object> on_error = null) where T : UnityEngine.Object;
}
8.2. Sample
var assetbundleService = SupportSystem.Instance[SupportSystem.ServiceIDs.AssetbundleService] as IAssetbundlesService;
string linkBundle = "https://monkeymedia.vcdn.com.vn/App/uploads/course_install/bundle/hdr/ios/";//course.d.w_b;
string linkWord = "https://vnmedia2.monkeyuni.net/App/zip/hdr/word_bundle/ios/";//course.d.w_b;
string bundle_name = "course_install_ai_speaking_v2_78_1716429352.bundle";
string asset_name = "course_installation.json";
string local_path = $"{Ultis.persistentDataPath}/zips/{bundle_name}";
var json = await assetbundleService.ResourceGetAssetInBundle<TextAsset>(bundle_name, asset_name, local_path, linkBundle + bundle_name,
(object d) => { Debug.LogError("Complete" + d?.ToString());}, (object d) => { Debug.LogError("Fail" + d?.ToString()); });