using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace Application.Web.Controllers.Pub;
///
/// 战斗接口
///
[Route("[controller]/[action]")]
[ApiController]
[Authorize]
public class FightController : ControllerBase
{
private readonly IGameFightService _fightService;
private readonly IGameMonsterService _monsterService;
private readonly IUnitUserAttrService _attrService;
private readonly IGameMapService _mapService;
private readonly IUnitUserAccService _accService;
private readonly IGameGoodsService _goodsService;
private readonly IUnitUserService _userService;
public FightController(IGameFightService fightService, IGameMonsterService monsterService,
IUnitUserAttrService attrService, IGameMapService mapService, IUnitUserAccService accService,
IGameGoodsService goodsService, IUnitUserService userService)
{
_fightService = fightService;
_monsterService = monsterService;
_attrService = attrService;
_mapService = mapService;
_accService = accService;
_goodsService = goodsService;
_userService = userService;
}
///
/// 添加怪物战斗
///
///
///
[HttpPost]
public async Task FightMonster([FromBody] FightMonsterParms parms)
{
string userId = StateHelper.userId;
if (await UserKeyTool.CheckUserThickenData(userId, parms.sign, parms.type.ToString(), parms.monsterId) == false)
{
return PoAction.Message("怪物不存在!", -1);
}
var mapInfo = await _mapService.GetUserOnToMapInfo(userId);
//添加怪物战斗
string fightId = StringAssist.NewGuid;
string areaCode = nameof(GameEnum.AreaCode.Own);
string keyId = fightId;
string mainId = string.Empty;
string code = nameof(GameEnum.FightCode.PVE);
string scene = mapInfo.code;
string mapId = mapInfo.mapId;
long exp = 0;
long copper = 0;
long petExp = 0;
string award = string.Empty;
string pars = "";
if (parms.type == 0)
{
var monster = await _monsterService.GetMapMonsterInfo(parms.monsterId);
mainId = monster.monsterId;
pars = JsonConvert.SerializeObject(new { type = "Default" });
}
else
{
var monster = await _monsterService.GetCreateMonsterInfo(parms.monsterId);
keyId = monster.umId;
mainId = monster.monsterId;
areaCode = monster.areaCode;
pars = JsonConvert.SerializeObject(new { type = "Create", code = monster.code, par = monster.par });
}
var monsterInfo = await _monsterService.GetMonsterInfo(mainId);
if (monsterInfo != null)
{
var userAttr = await _attrService.GetUserAttrModel(userId, scene);
//处理奖励和资源
exp = Convert.ToInt64(monsterInfo.exp * (1.0m + userAttr.addExp));
copper = Convert.ToInt64(monsterInfo.copper * (1.0m + userAttr.addGold));
petExp = (long)monsterInfo.petExp;
if (!string.IsNullOrEmpty(monsterInfo.award)) //生成奖励
{
var awardModel = JsonConvert.DeserializeObject(monsterInfo.award);
if (awardModel.type == nameof(MonsterEnum.AwardCode.Default))
{
var getAward = GameBus.GetRandomGoods(awardModel.award, 1, userAttr.burst);
award = JsonConvert.SerializeObject(new FightAwardModel() { code = "Default", award = getAward });
}
}
}
bool result = await _fightService.AddFight(fightId, areaCode, keyId, mainId, code, scene, mapId, userId, exp,
copper, petExp, award, pars);
if (result)
{
return PoAction.Ok(fightId);
}
else
{
return PoAction.Message("战斗生成错误!");
}
}
///
/// 战斗接口
///
///
///
[HttpPost]
public async Task Fight([FromBody] FightParms parms)
{
string userId = StateHelper.userId;
string fightId = parms.fightId;
int myHarm = 0;
int otHarm = 0;
var myFight = await _fightService.GetUserFight(userId);
if (myFight.Any(it => it == fightId) == false)
{
return PoAction.Message("战斗不存在!");
}
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗不存在!");
}
if (fight.state == 1)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束", 100);
}
#region 此处处理战斗
FightResultModel fightResult = new FightResultModel()
{
result = -1
};
if (!string.IsNullOrEmpty(parms.data))
{
fightResult = JsonConvert.DeserializeObject(parms.data);
}
#endregion
//更新攻击
if (fightResult.result == 0)
{
myHarm = fightResult.myHarm;
myHarm += await _fightService.GetUserFightHarm(userId);
otHarm = fightResult.otHarm;
await _attrService.UpdateUserBlood(userId, 1, fightResult.myHarm);
}
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
UserAttrModel otAttr = new UserAttrModel();
if (fight.code == nameof(GameEnum.FightCode.PVE))
{
otAttr = await _monsterService.GetMonsterAttrModel(fight.keyId, fight.mainId);
if (fightResult.result == 0)
{
otAttr.blood = otAttr.blood + fightResult.otHarm;
otAttr.blood = otAttr.blood < 0 ? 0 : otAttr.blood;
otAttr.blood = otAttr.blood > otAttr.upBlood ? otAttr.upBlood : otAttr.blood;
await _monsterService.SetMonsterBlood(fight.keyId, otAttr.blood);
}
}
else
{
if (fightResult.result == 0)
{
await _fightService.SetUserFightHarm(fight.mainId, fightResult.otHarm);
await _attrService.UpdateUserBlood(fight.mainId, 1, fightResult.otHarm);
}
otAttr = await _attrService.GetUserAttrModel(fight.mainId, fight.scene);
}
//结束战斗都此处直接返回
if (otAttr.blood < 1 || myAttr.blood < 1)
{
await _fightService.RemoveUserFight(userId, fightId);
if (otAttr.blood < 1)
{
await _fightService.SetFightWin(fight, userId);
}
else
{
await _fightService.SetFightWin(fight, fight.mainId);
}
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束", 100);
}
else
{
//自动使用药品
}
//获取个人药品栏
var myDrug = await _attrService.GetUserDrug(userId);
//获取个人负面
var myLoad = await _attrService.GetUserLoadState(userId);
//冷却时间
int cool = 1000;
if (myAttr.agility > otAttr.agility && otAttr.agility > 0)
{
decimal diff = myAttr.agility - otAttr.agility;
decimal bl = diff / otAttr.agility;
cool = Convert.ToInt32(bl * 1000.0m);
cool = cool > 1000 ? 1000 : cool;
}
//麻痹状态增加冷却时间
var atkCoolData = myLoad.Find(it => it.code == nameof(GameEnum.AttrCode.PalsyAtk));
if (atkCoolData != null)
{
cool = Convert.ToInt32(cool * (1 + atkCoolData.count * 0.1M));
}
//更新伤害提示
int exitCopper = otAttr.lev * 5;
var result = new
{
myHarm,
otHarm,
myAttr = myAttr,
otAttr = otAttr,
myDrug = myDrug.drug,
myLoad = myLoad,
exitCopper,
cool,
};
return PoAction.Ok(result);
}
///
/// 退出战斗
///
///
///
[HttpGet]
public async Task Exit(string fightId)
{
string userId = StateHelper.userId;
var myFight = await _fightService.GetUserFight(userId);
if (myFight.Any(it => it == fightId) == false)
{
return PoAction.Message("战斗不存在!");
}
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗不存在!");
}
if (fight.state == 1)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束", 100);
}
UserAttrModel otAttr = new UserAttrModel();
if (fight.code == nameof(GameEnum.FightCode.PVE))
{
otAttr = await _monsterService.GetMonsterAttrModel(fight.keyId, fight.mainId);
}
else
{
otAttr = await _attrService.GetUserAttrModel(fight.mainId, fight.scene);
}
int needCopper = otAttr.lev * 5;
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
if (myAcc < needCopper)
{
return PoAction.Message("铜贝不足!", 101);
}
if (await _accService.UpdateUserCopper(userId, 0, needCopper, "退出战斗"))
{
if (await _fightService.RemoveUserFight(userId, fightId))
{
await _fightService.SetFightWin(fight, fight.mainId);
await _fightService.RemoveFightHarm(fightId, userId); //移除伤害
var nextFight = myFight.FindAll(it => it != fightId);
string nextId = nextFight.Count > 0 ? nextFight[0] : "";
return PoAction.Ok(nextId);
}
else
{
return PoAction.Message("退出失败,请稍后尝试!", 101);
}
}
else
{
return PoAction.Message("退出失败,请稍后尝试!", 101);
}
}
///
/// 使用药品
///
///
///
///
[HttpGet]
public async Task UseFightDrug(string fightId, string drugId)
{
string userId = StateHelper.userId;
var myFight = await _fightService.GetUserFight(userId);
if (myFight.Any(it => it == fightId) == false)
{
return PoAction.Message("战斗不存在!");
}
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗不存在!");
}
if (fight.state == 1)
{
await _fightService.RemoveUserFight(userId, fightId);
return PoAction.Message("战斗结束!");
}
var myDrug = await _attrService.GetUserDrug(userId);
var onDrug = myDrug.drug.Find(it => it.id == drugId);
if (onDrug == null)
{
return PoAction.Message("药品不存在!");
}
if (onDrug.count < 1)
{
return PoAction.Message("药品不足!");
}
var drugConfig = await _goodsService.GetGoodsContent(onDrug.goodsId);
string check = await _goodsService.CheckUseDrug(userId, onDrug.goodsId, drugConfig, fightId);
if (!string.IsNullOrEmpty(check))
{
return PoAction.Message(check);
}
foreach (var item in myDrug.drug)
{
if (item.id == drugId)
{
item.count = item.count - 1;
break;
}
}
if (await _attrService.UpdateUserDrug(myDrug))
{
await _goodsService.UseDrug(userId, onDrug.goodsId, drugConfig, fightId);
return PoAction.Ok("药品使用成功!");
}
else
{
return PoAction.Message("药品使用失败!");
}
}
///
/// 战斗信息
///
///
///
[HttpGet]
public async Task GetFightMsg(string fightId)
{
string userId = StateHelper.userId;
var myFight = await _fightService.GetUserFight(userId);
var fight = await _fightService.GetFightInfo(fightId);
if (fight == null)
{
return PoAction.Message("战斗不存在!");
}
if (fight.userId != userId)
{
return PoAction.Message("战斗不存在!");
}
if (fight.state == 0)
{
return PoAction.Message("战斗未完成", 100);
}
int isWin = 0;
if (fight.winUser == fight.mainId)
{
isWin = 0;
}
else if(fight.winUser==userId)
{
isWin = 1;
}
else
{
isWin = 2;
}
string otName = string.Empty;
if (isWin == 0)
{
if (fight.code == nameof(GameEnum.FightCode.PVP))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.winUser);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.winUser);
otName = monsterInfo.name;
}
}
else if (isWin == 1)
{
if (fight.code == nameof(GameEnum.FightCode.PVP))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.mainId);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.mainId);
otName = monsterInfo.name;
}
}
else
{
if (fight.winCode == nameof(UserEnum.AttrCode.Person))
{
var userInfo = await _userService.GetUserInfoByUserId(fight.winUser);
otName = userInfo.nick;
}
else
{
var monsterInfo = await _monsterService.GetMonsterInfo(fight.winUser);
otName = monsterInfo.name;
}
}
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
return PoAction.Ok(new { isWin, otName, blood = myAttr.blood, upBlood = myAttr.upBlood, fight });
}
}