添加项目文件。
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using Common;
|
||||
using CommonDomain;
|
||||
using CommonInfrastructure;
|
||||
using DotNetCore.CAP;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RedisService;
|
||||
|
||||
namespace AdminApi.GameConfigs
|
||||
{
|
||||
[ApiController, Route("adminapi/Admin/[controller]")]
|
||||
[AllowAnonymous]
|
||||
public class TempSpawnFishController : BaseController
|
||||
{
|
||||
private readonly ICapPublisher _capPublisher;
|
||||
private readonly IRedisService _redisService;
|
||||
private readonly ILogger<TempSpawnFishController> _logger;
|
||||
|
||||
public TempSpawnFishController(ICapPublisher capPublisher, IRedisService redisService, ILogger<TempSpawnFishController> logger)
|
||||
{
|
||||
_capPublisher = capPublisher;
|
||||
_redisService = redisService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("SpawnFish")]
|
||||
public async Task<BaseResponse<string>> SpawnFish(
|
||||
string roomId,
|
||||
string fishNumber,
|
||||
int count = 1,
|
||||
int? seconds = null,
|
||||
int? routeId = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(roomId) || string.IsNullOrWhiteSpace(fishNumber))
|
||||
{
|
||||
|
||||
return new BaseResponse<string>
|
||||
{
|
||||
Code = CommonResponseCodeEnum.ParamError,
|
||||
Info = "roomId / fishNumber 不能为空"
|
||||
};
|
||||
}
|
||||
|
||||
var room = _redisService.GetGameRoom(roomId);
|
||||
if (room == null)
|
||||
{
|
||||
|
||||
return new BaseResponse<string>
|
||||
{
|
||||
Code = CommonResponseCodeEnum.ParamError,
|
||||
Info = $"房间 {roomId} 不存在或不在线"
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
count = Math.Clamp(count <= 0 ? 1 : count, 1, 2000);
|
||||
var effectiveSeconds = (seconds.HasValue && seconds.Value > 0) ? seconds.Value : 30;
|
||||
|
||||
var operationId = Guid.NewGuid().ToString("N");
|
||||
var publishedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
||||
var msg = new TempSpawnFishMessage
|
||||
{
|
||||
RoomId = roomId,
|
||||
FishNumber = fishNumber,
|
||||
Count = count,
|
||||
RouteId = routeId,
|
||||
Seconds = effectiveSeconds,
|
||||
OperationId = operationId,
|
||||
PublishedAtUnixMs = publishedAt
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _capPublisher.PublishAsync(TopicKeys.临时强制出鱼, msg);
|
||||
|
||||
return new BaseResponse<string>
|
||||
{
|
||||
Code = CommonResponseCodeEnum.Success,
|
||||
Info = "已推送临时出鱼指令(异步),operationId=" + operationId,
|
||||
Data = operationId
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex,
|
||||
"[TempSpawn] CAP发布失败 operationId={OperationId} roomId={RoomId} fishNumber={FishNumber} count={Count}",
|
||||
operationId, roomId, fishNumber, count);
|
||||
return new BaseResponse<string>
|
||||
{
|
||||
Code = CommonResponseCodeEnum.Error,
|
||||
Info = "发布失败:" + ex.Message
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user