992 lines
31 KiB
C#
992 lines
31 KiB
C#
using Newtonsoft.Json;
|
||
|
||
namespace Application.Domain;
|
||
|
||
public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserAttrService, ITransient
|
||
{
|
||
#region 个人属性
|
||
|
||
public async Task<UserAttrModel> GetUserAttrModel(string userId, string scene = "Default")
|
||
{
|
||
try
|
||
{
|
||
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;
|
||
|
||
List<AttrItem> ExtendAttrData = new List<AttrItem>();
|
||
|
||
#region 构造基础加层体系
|
||
|
||
UserAttrModel temp = new UserAttrModel();
|
||
temp.minAtk = (int)unitAttr.minAtk;
|
||
temp.maxAtk = (int)unitAttr.maxAtk;
|
||
temp.defense = (int)unitAttr.defense;
|
||
temp.agility = (int)unitAttr.agility;
|
||
temp.upBlood = (int)unitAttr.upBlood;
|
||
temp.upMorale = (int)unitAttr.upMorale;
|
||
|
||
var equService = App.GetService<IGameEquService>();
|
||
var equTemp = await equService.GetUserEquAttrModel(userId);
|
||
temp = GameAttrTool.GetRoleAttrTempMerge(temp, equTemp);
|
||
//套装加层
|
||
var suitAttr = await equService.GetUserEquSuitAttrInfo(userId);
|
||
ExtendAttrData.AddRange(suitAttr);
|
||
|
||
#endregion
|
||
|
||
|
||
#region 参数
|
||
|
||
int minAtk = 0;
|
||
int maxAtk = 0;
|
||
int Agility = 0;
|
||
int Defense = 0;
|
||
int upBlood = 0;
|
||
int upMorale = 0;
|
||
|
||
#endregion 参数
|
||
|
||
#region 队伍加层
|
||
|
||
var teamService = App.GetService<IGameTeamService>();
|
||
var MyTeam = await teamService.GetUserTeamInfo(userId);
|
||
if (MyTeam.state == 1)
|
||
{
|
||
int teamCount = MyTeam.user.Count(it => it.isOnline == 1);
|
||
if (teamCount > 0)
|
||
{
|
||
decimal teamAdd = 0.05m * teamCount;
|
||
teamAdd = teamAdd > 0.2m ? 0.2m : teamAdd;
|
||
minAtk += Convert.ToInt32(temp.minAtk * teamAdd);
|
||
maxAtk += Convert.ToInt32(temp.maxAtk * teamAdd);
|
||
Defense += Convert.ToInt32(temp.defense * teamAdd);
|
||
result.atkTips += $"(队+{Convert.ToInt32(teamAdd * 100)}%)";
|
||
result.defTips += $"(队+{Convert.ToInt32(teamAdd * 100)}%)";
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 称号加层
|
||
|
||
var maxNameService = App.GetService<IGameMaxNameService>();
|
||
var userMaxName = await maxNameService.GetUserMaxNameData(userId);
|
||
foreach (var maxName in userMaxName)
|
||
{
|
||
ExtendAttrData.AddRange(maxName.attr);
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
#region Buff加层
|
||
|
||
var MyState = await GetUserStateData(userId, scene);
|
||
if (MyState.Count > 0)
|
||
{
|
||
foreach (var item in MyState)
|
||
{
|
||
ExtendAttrData.AddRange(item.attr);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 加层处理
|
||
|
||
var otTemp = GameAttrTool.GetRoleAttrTemp(temp, ExtendAttrData);
|
||
|
||
result = GameAttrTool.GetRoleAttrTempMerge(result, otTemp); //加层其他
|
||
result = GameAttrTool.GetRoleAttrTempMerge(result, temp); //加层其他
|
||
result.minAtk += minAtk;
|
||
result.maxAtk += maxAtk;
|
||
result.defense += Defense;
|
||
result.agility += Agility;
|
||
result.upBlood += upBlood;
|
||
result.upMorale += upMorale;
|
||
|
||
#endregion
|
||
|
||
#region 数据效验
|
||
|
||
var userBlood = await GetUserBlood(userId);
|
||
result.blood = (int)userBlood.blood;
|
||
|
||
//士气
|
||
var userMorale = await GetUserMorale(userId);
|
||
result.morale = (int)userMorale.morale;
|
||
|
||
//负面状态处理
|
||
var loadState = await GetUserLoadState(userId);
|
||
result = await GameAttrTool.CheckRoleLoadBuff(result, loadState);
|
||
result = await ReviseUserRoleAttr(result);
|
||
|
||
#endregion
|
||
|
||
return result;
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine($"错误ID:{userId},错误信息:{e.Message}");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
private async Task<UserAttrModel> ReviseUserRoleAttr(UserAttrModel result)
|
||
{
|
||
//处理上限数据
|
||
if (result.blood > result.upBlood)
|
||
{
|
||
result.blood = result.upBlood;
|
||
await UpdateUserBlood(result.id, 2, result.blood, true);
|
||
}
|
||
|
||
if (result.morale > result.upMorale)
|
||
{
|
||
result.morale = result.upMorale;
|
||
await UpdateUserMorale(result.id, 2, Convert.ToInt32(result.morale));
|
||
}
|
||
|
||
if (result.NoHarm > 0.8M) //免伤上限
|
||
{
|
||
result.NoHarm = 0.8M;
|
||
}
|
||
|
||
if (result.Atk_Next > 0.3M) //连击上限
|
||
{
|
||
result.Atk_Next = 0.3M;
|
||
}
|
||
|
||
//评分
|
||
decimal _score = result.maxAtk + result.defense + result.agility + result.upBlood / 3.00M + result.upMorale;
|
||
result.score = Math.Round(_score, 2);
|
||
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;
|
||
var myAttr = await GetUserAttrModel(userId);
|
||
if (myAttr != null)
|
||
{
|
||
data.blood = data.blood > myAttr.upBlood ? myAttr.upBlood : data.blood;
|
||
}
|
||
}
|
||
}
|
||
|
||
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>().SetColumns(it => it.blood == data.blood)
|
||
.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>();
|
||
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 if (op == 2) //固定经验
|
||
{
|
||
result = await db.Updateable<unit_user_exp>().SetColumns(it => 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;
|
||
if (OnLev >= GameConfig.GameMaxLev)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
long upExp = (long)MyExp.upExp;
|
||
|
||
while (onExp >= upExp)
|
||
{
|
||
if (OnLev < GameConfig.GameMaxLev)
|
||
{
|
||
OnLev = OnLev + 1;
|
||
onExp -= upExp;
|
||
upExp = GameTool.GetUserUpExp(OnLev);
|
||
}
|
||
else
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
result = await db.Updateable<unit_user_exp>().SetColumns(it => it.exp == onExp)
|
||
.SetColumns(it => it.upExp == upExp).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;
|
||
IsUpUserAttr = result;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
result = false;
|
||
}
|
||
|
||
if (result && IsUpUserAttr)
|
||
{
|
||
await ClearUserAttrCache(userId);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public async Task DeductUserExp(string userId, int type, long count)
|
||
{
|
||
bool result = false;
|
||
var userAttr = await GetUserAttr(userId);
|
||
int OnLev = (int)userAttr.lev;
|
||
if (OnLev <= 30)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var userExp = await GetUserExp(userId);
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_exp>();
|
||
if (type == 0) //扣除经验
|
||
{
|
||
long sumExp = (long)userExp.exp;
|
||
while (count > 0 && OnLev > 30)
|
||
{
|
||
if (sumExp >= count)
|
||
{
|
||
count = 0;
|
||
sumExp = sumExp - count;
|
||
}
|
||
else
|
||
{
|
||
OnLev -= 1;
|
||
sumExp += GameTool.GetUserUpExp(OnLev);
|
||
}
|
||
}
|
||
|
||
if (OnLev == 30)
|
||
{
|
||
sumExp = 0;
|
||
}
|
||
var data = GameTool.GetAttrData(OnLev);
|
||
result = await db.Updateable<unit_user_attr>(data).Where(i => i.userId == userId)
|
||
.ExecuteCommandAsync() > 0;
|
||
if (result)
|
||
{
|
||
await UpdateUserExp(userId, sumExp, 2);
|
||
await ClearUserAttrCache(userId);
|
||
}
|
||
|
||
}
|
||
else if(type==1) //扣除等级
|
||
{
|
||
OnLev -= Convert.ToInt32(count);
|
||
var data = GameTool.GetAttrData(OnLev);
|
||
result = await db.Updateable<unit_user_attr>(data).Where(i => i.userId == userId)
|
||
.ExecuteCommandAsync() > 0;
|
||
if (result)
|
||
{
|
||
if (userExp.exp > 0)
|
||
{
|
||
await UpdateUserExp(userId, 0, 2);
|
||
}
|
||
|
||
await ClearUserAttrCache(userId);
|
||
}
|
||
}
|
||
|
||
if (result)
|
||
{
|
||
//下装备
|
||
var equService = App.GetService<IGameEquService>();
|
||
var onEqu = await equService.GetUserOnEqu(userId);
|
||
onEqu = onEqu.FindAll(it => it.lev > OnLev);
|
||
if (onEqu.Count > 0)
|
||
{
|
||
foreach (var item in onEqu)
|
||
{
|
||
await equService.EquOnOrDown(item, 0);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#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;
|
||
var myAttr = await GetUserAttrModel(userId);
|
||
data.morale = data.morale > myAttr.upMorale ? myAttr.upMorale : data.morale;
|
||
}
|
||
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 = "")
|
||
{
|
||
if (op == 1)
|
||
{
|
||
var userVigour = await GetUserVigourInfo(userId);
|
||
long endVigour = await GetUserMaxVitality(userId) - (long)userVigour.vitality;
|
||
count = endVigour > count ? count : endVigour;
|
||
}
|
||
|
||
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<long> GetUserMaxVitality(string userId)
|
||
{
|
||
var OnLev = await GetUserLev(userId);
|
||
return GameConfig.GameBaseVigour + (OnLev * 10);
|
||
}
|
||
|
||
#endregion 活力
|
||
|
||
#region 在线时间
|
||
|
||
public async Task<bool> UpdateOnLineTime(string userId, string code, int time)
|
||
{
|
||
if (code == "Default")
|
||
{
|
||
var hookService = App.GetService<IOnHookService>();
|
||
var hookInfo = await hookService.GetUserOnHook(userId);
|
||
if (hookInfo.status == 1)
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public async Task HandleOnLineTimeData()
|
||
{
|
||
int day = DateTime.Now.Day;
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_online_time>();
|
||
await db.Updateable<unit_user_online_time>().SetColumns(it => it.dayTime == 0)
|
||
.WhereIF(day == 1, it => it.monthTime == 0)
|
||
.Where(it => it.userId != "0").ExecuteCommandAsync();
|
||
}
|
||
|
||
#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;
|
||
}
|
||
|
||
public async Task<bool> UpdateUserDrug(string userId, string drugId, int op, int count)
|
||
{
|
||
var data = await GetUserDrug(userId);
|
||
foreach (var item in data.drug)
|
||
{
|
||
if (item.id == drugId)
|
||
{
|
||
if (op == 0)
|
||
{
|
||
item.count = item.count - count;
|
||
}
|
||
else
|
||
{
|
||
item.count = item.count + count;
|
||
}
|
||
|
||
break;
|
||
}
|
||
}
|
||
|
||
bool result = await UpdateUserDrug(data);
|
||
return result;
|
||
}
|
||
|
||
private async Task ClearUserDrug(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "UserTool", "DrugColumn");
|
||
await redis.DelHashAsync(key, userId);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region BUFF状态
|
||
|
||
private async Task<List<unit_user_state>> GetUserStateData(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "State");
|
||
var data = await redis.GetHashAsync<List<unit_user_state>>(key, userId);
|
||
if (data == null)
|
||
{
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_state>();
|
||
data = await db.Queryable<unit_user_state>().Where(it => it.userId == userId)
|
||
.ToListAsync();
|
||
await redis.AddHashAsync(key, userId, data);
|
||
}
|
||
|
||
long endTime = TimeExtend.GetTimeStampSeconds;
|
||
data = data.FindAll(it => it.endTime > endTime);
|
||
|
||
return data;
|
||
}
|
||
|
||
public async Task<List<unit_user_state>> GetUserStateData(string userId, string scene = "Default")
|
||
{
|
||
var data = await GetUserStateData(userId);
|
||
if (scene == "ALL")
|
||
{
|
||
return data;
|
||
}
|
||
|
||
data = data.FindAll(it => it.scene == "Default" || it.scene == scene);
|
||
return data;
|
||
}
|
||
|
||
public async Task<bool> AddUserState(string userId, string groupId, string name, string scene, string tips,
|
||
List<AttrItem> attr, int time, int count = 1)
|
||
{
|
||
bool result = false;
|
||
long onTime = TimeExtend.GetTimeStampSeconds;
|
||
string usId = $"{userId}_{groupId}";
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_state>();
|
||
var usData = await db.Queryable<unit_user_state>().Where(it => it.usId == usId).SingleAsync();
|
||
if (usData == null)
|
||
{
|
||
usData = new unit_user_state();
|
||
usData.usId = usId;
|
||
usData.userId = userId;
|
||
usData.goodsId = groupId;
|
||
usData.name = name;
|
||
usData.scene = scene;
|
||
usData.tips = tips;
|
||
usData.attr = attr;
|
||
usData.addTime = onTime;
|
||
usData.endTime = onTime + (time * 60 * count);
|
||
result = await db.Insertable(usData).ExecuteCommandAsync() > 0;
|
||
}
|
||
else
|
||
{
|
||
if (usData.endTime > onTime)
|
||
{
|
||
usData.endTime = usData.endTime + (time * 60 * count);
|
||
}
|
||
else
|
||
{
|
||
usData.addTime = onTime;
|
||
usData.endTime = onTime + (time * 60 * count);
|
||
}
|
||
|
||
result = await db.Updateable(usData).ExecuteCommandAsync() > 0;
|
||
}
|
||
|
||
if (result)
|
||
{
|
||
await ClearUserState(userId);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private async Task ClearUserState(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "State");
|
||
await redis.DelHashAsync(key, userId);
|
||
}
|
||
|
||
public async Task<decimal> GetUserStateDataTotal(string userId, string scene = "Default")
|
||
{
|
||
var data = await GetUserStateData(userId, scene);
|
||
List<AttrItem> result = new List<AttrItem>();
|
||
foreach (var item in data)
|
||
{
|
||
result.AddRange(item.attr);
|
||
}
|
||
|
||
return GameAttrTool.GetAttrItemValue(nameof(GameEnum.AttrCode.AddExp), result, 0);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 游戏资源储备
|
||
|
||
private async Task<List<unit_user_stock>> GetUserStock(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Stock");
|
||
var data = await redis.GetHashAsync<List<unit_user_stock>>(key, userId);
|
||
if (data == null)
|
||
{
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_stock>();
|
||
data = await db.Queryable<unit_user_stock>().Where(it => it.userId == userId).ToListAsync();
|
||
await redis.AddHashAsync(key, userId, data);
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
public async Task<List<unit_user_stock>> GetUserStock(string userId, string code = "Default")
|
||
{
|
||
var data = await GetUserStock(userId);
|
||
if (code != "Default")
|
||
{
|
||
data = data.FindAll(it => it.code == code);
|
||
}
|
||
|
||
long endTime = TimeExtend.GetTimeStampSeconds;
|
||
data = data.FindAll(it =>
|
||
((it.type == "Number" && it.sign > 0) || it.type != "Number") && it.endTime > endTime);
|
||
return data;
|
||
}
|
||
|
||
public async Task<bool> AddUserStock(string userId, string goodsId, string name, string type, string code,
|
||
long sign,
|
||
string par = "",
|
||
int minute = 0)
|
||
{
|
||
if (minute == 0)
|
||
{
|
||
minute = 525600;
|
||
}
|
||
|
||
bool result = false;
|
||
string usId = $"{userId}_{goodsId}";
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_stock>();
|
||
var usInfo = await db.Queryable<unit_user_stock>().Where(it => it.usId == usId).SingleAsync();
|
||
if (usInfo == null)
|
||
{
|
||
long endTime = TimeExtend.GetTimeStampSeconds + minute * 60;
|
||
unit_user_stock stock = new unit_user_stock()
|
||
{
|
||
usId = usId,
|
||
userId = userId,
|
||
goodsId = goodsId,
|
||
name = name,
|
||
type = type,
|
||
code = code,
|
||
sign = sign,
|
||
par = par,
|
||
endTime = endTime
|
||
};
|
||
result = await db.Insertable(stock).ExecuteCommandAsync() > 0;
|
||
}
|
||
else
|
||
{
|
||
if (usInfo.type == nameof(GameEnum.GameStockType.Number))
|
||
{
|
||
usInfo.sign += sign;
|
||
usInfo.endTime = TimeExtend.GetTimeStampSeconds + minute * 60;
|
||
}
|
||
else if (usInfo.type == nameof(GameEnum.GameStockType.Time))
|
||
{
|
||
long onTime = TimeExtend.GetTimeStampSeconds;
|
||
if (usInfo.endTime > onTime)
|
||
{
|
||
usInfo.endTime = usInfo.endTime + (minute * 60);
|
||
}
|
||
else
|
||
{
|
||
usInfo.endTime = TimeExtend.GetTimeStampSeconds + (minute * 60);
|
||
}
|
||
}
|
||
|
||
result = await db.Updateable(usInfo).ExecuteCommandAsync() > 0;
|
||
}
|
||
|
||
if (result)
|
||
{
|
||
await ClearUserStockCache(userId);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
private async Task ClearUserStockCache(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Stock");
|
||
await redis.DelHashAsync(key, userId);
|
||
}
|
||
|
||
public async Task<bool> UpdateUserStock(unit_user_stock data)
|
||
{
|
||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_stock>();
|
||
var result = await db.Updateable(data).ExecuteCommandAsync() > 0;
|
||
if (result)
|
||
{
|
||
await ClearUserStockCache(data.userId);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 负面状态
|
||
|
||
public async Task<List<unit_user_load>> GetUserLoadState(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "LoadState");
|
||
if (await redis.HExistsHashAsync(key, userId))
|
||
{
|
||
return await redis.GetHashAsync<List<unit_user_load>>(key, userId);
|
||
}
|
||
|
||
return new List<unit_user_load>();
|
||
}
|
||
|
||
public async Task<bool> AddUserLoadState(string userId, string name, string code)
|
||
{
|
||
bool result = false;
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "LoadState");
|
||
var data = await GetUserLoadState(userId);
|
||
var onData = data.Find(it => it.code == code);
|
||
if (onData == null)
|
||
{
|
||
unit_user_load load = new unit_user_load()
|
||
{
|
||
id = $"{userId}_{code}",
|
||
userId = userId,
|
||
name = name,
|
||
code = code,
|
||
count = 1
|
||
};
|
||
data.Add(load);
|
||
result = await redis.AddHashAsync(key, userId, data);
|
||
}
|
||
else
|
||
{
|
||
data.Remove(onData);
|
||
onData.count += 1;
|
||
onData.count = onData.count > 10 ? 10 : onData.count;
|
||
result = await redis.AddHashAsync(key, userId, data);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
public async Task AddUserLoadState(string userId, List<string> codes)
|
||
{
|
||
foreach (var code in codes)
|
||
{
|
||
string name = string.Empty;
|
||
switch (code)
|
||
{
|
||
case "Slow":
|
||
name = "迟缓";
|
||
break;
|
||
|
||
case "Poison":
|
||
name = "中毒";
|
||
break;
|
||
|
||
case "Curse":
|
||
name = "诅咒";
|
||
break;
|
||
|
||
case "Weaken":
|
||
name = "弱化";
|
||
break;
|
||
|
||
case "Depressed":
|
||
name = "消沉";
|
||
break;
|
||
|
||
case "Breach":
|
||
name = "突破";
|
||
break;
|
||
|
||
case "PalsyAtk":
|
||
name = "麻痹";
|
||
break;
|
||
}
|
||
|
||
await AddUserLoadState(userId, name, code);
|
||
}
|
||
}
|
||
|
||
public async Task<bool> RandomRemoveUserLoadState(string userId, int count)
|
||
{
|
||
var data = await GetUserLoadState(userId);
|
||
int opCount = data.Count - count;
|
||
opCount = opCount < 0 ? 0 : opCount;
|
||
if (opCount > 0)
|
||
{
|
||
RandomAssist random = new RandomAssist(opCount);
|
||
var result = random.RandomExtract(data);
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "LoadState");
|
||
await redis.AddHashAsync(key, userId, result);
|
||
}
|
||
else
|
||
{
|
||
await RemoveUserLoadState(userId);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public async Task RemoveUserLoadState(string userId)
|
||
{
|
||
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "LoadState");
|
||
await redis.DelHashAsync(key, userId);
|
||
}
|
||
|
||
#endregion
|
||
} |