651 lines
21 KiB
C#
651 lines
21 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Application.Web.Controllers.Pub;
|
|
|
|
/// <summary>
|
|
/// 战斗接口
|
|
/// </summary>
|
|
[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;
|
|
private readonly IOnHookService _hookService;
|
|
private readonly IUnitUserRelationService _relationService;
|
|
|
|
public FightController(IGameFightService fightService, IGameMonsterService monsterService,
|
|
IUnitUserAttrService attrService, IGameMapService mapService, IUnitUserAccService accService,
|
|
IGameGoodsService goodsService, IUnitUserService userService, IOnHookService hookService,
|
|
IUnitUserRelationService relationService)
|
|
{
|
|
_fightService = fightService;
|
|
_monsterService = monsterService;
|
|
_attrService = attrService;
|
|
_mapService = mapService;
|
|
_accService = accService;
|
|
_goodsService = goodsService;
|
|
_userService = userService;
|
|
_hookService = hookService;
|
|
_relationService = relationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加怪物战斗
|
|
/// </summary>
|
|
/// <param name="parms"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<IPoAction> 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<MonsterAwardModel>(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)
|
|
{
|
|
await _hookService.StopOnHook(userId);
|
|
return PoAction.Ok(fightId);
|
|
}
|
|
else
|
|
{
|
|
return PoAction.Message("战斗生成错误!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 角色战斗生成
|
|
/// </summary>
|
|
/// <param name="no"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> FightPerson(string? no)
|
|
{
|
|
string userId = StateHelper.userId;
|
|
var onMapInfo = await _mapService.GetUserOnToMapInfo(userId);
|
|
var otUser = await _userService.GetUserInfoByUserNo(no);
|
|
if (otUser == null)
|
|
{
|
|
return PoAction.Message("玩家不存在!");
|
|
}
|
|
|
|
if (onMapInfo.isPk == 0)
|
|
{
|
|
if (onMapInfo.retMap != onMapInfo.mapId)
|
|
{
|
|
if (await _relationService.CheckUserEnemy(userId, otUser.userId) == false)
|
|
{
|
|
return PoAction.Message("该场景不允许PK!");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return PoAction.Message("该场景不允许PK!");
|
|
}
|
|
}
|
|
|
|
var otUserOnMap = await _mapService.GetUserOnMapInfo(otUser.userId);
|
|
if (onMapInfo.mapId != otUserOnMap.mapId)
|
|
{
|
|
return PoAction.Message("玩家不存在!");
|
|
}
|
|
|
|
if (otUserOnMap.isOnline == 0)
|
|
{
|
|
return PoAction.Message("玩家已下线!");
|
|
}
|
|
|
|
if (onMapInfo.lookArea == 1)
|
|
{
|
|
if (otUser.areaId != StateHelper.areaId)
|
|
{
|
|
return PoAction.Message("该地图不允许跨服战斗!");
|
|
}
|
|
}
|
|
string fightId = StringAssist.NewGuid;
|
|
string areaCode = nameof(GameEnum.AreaCode.Own);
|
|
string code = nameof(GameEnum.FightCode.PVP);
|
|
string keyId = StringAssist.GetSortKey(userId,otUser.userId);
|
|
long exp = 0;
|
|
long copper = 0;
|
|
long petExp = 0;
|
|
string award = string.Empty;
|
|
string pars = "";
|
|
bool result = await _fightService.AddFight(fightId, areaCode, keyId, otUser.userId, code, onMapInfo.code, onMapInfo.mapId, userId, exp,
|
|
copper, petExp, award, pars);
|
|
if (result)
|
|
{
|
|
await _hookService.StopOnHook(userId);
|
|
return PoAction.Ok(fightId);
|
|
}
|
|
else
|
|
{
|
|
return PoAction.Message("战斗生成错误!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 战斗接口
|
|
/// </summary>
|
|
/// <param name="parms"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<IPoAction> 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<FightResultModel>(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);
|
|
await _attrService.AddUserLoadState(userId, fightResult.myStateData);
|
|
await _fightService.HandelFightEnd(userId, fightResult.myRemData);
|
|
}
|
|
|
|
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);
|
|
await _attrService.AddUserLoadState(fight.mainId, fightResult.otStateData);
|
|
await _fightService.HandelFightEnd(fight.mainId, fightResult.otRemData);
|
|
}
|
|
|
|
otAttr = await _attrService.GetUserAttrModel(fight.mainId, fight.scene);
|
|
}
|
|
|
|
//结束战斗都此处直接返回
|
|
if (otAttr.blood < 1 || myAttr.blood < 1)
|
|
{
|
|
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
|
|
{
|
|
if (fightResult.result == 0)
|
|
{
|
|
//自动使用药品
|
|
await _fightService.AutoUseDrug(myAttr, fight);
|
|
if (otAttr.code == nameof(UserEnum.AttrCode.Person))
|
|
{
|
|
await _fightService.AutoUseDrug(otAttr, fight);
|
|
}
|
|
}
|
|
}
|
|
|
|
//获取个人药品栏
|
|
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((1 - bl) * 1000.0m);
|
|
cool = cool < 10 ? 10 : cool;
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 退出战斗
|
|
/// </summary>
|
|
/// <param name="fightId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> 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, false);
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用药品
|
|
/// </summary>
|
|
/// <param name="fightId"></param>
|
|
/// <param name="drugId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> 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)
|
|
{
|
|
return PoAction.Message("战斗不存在!");
|
|
}
|
|
|
|
if (fight.state == 1)
|
|
{
|
|
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("药品使用失败!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 战斗信息
|
|
/// </summary>
|
|
/// <param name="fightId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> GetFightMsg(string fightId)
|
|
{
|
|
string userId = StateHelper.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;
|
|
}
|
|
|
|
int isCopy = 0;
|
|
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;
|
|
isCopy = (int)monsterInfo.isCopy;
|
|
}
|
|
}
|
|
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, isCopy });
|
|
}
|
|
|
|
#region 挂机相关
|
|
|
|
/// <summary>
|
|
/// 个人挂机信息
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> GetUserHook()
|
|
{
|
|
string userId = StateHelper.userId;
|
|
var onHook = await _hookService.GetUserOnHook(userId);
|
|
decimal expAdd = 0;
|
|
decimal copperAdd = 0;
|
|
if (onHook.status == 1)
|
|
{
|
|
var myAttr = await _attrService.GetUserAttrModel(userId, onHook.scene);
|
|
expAdd = myAttr.addExp;
|
|
copperAdd = myAttr.addGold;
|
|
onHook.exp = Convert.ToInt32(onHook.exp * (1.0M + expAdd));
|
|
onHook.copper = Convert.ToInt32(onHook.copper * (1.0M + copperAdd));
|
|
}
|
|
|
|
return PoAction.Ok(new { data = onHook, expAdd, copperAdd });
|
|
}
|
|
|
|
/// <summary>
|
|
/// 挂机
|
|
/// </summary>
|
|
/// <param name="fightId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> OnHook(string fightId)
|
|
{
|
|
string userId = StateHelper.userId;
|
|
var fight = await _fightService.GetFightInfo(fightId);
|
|
if (fight == null)
|
|
{
|
|
return PoAction.Message("战斗不存在!");
|
|
}
|
|
|
|
if (fight.userId != userId)
|
|
{
|
|
return PoAction.Message("战斗不存在!");
|
|
}
|
|
|
|
if (fight.code != nameof(GameEnum.FightCode.PVE))
|
|
{
|
|
return PoAction.Message("该战斗不可复刻挂机!");
|
|
}
|
|
|
|
var monster = await _monsterService.GetMonsterInfo(fight.mainId);
|
|
if (monster == null)
|
|
{
|
|
return PoAction.Message("该战斗不可复刻挂机!");
|
|
}
|
|
|
|
if (monster.isCopy == 0)
|
|
{
|
|
return PoAction.Message("该战斗不可复刻挂机!");
|
|
}
|
|
|
|
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
|
|
int diffTime = Convert.ToInt32(fight.okTime - fight.addTime);
|
|
decimal score = myAttr.score * 0.7M;
|
|
bool result = await _hookService.StartOnHook(userId, fight.scene, (long)monster.copper, (long)monster.exp,
|
|
monster.name,
|
|
monster.award, score, diffTime, 6);
|
|
if (result)
|
|
{
|
|
return PoAction.Ok("挂机成功!");
|
|
}
|
|
else
|
|
{
|
|
return PoAction.Message("挂机失败,请稍后尝试!");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 结束挂机
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<IPoAction> StopOnHook()
|
|
{
|
|
string userId = StateHelper.userId;
|
|
bool result = await _hookService.StopOnHook(userId);
|
|
if (result)
|
|
{
|
|
return PoAction.Ok(true);
|
|
}
|
|
else
|
|
{
|
|
return PoAction.Message("操作失败,请稍后尝试!");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |