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;
private readonly IUnitUserAttrService _attrService;
public GiveController(IGameGoodsService goodsService, IGameEquService equService, IUnitUserWeight weightService,
IUnitUserRelationService relationService, IUnitUserService userService, IGameChatService chatService,
IMessageService messageService,IUnitUserAttrService attrService)
{
_goodsService = goodsService;
_equService = equService;
_weightService = weightService;
_relationService = relationService;
_userService = userService;
_chatService = chatService;
_messageService = messageService;
_attrService = attrService;
}
///
/// 获取赠送礼物物品
///
///
///
[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 });
}
///
/// 获取赠送物品列表
///
///
///
///
///
///
[HttpGet]
public async Task GetGoodsData(string no, int type, string? query, int page)
{
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("玩家不存在!");
}
RefAsync total = 0;
if (type == 0)
{
var data = await _equService.GetUserEquData(userId, 0, query, page, 10, total, 1);
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick, 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,
1);
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick, 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, 1);
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick, data, total = total.Value
});
}
else
{
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick, data = new List(), total = total.Value
});
}
}
///
/// 赠送物品
///
///
///
///
///
///
[HttpGet]
public async Task GiveGoods(string no, int type, string propId, int count)
{
string userId = StateHelper.userId;
int myLev = await _attrService.GetUserLev(userId);
if (myLev < 60)
{
return PoAction.Message("等级达到60级才可以赠送哦!");
}
count = count < 1 ? 1 : count;
var fromInfo = await _userService.GetUserInfoByUserNo(no);
if (fromInfo == null)
{
return PoAction.Message("玩家不存在!");
}
int areaId = StateHelper.areaId;
if (areaId != fromInfo.areaId)
{
return PoAction.Message("玩家不存在!");
}
//此处判断赠送玩家的设置
var roleConfig = await _userService.GetUserConfigInfo(fromInfo.userId);
if (roleConfig.giveRole == 1)//判断是否为好友
{
if (await _relationService.CheckIsFriend(userId, fromInfo.userId)==false)
{
return PoAction.Message("玩家设置了拒绝陌生人赠送!");
}
}
if (type == 0)
{
var equInfo = await _equService.GetUserEquInfo(propId);
if (equInfo == null)
{
return PoAction.Message("装备不存在!");
}
if (equInfo.userId != userId || equInfo.isOn == 1 || equInfo.isLock == 1)
{
return PoAction.Message("该装备锁定或穿戴中!");
}
if (equInfo.isDeal == 0 || equInfo.isGive == 0)
{
return PoAction.Message("该装备无法交易或不可赠送!");
}
//验证负重
if (await _weightService.CheckUserWeight(fromInfo.userId, (int)equInfo.weight) == false)
{
return PoAction.Message("对方负重不足!");
}
if (await _equService.DeductUserEqu(userId, [equInfo], $"赠送给玩家:{fromInfo.userId}"))
{
equInfo.userId = fromInfo.userId;
if (await _equService.AddUserEqu(equInfo, $"收到赠送,来源玩家:{userId}"))
{
var myInfo = await _userService.GetUserInfoByUserId(userId);
string msg = $"收到[{UbbTool.HomeUbb(myInfo.userNo, myInfo.nick)}]赠送的装备[{equInfo.equName}]";
await _chatService.SendChat(fromInfo.userId, (int)fromInfo.areaId, nameof(GameChatEnum.Code.Notice),
msg);
return PoAction.Ok(true);
}
else
{
return PoAction.Message("赠送失败,请稍后尝试!");
}
}
else
{
return PoAction.Message("赠送失败,请稍后尝试!");
}
}
else if (type == 1 || type == 2)
{
int goodsId = Convert.ToInt32(propId);
var MyGoods = await _goodsService.GetUserGoodsInfo(userId, goodsId);
if (MyGoods == null)
{
return PoAction.Message("物品数量不足!");
}
if (MyGoods.count < count)
{
return PoAction.Message("物品数量不足!");
}
if (MyGoods.isDeal == 0 || MyGoods.isGive == 0)
{
return PoAction.Message("该无法无法交易或不可赠送!");
}
int useWeight = (int)MyGoods.weight * count;
if (await _weightService.CheckUserWeight(fromInfo.userId, useWeight) == false)
{
return PoAction.Message("对方负重不足!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, $"赠送给玩家:{fromInfo.userId}"))
{
if (await _goodsService.UpdateUserGoods(fromInfo.userId, 1, goodsId, count, $"收到赠送,来源玩家:{userId}"))
{
var myInfo = await _userService.GetUserInfoByUserId(userId);
string msg = $"收到[{UbbTool.HomeUbb(myInfo.userNo, myInfo.nick)}]赠送的物品[{MyGoods.goodsName}]×{count}";
await _chatService.SendChat(fromInfo.userId, (int)fromInfo.areaId, nameof(GameChatEnum.Code.Notice),
msg);
return PoAction.Ok(true);
}
else
{
return PoAction.Message("赠送失败,请稍后尝试");
}
}
else
{
return PoAction.Message("赠送失败,请稍后尝试");
}
}
else
{
return PoAction.Message("赠送失败,请稍后尝试");
}
}
///
/// 逗一下物品列表
///
///
///
[HttpGet]
public async Task GetUserDouGoods(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.Tease));
return PoAction.Ok(new
{
userNo = fromInfo.userNo,
nick = fromInfo.nick,
data = data
});
}
///
/// 使用逗物品
///
///
///
///
///
[HttpGet]
public async Task UseDou(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 myGoods = await _goodsService.GetUserGoodsInfo(userId, goodsId);
if (myGoods == null)
{
return PoAction.Message("您暂无该道具!");
}
if (myGoods.count < count)
{
return PoAction.Message("您的道具不足!");
}
if (myGoods.code != nameof(GoodsEnum.Code.Tease))
{
return PoAction.Message("该道具无法使用!");
}
if (await _goodsService.UpdateUserGoods(userId, 0, goodsId, count, "使用道具"))
{
var goodsInfo = await _goodsService.GetGoodsInfo(goodsId);
UserStateModel stateConfig = JsonConvert.DeserializeObject(goodsInfo.content);
if(await _attrService.AddUserState(fromInfo.userId, goodsInfo.goodsId.ToString(), stateConfig.name,
stateConfig.scene, stateConfig.tips, stateConfig.attr, stateConfig.time, count))
{
var myInfo = await _userService.GetUserInfoByUserId(userId);
string msg = $"[{UbbTool.HomeUbb(myInfo.userNo, myInfo.nick)}]对您使用了[{goodsInfo.goodsName}]×{count}";
await _chatService.SendChat(fromInfo.userId, (int)fromInfo.areaId, nameof(GameChatEnum.Code.Notice),
msg);
}
return PoAction.Ok(true);
}
else
{
return PoAction.Message("使用失败,请稍后尝试!");
}
}
}