395 lines
14 KiB
C#
395 lines
14 KiB
C#
namespace Application.Domain;
|
|
|
|
public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserAttrService, ITransient
|
|
{
|
|
#region 个人属性
|
|
|
|
public async Task<UserAttrModel> GetUserAttrModel(string userId)
|
|
{
|
|
UserAttrModel result = new UserAttrModel();
|
|
result.id = userId;
|
|
var userService = App.GetService<IUnitUserService>();
|
|
var userInfo = await userService.GetUserInfoByUserId(userId);
|
|
result.viceId = userInfo.userNo;
|
|
result.name = userInfo.nick;
|
|
result.area = (int)userInfo.areaId;
|
|
result.IsSystem = (int)userInfo.isSystem;
|
|
result.sex = userInfo.sex;
|
|
result.code = nameof(UserEnum.AttrCode.Person);
|
|
var unitAttr = await GetUserAttr(userId);
|
|
result.lev = (int)unitAttr.lev;
|
|
result.minAtk = (int)unitAttr.minAtk;
|
|
result.maxAtk = (int)unitAttr.maxAtk;
|
|
result.defense = (int)unitAttr.defense;
|
|
result.agility = (int)unitAttr.agility;
|
|
result.upBlood = (int)unitAttr.upBlood;
|
|
result.upMorale = (int)unitAttr.upMorale;
|
|
var bloodInfo = await GetUserBlood(userId);
|
|
result.blood = (int)bloodInfo.blood;
|
|
var moraleInfo = await GetUserMorale(userId);
|
|
result.morale = (int)moraleInfo.morale;
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<unit_user_attr> GetUserAttr(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "UserAttr");
|
|
var data = await redis.GetHashAsync<unit_user_attr>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_attr>();
|
|
data = await db.Queryable<unit_user_attr>().Where(it => it.userId == userId).SingleAsync();
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<int> GetUserLev(string userId)
|
|
{
|
|
int lev = 0;
|
|
var attrInfo = await GetUserAttr(userId);
|
|
if (attrInfo != null)
|
|
{
|
|
lev = (int)attrInfo.lev;
|
|
}
|
|
|
|
return lev;
|
|
}
|
|
|
|
private async Task ClearUserAttrCache(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "UserAttr");
|
|
await redis.DelHashAsync(key, userId);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 体力操作
|
|
|
|
public async Task<unit_user_blood> GetUserBlood(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Blood");
|
|
var data = await redis.GetHashAsync<unit_user_blood>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_blood>();
|
|
data = await db.Queryable<unit_user_blood>().Where(it => it.userId == userId).SingleAsync();
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<bool> UpdateUserBlood(string userId, int op, int count, bool mustUp = false)
|
|
{
|
|
bool result = false;
|
|
unit_user_blood data = new unit_user_blood();
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Blood");
|
|
if (op == 2)
|
|
{
|
|
data.userId = userId;
|
|
data.blood = count;
|
|
}
|
|
else
|
|
{
|
|
data = await GetUserBlood(userId);
|
|
if (op == 0)
|
|
{
|
|
data.blood -= count;
|
|
}
|
|
else if (op == 1)
|
|
{
|
|
data.blood += count;
|
|
}
|
|
}
|
|
|
|
data.blood = data.blood < 0 ? 0 : data.blood;
|
|
result = await redis.AddHashAsync(key, userId, data);
|
|
|
|
if (mustUp)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_blood>();
|
|
result = await db.Updateable<unit_user_blood>()
|
|
.SetColumnsIF(op == 0, it => it.blood == it.blood - count)
|
|
.SetColumnsIF(op == 1, it => it.blood == it.blood + count)
|
|
.SetColumnsIF(op == 2, it => it.blood == count)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 经验操作
|
|
|
|
public async Task<unit_user_exp> GetUserExp(string userId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_exp>();
|
|
return await db.Queryable<unit_user_exp>().Where(it => it.userId == userId).SingleAsync();
|
|
}
|
|
|
|
|
|
public async Task<bool> UpdateUserExp(string userId, long exp, int op = 1)
|
|
{
|
|
bool result = false;
|
|
bool IsUpUserAttr = false;
|
|
try
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_exp>();
|
|
await DbClient.AsTenant().BeginTranAsync();
|
|
if (op == 0) //扣除经验
|
|
{
|
|
result = await db.Updateable<unit_user_exp>().SetColumns(it => it.exp == it.exp - exp)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
}
|
|
else //增加经验
|
|
{
|
|
var MyExp = await db.Queryable<unit_user_exp>().Where(it => it.userId == userId).SingleAsync();
|
|
long onExp = (long)MyExp.exp + exp;
|
|
if (onExp < MyExp.upExp) //不足升级
|
|
{
|
|
result = await db.Updateable<unit_user_exp>().SetColumns(it => it.exp == onExp)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
}
|
|
else //足够升级
|
|
{
|
|
var myAttr = await GetUserAttr(userId);
|
|
int OnLev = (int)myAttr.lev + 1;
|
|
if (OnLev <= GameConfig.GameMaxLev)
|
|
{
|
|
long upExp = onExp - (long)MyExp.upExp;
|
|
decimal nextExp = GameTool.GetUserUpExp(OnLev);
|
|
result = await db.Updateable<unit_user_exp>().SetColumns(it => it.exp == upExp)
|
|
.SetColumns(it => it.upExp == nextExp).Where(it => it.userId == userId)
|
|
.ExecuteCommandAsync() > 0;
|
|
if (result) //更新成功,更新各项属性
|
|
{
|
|
var data = GameTool.GetAttrData(OnLev);
|
|
result = await db.Updateable<unit_user_attr>(data).Where(i => i.userId == userId)
|
|
.ExecuteCommandAsync() > 0;
|
|
int upVigour = GameConfig.GameBaseVigour + (OnLev * 10);
|
|
await UpdateUserUpVigour(userId, upVigour);
|
|
|
|
IsUpUserAttr = result;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
decimal maxOnExp = (decimal)MyExp.upExp - 1;
|
|
if ((decimal)MyExp.exp < maxOnExp)
|
|
{
|
|
result = await db.Updateable<unit_user_exp>().SetColumns(it => it.exp == maxOnExp)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
await DbClient.AsTenant().CommitTranAsync();
|
|
}
|
|
catch
|
|
{
|
|
result = false;
|
|
await DbClient.AsTenant().RollbackTranAsync();
|
|
}
|
|
|
|
if (result && IsUpUserAttr)
|
|
{
|
|
await ClearUserAttrCache(userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion 经验操作
|
|
|
|
#region 士气
|
|
|
|
public async Task<unit_user_morale> GetUserMorale(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Morale");
|
|
var data = await redis.GetHashAsync<unit_user_morale>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_morale>();
|
|
data = await db.Queryable<unit_user_morale>().Where(it => it.userId == userId).SingleAsync();
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<bool> UpdateUserMorale(string userId, int op, int count)
|
|
{
|
|
bool result = false;
|
|
var data = await GetUserMorale(userId);
|
|
if (op == 1)
|
|
{
|
|
data.morale += count;
|
|
}
|
|
else if (op == 2)
|
|
{
|
|
data.morale = count;
|
|
}
|
|
else
|
|
{
|
|
data.morale -= count;
|
|
}
|
|
|
|
data.morale = data.morale < 0 ? 0 : data.morale;
|
|
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Morale");
|
|
await redis.AddHashAsync(key, userId, data);
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_morale>();
|
|
result = await db.Updateable<unit_user_morale>().SetColumns(it => it.morale == data.morale)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> CheackRecoverMorale(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "Recover:Morale", userId);
|
|
return await redis.ExistsAsync(key);
|
|
}
|
|
public async Task LockRecoverMorale(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "Recover:Morale", userId);
|
|
await redis.SetAsync(key, DateTime.Now,TimeAssist.GetDateTimeYMD(1));
|
|
}
|
|
|
|
#endregion 士气
|
|
|
|
#region 活力
|
|
|
|
public async Task<unit_user_vigour> GetUserVigourInfo(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Vigour");
|
|
var data = await redis.GetHashAsync<unit_user_vigour>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_vigour>();
|
|
data = await db.Queryable<unit_user_vigour>().Where(i => i.userId == userId).SingleAsync();
|
|
if (data != null)
|
|
{
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private async Task ClearUserVigourCache(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Vigour");
|
|
await redis.DelHashAsync(key, userId);
|
|
}
|
|
|
|
public async Task<bool> UpdateUserVigour(string userId, int op, long count, string UpTime = "")
|
|
{
|
|
bool result = false;
|
|
try
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_vigour>();
|
|
await DbClient.AsTenant().BeginTranAsync();
|
|
result = await db.Updateable<unit_user_vigour>()
|
|
.SetColumnsIF(op == 0, it => it.vitality == it.vitality - count)
|
|
.SetColumnsIF(op == 1, it => it.vitality == it.vitality + count)
|
|
.SetColumnsIF(op == 2, it => it.vitality == count)
|
|
.SetColumnsIF(!string.IsNullOrEmpty(UpTime), it => it.upTime == UpTime)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
await DbClient.AsTenant().CommitTranAsync();
|
|
}
|
|
catch
|
|
{
|
|
result = false;
|
|
await DbClient.AsTenant().RollbackTranAsync();
|
|
}
|
|
|
|
if (result)
|
|
{
|
|
await ClearUserVigourCache(userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> UpdateUserUpVigour(string userId, decimal count)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_vigour>();
|
|
bool isok = await db.Updateable<unit_user_vigour>().SetColumns(i => i.upVitality == count)
|
|
.Where(i => i.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (isok)
|
|
{
|
|
await ClearUserVigourCache(userId);
|
|
}
|
|
|
|
return isok;
|
|
}
|
|
|
|
#endregion 活力
|
|
|
|
#region 在线时间
|
|
|
|
public async Task<bool> UpdateOnLineTime(string userId, string code, int time)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_online_time>();
|
|
bool result = await db.Updateable<unit_user_online_time>()
|
|
.SetColumns(it => it.dayTime == it.dayTime + time)
|
|
.SetColumns(it => it.monthTime == it.monthTime + time)
|
|
.SetColumns(it => it.totalTime == it.totalTime + time)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
return result;
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region 药品栏
|
|
|
|
public async Task<unit_user_drug> GetUserDrug(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserTool", "DrugColumn");
|
|
if (await redis.HExistsHashAsync(key, userId))
|
|
{
|
|
return await redis.GetHashAsync<unit_user_drug>(key, userId);
|
|
}
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_drug>();
|
|
var data = await db.Queryable<unit_user_drug>().Where(it => it.userId == userId).SingleAsync();
|
|
if (data == null)
|
|
{
|
|
data = new unit_user_drug();
|
|
data.userId = userId;
|
|
data.drug = new List<UserDrugModel>();
|
|
await db.Insertable(data).ExecuteCommandAsync();
|
|
}
|
|
|
|
await redis.AddHashAsync(key, userId, data);
|
|
return data;
|
|
}
|
|
|
|
public async Task<bool> UpdateUserDrug(unit_user_drug data)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_drug>();
|
|
bool result = await db.Updateable<unit_user_drug>(data).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserDrug(data. userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task ClearUserDrug(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserTool", "DrugColumn");
|
|
await redis.DelHashAsync(key, userId);
|
|
}
|
|
|
|
#endregion
|
|
} |