Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the new economy system #156

Merged
merged 9 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Runtime/Client/LootLockerEndPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public class LootLockerEndPoints
public static EndPointClass androidPurchaseVerification = new EndPointClass("v1/purchase", LootLockerHTTPMethod.POST);
public static EndPointClass pollingOrderStatus = new EndPointClass("v1/purchase/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass activatingARentalAsset = new EndPointClass("v1/asset/instance/{0}/activate", LootLockerHTTPMethod.POST);
public static EndPointClass purchaseCatalogItem = new EndPointClass("purchase", LootLockerHTTPMethod.POST);

// EventTrigger
[Header("EventTrigger")]
Expand Down Expand Up @@ -234,6 +235,25 @@ public class LootLockerEndPoints
public static EndPointClass ComputeAndLockDropTable = new EndPointClass("v1/player/droptables/{0}/compute?asset_details={1}", LootLockerHTTPMethod.POST);
public static EndPointClass PickDropsFromDropTable = new EndPointClass("v1/player/droptables/{0}/pick", LootLockerHTTPMethod.POST);

// Currencies
[Header("Currencies")]
public static EndPointClass listCurrencies = new EndPointClass("currencies", LootLockerHTTPMethod.GET);
public static EndPointClass getCurrencyDenominationsByCode = new EndPointClass("currency/code/{0}/denominations", LootLockerHTTPMethod.GET);

// Balances
[Header("Balances")]
public static EndPointClass listBalancesInWallet = new EndPointClass("balances/wallet/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass getWalletByWalletId = new EndPointClass("wallet/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass getWalletByHolderId = new EndPointClass("wallet/holder/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass creditBalanceToWallet = new EndPointClass("balances/credit", LootLockerHTTPMethod.POST);
public static EndPointClass debitBalanceToWallet = new EndPointClass("balances/debit", LootLockerHTTPMethod.POST);
public static EndPointClass createWallet = new EndPointClass("wallet", LootLockerHTTPMethod.POST);

// Catalogs
[Header("Catalogs")]
public static EndPointClass listCatalogs = new EndPointClass("catalogs", LootLockerHTTPMethod.GET);
public static EndPointClass listCatalogItemsByKey = new EndPointClass("catalog/key/{0}/prices", LootLockerHTTPMethod.GET);

// Misc
[Header("Misc")]
public static EndPointClass ping = new EndPointClass("ping", LootLockerHTTPMethod.GET);
Expand Down
3 changes: 2 additions & 1 deletion Runtime/Client/LootLockerServerRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public static T Deserialize<T>(LootLockerResponse serverResponse,
return response;
}
}

public class LootLockerPaginationResponse<TKey>
{
public int total { get; set; }
Expand Down Expand Up @@ -427,7 +428,7 @@ public struct LootLockerServerRequest

/// <summary>
/// Query parameters to append to the end of the request URI
/// <para>Example: If you include a dictionary with a key of "page" and a value of "42" (as a string) then the url would become "https: //mydomain.com/endpoint?page=42"</para>
/// Example: If you include a dictionary with a key of "page" and a value of "42" (as a string) then the url would become "https://mydomain.com/endpoint?page=42"
/// </summary>
public Dictionary<string, string> queryParams;

Expand Down
232 changes: 231 additions & 1 deletion Runtime/Game/LootLockerSDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Security.Cryptography;
using System.Text;
using LootLocker.LootLockerEnums;
using static LootLocker.LootLockerConfig;
using System.Linq;
using File = System.IO.File;
using System.Security.Cryptography;
#if UNITY_EDITOR
using UnityEditor;
#endif
Expand Down Expand Up @@ -4309,6 +4309,29 @@ public static void ActivateRentalAsset(int assetInstanceID, Action<LootLockerAct
data.getRequests.Add(assetInstanceID.ToString());
LootLockerAPIManager.ActivateRentalAsset(data, onComplete);
}

/// <summary>
/// Purchase one or more catalog items using a specified wallet
/// </summary>
/// <param name="walletId">The id of the wallet to use for the purchase</param>
/// <param name="itemsToPurchase">A list of items to purchase along with the quantity of each item to purchase</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void LootLockerPurchaseCatalogItems(string walletId, LootLockerCatalogItemAndQuantityPair[] itemsToPurchase, Action<LootLockerPurchaseCatalogItemResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerPurchaseCatalogItemResponse>());
return;
}
var body = LootLockerJson.SerializeObject(new LootLockerPurchaseCatalogItemRequest
{
wallet_id = walletId,
items = itemsToPurchase
});

LootLockerServerRequest.CallAPI(LootLockerEndPoints.purchaseCatalogItem.endPoint, LootLockerEndPoints.purchaseCatalogItem.httpMethod, body, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

#endregion

#region Collectables
Expand Down Expand Up @@ -4960,6 +4983,213 @@ public static void GetRemovedUGCForPlayer(GetRemovedUGCForPlayerInput input, Act

#endregion

#region Currency
/// <summary>
/// Get a list of available currencies for the game
/// </summary>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void ListCurrencies(Action<LootLockerListCurrenciesResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerListCurrenciesResponse>());
return;
}

LootLockerServerRequest.CallAPI(LootLockerEndPoints.listCurrencies.endPoint, LootLockerEndPoints.listCurrencies.httpMethod, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Get a list of the denominations available for a specific currency
/// </summary>
/// <param name="currencyCode">The code of the currency to fetch denominations for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void GetCurrencyDenominationsByCode(string currencyCode, Action<LootLockerListDenominationsResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerListDenominationsResponse>());
return;
}

var endpoint = string.Format(LootLockerEndPoints.getCurrencyDenominationsByCode.endPoint, currencyCode);

LootLockerServerRequest.CallAPI(endpoint, LootLockerEndPoints.getCurrencyDenominationsByCode.httpMethod, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

#endregion

#region Balances
/// <summary>
/// Get a list of balances in a specified wallet
/// </summary>
/// <param name="walletId">Unique ID of the wallet to get balances for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void ListBalancesInWallet(string walletId, Action<LootLockerListBalancesForWalletResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerListBalancesForWalletResponse>());
return;
}
var endpoint = string.Format(LootLockerEndPoints.listBalancesInWallet.endPoint, walletId);

LootLockerServerRequest.CallAPI(endpoint, LootLockerEndPoints.listBalancesInWallet.httpMethod, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Get information about a specified wallet
/// </summary>
/// <param name="walletId">Unique ID of the wallet to get information for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void GetWalletByWalletId(string walletId, Action<LootLockerGetWalletResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerGetWalletResponse>());
return;
}
var endpoint = string.Format(LootLockerEndPoints.getWalletByWalletId.endPoint, walletId);

LootLockerServerRequest.CallAPI(endpoint, LootLockerEndPoints.getWalletByWalletId.httpMethod, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Get information about a wallet for a specified holder
/// </summary>
/// <param name="holderUlid">ULID of the holder of the wallet you want to get information for</param>
/// <param name="holderType">The type of the holder to get the wallet for</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void GetWalletByHolderId(string holderUlid, LootLockerWalletHolderTypes holderType, Action<LootLockerGetWalletResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerGetWalletResponse>());
return;
}
var endpoint = string.Format(LootLockerEndPoints.getWalletByHolderId.endPoint, holderUlid);

LootLockerServerRequest.CallAPI(endpoint, LootLockerEndPoints.getWalletByHolderId.httpMethod, onComplete:
(serverResponse) =>
{
var parsedResponse = LootLockerResponse.Deserialize<LootLockerGetWalletResponse>(serverResponse);
if (!parsedResponse.success && parsedResponse.statusCode == 404)
{
LootLockerCreateWalletRequest request = new LootLockerCreateWalletRequest()
{
holder_id = holderUlid,
holder_type = holderType.ToString()
};
LootLockerServerRequest.CallAPI(LootLockerEndPoints.createWallet.endPoint,
LootLockerEndPoints.createWallet.httpMethod, LootLockerJson.SerializeObject(request),
createWalletResponse =>
{
if (createWalletResponse.success)
{
LootLockerServerRequest.CallAPI(endpoint,
LootLockerEndPoints.getWalletByHolderId.httpMethod, null,
secondResponse =>
{
LootLockerResponse.Deserialize(onComplete, secondResponse);
});
return;
}

onComplete?.Invoke(parsedResponse);
});
return;
}

onComplete?.Invoke(parsedResponse);
}
);
}

/// <summary>
/// Credit (increase) the specified amount of the provided currency to the provided wallet
/// </summary>
/// <param name="walletId">Unique ID of the wallet to credit the given amount of the given currency to</param>
/// <param name="currencyId">Unique ID of the currency to credit</param>
/// <param name="amount">The amount of the given currency to credit to the given wallet</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void CreditBalanceToWallet(string walletId, string currencyId, string amount, Action<LootLockerCreditWalletResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerCreditWalletResponse>());
return;
}

var json = LootLockerJson.SerializeObject(new LootLockerCreditRequest() { amount = amount, currency_id = currencyId, wallet_id = walletId });

LootLockerServerRequest.CallAPI(LootLockerEndPoints.creditBalanceToWallet.endPoint, LootLockerEndPoints.creditBalanceToWallet.httpMethod, json, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// Debit (decrease) the specified amount of the provided currency to the provided wallet
/// </summary>
/// <param name="walletId">Unique ID of the wallet to debit the given amount of the given currency from</param>
/// <param name="currencyId">Unique ID of the currency to debit</param>
/// <param name="amount">The amount of the given currency to debit from the given wallet</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void DebitBalanceToWallet(string walletId, string currencyId, string amount, Action<LootLockerDebitWalletResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerDebitWalletResponse>());
return;
}

var json = LootLockerJson.SerializeObject(new LootLockerDebitRequest() { amount = amount, currency_id = currencyId, wallet_id = walletId });

LootLockerServerRequest.CallAPI(LootLockerEndPoints.debitBalanceToWallet.endPoint, LootLockerEndPoints.debitBalanceToWallet.httpMethod, json, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

#endregion

#region Catalog
/// <summary>
/// List the catalogs available for the game
/// </summary>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void ListCatalogs(Action<LootLockerListCatalogsResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerListCatalogsResponse>());
return;
}

LootLockerServerRequest.CallAPI(LootLockerEndPoints.listCatalogs.endPoint, LootLockerEndPoints.listCatalogs.httpMethod, onComplete: (serverResponse) => { LootLockerResponse.Deserialize(onComplete, serverResponse); });
}

/// <summary>
/// List the items available in a specific catalog
/// </summary>
/// <param name="catalogKey">Unique Key of the catalog that you want to get items for</param>
/// <param name="count">Amount of catalog items to receive. Use null to simply get the default amount.</param>
/// <param name="after">Used for pagination, this is the cursor to start getting items from. Use null to get items from the beginning. Use the cursor from a previous call to get the next count of items in the list.</param>
/// <param name="onComplete">onComplete Action for handling the response</param>
public static void ListCatalogItems(string catalogKey, int count, string after, Action<LootLockerListCatalogPricesResponse> onComplete)
{
if (!CheckInitialized())
{
onComplete?.Invoke(LootLockerResponseFactory.SDKNotInitializedError<LootLockerListCatalogPricesResponse>());
return;
}
var endpoint = string.Format(LootLockerEndPoints.listCatalogItemsByKey.endPoint, catalogKey);

endpoint += "?";
if (count > 0)
endpoint += $"per_page={count}&";

if (!string.IsNullOrEmpty(after))
endpoint += $"cursor={after}&";

LootLockerServerRequest.CallAPI(endpoint, LootLockerEndPoints.listCatalogItemsByKey.httpMethod, onComplete: (serverResponse) => { onComplete?.Invoke(new LootLockerListCatalogPricesResponse(serverResponse)); });
}
#endregion

#region Misc

/// <summary>
Expand Down
Loading
Loading