using Microsoft.AspNetCore.Mvc;
namespace Application.Web.Controllers.Map;
///
/// 商店接口
///
[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;
}
///
/// 获取npc商店信息
///
///
///
///
[HttpGet]
public async Task 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 Total = 0;
var data = await _storeService.GetStoreData(npcId, areaId, page, 10, Total);
return PoAction.Ok(new { data, total = Total.Value, npcInfo.npcName });
}
///
/// 购买物品
///
///
///
[HttpPost]
public async Task 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("购买失败,请稍后尝试!");
}
}
///
/// 出售物品
///
///
///
///
///
///
[HttpGet]
public async Task 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 total = 0;
if (type == 0)
{
var data = await _equService.GetUserEquData(userId, 0, query, page, 10, total, true, true);
return PoAction.Ok(new { data, total = total.Value });
}
else if (type == 1)
{
List code = new List() { nameof(GoodsEnum.Code.Drug) };
var data = await _goodsService.GetUserGoodsData(userId, code, new List(), query, page, 10, total);
return PoAction.Ok(new { data, total = total.Value });
}
else if (type == 2)
{
List code = new List();
List noCode = new List() { nameof(GoodsEnum.Code.Drug) };
var data = await _goodsService.GetUserGoodsData(userId, code, noCode, query, page, 10, total);
return PoAction.Ok(new { data, total = total.Value });
}
else
{
return PoAction.Ok(new { data = new List(), total = total.Value });
}
}
///
/// 出售物品
///
///
///
[HttpPost]
public async Task 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() { 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("出售失败,请稍后尝试!");
}
}
}
///
/// 批量出售装备
///
///
///
///
[HttpGet]
public async Task BathSaleEqu(int npcId, string? query)
{
if (string.IsNullOrEmpty(query))
{
return PoAction.Message("需要筛选搜索后才可以一键出售!");
}
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 (GameTool.AreaVerify(areaId, npcInfo.areaId) == false)
{
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 myEqu = await _equService.GetUserEquData(userId, query);
myEqu = myEqu.FindAll(it => it.isOn == 0 && it.isLock == 0 && it.intensifyLev == 0);
long getCopper = myEqu.Sum(it => (int)it.sysPrice);
if (await _equService.DeductUserEqu(userId, myEqu, "出售装备"))
{
await _accService.UpdateUserCopper(userId, 1, getCopper, "出售装备");
return PoAction.Ok(true, $"共出售{myEqu.Count}件装备,获得{getCopper}铜贝!");
}
else
{
return PoAction.Message("出售失败,请稍后尝试!");
}
}
}