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; public FightController(IGameFightService fightService, IGameMonsterService monsterService, IUnitUserAttrService attrService, IGameMapService mapService) { _fightService = fightService; _monsterService = monsterService; _attrService = attrService; _mapService = mapService; } /// /// 添加怪物战斗 /// /// /// [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) { return PoAction.Ok(true); } }