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

@@ -0,0 +1,29 @@
using SqlSugar;
using System;
namespace Application.Domain.Entity;
[SplitTable(SplitType.Day)]
[SugarTable("unit_user_equ_log_{year}{month}{day}")]
[Tenant("Kg.SeaTime.Log")]
public class unit_user_equ_log
{
[SugarColumn(IsPrimaryKey = true)] public string logId { get; set; }
[SugarColumn(Length = 50, IsNullable = true)]
public string? ueId { get; set; }
public int? equId { get; set; }
[SugarColumn(Length = 50, IsNullable = true)]
public string? code { get; set; }
[SugarColumn(ColumnDataType = "varchar(max)", IsNullable = true, IsJson = true)]
public unit_user_equ? equData { get; set; }
[SugarColumn(Length = 255, IsNullable = true)]
public string? remark { get; set; }
public DateTime? addTime { get; set; }
}

View File

@@ -0,0 +1,57 @@
namespace Application.Domain.Entity;
public class UserAttrModel
{
public string id { get; set; }
public int area { get; set; }
public int IsSystem { get; set; }
public int isCopy { get; set; }
public string viceId { get; set; }
public string name { get; set; }
public int figState { get; set; }
public string sex { get; set; }
public string code { get; set; }
public string remark { get; set; }
public string TmImg { get; set; }
public int lev { get; set; }//等级
public decimal minAtk { get; set; }//最小攻击
public decimal maxAtk { get; set; }//最大攻击
public decimal defense { get; set; }//防御
public decimal agility { get; set; }//敏捷
public decimal blood { get; set; }//血量
public decimal upBlood { get; set; }//最大血量
public decimal morale { get; set; }//士气
public decimal upMorale { get; set; }//最大士气
public decimal score { get; set; }//评分
public decimal InBlood { get; set; }//吸血
public decimal Crit { get; set; }//暴击
public decimal NoHarm { get; set; }//免伤
public decimal Dodge { get; set; }//闪避
public decimal Rebound { get; set; }//反弹
public decimal ReboundLess { get; set; }//抗反弹
public decimal Punish { get; set; }//制裁
public decimal Slow { get; set; }//迟缓
public decimal SlowLess { get; set; }//迟缓
public decimal Poison { get; set; }//毒攻
public decimal PoisonLess { get; set; }//抗毒攻
public decimal Curse { get; set; }//诅咒
public decimal CurseLess { get; set; }//抗诅咒
public decimal Weaken { get; set; }//虚弱
public decimal WeakenLess { get; set; }//抗虚弱
public decimal Deadly { get; set; }//致命
public decimal DeadlyLess { get; set; }//抗致命
public decimal Depressed { get; set; }//消沉
public decimal DepressedLess { get; set; }//抗消沉
public decimal Breach { get; set; }//突破
public decimal BreachLess { get; set; }//抗突破
public decimal Del_Fashion { get; set; }//卸时装
public decimal Atk_Next { get; set; }//连击
public decimal Harm_Add { get; set; }//额外伤害
public decimal PalsyAtk { get; set; }//麻痹
public decimal PalsyAtkLess { get; set; }//抗麻痹
public string atkTips { get; set; }
public string defTips { get; set; }
public string agilityTips { get; set; }
public string moraleTips { get; set; }
public string bloodTips { get; set; }
}

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>

View File

@@ -12,12 +12,15 @@ public class BagController : ControllerBase
private readonly IUnitUserWeight _weightService;
private readonly IGameGoodsService _goodsService;
private readonly IGameEquService _equService;
private readonly IUnitUserAccService _accService;
public BagController(IUnitUserWeight weightService, IGameGoodsService goodsService, IGameEquService equService)
public BagController(IUnitUserWeight weightService, IGameGoodsService goodsService, IGameEquService equService,
IUnitUserAccService accService)
{
_weightService = weightService;
_goodsService = goodsService;
_equService = equService;
_accService = accService;
}
/// <summary>
@@ -28,14 +31,15 @@ public class BagController : ControllerBase
public async Task<IPoAction> GetUserBagData()
{
string userId = StateHelper.userId;
int onWeight = 0;
int maxWeight = 0;
int gold = 0;
int cowry = 0;
long copper = 0;
var accInfo = await _accService.GetUserAccInfo(userId);
long gold = (long)accInfo.gold;
long cowry = (long)accInfo.cowry;
long copper = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
var userWeight = await _weightService.GetUserWeightInfo(userId);
onWeight = (int)userWeight.onWeight;
maxWeight = (int)userWeight.maxWeight;
int onWeight = (int)userWeight.onWeight;
int maxWeight = (int)userWeight.maxWeight;
return PoAction.Ok(new { onWeight, maxWeight, cowry, gold, copper });
}

View File

@@ -0,0 +1,83 @@
using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.User;
[ApiExplorerSettings(GroupName = "User")]
[Route("User/[controller]/[action]")]
[ApiController]
[Authorize]
public class UserController : ControllerBase
{
private readonly IUnitUserService _userService;
private readonly IUnitUserAttrService _attrService;
private readonly IUnitUserAccService _accService;
private readonly IGameMapService _mapService;
public UserController(IUnitUserService userService, IUnitUserAttrService attrService,
IUnitUserAccService accService, IGameMapService mapService)
{
_userService = userService;
_attrService = attrService;
_accService = accService;
_mapService = mapService;
}
/// <summary>
/// 获取个人资料信息
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetUserInfo()
{
string userId = StateHelper.userId;
var userInfo = await _userService.GetUserInfoByUserId(userId); //基础资料
object user = new { userInfo.userNo, userInfo.nick, userInfo.sex, userInfo.sign };
var attrInfo = await _attrService.GetUserAttrModel(userId); //基础属性
var expInfo = await _attrService.GetUserExp(userId);
object exp = new { expInfo.exp, expInfo.upExp };
var vigourInfo = await _attrService.GetUserVigourInfo(userId);
var accInfo = await _accService.GetUserAccInfo(userId);
object acc = new { accInfo.gold, accInfo.cowry, accInfo.teach, accInfo.renown };
object result = new { user, attr = attrInfo, exp, vigourInfo.vitality, acc };
return PoAction.Ok(result);
}
/// <summary>
/// 获取用户资料
/// </summary>
/// <param name="no"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetHomeInfo(string? no)
{
string userId = StateHelper.userId;
var userInfo = await _userService.GetUserInfoByUserNo(no); //基础资料
if (userInfo == null)
{
return PoAction.Message("玩家不存在!", 102);
}
if (userInfo.userId == userId)
{
return PoAction.Message("用户本身!", 101);
}
object user = new { userInfo.userNo, userInfo.nick, userInfo.sex, userInfo.sign };
var attrInfo = await _attrService.GetUserAttrModel(userInfo.userId); //基础属性
var accInfo = await _accService.GetUserAccInfo(userInfo.userId);
object acc = new { accInfo.teach };
var online = await _mapService.GetUserOnMap(userInfo.userId);
int isOnline = online.upTime > TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddMinutes(GameConfig.OnLineTime))
? 1
: 0;
var onMapInfo = await _mapService.GetMapInfo(online.mapId);
var onCity = await _mapService.GetCityInfo((int)onMapInfo.cityId);
string onMapName = $"{onCity.cityName}-{onMapInfo.mapName}";
object result = new { user, attr = new { attrInfo.lev }, acc, isOnline, onMapName };
return PoAction.Ok(result);
}
}