From 5eb0bfd79271d7664a87b51eef876f87ee2a41e6 Mon Sep 17 00:00:00 2001 From: Putoo <290555932@qq.com> Date: Thu, 2 Jul 2026 18:52:36 +0800 Subject: [PATCH] 1212 --- .../logdb/db/game_fight_data.cs | 117 +++++++++++++ .../Application.Domain/Cache/FightCache.cs | 7 + .../Interface/Fight/IGameFightService.cs | 27 +++ .../Service/Fight/GameFightService.cs | 159 ++++++++++++++++++ 4 files changed, 310 insertions(+) create mode 100644 Service/Application.Domain.Entity/logdb/db/game_fight_data.cs create mode 100644 Service/Application.Domain/Cache/FightCache.cs create mode 100644 Service/Application.Domain/Services/Interface/Fight/IGameFightService.cs create mode 100644 Service/Application.Domain/Services/Service/Fight/GameFightService.cs diff --git a/Service/Application.Domain.Entity/logdb/db/game_fight_data.cs b/Service/Application.Domain.Entity/logdb/db/game_fight_data.cs new file mode 100644 index 0000000..f828c54 --- /dev/null +++ b/Service/Application.Domain.Entity/logdb/db/game_fight_data.cs @@ -0,0 +1,117 @@ +using SqlSugar; +using System; + +namespace Application.Domain.Entity +{ + [Tenant("Kg.SeaTime.Log")] + public class game_fight_data + { + /// + /// fightId + /// + [SugarColumn(IsPrimaryKey = true, Length = 50)] + public string fightId { get; set; } + + /// + /// 作用区域 + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? areaCode { get; set; } + + /// + /// 关联ID + /// + [SugarColumn(Length = 255, IsNullable = true)] + public string? keyId { get; set; } + + /// + /// 对方主ID + /// + [SugarColumn(Length = 255, IsNullable = true)] + public string? mainId { get; set; } + + /// + /// 战斗类型 + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? code { get; set; } + + /// + /// 场景 + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? scene { get; set; } + + /// + /// mapId + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? mapId { get; set; } + + /// + /// userId + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? userId { get; set; } + + /// + /// state + /// + [SugarColumn(IsNullable = true)] + public int? state { get; set; } + + /// + /// 胜利方 + /// + [SugarColumn(Length = 50, IsNullable = true)] + public string? winUser { get; set; } + + /// + /// 经验 + /// + [SugarColumn(IsNullable = true)] + public long? exp { get; set; } + + /// + /// 铜贝 + /// + [SugarColumn(IsNullable = true)] + public long? copper { get; set; } + + /// + /// 宠物经验 + /// + [SugarColumn(IsNullable = true)] + public long? petExp { get; set; } + + /// + /// 奖励 + /// + [SugarColumn(IsNullable = true)] + public string? award { get; set; } + /// + /// 伤害 + /// + [SugarColumn(IsNullable = true)] + public long? harm { get; set; } + [SugarColumn(IsNullable = true)] + public string? pars { get; set; } + /// + /// addTime + /// + [SugarColumn(IsNullable = true)] + public long? addTime { get; set; } + + /// + /// 战斗结束时间 + /// + [SugarColumn(IsNullable = true)] + public long? okTime { get; set; } + + /// + /// 删除时间 + /// + [SugarColumn(IsNullable = true)] + public long? endTime { get; set; } + } +} diff --git a/Service/Application.Domain/Cache/FightCache.cs b/Service/Application.Domain/Cache/FightCache.cs new file mode 100644 index 0000000..d58859e --- /dev/null +++ b/Service/Application.Domain/Cache/FightCache.cs @@ -0,0 +1,7 @@ +namespace Application.Domain; + +public class FightCache +{ + public static string FightCacheKey = "GameFightCache:{0}"; + public static string FightCacheKeys = "GameFightCache:{0}:{1}"; +} \ No newline at end of file diff --git a/Service/Application.Domain/Services/Interface/Fight/IGameFightService.cs b/Service/Application.Domain/Services/Interface/Fight/IGameFightService.cs new file mode 100644 index 0000000..4ed1590 --- /dev/null +++ b/Service/Application.Domain/Services/Interface/Fight/IGameFightService.cs @@ -0,0 +1,27 @@ +namespace Application.Domain; + +public interface IGameFightService +{ + #region 用户战斗索引 + + Task> GetUserFight(string userId); + Task AddUserFight(string userId, string fightId); + Task RemoveUserFight(string userId, string fightId); + #endregion + #region 战斗信息 + Task GetFightInfo(string fightId); + Task GetFightBlood(string keyId, long maxBlood); + Task SetFightBlood(string keyId, long blood); + + Task AddFight(string fightId, string areaCode, string keyId, string mainId, string code, + string scene, string mapId, + string userId, long exp = 0, long copper = 0, long petExp = 0, string award = "", string pars = ""); + + Task SetFightWin(string fightId, string winUser); + + + #endregion + #region 战斗相关 + + #endregion +} \ No newline at end of file diff --git a/Service/Application.Domain/Services/Service/Fight/GameFightService.cs b/Service/Application.Domain/Services/Service/Fight/GameFightService.cs new file mode 100644 index 0000000..6d2f939 --- /dev/null +++ b/Service/Application.Domain/Services/Service/Fight/GameFightService.cs @@ -0,0 +1,159 @@ +namespace Application.Domain; + +public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGameFightService, ITransient +{ + #region 用户战斗索引 + + public async Task> GetUserFight(string userId) + { + string key = string.Format(FightCache.FightCacheKey, "UserFight"); + if (await redis.HExistsHashAsync(key, userId)) + { + return await redis.GetHashAsync>(key, userId); + } + + List data = new List(); + await redis.AddHashAsync(key, userId, data); + return data; + } + + public async Task AddUserFight(string userId, string fightId) + { + var data = await GetUserFight(userId); + data.Add(fightId); + string key = string.Format(FightCache.FightCacheKey, "UserFight"); + bool result = await redis.AddHashAsync(key, userId, data); + + return result; + } + + public async Task RemoveUserFight(string userId, string fightId) + { + var data = await GetUserFight(userId); + data.Remove(fightId); + string key = string.Format(FightCache.FightCacheKey, "UserFight"); + bool result = await redis.AddHashAsync(key, userId, data); + return result; + } + + #endregion + + #region 战斗信息 + + public async Task GetFightInfo(string fightId) + { + string key = string.Format(FightCache.FightCacheKeys, "FightData:Info", fightId); + if (await redis.ExistsAsync(key)) + { + return await redis.GetAsync(key); + } + + var db = DbClient.AsTenant().GetConnectionWithAttr(); + game_fight_data data = await db.Queryable().Where(it => it.fightId == fightId).SingleAsync(); + await redis.SetAsync(key, data, 3600); + return data; + } + + public async Task GetFightBlood(string keyId, long maxBlood) + { + long blood = maxBlood; + string key = string.Format(FightCache.FightCacheKeys, "FightData:Blood", keyId); + if (await redis.ExistsAsync(key)) + { + blood = await redis.GetAsync(key); + } + + blood = blood > maxBlood ? maxBlood : blood; + return blood; + } + + public async Task SetFightBlood(string keyId, long blood) + { + string key = string.Format(FightCache.FightCacheKeys, "FightData:Blood", keyId); + await redis.SetAsync(key, blood, 3600); + } + + + private async Task ClearFightInfoCache(string fightId, string keyId) + { + string key1 = string.Format(FightCache.FightCacheKeys, "FightData:Info", fightId); + string key2 = string.Format(FightCache.FightCacheKeys, "FightData:Blood", keyId); + await redis.DelAsync(key1, key2); + } + + public async Task AddFight(string fightId, string areaCode, string keyId, string mainId, string code, + string scene, string mapId, + string userId, long exp = 0, long copper = 0, long petExp = 0, string award = "", string pars = "") + { + long addTime = TimeExtend.GetTimeStampSeconds; + long endTime = addTime + 24 * 60 * 60; + + game_fight_data data = new game_fight_data() + { + fightId = fightId, + areaCode = areaCode, + keyId = keyId, + mainId = mainId, + code = code, + scene = scene, + mapId = mapId, + userId = userId, + state = 0, + winUser = "", + exp = exp, + copper = copper, + petExp = petExp, + award = award, + harm = 0, + pars = pars, + addTime = addTime, + okTime = 0, + endTime = endTime + }; + var db = DbClient.AsTenant().GetConnectionWithAttr(); + bool result = await db.Insertable(data).ExecuteCommandAsync() > 0; + if (result) + { + await AddUserFight(userId, fightId); + } + + return result; + } + + public async Task SetFightWin(string fightId, string winUser) + { + bool result = false; + var data = await GetFightInfo(fightId); + if (data != null) + { + long okTime = TimeExtend.GetTimeStampSeconds; + data.state = 1; + data.winUser = winUser; + data.okTime = okTime; + var db = DbClient.AsTenant().GetConnectionWithAttr(); + result = await db.Updateable().SetColumns(it => it.state == 1) + .SetColumns(it => it.winUser == winUser) + .SetColumns(it => it.okTime == okTime) + .Where(it => it.fightId == fightId).ExecuteCommandAsync() > 0; + } + + if (result) + { + await ClearFightInfoCache(fightId, data.keyId); + //此处调用战斗结束相关 + await HandleFight(data); + } + + return result; + } + + #endregion + + #region 战斗相关 + + private async Task HandleFight(game_fight_data data) + { + } + + #endregion +} \ No newline at end of file