This commit is contained in:
Putoo
2026-07-03 18:16:47 +08:00
parent 5eb0bfd792
commit ea32e7046e
29 changed files with 390 additions and 75 deletions

View File

@@ -17,7 +17,7 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
return data;
}
public async Task<bool> AddUserFight(string userId, string fightId)
private async Task<bool> AddUserFight(string userId, string fightId)
{
var data = await GetUserFight(userId);
data.Add(fightId);
@@ -61,16 +61,35 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
if (await redis.ExistsAsync(key))
{
blood = await redis.GetAsync<long>(key);
blood = blood > maxBlood ? maxBlood : blood;
}
else
{
await SetFightBlood(keyId, blood);
}
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);
await redis.SetAsync(key, blood, 43200);
}
public async Task AddFightHarm(string fightId,string userId, long harm)
{
string key = string.Format(FightCache.FightCacheKeys, "FightData:Harm", fightId);
if (await redis.HExistsHashAsync(key, userId))
{
long sum = await redis.GetHashAsync<long>(key, userId);
sum += harm;
await redis.AddHashAsync(key, userId, sum);
}
else
{
await redis.AddHashAsync(key, userId, harm);
}
}
@@ -88,6 +107,8 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
long addTime = TimeExtend.GetTimeStampSeconds;
long endTime = addTime + 24 * 60 * 60;
List<game_fight_data> fights = new List<game_fight_data>();
game_fight_data data = new game_fight_data()
{
fightId = fightId,
@@ -104,17 +125,45 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
copper = copper,
petExp = petExp,
award = award,
harm = 0,
pars = pars,
addTime = addTime,
okTime = 0,
endTime = endTime
};
fights.Add(data);
if (code == nameof(GameEnum.FightCode.PVP))
{
fights.Add(new game_fight_data()
{
fightId = StringAssist.NewGuid,
areaCode = areaCode,
keyId = keyId,
mainId = userId,
code = code,
scene = scene,
mapId = mapId,
userId = mainId,
state = 0,
winUser = "",
exp = exp,
copper = copper,
petExp = petExp,
award = award,
pars = pars,
addTime = addTime,
okTime = 0,
endTime = endTime
});
}
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
bool result = await db.Insertable(data).ExecuteCommandAsync() > 0;
bool result = await db.Insertable(fights).ExecuteCommandAsync() > 0;
if (result)
{
await AddUserFight(userId, fightId);
foreach (var item in fights)
{
await AddUserFight(item.userId, item.fightId);
}
}
return result;