This commit is contained in:
Putoo
2026-05-26 18:38:55 +08:00
parent 61cf882b66
commit 6b7193b4c0
33 changed files with 1021 additions and 42 deletions

View File

@@ -2,8 +2,11 @@
public static class GameConfig
{
public const int GameMaxLev = 320;//最大等级
public const int OnLineTime = 30;//在线延迟时间(分钟)
public const int SendChatGoodsBase = 10014;//小海螺
public const int SendChatGoodsArea = 10001;//大海螺
public const int SendChatGoodsService = 10002;//金海螺
public const int UpLevAddWeight = 10;//升级增加负重
public const int GameBaseVigour = 50;//初始活力
}

View File

@@ -0,0 +1,33 @@
namespace Application.Domain;
public static class AccEnum
{
public enum AccType
{
copper,//铜
cowry,//海贝
gold,//金
teach,//师德
renown,//声望
charm,//魅力
evil,//罪恶值
}
public enum Name
{
,
,
,
广,
,
,
Npc商城,
,
}
public enum Code
{
,
}
}

View File

@@ -0,0 +1,9 @@
namespace Application.Domain;
public static class UserEnum
{
public enum AttrCode
{
Person
}
}

View File

@@ -0,0 +1,21 @@
namespace Application.Domain;
public interface IUnitUserAccService
{
Task<long> GetUserAccInfo(string userId, string accType);
#region
Task<unit_user_acc> GetUserAccInfo(string userId);
Task<bool> UpdateUserAcc(string userId, int type, string accType, long count, string name = "",
string remark = "");
Task<bool> UpdateUserAccBath(string userId, int type, string accType, long count, string name = "",
string remark = "");
Task<unit_user_copper> GetUserCopperInfo(string userId);
Task<bool> UpdateUserCopper(string userId, int type, long count, string remark = "");
#endregion
}

View File

@@ -2,6 +2,39 @@
public interface IUnitUserAttrService
{
#region
Task<UserAttrModel> GetUserAttrModel(string userId);
Task<unit_user_attr> GetUserAttr(string userId);
#endregion
#region
Task<unit_user_blood> GetUserBlood(string userId);
Task<bool> UpdateUserBlood(string userId, int op, int count, bool mustUp = false);
#endregion
#region
Task<unit_user_exp> GetUserExp(string userId);
Task<bool> UpdateUserExp(string userId, long exp, int op = 1);
#endregion
#region
Task<unit_user_morale> GetUserMorale(string userId);
Task<bool> UpdateUserMorale(string userId, int op, int count);
#endregion
#region
Task<unit_user_vigour> GetUserVigourInfo(string userId);
Task<bool> UpdateUserVigour(string userId, int op, decimal count, string UpTime = "");
Task<bool> UpdateUserUpVigour(string userId, decimal count);
#endregion
}

View File

@@ -0,0 +1,137 @@
namespace Application.Domain;
public class UnitUserAccService(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserAccService, ITransient
{
public async Task<long> GetUserAccInfo(string userId, string accType)
{
long result = 0;
if (accType == AccEnum.AccType.copper.ToString())
{
var info = await GetUserCopperInfo(userId);
result = (long)info.copper;
}
else if (accType == AccEnum.AccType.cowry.ToString())
{
var info = await GetUserAccInfo(userId);
result = (long)info.cowry;
}
else if (accType == AccEnum.AccType.gold.ToString())
{
var info = await GetUserAccInfo(userId);
result = (long)info.gold;
}
else if (accType == AccEnum.AccType.teach.ToString())
{
var info = await GetUserAccInfo(userId);
result = (long)info.teach;
}
else if (accType == AccEnum.AccType.renown.ToString())
{
var info = await GetUserAccInfo(userId);
result = (long)info.renown;
}
return result;
}
#region
public async Task<unit_user_acc> GetUserAccInfo(string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_acc>();
return await db.Queryable<unit_user_acc>().Where(i => i.userId == userId).SingleAsync();
}
public async Task<bool> UpdateUserAcc(string userId, int type, string accType, long count, string name = "",
string remark = "")
{
bool isOk = false;
decimal opCount = type == 0 ? (0 - count) : count;
try
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_acc>();
await DbClient.AsTenant().BeginTranAsync();
isOk = await db.Updateable<unit_user_acc>()
.SetColumnsIF(accType.Equals(AccEnum.AccType.cowry.ToString()), it => it.cowry == it.cowry + opCount)
.SetColumnsIF(accType.Equals(AccEnum.AccType.gold.ToString()), it => it.gold == it.gold + opCount)
.SetColumnsIF(accType.Equals(AccEnum.AccType.teach.ToString()), it => it.teach == it.teach + opCount)
.SetColumnsIF(accType.Equals(AccEnum.AccType.renown.ToString()), it => it.renown == it.renown + opCount)
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
//生成日志
if (isOk)
{
string code = type == 0 ? nameof(AccEnum.Code.) : nameof(AccEnum.Code.);
unit_user_acc_log userAccLog = new unit_user_acc_log();
userAccLog.accId = StringAssist.NewGuid;
userAccLog.userId = userId;
userAccLog.accType = accType;
userAccLog.code = code;
userAccLog.name = name;
userAccLog.amount = count;
userAccLog.addTime = DateTime.Now;
userAccLog.endTime = DateTime.Now.AddDays(3);
userAccLog.remark = remark;
await db.Insertable(userAccLog).ExecuteCommandAsync();
}
await DbClient.AsTenant().CommitTranAsync();
}
catch
{
isOk = false;
await DbClient.AsTenant().RollbackTranAsync();
}
return isOk;
}
public async Task<bool> UpdateUserAccBath(string userId, int type, string accType, long count, string name = "",
string remark = "")
{
bool result = false;
if (accType == nameof(AccEnum.AccType.copper))
{
result = await UpdateUserCopper(userId, type, count, remark);
}
else
{
result = await UpdateUserAcc(userId, type, accType, count, name, remark);
}
return result;
}
public async Task<unit_user_copper> GetUserCopperInfo(string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_copper>();
return await db.Queryable<unit_user_copper>().Where(i => i.userId == userId).SingleAsync();
}
public async Task<bool> UpdateUserCopper(string userId, int type, long count, string remark = "")
{
bool isOk = false;
decimal opCount = type == 0 ? (0 - count) : count;
try
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_copper>();
await DbClient.AsTenant().BeginTranAsync();
isOk = await db.Updateable<unit_user_copper>()
.SetColumns(it => it.copper == it.copper + opCount)
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
await DbClient.AsTenant().CommitTranAsync();
}
catch
{
isOk = false;
await DbClient.AsTenant().RollbackTranAsync();
}
return isOk;
}
#endregion
}

View File

@@ -2,11 +2,62 @@
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 = (decimal)unitAttr.minAtk;
result.maxAtk = (decimal)unitAttr.maxAtk;
result.defense = (decimal)unitAttr.defense;
result.agility = (decimal)unitAttr.agility;
result.upBlood = (decimal)unitAttr.upBlood;
result.upMorale = (decimal)unitAttr.upMorale;
result.blood = 1;
result.morale = 1;
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;
}
private async Task ClearUserAttrCache(string userId)
{
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "UserAttr");
await redis.DelHashAsync(key, userId);
}
#endregion
#region
#region
public async Task<unit_user_blood> GetUserBlood(string userId)
{
string key = string.Format(UserCache.BaseCacheKeys, "AttrData","Blood");
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Blood");
var data = await redis.GetHashAsync<unit_user_blood>(key, userId);
if (data == null)
{
@@ -14,6 +65,7 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
data = await db.Queryable<unit_user_blood>().Where(it => it.userId == userId).SingleAsync();
await redis.AddHashAsync(key, userId, data);
}
return data;
}
@@ -21,7 +73,7 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
{
bool result = false;
unit_user_blood data = new unit_user_blood();
string key = string.Format(UserCache.BaseCacheKeys, "AttrData","Blood");
string key = string.Format(UserCache.BaseCacheKeys, "AttrData", "Blood");
if (op == 2)
{
data.userId = userId;
@@ -55,8 +107,207 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
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)
{
//增加负重
var wightService = App.GetService<IUnitUserWeight>();
await wightService.UpdateUserMaxWeight(userId, 1, GameConfig.UpLevAddWeight);
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;
}
#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, decimal 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
}

View File

@@ -191,8 +191,8 @@ public class UnitUserService : IUnitUserService, ITransient
//注册活力
unit_user_vigour vitality = new unit_user_vigour();
vitality.userId = userId;
vitality.vitality = 50;
vitality.upVitality = 50;
vitality.vitality = GameConfig.GameBaseVigour;
vitality.upVitality = GameConfig.GameBaseVigour;
vitality.upTime = TimeAssist.GetDateTimeYMDString(0);
db.Insertable(vitality).AddQueue();

View File

@@ -18,7 +18,7 @@ public class GameTool
data.agility = lev;
data.upBlood = ((lev - 1) * 5) + 80;
data.upMorale = 100 + (lev / 5) * 10;
data.levUpdate = TimeAssist.GetTimeStampNum;
data.levUpdate = TimeExtend.GetTimeStampSeconds;
return data;
}
/// <summary>