任务系统一半多,商城筛选功能,一些细节和免费包
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
|
||||
public static class StoreExpBottlePurchaseService
|
||||
{
|
||||
public static bool TryPurchase(Player_SO playerData, storeItemSO itemSO, int packageCount, out string failureMessage, out int grantedCount)
|
||||
{
|
||||
failureMessage = string.Empty;
|
||||
grantedCount = 0;
|
||||
|
||||
if (playerData == null)
|
||||
{
|
||||
failureMessage = "玩家数据丢失";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (itemSO == null)
|
||||
{
|
||||
failureMessage = "商品数据丢失";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!itemSO.isOnShelf || !itemSO.canbepurchased)
|
||||
{
|
||||
failureMessage = "物品未上架";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (packageCount <= 0)
|
||||
{
|
||||
failureMessage = "本店禁止无实物交易";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (itemSO.costRequirements == null || itemSO.costRequirements.Count == 0)
|
||||
{
|
||||
failureMessage = "不可购买";
|
||||
return false;
|
||||
}
|
||||
|
||||
var primaryCost = itemSO.costRequirements[0];
|
||||
if (primaryCost.currencyType != storeItemSO.CurrencyType.coins)
|
||||
{
|
||||
failureMessage = "不可购买";
|
||||
return false;
|
||||
}
|
||||
|
||||
ExpBottleKind kind;
|
||||
if (!StoreExpBottleGrantResolver.TryResolve(itemSO, out kind))
|
||||
{
|
||||
failureMessage = "当前仅支持经验瓶发放";
|
||||
return false;
|
||||
}
|
||||
|
||||
long longGrantedCount = (long)Mathf.Max(1, itemSO.itemSinglePurchaseQty) * packageCount;
|
||||
if (longGrantedCount > int.MaxValue)
|
||||
{
|
||||
failureMessage = "达到单次购买限额";
|
||||
return false;
|
||||
}
|
||||
|
||||
grantedCount = (int)longGrantedCount;
|
||||
if (itemSO.itemPurchaseQuota >= 0 && itemSO.purchasedCount + grantedCount > itemSO.itemPurchaseQuota)
|
||||
{
|
||||
failureMessage = "超过限购额度";
|
||||
return false;
|
||||
}
|
||||
|
||||
long totalCostLong = (long)primaryCost.amount * packageCount;
|
||||
if (totalCostLong > int.MaxValue)
|
||||
{
|
||||
failureMessage = "达到单次购买限额";
|
||||
return false;
|
||||
}
|
||||
|
||||
int totalCost = (int)totalCostLong;
|
||||
|
||||
PlayerEconomyLedger.EnsureInstance().AttachPlayerData(playerData);
|
||||
ExpBottleLedger.EnsureInstance().AttachPlayerData(playerData);
|
||||
|
||||
if (!PlayerEconomyLedger.EnsureInstance().HasEnoughCoins(totalCost))
|
||||
{
|
||||
failureMessage = "货币不足";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PlayerEconomyLedger.EnsureInstance().TrySpendCoins(totalCost))
|
||||
{
|
||||
failureMessage = "货币不足";
|
||||
return false;
|
||||
}
|
||||
|
||||
ExpBottleLedger.EnsureInstance().Add(kind, grantedCount);
|
||||
DebugPurchaseSuccess(itemSO, totalCost, grantedCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void DebugPurchaseSuccess(storeItemSO itemSO, int totalCost, int grantedCount)
|
||||
{
|
||||
var snapshot = ExpBottleLedger.EnsureInstance().GetSnapshot();
|
||||
var builder = new StringBuilder();
|
||||
builder.Append("[StorePurchase] 已成功购买:");
|
||||
builder.Append(itemSO != null ? itemSO.itemName : "未知物品");
|
||||
builder.Append(" | 花费Coins=");
|
||||
builder.Append(totalCost);
|
||||
builder.Append(" | 发放数量=");
|
||||
builder.Append(grantedCount);
|
||||
builder.Append(" | 经验瓶库存:");
|
||||
|
||||
bool appendedAny = false;
|
||||
for (int i = 0; i < snapshot.Count; i++)
|
||||
{
|
||||
var entry = snapshot[i];
|
||||
ExpBottleKind kind;
|
||||
if (!ExpBottleCatalog.TryGetKind(entry.key, out kind))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (appendedAny)
|
||||
{
|
||||
builder.Append(" ; ");
|
||||
}
|
||||
|
||||
builder.Append(ExpBottleCatalog.GetDisplayName(kind));
|
||||
builder.Append("=");
|
||||
builder.Append(entry.count);
|
||||
appendedAny = true;
|
||||
}
|
||||
|
||||
Debug.Log(builder.ToString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user