Files
Kg.SeaTime/Service/Application.Web/Controllers/Map/StoreController.cs
2026-06-18 18:54:22 +08:00

295 lines
9.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Map;
/// <summary>
/// 商店接口
/// </summary>
[ApiExplorerSettings(GroupName = "Map")]
[Route("Map/[controller]/[action]")]
[ApiController]
[Authorize]
public class StoreController : ControllerBase
{
private readonly IGameStoreService _storeService;
private readonly IGameMapService _mapService;
private readonly IUnitUserAccService _accService;
private readonly IUnitUserWeight _weightService;
private readonly IGameEquService _equService;
private readonly IGameGoodsService _goodsService;
public StoreController(IGameStoreService storeService, IGameMapService mapService, IUnitUserAccService accService,
IUnitUserWeight weightService, IGameEquService equService, IGameGoodsService goodsService)
{
_storeService = storeService;
_mapService = mapService;
_accService = accService;
_weightService = weightService;
_equService = equService;
_goodsService = goodsService;
}
/// <summary>
/// 获取npc商店信息
/// </summary>
/// <param name="npcId"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetNpcStore(int npcId, int page)
{
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.Store)))
{
return PoAction.Message("业务不可用!");
}
var onMap = await _mapService.GetUserOnMapId(userId);
if (npcInfo.mapId != onMap)
{
return PoAction.Message("Npc不存在!");
}
RefAsync<int> Total = 0;
var data = await _storeService.GetStoreData(npcId, areaId, page, 10, Total);
return PoAction.Ok(new { data, total = Total.Value, npcInfo.npcName });
}
/// <summary>
/// 购买物品
/// </summary>
/// <param name="parms"></param>
/// <returns></returns>
[HttpPost]
public async Task<IPoAction> Buy([FromBody] StoreBuyParms parms)
{
parms.count = parms.count < 1 ? 1 : parms.count;
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
var npcInfo = await _mapService.GetNpcInfo(parms.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.Store)))
{
return PoAction.Message("业务不可用!");
}
var onMap = await _mapService.GetUserOnMapId(userId);
if (npcInfo.mapId != onMap)
{
return PoAction.Message("Npc不存在!");
}
var storeInfo = await _storeService.GetStoreInfo(parms.storeId);
if (storeInfo == null)
{
return PoAction.Message("商品已下架!");
}
if (!GameTool.AreaVerify(areaId, storeInfo.areaId))
{
return PoAction.Message("商品已下架!");
}
if (storeInfo.npcId != parms.npcId)
{
return PoAction.Message("商品已下架!");
}
//此处需要计算负重
var needWeight = await GameTool.GetPropWeight(storeInfo.type, storeInfo.parameter);
needWeight = needWeight * parms.count;
if (await _weightService.CheckUserWeight(userId, needWeight) == false)
{
return PoAction.Message("负重不足!");
}
long need = parms.count * (long)storeInfo.price;
var myAcc = await _accService.GetUserAccInfo(userId, storeInfo.payType);
if (myAcc < need)
{
return PoAction.Message($"您的{GameTool.GetCurrencyName(storeInfo.payType)}不足!");
}
if (await _accService.UpdateUserAccBath(userId, 0, storeInfo.payType, need, nameof(AccEnum.Name.Npc商城),
$"商城购买物品:{storeInfo.name}×{parms.count}"))
{
await GameBus.UpdateBag(userId, 1, storeInfo.type, storeInfo.parameter, parms.count, "Npc商店购买");
return PoAction.Ok(true);
}
else
{
return PoAction.Message("购买失败,请稍后尝试!");
}
}
/// <summary>
/// 出售物品
/// </summary>
/// <param name="npcId"></param>
/// <param name="type"></param>
/// <param name="query"></param>
/// <param name="page"></param>
/// <returns></returns>
[HttpGet]
public async Task<IPoAction> GetNpcSale(int npcId, int type, string? query, int page)
{
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.Store)))
{
return PoAction.Message("业务不可用!");
}
var onMap = await _mapService.GetUserOnMapId(userId);
if (npcInfo.mapId != onMap)
{
return PoAction.Message("Npc不存在!");
}
RefAsync<int> total = 0;
if (type == 0)
{
var data = await _equService.GetUserEquData(userId, 0, query, page, 10, total, true);
return PoAction.Ok(new { data, total.Value });
}
else if (type == 1)
{
List<string> code = new List<string>() { nameof(GoodsEnum.Code.Drug) };
var data = await _goodsService.GetUserGoodsData(userId, code, new List<string>(), query, page, 10, total);
return PoAction.Ok(new { data, total.Value });
}
else if (type == 2)
{
List<string> code = new List<string>();
List<string> noCode = new List<string>() { nameof(GoodsEnum.Code.Drug) };
var data = await _goodsService.GetUserGoodsData(userId, code, noCode, query, page, 10, total);
return PoAction.Ok(new { data, total.Value });
}
else
{
return PoAction.Ok(new { data = new List<string>(), total.Value });
}
}
/// <summary>
/// 出售物品
/// </summary>
/// <param name="parms"></param>
/// <returns></returns>
[HttpPost]
public async Task<IPoAction> Sale([FromBody] StoreSaleParms parms)
{
parms.count = parms.count < 1 ? 1 : parms.count;
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
var npcInfo = await _mapService.GetNpcInfo(parms.npcId);
if (npcInfo == null)
{
return PoAction.Message("Npc不存在!");
}
if (npcInfo.status != 1)
{
return PoAction.Message("Npc不存在!");
}
var onMap = await _mapService.GetUserOnMapId(userId);
if (npcInfo.mapId != onMap)
{
return PoAction.Message("Npc不存在!");
}
if (parms.type == 0)//装备
{
var equInfo = await _equService.GetUserEquInfo(parms.id);
if (equInfo == null)
{
return PoAction.Message("装备不存在!");
}
if (equInfo.userId != userId)
{
return PoAction.Message("装备不存在!");
}
if (equInfo.isOn == 1)
{
return PoAction.Message("穿戴中的装备无法出售!");
}
if (equInfo.isLock == 1)
{
return PoAction.Message("装备已锁定!");
}
if (await _equService.DeductUserEqu(userId, new List<unit_user_equ>() { equInfo }, "出售装备"))
{
await _accService.UpdateUserCopper(userId, 1, (int)equInfo.sysPrice, "出售装备");
return PoAction.Ok(true, $"出售成功,获得{equInfo.sysPrice}铜贝!");
}
else
{
return PoAction.Message("出售失败,请稍后尝试!");
}
}
else //物品
{
var goodsInfo = await _goodsService.GetUserGoodsInfo(parms.id);
if (goodsInfo == null)
{
return PoAction.Message("物品不存在!");
}
if (goodsInfo.userId != userId)
{
return PoAction.Message("物品不存在!");
}
if (goodsInfo.count < parms.count)
{
return PoAction.Message("物品不足!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, (int)goodsInfo.goodsId, parms.count, "出售物品"))
{
long copper = (int)goodsInfo.sysPrice * parms.count;
await _accService.UpdateUserCopper(userId, 1, copper, "出售装备");
return PoAction.Ok(true, $"出售成功,获得{copper}铜贝!");
}
else
{
return PoAction.Message("出售失败,请稍后尝试!");
}
}
}
}