121212
This commit is contained in:
@@ -27,5 +27,13 @@ public interface IGameGoodsService
|
||||
Task<int> GetUserGoodWeightSum(string userId, int type);
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 药品
|
||||
|
||||
Task<string> CheckUseDrug(string userId, int goodsId, string drugConfig, string scene = "");
|
||||
Task<bool> UseDrug(string userId, int goodsId, string drugConfig, string scene = "");
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -7,6 +7,7 @@ public interface IUnitUserAttrService
|
||||
Task<UserAttrModel> GetUserAttrModel(string userId, string scene = "Default");
|
||||
Task<unit_user_attr> GetUserAttr(string userId);
|
||||
Task<int> GetUserLev(string userId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 体力操作
|
||||
@@ -29,6 +30,7 @@ public interface IUnitUserAttrService
|
||||
Task<bool> UpdateUserMorale(string userId, int op, int count);
|
||||
Task<bool> CheackRecoverMorale(string userId);
|
||||
Task LockRecoverMorale(string userId);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 活力
|
||||
@@ -43,23 +45,42 @@ public interface IUnitUserAttrService
|
||||
|
||||
Task<bool> UpdateOnLineTime(string userId, string code, int time);
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 药品栏
|
||||
#region 药品栏
|
||||
|
||||
Task<unit_user_drug> GetUserDrug(string userId);
|
||||
Task<bool> UpdateUserDrug(unit_user_drug data);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 状态
|
||||
#region BUFF状态
|
||||
|
||||
Task<List<unit_user_state>> GetUserStateData(string userId, string scene = "Default");
|
||||
|
||||
Task<bool> AddUserState(string userId, string groupId, string name, string scene, string tips,
|
||||
List<AttrItem> attr, int time, int count = 1);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 游戏资源储备
|
||||
|
||||
Task<List<unit_user_stock>> GetUserStock(string userId, string code = "Default");
|
||||
|
||||
Task<bool> AddUserStock(string userId, string goodsId,string name, string type, string code, long sign,
|
||||
string par = "",
|
||||
int minute = 0);
|
||||
|
||||
Task<bool> UpdateUserStock(unit_user_stock data);
|
||||
|
||||
#endregion
|
||||
|
||||
#region 负面状态
|
||||
|
||||
Task<List<unit_user_load>> GetUserLoadState(string userId);
|
||||
Task<bool> AddUserLoadState(string userId, string name, string code);
|
||||
Task<bool> RandomRemoveUserLoadState(string userId, int count);
|
||||
Task RemoveUserLoadState(string userId);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection.Metadata;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Application.Domain;
|
||||
|
||||
@@ -68,6 +69,7 @@ public class GameGoodsService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
|
||||
.OrderBy(it => it.count, OrderByType.Desc)
|
||||
.ToPageListAsync(page, limit, total);
|
||||
}
|
||||
|
||||
public async Task<List<unit_user_goods>> GetUserGoodsData(string userId, string code)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_goods>();
|
||||
@@ -163,8 +165,6 @@ public class GameGoodsService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
|
||||
return await db.Insertable(bagLog).SplitTable().ExecuteCommandAsync() > 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region 辅助
|
||||
@@ -187,10 +187,142 @@ public class GameGoodsService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
|
||||
|
||||
#endregion
|
||||
|
||||
#region 药品
|
||||
#region 药品
|
||||
|
||||
|
||||
|
||||
private async Task<long> GetUserDrugLockTime(string userId, int goodsId)
|
||||
{
|
||||
long result = 0;
|
||||
string key = string.Format(UserCache.BaseCacheKey, "UserDrug", $"Lock:{userId}_{goodsId}");
|
||||
if (await redis.ExistsAsync(key))
|
||||
{
|
||||
long end = await redis.GetAsync<long>(key);
|
||||
result = end - TimeExtend.GetTimeStampSeconds;
|
||||
result = result < 0 ? 0 : result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task AddUserDrugLockTime(string userId, int goodsId, int time)
|
||||
{
|
||||
if (time > 0)
|
||||
{
|
||||
long end = TimeExtend.GetTimeStampSeconds + time;
|
||||
string key = string.Format(UserCache.BaseCacheKey, "UserDrug", $"Lock:{userId}_{goodsId}");
|
||||
await redis.SetAsync(key, end, time);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> GetUserDrugLockCount(string userId, int goodsId, string scene = "")
|
||||
{
|
||||
int result = 0;
|
||||
if (!string.IsNullOrEmpty(scene))
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKey, "UserDrug", $"Count:{scene}:{userId}_{goodsId}");
|
||||
if (await redis.ExistsAsync(key))
|
||||
{
|
||||
result = await redis.GetAsync<int>(key);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task AddUserDrugLockCount(string userId, int goodsId, string scene = "")
|
||||
{
|
||||
if (!string.IsNullOrEmpty(scene))
|
||||
{
|
||||
var count = await GetUserDrugLockCount(userId, goodsId, scene);
|
||||
count += 1;
|
||||
string key = string.Format(UserCache.BaseCacheKey, "UserDrug", $"Count:{scene}:{userId}_{goodsId}");
|
||||
await redis.SetAsync(key, count, 43200);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<string> CheckUseDrug(string userId, int goodsId, string drugConfig, string scene = "")
|
||||
{
|
||||
string result = string.Empty;
|
||||
dynamic _drugConfig = JsonConvert.DeserializeObject<dynamic>(drugConfig);
|
||||
if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Blood))
|
||||
{
|
||||
//判断所定
|
||||
long lockTime = await GetUserDrugLockTime(userId, goodsId);
|
||||
if (lockTime > 0)
|
||||
{
|
||||
result = $"药品冷却中,{lockTime}秒后可使用!";
|
||||
}
|
||||
}
|
||||
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.BloodStock))
|
||||
{
|
||||
if (_drugConfig.type == nameof(GameEnum.GameStockType.Number))
|
||||
{
|
||||
if (string.IsNullOrEmpty(scene))
|
||||
{
|
||||
var attrService = App.GetService<IUnitUserAttrService>();
|
||||
var userStock = await attrService.GetUserStock(userId);
|
||||
if (userStock.Count(it => it.goodsId == goodsId.ToString()) > 0)
|
||||
{
|
||||
result = "您已经使用过该药品啦!";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//要判断使用个数
|
||||
if (_drugConfig.use > 0)
|
||||
{
|
||||
var count = await GetUserDrugLockCount(userId, goodsId, scene);
|
||||
if (count >= _drugConfig.use)
|
||||
{
|
||||
result = $"当前最多可用{_drugConfig.use}个该物品!";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> UseDrug(string userId, int goodsId, string drugConfig, string scene = "")
|
||||
{
|
||||
bool result = false;
|
||||
var attrService = App.GetService<IUnitUserAttrService>();
|
||||
dynamic _drugConfig = JsonConvert.DeserializeObject<dynamic>(drugConfig);
|
||||
if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Blood))
|
||||
{
|
||||
result = await attrService.UpdateUserBlood(userId, 1, Convert.ToInt32(_drugConfig.num), true);
|
||||
if (result)
|
||||
{
|
||||
await AddUserDrugLockTime(userId, goodsId, Convert.ToInt32(_drugConfig.time));
|
||||
}
|
||||
}
|
||||
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Morale))
|
||||
{
|
||||
result = await attrService.UpdateUserMorale(userId, 1, Convert.ToInt32(_drugConfig.num));
|
||||
}
|
||||
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.Vigour))
|
||||
{
|
||||
result = await attrService.UpdateUserVigour(userId, 1, Convert.ToInt32(_drugConfig.num));
|
||||
}
|
||||
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.LoadBuff))
|
||||
{
|
||||
result = await attrService.RandomRemoveUserLoadState(userId, Convert.ToInt32(_drugConfig.num));
|
||||
}
|
||||
else if (_drugConfig.cls == nameof(GoodsEnum.DrugCls.BloodStock))
|
||||
{
|
||||
result = await attrService.AddUserStock(userId, goodsId.ToString(), Convert.ToString(_drugConfig.name),Convert.ToString(_drugConfig.type),
|
||||
Convert.ToString(_drugConfig.code),
|
||||
Convert.ToInt64(_drugConfig.num), Convert.ToString(_drugConfig.par),
|
||||
Convert.ToInt32(_drugConfig.minute));
|
||||
if (result)
|
||||
{
|
||||
await AddUserDrugLockCount(userId, goodsId, scene);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -129,7 +129,8 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
result.morale = (int)userMorale.morale;
|
||||
|
||||
//负面状态处理
|
||||
//result = await GameAttrTool.CheakRoleLoadBuff(result);
|
||||
var loadState = await GetUserLoadState(userId);
|
||||
result = await GameAttrTool.CheckRoleLoadBuff(result, loadState);
|
||||
result = await ReviseUserRoleAttr(result);
|
||||
return result;
|
||||
|
||||
@@ -241,6 +242,11 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,6 +375,8 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
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)
|
||||
{
|
||||
@@ -431,6 +439,13 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
|
||||
public async Task<bool> UpdateUserVigour(string userId, int op, long count, string UpTime = "")
|
||||
{
|
||||
if (op == 1)
|
||||
{
|
||||
var userVigour = await GetUserVigourInfo(userId);
|
||||
long endVigour = (long)userVigour.upVitality - (long)userVigour.vitality;
|
||||
count = endVigour > count ? count : endVigour;
|
||||
}
|
||||
|
||||
bool result = false;
|
||||
try
|
||||
{
|
||||
@@ -532,7 +547,7 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
|
||||
#endregion
|
||||
|
||||
#region 状态
|
||||
#region BUFF状态
|
||||
|
||||
private async Task<List<unit_user_state>> GetUserStateData(string userId)
|
||||
{
|
||||
@@ -557,6 +572,7 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
{
|
||||
return data;
|
||||
}
|
||||
|
||||
data = data.FindAll(it => it.scene == "Default" || it.scene == scene);
|
||||
return data;
|
||||
}
|
||||
@@ -613,4 +629,186 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
}
|
||||
|
||||
#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 ClearUserAttrCache(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;
|
||||
result = await redis.AddHashAsync(key, userId, data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user