CTT/Server/Hotfix/Game/System/UI/ShopSystem.cs

220 lines
8.8 KiB
C#

using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Text;
namespace ET
{
public class ShopAwakeSystem : AwakeSystem<Shop>
{
public override void Awake(Shop self)
{
self.Awake();
}
}
public class ShopLoadSystem : LoadSystem<Shop>
{
public override void Load(Shop self)
{
self.Load();
}
}
public static class ShopSystem
{
public static void Awake(this Shop self)
{
self.Load();
}
private static HashSet<(int, MarketBase)> _set = new HashSet<(int, MarketBase)>();
public static void Load(this Shop self)
{
self.MarketPeriodDic.Clear();
self.ShopPageDic.Clear();
foreach (MarketBase item in DataTableHelper.GetAll<MarketBase>())
{
if (item.Period == 0)
{
_set.Add((item.Page, item));
}
else
{
self.MarketPeriodDic.Add(item.Period, (item.Page, item));
}
}
for (int i = 1; i <= 5; i++)
{
foreach ((int, MarketBase) item in _set)
{
self.MarketPeriodDic.Add(i, item);
}
}
_set.Clear();
foreach (ShopBase item in DataTableHelper.GetAll<ShopBase>())
{
self.ShopPageDic.Add(item.Page, item);
}
}public static string BuyInMultiShop(this Shop self, Unit unit, ShopType type, int index, int count)
{
if (type == ShopType.Family)
return "暂未开放";
List<MultiShop> list = MultiShopCategory.Instance.GetByType(type);
if (list == null || list.Count == 0)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买type = {type} index = {index}的【PageIndex】不存在的物品");
return "系统错误";
}
MultiShop shopBase = list[index];
if (shopBase == null)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买type = {type} index = {index}的【index】不存在的物品");
return "系统错误";
}
if (!ItemComponent.Instance.CanAddItem(unit, shopBase.ItemId, count))
{
return "背包已满,请整理您的背包!";
}
long price = count * shopBase.Price;
if (price <= 0)
{
Log.Error($"{unit.Id.GetPlayerFormatName()} 购买价格 < 0 : {price}");
return "系统错误";
}
string payRet = null;
switch (type)
{
case ShopType.None:
break;
case ShopType.Gem:payRet= CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Gem, price);
break;
case ShopType.Honor:payRet= CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Hornor, price);
break;
case ShopType.Pvp: payRet= CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.PVPMoney, price);
break;
case ShopType.Family:payRet= CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Family, price);
break;
default:
throw new ArgumentOutOfRangeException(nameof (type), type, null);
}
if (payRet != null)
{
return payRet;
}
//!给货
BagHelper.AddItem(unit, shopBase.ItemId, count, true,getSource:"商店");
UnitHelper.Save<Bag>(unit).Coroutine();
UnitHelper.Save<NumericComponent>(unit).Coroutine();
return string.Empty;
}
public static string BuyInShop(this Shop self, Unit unit, int page, int index, int count)
{
List<ShopBase> list = self.ShopPageDic[page];
if (list == null || list.Count == 0)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买PageIndex = {page} index = {index}的【PageIndex】不存在的物品");
return "系统错误";
}
ShopBase shopBase = list?[index];
if (shopBase == null)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买PageIndex = {page} index = {index}的【index】不存在的物品");
return "系统错误";
}
if (!BagHelper.CanAddItem(unit, shopBase.ItemId, count))
{
return "背包已满,请整理您的背包!";
}
long price = count * shopBase.Price;
if (price <= 0)
{
Log.Error($"{unit.Id.GetPlayerFormatName()} 购买价格 < 0 : {price}");
return "系统错误";
}
string payRet = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Coin, price);
if (payRet != null)
{
return payRet;
}
//!给货
BagHelper.AddItem(unit, shopBase.ItemId, count, true,getSource:"商店");
UnitHelper.Save<Bag>(unit).Coroutine();
UnitHelper.Save<NumericComponent>(unit).Coroutine();
return string.Empty;
}
public static string BuyInMarket(this Shop self, Unit unit, MarketType marketType, int page, int index, int count)
{
PlayerData data = unit.GetComponent<PlayerData>();
List<(int, MarketBase)> list = self.MarketPeriodDic[data.MarketPeriod];
if (list == null || list.Count == 0)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买 period = {data.MarketPeriod} page = {page} index = {index}的【PageIndex】不存在的物品");
return "系统错误";
}
MarketBase marketBase = null;
int marketBaseIndex = 0;
foreach ((int pageIndex, MarketBase __marketBase) in list)
{
if (pageIndex == page)
{
if (marketBaseIndex == index)
{
marketBase = __marketBase;
}
marketBaseIndex++;
}
}
if (marketBase == null)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买 period = {data.MarketPeriod} page = {page} index = {index}的【page】【index】不存在的物品");
return "系统错误";
}
if (!ItemComponent.Instance.CanAddItem(unit, marketBase.ItemId, count))
{
return "背包已满,请整理您的背包!";
}
//!收钱
int price = count * marketBase.Price_YuanBao;
if (price <= 0)
{
Log.Error($"{unit.Id.GetPlayerFormatName()} 购买价格 < 0 : {price}");
return "系统错误";
}
CharacterHelper.MoneyType moneyType;
bool islock = true;
switch (marketType)
{
default:
case MarketType.NoneMarket:
throw new Exception("商场类型错误");
case MarketType.VoucherMarket:
moneyType = CharacterHelper.MoneyType.Voucher;
if (marketBase.OnlyYuanBao)
{
Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买 period = {data.MarketPeriod} page = {page} index = {index}的【page】【index】 的物品,但是这只能用元宝购买");
return "系统错误";
}
price *= 2;
break;
case MarketType.YuanBaoMarket:
islock = false;
moneyType = CharacterHelper.MoneyType.YuanBao;
break;
}
price = MathHelper.RoundToInt(price * data.MarketDiscount);
string payRet = CharacterHelper.ReduceMoney(unit, moneyType, price);
if (payRet != null)
{
return payRet;
}
//!给货
BagHelper.AddItem(unit, marketBase.ItemId, count, islock,getSource:"商城");
UnitHelper.Save<Bag>(unit).Coroutine();
UnitHelper.Save<NumericComponent>(unit).Coroutine();
return string.Empty;
}
}
}