From 9955a77baedc320ffa6e7afa864d665d00b01006 Mon Sep 17 00:00:00 2001 From: Omar Akermi Date: Sun, 27 Apr 2025 02:55:01 +0200 Subject: [PATCH 1/2] feat: moneymanager --- S1API/Money/MoneyManager.cs | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 S1API/Money/MoneyManager.cs diff --git a/S1API/Money/MoneyManager.cs b/S1API/Money/MoneyManager.cs new file mode 100644 index 00000000..92e68168 --- /dev/null +++ b/S1API/Money/MoneyManager.cs @@ -0,0 +1,109 @@ +// REMOVE dynamic usage. Use the real FloatContainer. + +using S1API.Internal.Abstraction; +using UnityEngine; + +namespace S1API.Money +{ + /// + /// Provides methods for managing financial data, including cash balance, + /// online transactions, and net worth calculations. + /// + public class MoneyManager : Registerable + { +#if IL2CPP + private static Il2CppScheduleOne.Money.MoneyManager InternalInstance => Il2CppScheduleOne.Money.MoneyManager.Instance; +#else + /// + /// Provides internal access to the singleton instance of the underlying `ScheduleOne.Money.MoneyManager`. + /// This property is used internally to interact with the core money management system. + /// + private static ScheduleOne.Money.MoneyManager InternalInstance => ScheduleOne.Money.MoneyManager.Instance; +#endif + + /// + /// Invoked when the instance of the class is created and initialized. + /// Provides an entry point for subclasses to include additional setup logic during creation. + /// + protected override void OnCreated() => base.OnCreated(); + + /// + /// Executes cleanup or teardown logic when the instance is being destroyed. + /// This method is invoked when the object lifecycle ends and should typically + /// handle resource deallocation, event unsubscriptions, or other finalization tasks. + /// + protected override void OnDestroyed() => base.OnDestroyed(); + + /// + /// Changes the cash balance by the specified amount. + /// + /// The amount to adjust the cash balance by. Positive values increase the balance, while negative values decrease it. + /// Indicates whether the change in cash balance should be visually represented. + /// Indicates whether a sound effect should play when the cash balance is changed. + public static void ChangeCashBalance(float amount, bool visualizeChange = true, bool playCashSound = false) + { + InternalInstance?.ChangeCashBalance(amount, visualizeChange, playCashSound); + } + + /// + /// Creates an online transaction with specified details. + /// + /// The name of the transaction. + /// The monetary value of a single unit in the transaction. + /// The quantity involved in the transaction. + /// A note or description for the transaction. + public static void CreateOnlineTransaction(string transactionName, float unitAmount, float quantity, string transactionNote) + { + InternalInstance?.CreateOnlineTransaction(transactionName, unitAmount, quantity, transactionNote); + } + + /// + /// Retrieves the current net worth. + /// + /// The calculated net worth as a floating-point number. + public static float GetNetWorth() + { + return InternalInstance != null ? InternalInstance.GetNetWorth() : 0f; + } + + /// + /// Retrieves the current cash balance. + /// + /// The current cash balance as a floating-point value. + public static float GetCashBalance() + { + return InternalInstance != null ? InternalInstance.cashBalance : 0f; + } + + /// + /// Retrieves the current online balance of the user. + /// + /// + /// A float representing the user's current online balance. Returns 0 if the internal instance is null. + /// + public static float GetOnlineBalance() + { + return InternalInstance != null ? InternalInstance.sync___get_value_onlineBalance() : 0f; + } + + /// + /// Register a networth calculation event. + /// + /// An action to invoke during the networth calculation event. + public static void AddNetworthCalculation(System.Action callback) + { + if (InternalInstance != null) + InternalInstance.onNetworthCalculation += callback; + } + + /// + /// Remove a networth calculation event. + /// + /// The callback function to remove from the networth calculation event. + public static void RemoveNetworthCalculation(System.Action callback) + { + if (InternalInstance != null) + InternalInstance.onNetworthCalculation -= callback; + } + } +} From 8196467b9059dc4c67cfde329e54226135ad1af3 Mon Sep 17 00:00:00 2001 From: Omar Akermi Date: Sun, 27 Apr 2025 03:59:42 +0200 Subject: [PATCH 2/2] Refactor: Replace MoneyManager with static Money API --- S1API/Money/Money.cs | 132 ++++++++++++++++++++++++++++++++++++ S1API/Money/MoneyManager.cs | 109 ----------------------------- 2 files changed, 132 insertions(+), 109 deletions(-) create mode 100644 S1API/Money/Money.cs delete mode 100644 S1API/Money/MoneyManager.cs diff --git a/S1API/Money/Money.cs b/S1API/Money/Money.cs new file mode 100644 index 00000000..77ab3f3f --- /dev/null +++ b/S1API/Money/Money.cs @@ -0,0 +1,132 @@ +#if IL2CPP +using Il2CppScheduleOne; +using S1ItemFramework = Il2CppScheduleOne.ItemFramework; +using S1MoneyFramework = Il2CppScheduleOne.Money; +#else +using ScheduleOne; +using S1ItemFramework = ScheduleOne.ItemFramework; +using S1MoneyFramework = ScheduleOne.Money; + +#endif + +using S1API.Internal.Utils; +using System; +using UnityEngine; + +namespace S1API.Money +{ + /// + /// Provides static access to financial operations, including methods for managing cash balance, + /// creating online transactions, and calculating net worth. + /// + public static class Money + { + /// + /// Provides internal access to the underlying financial operations manager. + /// This property is utilized for interacting with the core financial functionality. + /// + private static S1MoneyFramework.MoneyManager Internal => S1MoneyFramework.MoneyManager.Instance; + + /// + /// Event triggered whenever there is a change in the balance, + /// including cash balance or online transactions. + /// + public static event Action? OnBalanceChanged; + + /// + /// Adjusts the cash balance by the specified amount. + /// + /// The amount to modify the cash balance by. Positive values increase the balance, and negative values decrease it. + /// Indicates whether the cash change should be visualized on the HUD. + /// Indicates whether a sound effect should be played to signify the cash adjustment. + public static void ChangeCashBalance(float amount, bool visualizeChange = true, bool playCashSound = false) + { + Internal?.ChangeCashBalance(amount, visualizeChange, playCashSound); + OnBalanceChanged?.Invoke(); + } + + /// + /// Creates an online transaction. + /// + /// The name of the transaction. + /// The monetary amount per unit involved in the transaction. + /// The number of units in the transaction. + /// An optional note or description for the transaction. + public static void CreateOnlineTransaction(string transactionName, float unitAmount, float quantity, + string transactionNote) + { + Internal?.CreateOnlineTransaction(transactionName, unitAmount, quantity, transactionNote); + OnBalanceChanged?.Invoke(); + } + + /// + /// Retrieves the total net worth, including all cash and online balances combined. + /// + /// The total net worth as a floating-point value. + public static float GetNetWorth() + { + return Internal != null ? Internal.GetNetWorth() : 0f; + } + + /// + /// Retrieves the current cash balance. + /// + /// The current cash balance as a floating-point value. + public static float GetCashBalance() + { + return Internal != null ? Internal.cashBalance : 0f; + } + + /// + /// Retrieves the current online balance. + /// + /// The current amount of online balance. + public static float GetOnlineBalance() + { + return Internal != null ? Internal.sync___get_value_onlineBalance() : 0f; + } + + /// + /// Registers a callback to be invoked during net worth calculation. + /// + /// The callback to be executed when net worth is calculated. It receives an object as its parameter. + public static void AddNetworthCalculation(System.Action callback) + { + if (Internal != null) + Internal.onNetworthCalculation += callback; + } + + /// + /// Removes a previously registered networth calculation callback. + /// + /// The callback to be removed from the networth calculation updates. + public static void RemoveNetworthCalculation(System.Action callback) + { + if (Internal != null) + Internal.onNetworthCalculation -= callback; + } + + /// + /// Creates a new cash instance with the specified balance. + /// + /// The initial amount of cash to set in the instance. + /// A newly created instance of cash with the specified balance. + public static CashInstance CreateCashInstance(float amount) + { +#if IL2CPP + var cashItem = Registry.GetItem("cash"); + var instance = CrossType.As(cashItem.GetDefaultInstance(1)); + var cashInstance = new CashInstance(instance); + cashInstance.SetQuantity(amount); + return cashInstance; +#else + var cashItem = Registry.GetItem("cash"); + var instance = CrossType.As(cashItem.GetDefaultInstance(1)); + var cashInstance = new CashInstance(instance); + cashInstance.SetQuantity(amount); + return cashInstance; + +#endif + } + } +} diff --git a/S1API/Money/MoneyManager.cs b/S1API/Money/MoneyManager.cs deleted file mode 100644 index 92e68168..00000000 --- a/S1API/Money/MoneyManager.cs +++ /dev/null @@ -1,109 +0,0 @@ -// REMOVE dynamic usage. Use the real FloatContainer. - -using S1API.Internal.Abstraction; -using UnityEngine; - -namespace S1API.Money -{ - /// - /// Provides methods for managing financial data, including cash balance, - /// online transactions, and net worth calculations. - /// - public class MoneyManager : Registerable - { -#if IL2CPP - private static Il2CppScheduleOne.Money.MoneyManager InternalInstance => Il2CppScheduleOne.Money.MoneyManager.Instance; -#else - /// - /// Provides internal access to the singleton instance of the underlying `ScheduleOne.Money.MoneyManager`. - /// This property is used internally to interact with the core money management system. - /// - private static ScheduleOne.Money.MoneyManager InternalInstance => ScheduleOne.Money.MoneyManager.Instance; -#endif - - /// - /// Invoked when the instance of the class is created and initialized. - /// Provides an entry point for subclasses to include additional setup logic during creation. - /// - protected override void OnCreated() => base.OnCreated(); - - /// - /// Executes cleanup or teardown logic when the instance is being destroyed. - /// This method is invoked when the object lifecycle ends and should typically - /// handle resource deallocation, event unsubscriptions, or other finalization tasks. - /// - protected override void OnDestroyed() => base.OnDestroyed(); - - /// - /// Changes the cash balance by the specified amount. - /// - /// The amount to adjust the cash balance by. Positive values increase the balance, while negative values decrease it. - /// Indicates whether the change in cash balance should be visually represented. - /// Indicates whether a sound effect should play when the cash balance is changed. - public static void ChangeCashBalance(float amount, bool visualizeChange = true, bool playCashSound = false) - { - InternalInstance?.ChangeCashBalance(amount, visualizeChange, playCashSound); - } - - /// - /// Creates an online transaction with specified details. - /// - /// The name of the transaction. - /// The monetary value of a single unit in the transaction. - /// The quantity involved in the transaction. - /// A note or description for the transaction. - public static void CreateOnlineTransaction(string transactionName, float unitAmount, float quantity, string transactionNote) - { - InternalInstance?.CreateOnlineTransaction(transactionName, unitAmount, quantity, transactionNote); - } - - /// - /// Retrieves the current net worth. - /// - /// The calculated net worth as a floating-point number. - public static float GetNetWorth() - { - return InternalInstance != null ? InternalInstance.GetNetWorth() : 0f; - } - - /// - /// Retrieves the current cash balance. - /// - /// The current cash balance as a floating-point value. - public static float GetCashBalance() - { - return InternalInstance != null ? InternalInstance.cashBalance : 0f; - } - - /// - /// Retrieves the current online balance of the user. - /// - /// - /// A float representing the user's current online balance. Returns 0 if the internal instance is null. - /// - public static float GetOnlineBalance() - { - return InternalInstance != null ? InternalInstance.sync___get_value_onlineBalance() : 0f; - } - - /// - /// Register a networth calculation event. - /// - /// An action to invoke during the networth calculation event. - public static void AddNetworthCalculation(System.Action callback) - { - if (InternalInstance != null) - InternalInstance.onNetworthCalculation += callback; - } - - /// - /// Remove a networth calculation event. - /// - /// The callback function to remove from the networth calculation event. - public static void RemoveNetworthCalculation(System.Action callback) - { - if (InternalInstance != null) - InternalInstance.onNetworthCalculation -= callback; - } - } -}