using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Application.Web.Controllers.Pub;
///
/// 物品接口
///
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class GoodsController : ControllerBase
{
private readonly IGameGoodsService _goodsService;
private readonly IUnitUserAttrService _attrService;
private readonly IMessageService _messageService;
private readonly IUnitUserWeight _weightService;
public GoodsController(IGameGoodsService goodsService, IUnitUserAttrService attrService,
IMessageService messageService, IUnitUserWeight weightService)
{
_goodsService = goodsService;
_attrService = attrService;
_messageService = messageService;
_weightService = weightService;
}
///
/// 获取物品信息
///
/// 物品ID
///
[HttpGet]
public async Task GetGoodsInfo(int goodsId)
{
var goodsInfo = await _goodsService.GetGoodsInfo(goodsId);
if (goodsInfo == null)
{
return PoAction.Message("物品不存在!");
}
string userId = StateHelper.userId;
int count = await _goodsService.GetUserGoodsCount(userId, goodsId);
int UseState = 0;
string[] ShowViewNum = ["Pack", "Weight", "State"]; //显示带数量窗口
string[] ShowView = ["Ship", "Drug", "Durability"]; //显示只能使用1个的窗口
if (ShowViewNum.Any(it => it == goodsInfo.code))
{
UseState = 1;
}
else if (ShowView.Any(it => it == goodsInfo.code))
{
UseState = 2;
}
else if (goodsInfo.code == nameof(GoodsEnum.Code.ChoicePack))
{
UseState = 3;
}
return PoAction.Ok(new { goods = goodsInfo, count, use = UseState });
}
///
/// 使用物品
///
///
///
///
[HttpGet]
public async Task UseGoods(int goodsId, int count)
{
count = count < 1 ? 1 : count;
string userId = StateHelper.userId;
var goodsInfo = await _goodsService.GetGoodsInfo(goodsId);
if (goodsInfo == null)
{
return PoAction.Message("物品不存在!");
}
var MyGoods = await _goodsService.GetUserGoodsCount(userId, goodsId);
if (MyGoods < count)
{
return PoAction.Message("物品不足!");
}
var myLev = await _attrService.GetUserLev(userId);
if (myLev < (int)goodsInfo.lev)
{
return PoAction.Message($"该物品{goodsInfo.lev}级才可使用!");
}
#region 必要判定
if (goodsInfo.code == nameof(GoodsEnum.Code.Ship))
{
var myShip = await _weightService.GetUserShip(userId);
if (myShip.Count >= 5)
{
return PoAction.Message($"每人最多拥有5个船只,您已达到上限!");
}
}
if (goodsInfo.code == nameof(GoodsEnum.Code.Drug))
{
if (await UserStateTool.CheckUserFight(userId))
{
return PoAction.Message($"战斗状态下无法使用!");
}
var ck = await _goodsService.CheckUseDrug(userId, goodsId, goodsInfo.content);
if (!string.IsNullOrEmpty(ck))
{
return PoAction.Message(ck);
}
}
#endregion
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, "使用物品"))
{
string message = "";
if (goodsInfo.code == nameof(GoodsEnum.Code.Pack)) //使用礼包
{
var park = JsonConvert.DeserializeObject(goodsInfo.content);
var award = GameBus.GetRandomGoods(park, count);
if (award.Count > 0)
{
message = $"打开[{goodsInfo.goodsName}]×{count},获得:";
foreach (var item in award)
{
message += $"{item.name}+{item.count},";
}
message = message.TrimEnd(',');
await GameBus.UpdateBag(userId, 1, award, $"打开礼包:{goodsInfo.goodsName}×{count}获得");
await _messageService.SendBroadcast(userId, nameof(MessageEnum.BroadcastCode.Award), message);
}
else
{
message = "空空如也,什么也没有得到!";
}
}
else if (goodsInfo.code == nameof(GoodsEnum.Code.Weight))
{
int unitWeight = Convert.ToInt32(goodsInfo.content);
await _weightService.UpdateUserMaxWeight(userId, 1, unitWeight, count, goodsInfo.goodsId,
goodsInfo.goodsName);
message = $"使用[{goodsInfo.goodsName}]×{count},负重+{unitWeight * count}";
}
else if (goodsInfo.code == nameof(GoodsEnum.Code.State))
{
UserStateModel stateConfig = JsonConvert.DeserializeObject(goodsInfo.content);
await _attrService.AddUserState(userId, goodsInfo.goodsId.ToString(), stateConfig.name,
stateConfig.scene, stateConfig.tips, stateConfig.attr, stateConfig.time, count);
message = $"使用[{goodsInfo.goodsName}]×{count},状态时间+{stateConfig.time * count}分钟";
}
else if (goodsInfo.code == nameof(GoodsEnum.Code.Ship))
{
dynamic shipInfo = JsonConvert.DeserializeObject(goodsInfo.content);
string name = shipInfo.name;
int speed = shipInfo.speed;
int weight = shipInfo.weight;
int copper = shipInfo.copper;
long sale = shipInfo.sale;
string remark = shipInfo.remark;
await _weightService.AddUserShip(userId, name, goodsId, speed, weight, copper, sale, remark);
message = $"使用[{goodsInfo.goodsName}],获得{name}+1";
}
if (goodsInfo.code == nameof(GoodsEnum.Code.Drug))
{
message = "使用成功!";
await _goodsService.UseDrug(userId, goodsId, goodsInfo.content);
}
if (goodsInfo.code == nameof(GoodsEnum.Code.Durability))
{
message = "使用成功!";
dynamic _drugConfig = JsonConvert.DeserializeObject(goodsInfo.content);
await _attrService.AddUserStock(userId, goodsId.ToString(), Convert.ToString(_drugConfig.name),
Convert.ToString(_drugConfig.type),
Convert.ToString(_drugConfig.code),
Convert.ToInt64(_drugConfig.num), Convert.ToString(_drugConfig.par),
Convert.ToInt32(_drugConfig.minute));
}
return PoAction.Ok(true, message);
}
else
{
return PoAction.Message("使用失败,请稍后尝试!");
}
}
///
/// 使用选择物品礼包
///
///
///
///
[HttpGet]
public async Task UseChoiceGoods(int goodsId, int num)
{
string userId = StateHelper.userId;
var goodsInfo = await _goodsService.GetGoodsInfo(goodsId);
if (goodsInfo == null)
{
return PoAction.Message("物品不存在!");
}
var MyGoods = await _goodsService.GetUserGoodsCount(userId, goodsId);
if (MyGoods < 1)
{
return PoAction.Message("物品不足!");
}
var myLev = await _attrService.GetUserLev(userId);
if (myLev < (int)goodsInfo.lev)
{
return PoAction.Message($"该物品{goodsInfo.lev}级才可使用!");
}
if (goodsInfo.code != nameof(GoodsEnum.Code.ChoicePack))
{
return PoAction.Message("无法使用该物品!");
}
List choiceGoods = JsonConvert.DeserializeObject>(goodsInfo.content);
var onAward = choiceGoods.Find(it => it.id == num);
if (onAward == null)
{
return PoAction.Message("物品中不包含该选择!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, 1, "使用物品"))
{
if (await GameBus.UpdateBag(userId, 1, onAward.award, "打开礼包获取"))
{
string message = $"打开[{goodsInfo.goodsName}],获得:";
foreach (var item in onAward.award)
{
message += $"{item.name}+{item.count},";
}
message = message.TrimEnd(',');
await _messageService.SendBroadcast(userId, nameof(MessageEnum.BroadcastCode.Award), message);
return PoAction.Ok(true, message);
}
else
{
return PoAction.Message("使用失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("使用失败,请稍后尝试!");
}
}
}