using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Application.Web.Controllers.User;
///
/// 赠送接口
///
[ApiExplorerSettings(GroupName = "User")]
[Route("User/[controller]/[action]")]
[ApiController]
[Authorize]
public class GiveController : ControllerBase
{
private readonly IGameGoodsService _goodsService;
private readonly IGameEquService _equService;
private readonly IUnitUserWeight _weightService;
private readonly IUnitUserRelationService _relationService;
private readonly IUnitUserService _userService;
private readonly IGameChatService _chatService;
private readonly IMessageService _messageService;
public GiveController(IGameGoodsService goodsService, IGameEquService equService, IUnitUserWeight weightService,
IUnitUserRelationService relationService, IUnitUserService userService, IGameChatService chatService,
IMessageService messageService)
{
_goodsService = goodsService;
_equService = equService;
_weightService = weightService;
_relationService = relationService;
_userService = userService;
_chatService = chatService;
_messageService = messageService;
}
///
/// 获取赠送礼物物品
///
///
///
[HttpGet]
public async Task GetUserGiftGoods(string no)
{
var fromInfo = await _userService.GetUserInfoByUserNo(no);
if (fromInfo == null)
{
return PoAction.Message("玩家不存在!");
}
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
if (areaId != fromInfo.areaId)
{
return PoAction.Message("玩家不存在!");
}
var data = await _goodsService.GetUserGoodsData(userId, nameof(GoodsEnum.Code.Gift));
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick,
data = data
});
}
///
/// 赠送礼物
///
///
///
///
///
[HttpGet]
public async Task ToGift(string no, int goodsId, int count)
{
var fromInfo = await _userService.GetUserInfoByUserNo(no);
if (fromInfo == null)
{
return PoAction.Message("玩家不存在!");
}
string userId = StateHelper.userId;
int areaId = StateHelper.areaId;
if (areaId != fromInfo.areaId)
{
return PoAction.Message("玩家不存在!");
}
count = count < 1 ? 1 : count;
var goodsInfo = await _goodsService.GetUserGoodsInfo(userId, goodsId);
if (goodsInfo.code != nameof(GoodsEnum.Code.Gift))
{
return PoAction.Message("该道具无法赠送!");
}
if (goodsInfo.count < count)
{
return PoAction.Message("您礼物数量不足!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, "赠送礼物"))
{
var goods = await _goodsService.GetGoodsInfo(goodsId);
dynamic giftConfig = JsonConvert.DeserializeObject(goods.content);
int charm = Convert.ToInt32(giftConfig.charm);
int isBroad = Convert.ToInt32(giftConfig.isBroad);
int broadCount = Convert.ToInt32(giftConfig.broadCount);
string gifUbb = UbbTool.GiftUbb(goods.img, goods.goodsName);
if (await _relationService.AddCharm(fromInfo.userId, userId, goodsId, goods.goodsName, goods.img, charm,
count))
{
var myInfo = await _userService.GetUserInfoByUserId(userId);
string noticeMsg = $"[{UbbTool.HomeUbb(myInfo.userNo, myInfo.nick)}]赠送您{gifUbb}×{count}!";
await _chatService.SendChat(fromInfo.userId, nameof(GameChatEnum.Code.Notice), noticeMsg);
if (isBroad == 1 && count >= broadCount)
{
//发送系统广布
string tip =
$"赠送给[{UbbTool.HomeUbb(fromInfo.userNo, fromInfo.nick)}] {gifUbb} {UbbTool.GiveGiftStr(count)}";
await _messageService.SendBroadcast(userId, nameof(MessageEnum.BroadcastCode.Gift), tip);
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("赠送失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("赠送失败,请稍后尝试!");
}
}
///
/// 获取礼物记录
///
///
///
///
[HttpGet]
public async Task GetGiftLog(string? no, int page)
{
string userId = StateHelper.userId;
if (!string.IsNullOrEmpty(no))
{
var noInfo = await _userService.GetUserInfoByUserNo(no);
if (noInfo != null)
{
userId = noInfo.userId;
}
}
RefAsync total = 0;
var data = await _relationService.GetUserGiftLog(userId, page, 10, total);
return PoAction.Ok(new { data, total = total.Value });
}
}