12121
This commit is contained in:
169
Service/Application.Web/Controllers/Pub/ExchangeController.cs
Normal file
169
Service/Application.Web/Controllers/Pub/ExchangeController.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Application.Web.Controllers.Pub;
|
||||
|
||||
/// <summary>
|
||||
/// 兑换接口
|
||||
/// </summary>
|
||||
[Route("[controller]/[action]")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ExchangeController : ControllerBase
|
||||
{
|
||||
private readonly IGameMapService _mapService;
|
||||
private readonly IExchangeService _exchangeService;
|
||||
|
||||
public ExchangeController(IGameMapService mapService, IExchangeService exchangeService)
|
||||
{
|
||||
_mapService = mapService;
|
||||
_exchangeService = exchangeService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取兑换信息
|
||||
/// </summary>
|
||||
/// <param name="npcId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetExchangeData(int npcId)
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
int areaId = StateHelper.areaId;
|
||||
var npcInfo = await _mapService.GetNpcInfo(npcId);
|
||||
if (npcInfo == null)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
if (npcInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.Exchange)))
|
||||
{
|
||||
return PoAction.Message("业务不可用!");
|
||||
}
|
||||
|
||||
var onMap = await _mapService.GetUserOnMapId(userId);
|
||||
if (npcInfo.mapId != onMap)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
var data = await _exchangeService.GetExchangeData(npcId);
|
||||
data = data.FindAll(it => GameTool.AreaVerify(areaId, it.areaId) && it.endTime > DateTime.Now);
|
||||
|
||||
return PoAction.Ok(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 兑换物品
|
||||
/// </summary>
|
||||
/// <param name="npcId"></param>
|
||||
/// <param name="exId"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> ExchangeOk(int npcId, int exId, int count)
|
||||
{
|
||||
count = count < 1 ? 1 : count;
|
||||
string userId = StateHelper.userId;
|
||||
int areaId = StateHelper.areaId;
|
||||
var npcInfo = await _mapService.GetNpcInfo(npcId);
|
||||
if (npcInfo == null)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
if (npcInfo.status != 1)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.Exchange)))
|
||||
{
|
||||
return PoAction.Message("业务不可用!");
|
||||
}
|
||||
|
||||
var onMap = await _mapService.GetUserOnMapId(userId);
|
||||
if (npcInfo.mapId != onMap)
|
||||
{
|
||||
return PoAction.Message("Npc不存在!");
|
||||
}
|
||||
|
||||
var exInfo = await _exchangeService.GetExchangeInfo(exId);
|
||||
if (exInfo == null)
|
||||
{
|
||||
return PoAction.Message("兑换不存在!");
|
||||
}
|
||||
|
||||
if (exInfo.endTime < DateTime.Now)
|
||||
{
|
||||
return PoAction.Message("兑换已结束!");
|
||||
}
|
||||
|
||||
if (GameTool.AreaVerify(areaId, exInfo.areaId) == false)
|
||||
{
|
||||
return PoAction.Message("兑换不存在!");
|
||||
}
|
||||
|
||||
int sumCount = await _exchangeService.GetExchangeCount(userId, exId);
|
||||
if ((sumCount + count) > exInfo.count)
|
||||
{
|
||||
int endCount = (int)exInfo.count - sumCount;
|
||||
if (endCount == 0)
|
||||
{
|
||||
return PoAction.Message("您已达到兑换上限!");
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message($"您最多还能兑换{endCount}次!");
|
||||
}
|
||||
}
|
||||
|
||||
var result = await GameBus.CheckNeed(userId, exInfo.needData, count);
|
||||
if (result.result)
|
||||
{
|
||||
if (await GameBus.UpdateBag(userId, 0, exInfo.needData, "兑换", count))
|
||||
{
|
||||
long endTime = 0;
|
||||
switch (exInfo.exType)
|
||||
{
|
||||
case "Long":
|
||||
endTime = TimeExtend.GetTimeStampBySeconds((DateTime)exInfo.endTime);
|
||||
break;
|
||||
case "Day":
|
||||
endTime = TimeExtend.GetTimeStampBySeconds(TimeAssist.GetDateTimeYMD(1));
|
||||
break;
|
||||
case "Month":
|
||||
DateTime nt = DateTime.Now.AddMonths(1);
|
||||
nt = Convert.ToDateTime($"{nt.Year}-{nt.Month}-01");
|
||||
endTime = TimeExtend.GetTimeStampBySeconds(nt);
|
||||
break;
|
||||
case "Week":
|
||||
endTime = TimeExtend.GetTimeStampBySeconds(TimeAssist.WeekEndTime);
|
||||
break;
|
||||
}
|
||||
|
||||
if (await _exchangeService.AddUserExchangeLog(userId, exId, count, endTime))
|
||||
{
|
||||
await GameBus.UpdateBag(userId, 1, exInfo.getData, "兑换", count);
|
||||
return PoAction.Ok(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("兑换失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("兑换失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("兑换失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user