327 lines
11 KiB
C#
327 lines
11 KiB
C#
namespace Application.Domain;
|
|
|
|
public class GameEquService(ISqlSugarClient DbClient, IRedisCache redis) : IGameEquService, ITransient
|
|
{
|
|
#region 装备资源
|
|
|
|
public async Task<game_equ> GetEquInfo(int equId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "EquData", "EquInfo");
|
|
var data = await redis.GetHashAsync<game_equ>(key, equId.ToString());
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<game_equ>();
|
|
data = await db.Queryable<game_equ>().Where(it => it.equId == equId).SingleAsync();
|
|
await redis.AddHashAsync(key, equId.ToString(), data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<game_equ_suit> GetEuqSuitInfo(string suitCode)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "EquData", "SuitInfo");
|
|
var data = await redis.GetHashAsync<game_equ_suit>(key, suitCode);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<game_equ_suit>();
|
|
data = await db.Queryable<game_equ_suit>().Where(it => it.suitCode == suitCode).SingleAsync();
|
|
await redis.AddHashAsync(key, suitCode, data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 用户装备
|
|
|
|
public async Task<List<unit_user_equ>> GetUserEquData(string userId, int type, string equName, int PageIndex,
|
|
int PageSize, RefAsync<int> Total, bool isRemOn = false)
|
|
{
|
|
long onTime = TimeExtend.GetTimeStampSeconds;
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
return await db.Queryable<unit_user_equ>().Where(it => it.userId == userId)
|
|
.WhereIF(type == 2, it => it.useEndTime < onTime)
|
|
.WhereIF(type == 1, it => it.isOn == 1)
|
|
.WhereIF(type == 3, it => it.isAppr == 0)
|
|
.WhereIF(isRemOn, it => it.isOn == 0)
|
|
.WhereIF(!string.IsNullOrEmpty(equName),
|
|
it => it.equName.Contains(equName) || it.unitEquName.Contains(equName))
|
|
.OrderByDescending(it => it.lev)
|
|
.OrderByDescending(it => it.ueId).ToPageListAsync(PageIndex, PageSize, Total);
|
|
}
|
|
|
|
public async Task<List<unit_user_equ>> GetUserEquData(string userId, int equId)
|
|
{
|
|
long onTime = TimeExtend.GetTimeStampSeconds;
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
return await db.Queryable<unit_user_equ>().Where(it => it.userId == userId && it.equId == equId).ToListAsync();
|
|
}
|
|
|
|
public async Task<unit_user_equ> GetUserEquInfo(string ueId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
return await db.Queryable<unit_user_equ>().Where(i => i.ueId == ueId).SingleAsync();
|
|
}
|
|
|
|
public async Task<int> GetUserEquByEquIdCount(string userId, int equId)
|
|
{
|
|
int result = 0;
|
|
var data = await GetUserEquByEquId(userId, equId);
|
|
result = data.Count;
|
|
return result;
|
|
}
|
|
|
|
public async Task<List<unit_user_equ>> GetUserEquByEquId(string userId, int equId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
return await db.Queryable<unit_user_equ>().Where(i => i.userId == userId && i.equId == equId).ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> AddUserEqu(string userId, int equId, int count, string remark)
|
|
{
|
|
bool result = false;
|
|
var equ = await GetEquInfo(equId);
|
|
if (equ != null)
|
|
{
|
|
List<unit_user_equ> adds = new List<unit_user_equ>();
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
unit_user_equ ue = new unit_user_equ();
|
|
string ueId = StringAssist.NewGuid;
|
|
ue.ueId = ueId;
|
|
ue.userId = userId;
|
|
ue.owerId = userId;
|
|
ue.equId = equ.equId;
|
|
ue.equName = equ.equName;
|
|
ue.unitEquName = equ.equName;
|
|
ue.img = equ.img;
|
|
ue.sign = "";
|
|
if (equ.isAppr == 0)
|
|
{
|
|
ue.minAtk = equ.minAtk;
|
|
ue.maxAtk = equ.maxAtk;
|
|
ue.Defense = Convert.ToInt32(equ.defense);
|
|
ue.Agility = Convert.ToInt32(equ.agility);
|
|
ue.Morale = Convert.ToInt32(equ.morale);
|
|
ue.Blood = Convert.ToInt32(equ.blood);
|
|
ue.isAppr = 1;
|
|
}
|
|
else
|
|
{
|
|
ue.minAtk = 0;
|
|
ue.maxAtk = 0;
|
|
ue.Defense = 0;
|
|
ue.Agility = 0;
|
|
ue.Morale = 0;
|
|
ue.Blood = 0;
|
|
ue.isAppr = 0;
|
|
}
|
|
|
|
ue.code = equ.code;
|
|
ue.suitCode = equ.suitCode;
|
|
ue.lev = equ.lev;
|
|
ue.durability = equ.durability;
|
|
ue.maxdurability = equ.durability;
|
|
ue.isIntensify = equ.isIntensify;
|
|
ue.intensifyLev = 0;
|
|
ue.isLock = 0;
|
|
ue.holeCount = equ.holeCount;
|
|
ue.sex = equ.sex;
|
|
ue.quality = 0;
|
|
ue.qualityName = "普通";
|
|
ue.qualityAttr = new List<AttrItem>();
|
|
ue.weight = equ.weight;
|
|
ue.sysPrice = equ.sysPrice;
|
|
ue.EquAttr = equ.equAttr;
|
|
ue.canEqualUp = equ.canEqualUp;
|
|
ue.isDeal = equ.isDeal;
|
|
ue.isGive = equ.isGive;
|
|
ue.opTime = 0;
|
|
ue.start = 0;
|
|
ue.EquMent = new EquMent();
|
|
ue.EquAwaken = new List<EquAwaken>();
|
|
ue.GemMent = new List<EquGem>();
|
|
ue.useEndTime = TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddYears(20));
|
|
ue.isOn = 0;
|
|
adds.Add(ue);
|
|
}
|
|
|
|
if (adds.Count > 0)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
result = await db.Insertable(adds).ExecuteCommandAsync() > 0;
|
|
}
|
|
|
|
if (result)
|
|
{
|
|
//添加日志
|
|
await AddEquLogs(adds, 1, remark, true);
|
|
//更新负重
|
|
int weightCount = Convert.ToInt32(equ.weight) * count;
|
|
if (weightCount > 0)
|
|
{
|
|
var attrService = App.GetService<IUnitUserWeight>();
|
|
await attrService.UpdateUserWeight(userId, 1, weightCount);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> UpdateUserEquInfo(unit_user_equ equData, bool isAddLog = false, string code = "",
|
|
string remark = "")
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
bool result = await db.Updateable(equData).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
if (isAddLog)
|
|
{
|
|
await AddEquLog(equData, code, remark);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> DeductUserEqu(string userId, List<unit_user_equ> equData, string remark)
|
|
{
|
|
bool result = false;
|
|
List<string> DelEqu = new List<string>();
|
|
List<unit_user_equ_log> logs = new List<unit_user_equ_log>();
|
|
int weightCount = 0;
|
|
foreach (var item in equData)
|
|
{
|
|
weightCount += (int)item.weight;
|
|
DelEqu.Add(item.ueId);
|
|
//生成日志集合
|
|
unit_user_equ_log log = new unit_user_equ_log();
|
|
log.logId = StringAssist.NewGuid;
|
|
log.ueId = item.ueId;
|
|
log.equId = item.equId;
|
|
log.code = nameof(GameEnum.LogCode.减少);
|
|
log.equData = item;
|
|
log.remark = remark;
|
|
log.addTime = DateTime.Now;
|
|
logs.Add(log);
|
|
}
|
|
|
|
if (DelEqu.Count > 0)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
result = await db.Deleteable<unit_user_equ>().Where(i => DelEqu.Contains(i.ueId)).ExecuteCommandAsync() > 0;
|
|
if (result) //生成成功
|
|
{
|
|
//更新负重
|
|
if (weightCount > 0)
|
|
{
|
|
var attrService = App.GetService<IUnitUserWeight>();
|
|
await attrService.UpdateUserWeight(userId, 0, weightCount);
|
|
}
|
|
|
|
if (logs.Count > 0)
|
|
{
|
|
await AddEquLogs(logs);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> DeductUserEqu(string userId, int equId, int count, string remark)
|
|
{
|
|
var userEqu = await GetUserEquData(userId, equId);
|
|
if (userEqu.Count < 1)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
userEqu = userEqu.FindAll(it => it.isLock == 0 && it.isOn == 0);
|
|
if (userEqu.Count < count)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
userEqu = userEqu.OrderBy(it => (int)it.intensifyLev)
|
|
.OrderBy(it => it.quality)
|
|
.ToList();
|
|
var opData = userEqu.Take(count).ToList();
|
|
return await DeductUserEqu(userId, opData, remark);
|
|
}
|
|
|
|
public async Task AddEquLog(unit_user_equ ueData, string code, string remark,
|
|
bool isAddAttr = false)
|
|
{
|
|
unit_user_equ_log log = new unit_user_equ_log();
|
|
log.logId = StringAssist.NewGuid;
|
|
log.ueId = ueData.ueId;
|
|
log.equId = ueData.equId;
|
|
log.code = code;
|
|
if (isAddAttr)
|
|
{
|
|
log.equData = ueData;
|
|
}
|
|
|
|
log.addTime = DateTime.Now;
|
|
log.remark = remark;
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ_log>();
|
|
await db.Insertable(log).SplitTable().ExecuteCommandAsync();
|
|
}
|
|
|
|
private async Task AddEquLogs(List<unit_user_equ> ues, int code, string remark,
|
|
bool isAddAttr = false)
|
|
{
|
|
List<unit_user_equ_log> adds = new List<unit_user_equ_log>();
|
|
foreach (var item in ues)
|
|
{
|
|
unit_user_equ_log bagLog = new unit_user_equ_log();
|
|
bagLog.logId = StringAssist.NewGuid;
|
|
bagLog.equId = item.equId;
|
|
bagLog.ueId = item.ueId;
|
|
bagLog.code = code == 0
|
|
? bagLog.code = nameof(GameEnum.LogCode.减少)
|
|
: bagLog.code = nameof(GameEnum.LogCode.增加);
|
|
if (isAddAttr)
|
|
{
|
|
bagLog.equData = item;
|
|
}
|
|
|
|
bagLog.addTime = DateTime.Now;
|
|
bagLog.remark = remark;
|
|
adds.Add(bagLog);
|
|
}
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ_log>();
|
|
await db.Insertable(adds).SplitTable().ExecuteCommandAsync();
|
|
}
|
|
|
|
private async Task AddEquLogs(List<unit_user_equ_log> logs)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ_log>();
|
|
await db.Insertable(logs).SplitTable().ExecuteCommandAsync();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region 辅助方法
|
|
|
|
public async Task<int> GetUserEquWeightSum(string userId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_equ>();
|
|
var data = await db.Queryable<unit_user_equ>().Where(it => it.userId == userId).ToListAsync();
|
|
return data.Sum(it => (int)it.weight);
|
|
}
|
|
|
|
#endregion
|
|
} |