94 lines
3.7 KiB
C#
94 lines
3.7 KiB
C#
using CommonDomain;
|
|
using DotNetCore.CAP;
|
|
using FishGameService.GameService;
|
|
using FishGameService.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using RedisService;
|
|
using UserService.User;
|
|
|
|
namespace FishGameService.Subscribers
|
|
{
|
|
public class PlayerFactorSubscriber : ICapSubscribe
|
|
{
|
|
private readonly FishSpawnConfigStore _fishSpawnConfigStore;
|
|
private readonly FishSpawnService _fishSpawnService;
|
|
private readonly IRedisService _redisService;
|
|
private readonly IUserService _userService;
|
|
private readonly ILogger<PlayerFactorSubscriber> _logger;
|
|
|
|
public PlayerFactorSubscriber(
|
|
FishSpawnConfigStore fishSpawnConfigStore,
|
|
FishSpawnService fishSpawnService,
|
|
IRedisService redisService,
|
|
IUserService userService,
|
|
ILogger<PlayerFactorSubscriber> logger)
|
|
{
|
|
_fishSpawnConfigStore = fishSpawnConfigStore;
|
|
_fishSpawnService = fishSpawnService;
|
|
_redisService = redisService;
|
|
_userService = userService;
|
|
_logger = logger;
|
|
}
|
|
|
|
[CapSubscribe(TopicKeys.因子变更)]
|
|
public async Task HandlePlayerFactorChanged(PlayerFactorChangedMessage msg)
|
|
{
|
|
var receivedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
var deliveryDelay = msg.PublishedAtUnixMs > 0 ? receivedAt - msg.PublishedAtUnixMs : -1;
|
|
|
|
try
|
|
{
|
|
var removedCount = FishingGameService.ClearPlayerFactorCache(msg.UserId);
|
|
var syncedRoom = false;
|
|
var session = _redisService.GetRedisUserCId(msg.UserId);
|
|
if (!string.IsNullOrWhiteSpace(session?.RoomId))
|
|
{
|
|
var user = await _userService.GetUser(msg.UserId);
|
|
var room = _redisService.GetGameRoom(session.RoomId);
|
|
var player = room?.Players?.FirstOrDefault(p => p.UserId == msg.UserId);
|
|
if (user?.PlayerFactor != null && room != null && player != null)
|
|
{
|
|
player.PlayerFactor = user.PlayerFactor;
|
|
_redisService.SetPlayerFactor(room.RoomId, msg.UserId, user.PlayerFactor);
|
|
_redisService.SetGameRoom(room);
|
|
syncedRoom = true;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "[FactorChange] 本地缓存刷新或生效失败 operationId={OperationId} userId={UserId} result=Failed",
|
|
msg.OperationId, msg.UserId);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
[CapSubscribe(TopicKeys.配置变更)]
|
|
public void HandleConfigChanged(ConfigChangedMessage msg)
|
|
{
|
|
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
var deliveryDelay = msg.PublishedAtUnixMs > 0 ? now - msg.PublishedAtUnixMs : -1;
|
|
|
|
try
|
|
{
|
|
FishingGameService.ClearAllConfigCache();
|
|
|
|
_fishSpawnConfigStore.InvalidateAll();
|
|
|
|
_fishSpawnService.ReloadAllRoomConfigs();
|
|
var effectiveAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
var elapsed = effectiveAt - now;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "[Config] 本地缓存刷新或配置生效失败 operationId={OperationId} ConfigType={ConfigType} result=Failed",
|
|
msg.OperationId, msg.ConfigType);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|