Files
Kg.SeaTime/Service/Application.Domain/Tool/Base/GameBus.cs
2026-07-06 14:44:15 +08:00

482 lines
16 KiB
C#

namespace Application.Domain;
public static class GameBus
{
#region
public static async Task<bool> UpdateBag(string userId, int operate, string goodsType, string parameter, long count,
string remark)
{
bool isok = false;
if (goodsType.Equals(nameof(GameEnum.PropCode.Goods)))
{
var goodsService = App.GetService<IGameGoodsService>();
isok = await goodsService.UpdateUserGoods(userId, operate, Convert.ToInt32(parameter), (int)count, remark);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.Equ)))
{
var equService = App.GetService<IGameEquService>();
if (operate == 1)
{
isok = await equService.AddUserEqu(userId, Convert.ToInt32(parameter), (int)count, remark);
}
else
{
isok = await equService.DeductUserEqu(userId, Convert.ToInt32(parameter), (int)count, remark);
}
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.copper)))
{
var accService = App.GetService<IUnitUserAccService>();
isok = await accService.UpdateUserCopper(userId, operate, count);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.gold)))
{
var accService = App.GetService<IUnitUserAccService>();
isok = await accService.UpdateUserAcc(userId, operate, nameof(AccEnum.AccType.gold), count,
nameof(AccEnum.Name.), remark);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.cowry)))
{
var accService = App.GetService<IUnitUserAccService>();
isok = await accService.UpdateUserAcc(userId, operate, nameof(AccEnum.AccType.cowry), count,
nameof(AccEnum.Name.), remark);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.vigour)))
{
var attrService = App.GetService<IUnitUserAttrService>();
isok = await attrService.UpdateUserVigour(userId, operate, count);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.exp)))
{
var attrService = App.GetService<IUnitUserAttrService>();
isok = await attrService.UpdateUserExp(userId, count, operate);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.renown)))
{
var accService = App.GetService<IUnitUserAccService>();
isok = await accService.UpdateUserAcc(userId, operate, nameof(AccEnum.AccType.renown), count,
nameof(AccEnum.Name.), remark);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.teach)))
{
var accService = App.GetService<IUnitUserAccService>();
isok = await accService.UpdateUserAcc(userId, operate, nameof(AccEnum.AccType.teach), count,
nameof(AccEnum.Name.), remark);
}
else if (goodsType.Equals(nameof(GameEnum.PropCode.map)))
{
var mapService = App.GetService<IGameMapService>();
isok = await mapService.AddUserCityMap(userId, Convert.ToInt32(parameter));
}
return isok;
}
public static async Task<bool> UpdateBag(string userId, int operate, dynamic Reward, string remark = "",
long count = 1)
{
try
{
foreach (var item in Reward)
{
long num = Convert.ToInt64(item.count) * count;
if (num <= 0)
{
continue;
}
await UpdateBag(userId, operate, item.code, item.parameter, num, remark);
}
return true;
}
catch
{
return false;
}
}
#endregion
#region
public static async Task<CheckTowerNeed> CheckNeed(string userId, TowerNeed item, int count = 1)
{
CheckTowerNeed result = new CheckTowerNeed();
result.Needs = new List<TowerNeed>();
bool isok = true;
int isDeal = 1;
int isGive = 1;
item.isAsk = 1;
if (item.code == nameof(GameEnum.PropCode.Equ))
{
var equService = App.GetService<IGameEquService>();
int MyCount = await equService.GetUserEquByEquIdCount(userId, Convert.ToInt32(item.parameter));
long needCount = item.count * count;
item.count = needCount;
item.onCount = MyCount;
if (MyCount < needCount)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.Goods))
{
int MyCount = 0;
var goodsService = App.GetService<IGameGoodsService>();
var goodsInfo = await goodsService.GetUserGoodsInfo(userId, Convert.ToInt32(item.parameter));
if (goodsInfo != null)
{
MyCount = (int)goodsInfo.count;
if (goodsInfo.isDeal == 0)
{
isDeal = 0;
}
if (goodsInfo.isGive == 0)
{
isGive = 0;
}
}
long needCount = item.count * count;
item.count = needCount;
item.onCount = MyCount;
if (MyCount < needCount)
{
isok = false;
item.isAsk = 0;
}
}
else if (item.code == nameof(GameEnum.PropCode.copper))
{
var accService = App.GetService<IUnitUserAccService>();
var MyAcc = await accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = MyAcc;
if (MyAcc < needAcc)
{
isok = false;
item.isAsk = 0;
}
}
else if (item.code == nameof(GameEnum.PropCode.gold))
{
var accService = App.GetService<IUnitUserAccService>();
var MyAcc = await accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.gold));
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = MyAcc;
if (MyAcc < needAcc)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.vigour)) //活力
{
var attrService = App.GetService<IUnitUserAttrService>();
var MyAcc = await attrService.GetUserVigourInfo(userId);
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = (long)MyAcc.vitality;
if (MyAcc.vitality < needAcc)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.lev))
{
var attrService = App.GetService<IUnitUserAttrService>();
var MyLev = await attrService.GetUserLev(userId);
item.count = item.count;
item.onCount = MyLev;
if (MyLev < item.count)
{
isok = false;
}
}
result.result = isok;
result.isDeal = isDeal;
result.isGive = isGive;
result.Needs.Add(item);
return result;
}
public static async Task<CheckTowerNeed> CheckNeed(string userId, List<TowerNeed> needs, int count = 1)
{
CheckTowerNeed result = new CheckTowerNeed();
result.Needs = new List<TowerNeed>();
bool isok = true;
int isDeal = 1;
int isGive = 1;
foreach (var item in needs)
{
var cheakResult = await CheckNeed(userId, item, count);
if (isok)
{
isok = cheakResult.result;
}
if (cheakResult.isDeal == 0)
{
isDeal = 0;
}
if (cheakResult.isGive == 0)
{
isGive = 0;
}
result.Needs.AddRange(cheakResult.Needs);
}
result.result = isok;
result.isDeal = isDeal;
result.isGive = isGive;
return result;
}
public static async Task<CheckTowerNeeds> CheckNeeds(string userId, List<TowerNeeds> needs, int count = 1)
{
CheckTowerNeeds result = new CheckTowerNeeds();
bool isok = true;
int isDeal = 1;
int isGive = 1;
result.Needs = new List<TowerNeeds>();
foreach (var item in needs)
{
item.isAsk = 1;
if (item.code == nameof(GameEnum.PropCode.Equ))
{
var equService = App.GetService<IGameEquService>();
int MyCount = await equService.GetUserEquByEquIdCount(userId, Convert.ToInt32(item.parameter));
long needCount = item.count * count;
item.count = needCount;
item.onCount = MyCount;
if (MyCount < needCount)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.Goods))
{
int MyCount = 0;
var goodsService = App.GetService<IGameGoodsService>();
var goodsInfo = await goodsService.GetUserGoodsInfo(userId, Convert.ToInt32(item.parameter));
if (goodsInfo != null)
{
MyCount = (int)goodsInfo.count;
if (goodsInfo.isDeal == 0)
{
isDeal = 0;
}
if (goodsInfo.isGive == 0)
{
isGive = 0;
}
}
long needCount = item.count * count;
item.count = needCount;
item.onCount = MyCount;
if (MyCount < needCount)
{
isok = false;
item.isAsk = 0;
}
}
else if (item.code == nameof(GameEnum.PropCode.copper))
{
var accService = App.GetService<IUnitUserAccService>();
var MyAcc = await accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = MyAcc;
if (MyAcc < needAcc)
{
isok = false;
item.isAsk = 0;
}
}
else if (item.code == nameof(GameEnum.PropCode.gold))
{
var accService = App.GetService<IUnitUserAccService>();
var MyAcc = await accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.gold));
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = MyAcc;
if (MyAcc < needAcc)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.vigour)) //活力
{
var attrService = App.GetService<IUnitUserAttrService>();
var MyAcc = await attrService.GetUserVigourInfo(userId);
long needAcc = item.count * count;
item.count = needAcc;
item.onCount = (long)MyAcc.vitality;
if (MyAcc.vitality < needAcc)
{
item.isAsk = 0;
isok = false;
}
}
else if (item.code == nameof(GameEnum.PropCode.lev))
{
var attrService = App.GetService<IUnitUserAttrService>();
var MyLev = await attrService.GetUserLev(userId);
item.count = item.count;
item.onCount = MyLev;
if (MyLev < item.count)
{
isok = false;
}
}
result.Needs.Add(item);
}
result.result = isok;
result.isDeal = isDeal;
result.isGive = isGive;
return result;
}
#endregion
#region
public static RandomDataBase GetRandomByWeight(List<RandomDataBase> data)
{
var _randomInstance = new Random();
if (data.Count == 0)
{
return null;
}
double totalWeight = data.Sum(it => it.random);
int rnd = _randomInstance.Next(Convert.ToInt32(totalWeight) + 1);
double current = 0;
foreach (var item in data)
{
current += item.random;
if (rnd < current)
{
return item;
}
}
return null;
}
public static List<TowerGet> GetRandomGoods(RandomModel random, int count = 1, decimal luck = 0)
{
List<TowerGet> result = new List<TowerGet>();
for (int i = 0; i < count; i++)
{
List<RandomData> timeAward = new List<RandomData>();
if (random.code == nameof(RandomModel.RandomCode.Weight))
{
timeAward = RandomHandleByWeight(random, luck);
}
else
{
timeAward = RandomHandleByChance(random, luck);
}
foreach (var item in timeAward)
{
var onAward = result.FindIndex(it => it.code == item.code && it.parameter == item.par);
if (onAward > -1)
{
result[onAward].count += RandomAssist.GetFormatedNumeric(item.minCount, item.maxCount);
}
else
{
TowerGet add = new TowerGet()
{
code = item.code,
name = item.name,
parameter = item.par,
count = RandomAssist.GetFormatedNumeric(item.minCount, item.maxCount)
};
result.Add(add);
}
}
}
return result;
}
private static List<RandomData> RandomHandleByWeight(RandomModel random, decimal luck = 0)
{
var _randomInstance = new Random();
List<RandomData> result = new List<RandomData>();
double empty = random.empty * (1.0 + (double)luck) / 100;
if (_randomInstance.NextDouble() > empty)
{
return result;
}
if (random.data.Count == 0)
{
return result;
}
double totalWeight = random.data.Sum(it => it.chance);
int rnd = _randomInstance.Next(Convert.ToInt32(totalWeight) + 1);
double current = 0;
foreach (var item in random.data)
{
current += item.chance;
if (rnd < current)
{
result.Add(item);
return result;
}
}
return result;
}
private static List<RandomData> RandomHandleByChance(RandomModel random, decimal luck = 0)
{
var _randomInstance = new Random();
List<RandomData> result = new List<RandomData>();
double empty = (random.empty / 100);
if (_randomInstance.NextDouble() > empty)
{
return result;
}
if (random.data.Count == 0)
{
return result;
}
foreach (var item in random.data)
{
double rnd = _randomInstance.NextDouble();
double okChance = (item.chance / 100) * (1.0 + Convert.ToDouble(luck));
if (rnd < okChance)
{
result.Add(item);
}
}
return result;
}
#endregion
}