117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
using System.Threading.Tasks;
|
|
using System.Text.Json;
|
|
using CommonDomain;
|
|
using CommonDomain.BaseModel;
|
|
using CommonDomain.Enum;
|
|
using DotNetCore.CAP;
|
|
using Microsoft.Extensions.Logging;
|
|
using RedisService;
|
|
using UserDomain.Enum;
|
|
using UserService.Param;
|
|
using UserService.User;
|
|
|
|
namespace UserService.SummonCard
|
|
{
|
|
public class SummonCardService : ISummonCardService
|
|
{
|
|
private const string SummonCardNumber = "40006";
|
|
|
|
private readonly IUserService _userService;
|
|
private readonly IRedisService _redisService;
|
|
private readonly ICapPublisher _capPublisher;
|
|
|
|
public SummonCardService(IUserService userService, IRedisService redisService, ICapPublisher capPublisher)
|
|
{
|
|
_userService = userService;
|
|
_redisService = redisService;
|
|
_capPublisher = capPublisher;
|
|
}
|
|
|
|
public async Task<(bool success, string message)> Use(string userId)
|
|
{
|
|
|
|
var user = await _userService.GetUser(userId);
|
|
if (user == null)
|
|
{
|
|
|
|
return (false, "用户不存在");
|
|
}
|
|
|
|
var prop = user.Props.FirstOrDefault(p => p.Number == SummonCardNumber);
|
|
if (prop == null || prop.Count <= 0)
|
|
{
|
|
|
|
return (false, "召唤卡数量不足");
|
|
}
|
|
|
|
var session = _redisService.GetRedisUserCId(userId);
|
|
if (string.IsNullOrWhiteSpace(session?.RoomId))
|
|
{
|
|
|
|
return (false, "请进入捕鱼房间后使用召唤卡");
|
|
}
|
|
|
|
var room = _redisService.GetGameRoom(session.RoomId);
|
|
if (room == null || room.GameType == GameTypeEnum.中国象棋 ||
|
|
!room.Players.Any(p => !p.IsBot && p.UserId == userId && p.State == 0))
|
|
{
|
|
|
|
return (false, "当前不在有效的捕鱼房间中");
|
|
}
|
|
|
|
var targetFishType = room.GameType == GameTypeEnum.星际迷航
|
|
? FishTypeEnum.Boss鱼
|
|
: FishTypeEnum.黄金鱼;
|
|
|
|
|
|
var spawnConfigJson = _redisService.GetFishSpawnConfig();
|
|
var spawnConfig = string.IsNullOrWhiteSpace(spawnConfigJson)
|
|
? null
|
|
: JsonSerializer.Deserialize<FishSpawnConfigStoreModel>(spawnConfigJson,
|
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
var levelKey = $"{(int)room.GameType}:{room.RoomLevel}";
|
|
if (spawnConfig?.Levels == null ||
|
|
!spawnConfig.Levels.TryGetValue(levelKey, out var levelConfig) ||
|
|
levelConfig?.Fishes == null ||
|
|
!levelConfig.Fishes.Any(f => f.FishType == (int)targetFishType && f.FishWeight > 0))
|
|
{
|
|
|
|
return (false, $"当前场次未配置可召唤的{(targetFishType == FishTypeEnum.Boss鱼 ? "Boss" : "黄金鱼")}");
|
|
}
|
|
|
|
var deductResult = await _userService.UpdateAssets(new UserAssetParam
|
|
{
|
|
UserId = userId,
|
|
ChangeType = UserAssetChangeTypeEnum.减少,
|
|
RewardItems = new System.Collections.Generic.List<CommonDomain.BaseModel.BaseProductCount>
|
|
{
|
|
new CommonDomain.BaseModel.BaseProductCount { Number = SummonCardNumber, Count = 1 }
|
|
},
|
|
Source = RewardSource.召唤卡,
|
|
Remark = "使用召唤卡",
|
|
IsAddRecord = false
|
|
});
|
|
|
|
if (!deductResult.isok)
|
|
{
|
|
|
|
return (false, deductResult.msg);
|
|
}
|
|
|
|
var operationId = Guid.NewGuid().ToString("N");
|
|
var publishedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
await _capPublisher.PublishAsync(TopicKeys.召唤卡出鱼, new SummonCardFishMessage
|
|
{
|
|
UserId = userId,
|
|
RoomId = room.RoomId,
|
|
FishType = (int)targetFishType,
|
|
OperationId = operationId,
|
|
PublishedAtUnixMs = publishedAt
|
|
});
|
|
|
|
|
|
return (true, "使用成功");
|
|
}
|
|
}
|
|
}
|