This commit is contained in:
Putoo
2026-07-02 18:52:36 +08:00
parent 7ce9787172
commit 5eb0bfd792
4 changed files with 310 additions and 0 deletions

View File

@@ -0,0 +1,117 @@
using SqlSugar;
using System;
namespace Application.Domain.Entity
{
[Tenant("Kg.SeaTime.Log")]
public class game_fight_data
{
/// <summary>
/// fightId
/// </summary>
[SugarColumn(IsPrimaryKey = true, Length = 50)]
public string fightId { get; set; }
/// <summary>
/// 作用区域
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? areaCode { get; set; }
/// <summary>
/// 关联ID
/// </summary>
[SugarColumn(Length = 255, IsNullable = true)]
public string? keyId { get; set; }
/// <summary>
/// 对方主ID
/// </summary>
[SugarColumn(Length = 255, IsNullable = true)]
public string? mainId { get; set; }
/// <summary>
/// 战斗类型
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? code { get; set; }
/// <summary>
/// 场景
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? scene { get; set; }
/// <summary>
/// mapId
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? mapId { get; set; }
/// <summary>
/// userId
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? userId { get; set; }
/// <summary>
/// state
/// </summary>
[SugarColumn(IsNullable = true)]
public int? state { get; set; }
/// <summary>
/// 胜利方
/// </summary>
[SugarColumn(Length = 50, IsNullable = true)]
public string? winUser { get; set; }
/// <summary>
/// 经验
/// </summary>
[SugarColumn(IsNullable = true)]
public long? exp { get; set; }
/// <summary>
/// 铜贝
/// </summary>
[SugarColumn(IsNullable = true)]
public long? copper { get; set; }
/// <summary>
/// 宠物经验
/// </summary>
[SugarColumn(IsNullable = true)]
public long? petExp { get; set; }
/// <summary>
/// 奖励
/// </summary>
[SugarColumn(IsNullable = true)]
public string? award { get; set; }
/// <summary>
/// 伤害
/// </summary>
[SugarColumn(IsNullable = true)]
public long? harm { get; set; }
[SugarColumn(IsNullable = true)]
public string? pars { get; set; }
/// <summary>
/// addTime
/// </summary>
[SugarColumn(IsNullable = true)]
public long? addTime { get; set; }
/// <summary>
/// 战斗结束时间
/// </summary>
[SugarColumn(IsNullable = true)]
public long? okTime { get; set; }
/// <summary>
/// 删除时间
/// </summary>
[SugarColumn(IsNullable = true)]
public long? endTime { get; set; }
}
}

View File

@@ -0,0 +1,7 @@
namespace Application.Domain;
public class FightCache
{
public static string FightCacheKey = "GameFightCache:{0}";
public static string FightCacheKeys = "GameFightCache:{0}:{1}";
}

View File

@@ -0,0 +1,27 @@
namespace Application.Domain;
public interface IGameFightService
{
#region
Task<List<string>> GetUserFight(string userId);
Task<bool> AddUserFight(string userId, string fightId);
Task<bool> RemoveUserFight(string userId, string fightId);
#endregion
#region
Task<game_fight_data> GetFightInfo(string fightId);
Task<long> GetFightBlood(string keyId, long maxBlood);
Task SetFightBlood(string keyId, long blood);
Task<bool> 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<bool> SetFightWin(string fightId, string winUser);
#endregion
#region
#endregion
}

View File

@@ -0,0 +1,159 @@
namespace Application.Domain;
public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGameFightService, ITransient
{
#region
public async Task<List<string>> GetUserFight(string userId)
{
string key = string.Format(FightCache.FightCacheKey, "UserFight");
if (await redis.HExistsHashAsync(key, userId))
{
return await redis.GetHashAsync<List<string>>(key, userId);
}
List<string> data = new List<string>();
await redis.AddHashAsync(key, userId, data);
return data;
}
public async Task<bool> 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<bool> 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<game_fight_data> GetFightInfo(string fightId)
{
string key = string.Format(FightCache.FightCacheKeys, "FightData:Info", fightId);
if (await redis.ExistsAsync(key))
{
return await redis.GetAsync<game_fight_data>(key);
}
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
game_fight_data data = await db.Queryable<game_fight_data>().Where(it => it.fightId == fightId).SingleAsync();
await redis.SetAsync(key, data, 3600);
return data;
}
public async Task<long> 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<long>(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<bool> 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<game_fight_data>();
bool result = await db.Insertable(data).ExecuteCommandAsync() > 0;
if (result)
{
await AddUserFight(userId, fightId);
}
return result;
}
public async Task<bool> 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<game_fight_data>();
result = await db.Updateable<game_fight_data>().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
}