using Newtonsoft.Json; namespace Application.Web.Controllers.Map; /// /// 地图接口 /// [ApiExplorerSettings(GroupName = "Map")] [Route("Map/[controller]/[action]")] [ApiController] [Authorize] public class MapController : ControllerBase { private readonly IUnitUserService _userService; private readonly IGameMapService _mapService; private readonly IGameChatService _chatService; private readonly IUnitUserAttrService _attrService; private readonly IMessageService _messageService; private readonly IUnitUserWeight _weightService; private readonly IUnitUserAccService _accService; private readonly IGameSkillService _skillService; private readonly IGameGoodsService _goodsService; private readonly IGameTeamService _teamService; private readonly IGameMonsterService _monsterService; private readonly IGameFightService _fightService; private readonly IOnHookService _hookService; private readonly IGameTaskService _taskService; private readonly INoticeService _noticeService; public MapController(IUnitUserService userService, IGameMapService mapService, IGameChatService chatService, IUnitUserAttrService attrService, IMessageService messageService, IUnitUserWeight weightService, IUnitUserAccService accService, IGameSkillService skillService, IGameGoodsService goodsService, IGameTeamService teamService, IGameMonsterService monsterService, IGameFightService fightService, IOnHookService hookService, IGameTaskService taskService, INoticeService noticeService) { _userService = userService; _mapService = mapService; _chatService = chatService; _attrService = attrService; _messageService = messageService; _weightService = weightService; _accService = accService; _skillService = skillService; _goodsService = goodsService; _teamService = teamService; _monsterService = monsterService; _fightService = fightService; _hookService = hookService; _taskService = taskService; _noticeService = noticeService; } #region 地图相关 /// /// 获取地图主页信息 /// /// /// [HttpGet] public async Task GetMapData(string? map) { string userId = StateHelper.userId; int area = StateHelper.areaId; string gameTips = string.Empty; game_city_map mapInfo = new game_city_map(); #region 前置条件处理 var userRunInfo = await _mapService.GetUserRun(userId); if (userRunInfo.status != 0) { return PoAction.Message("正在航行状态", 103); } #endregion #region 地图导向处理 bool isSelfMap = string.IsNullOrEmpty(map); if (!isSelfMap) { //血量为0得时候地图点为本身 var myBlood = await _attrService.GetUserBlood(userId); if (myBlood.blood < 1) { //设置默认地图点 await _mapService.SetUserMapDefault(userId); isSelfMap = true; gameTips = "➢当前体力为0,恢复一下吧!"; } } if (isSelfMap) { var onMap = await _mapService.GetUserOnMap(userId); mapInfo = await _mapService.GetMapInfo(onMap.mapId); } else { mapInfo = await _mapService.GetToMapInfo(userId, map, false); } #endregion //公聊信息 var myTeam = await _teamService.GetUserTeamData(userId); string teamId = myTeam.teamId; string groupId = ""; var chatData = await _chatService.GetChatTop(userId, area, 2, teamId, groupId); var npcData = await _mapService.GetMapNpcModel(userId, mapInfo.mapId, area); //NPC信息 //获取怪物 var monsterData = await _monsterService.GetMapMonster(userId, mapInfo.mapId, teamId); var nearUser = await _mapService.GetMapUser(mapInfo.mapId, area, (int)mapInfo.lookArea, [userId], 3); //获取附近的人 var cityInfo = await _mapService.GetCityInfo((int)mapInfo.cityId); //城市信息 var cityShow = await _mapService.GetCityShowMap(cityInfo.cityId); //城内地图 var noReadMsg = await _messageService.GetNoReadCount(userId); #region 更新在线 string ip = ComHelper.GetClientUserIp(HttpContext); await _mapService.UpdateUserOnMap(userId, ip, mapInfo.mapId); #endregion var busData = await _mapService.GetMapBus(mapInfo.mapId, 0, 0); //地图业务 IEnumerable business = busData.Select(it => new { busId = it.busId, busName = it.busName }); List mapRes = await _mapService.GetMapResData(mapInfo.mapId, area); //地图资源 var broadcast = await _messageService.GetBroadcastData(area); var userFight = await _fightService.GetUserFight(userId); string fightId = userFight.Count > 0 ? userFight[0] : ""; var mapGoods = await _fightService.GetMapFightGoods(mapInfo.mapId); mapGoods = mapGoods.FindAll(it => it.userId == "0" || it.userId == userId).Take(3).ToList(); var taskData = await _taskService.GetUserTaskData(userId); int isHook = 0; //挂机处理 var onHook = await _hookService.GetUserOnHook(userId); isHook = (int)onHook.status; var notice = await _noticeService.GetNoticeDataByMap(); object ret = new { mapInfo, cityInfo, npcData, chatData, cityShow, nearUser, noReadMsg = noReadMsg[3], business, mapRes, mapGoods, broadcast, monster = monsterData, fightId, gameTips, isHook, notice, taskCount = taskData.Count }; return PoAction.Ok(ret); } /// /// 获取地图在线玩家 /// /// /// /// [HttpGet] public async Task GetMapUser(int page) { RefAsync total = 0; string userId = StateHelper.userId; int areaId = StateHelper.areaId; var onMap = await _mapService.GetUserOnMap(userId); var mapInfo = await _mapService.GetMapInfo(onMap.mapId); if (mapInfo == null) { return PoAction.Message("地图不存在!"); } var data = await _mapService.GetMapUser(onMap.mapId, areaId, (int)mapInfo.lookArea, [userId], page, 10, total); return PoAction.Ok(new { data, total = total.Value, map = mapInfo }); } /// /// 获取当前城市所有地图 /// /// [HttpGet] public async Task GetUserOnCityMap(string? search) { string userId = StateHelper.userId; var onMap = await _mapService.GetUserOnMap(userId); var mapInfo = await _mapService.GetMapInfo(onMap.mapId); var data = await _mapService.GetCityMap((int)mapInfo.cityId); data = data.FindAll(it => it.code == nameof(MapEnum.MapCode.DEF_MAP)); if (!string.IsNullOrEmpty(search)) { data = data.FindAll(it => it.mapName.Contains(search)); } return PoAction.Ok(data); } /// /// 获取自动寻路路径 /// /// /// [HttpGet] public async Task GetAutoMapPath(string? toMap) { if (string.IsNullOrEmpty(toMap)) { return PoAction.Message("目标地点不能为空!"); } string userId = StateHelper.userId; var onMap = await _mapService.GetUserOnMap(userId); var result = await _mapService.CreateAutoMap(onMap.mapId, toMap); if (result.Count > 0) { return PoAction.Ok(result); } else { return PoAction.Message("无法自动寻路!"); } } /// /// 获取传送/航海城市列表 /// /// /// /// [HttpGet] public async Task GetCityData(int npcId, int cityId) { string userId = StateHelper.userId; #region NPC验证 var npcInfo = await _mapService.GetNpcInfo(npcId); if (npcInfo == null) { return PoAction.Message("Npc不存在!"); } if (npcInfo.status != 1) { return PoAction.Message("Npc不存在!"); } if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.MapTo))) { return PoAction.Message("业务不可用!"); } var onMap = await _mapService.GetUserOnMapId(userId); if (npcInfo.mapId != onMap) { return PoAction.Message("Npc不存在!"); } #endregion var cityData = await _mapService.GetCityData(); cityData = cityData.FindAll(it => it.type == "DEF_MAP"); List area = cityData.FindAll(it => it.parent == 0); if (cityId == 0) { cityId = area[0].cityId; } var onCityData = cityData.FindAll(it => it.parent == cityId); var userCity = await _mapService.GetUserCityMapData(userId); var AtCity = await _mapService.GetMapCityByMapId(onMap); List city = new List(); onCityData.ForEach(item => { if (userCity.Any(it => it.cityId == item.cityId) && item.cityId != AtCity) { MapTemp temp = new MapTemp(); temp.cityId = item.cityId; temp.cityName = item.cityName; temp.distance = GameTool.ComputeMile(onMap, item.toMap); temp.copper = temp.distance * GameConfig.GameToMapNeedCopper; city.Add(temp); } }); string tips = $"*传送消耗:{GameConfig.GameToMapNeedCopper}铜贝/海里
*等级低于80级不需要传送费!"; return PoAction.Ok(new { area, city, cityId, tips }); } /// /// 用户传送到地图 /// /// /// /// [HttpGet] public async Task UserToMap(int npcId, int cityId) { string userId = StateHelper.userId; #region NPC验证 var npcInfo = await _mapService.GetNpcInfo(npcId); if (npcInfo == null) { return PoAction.Message("Npc不存在!"); } if (npcInfo.status != 1) { return PoAction.Message("Npc不存在!"); } if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.MapTo))) { return PoAction.Message("业务不可用!"); } var onMap = await _mapService.GetUserOnMap(userId); if (npcInfo.mapId != onMap.mapId) { return PoAction.Message("Npc不存在!"); } #endregion var cityInfo = await _mapService.GetCityInfo(cityId); if (cityInfo == null) { return PoAction.Message("城市不存在!"); } var userWeight = await _weightService.GetUserWeightInfo(userId); if (userWeight.shipOnWeight > 0) { return PoAction.Message("背包中存在货物,不可传送,你可以进行航行!"); } var onMapInfo = await _mapService.GetMapInfo(onMap.mapId); if (onMapInfo.cityId == cityId) { return PoAction.Message("您已在当前城市,无需传送!"); } var MySeaMap = await _mapService.GetUserCityMapData(userId); if (MySeaMap.Any(it => it.cityId == cityId) == false) { return PoAction.Message("您暂未获取该城市海图!"); } bool isCanTo = false; var MyLev = await _attrService.GetUserLev(userId); if (MyLev < 80) { isCanTo = true; } int need = 0; if (isCanTo == false) //判断费用 { var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper)); need = GameTool.ComputeMile(onMap.mapId, cityInfo.toMap) * GameConfig.GameToMapNeedCopper; if (myAcc < need) { return PoAction.Message("铜贝不足!"); } isCanTo = true; } if (isCanTo) { if (await _mapService.UpdateUserOnMap(onMap, cityInfo.toMap)) { if (need > 0) { await _accService.UpdateUserCopper(userId, 0, need, $"传送到【{cityInfo.cityName}】费用"); } return PoAction.Ok(true); } else { return PoAction.Message("传送失败,请稍后尝试!"); } } else { return PoAction.Message("传送失败,请稍后尝试!"); } } /// /// 用户航海接口 /// /// /// /// [HttpGet] public async Task UserRunMap(int npcId, int cityId) { string userId = StateHelper.userId; #region NPC验证 var npcInfo = await _mapService.GetNpcInfo(npcId); if (npcInfo == null) { return PoAction.Message("Npc不存在!"); } if (npcInfo.status != 1) { return PoAction.Message("Npc不存在!"); } if (!npcInfo.bus.Any(it => it.code == nameof(GameEnum.NpcBusCode.MapTo))) { return PoAction.Message("业务不可用!"); } var onMap = await _mapService.GetUserOnMap(userId); if (npcInfo.mapId != onMap.mapId) { return PoAction.Message("Npc不存在!"); } #endregion var cityInfo = await _mapService.GetCityInfo(cityId); if (cityInfo == null) { return PoAction.Message("城市不存在!"); } var onMapInfo = await _mapService.GetMapInfo(onMap.mapId); if (onMapInfo.cityId == cityId) { return PoAction.Message("您已在当前城市,无需航行!"); } var MySeaMap = await _mapService.GetUserCityMapData(userId); if (MySeaMap.Any(it => it.cityId == cityId) == false) { return PoAction.Message("您暂未获取该城市海图!"); } var userShip = await _weightService.GetUserShip(userId); if (userShip.Count == 0) { return PoAction.Message("购买船只后才可以航行哦!"); } var runInfo = await _mapService.GetUserRun(userId); if (runInfo.status != 0) { return PoAction.Message("您已经在航线上啦!"); } bool isCanTo = false; var MyLev = await _attrService.GetUserLev(userId); if (MyLev < 80) { isCanTo = true; } int need = 0; if (isCanTo == false) //判断费用 { var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper)); int jl = GameTool.ComputeMile(onMap.mapId, cityInfo.toMap); need = userShip.Max(it => (int)it.copper) * jl; if (myAcc < need) { return PoAction.Message("铜贝不足!"); } isCanTo = true; } if (isCanTo) { if (await _mapService.SaveUserRunInfo(userId, StateHelper.areaId, onMap.mapId, cityInfo.toMap)) { if (need > 0) { await _accService.UpdateUserCopper(userId, 0, need, $"传送到【{cityInfo.cityName}】费用"); } return PoAction.Ok(true); } else { return PoAction.Message("传送失败,请稍后尝试!"); } } else { return PoAction.Message("传送失败,请稍后尝试!"); } } /// /// 获取用户航行状态 /// /// [HttpGet] public async Task GetUserRun() { var userId = StateHelper.userId; var runInfo = await _mapService.GetUserRun(userId); if (runInfo.status != 1) { return PoAction.Message("不在航行状态", 100); } var onCity = await _mapService.GetCityInfoByMapId(runInfo.onMap); var toCity = await _mapService.GetCityInfoByMapId(runInfo.toMap); int isShip = 0; int safety = 0; string busCode = $"{runInfo.onMap}_{runInfo.toMap}"; var busData = await _mapService.GetMapBus(busCode, (int)runInfo.position, (int)runInfo.depth); var business = busData.Select(it => new { busId = it.busId, busName = it.busName }); return PoAction.Ok(new { onCity = onCity.cityName, toCity = toCity.cityName, distance = runInfo.distance, position = runInfo.position, isShip = isShip, safety = safety, business }); } /// /// 取消航行 /// /// [HttpGet] public async Task StopUserRun() { var userId = StateHelper.userId; var runInfo = await _mapService.GetUserRun(userId); if (runInfo.status != 1) { return PoAction.Message("不在航行状态", 100); } if (await _mapService.StopUserRun(userId)) { return PoAction.Ok(true); } else { return PoAction.Message("取消失败,请稍后尝试!"); } } [HttpGet] public async Task UserMapRuning() { var userId = StateHelper.userId; var runInfo = await _mapService.GetUserRun(userId); if (runInfo.status != 1) { return PoAction.Message("不在航行状态", 100); } var userShip = await _weightService.GetUserShip(userId); if (userShip.Count == 0) { return PoAction.Message("购买船只后才可以航行哦!"); } int spend = userShip.Sum(it => (int)it.speed) / userShip.Count; if ((runInfo.position + spend) >= runInfo.distance) { await _mapService.StopUserRun(userId); var onMap = await _mapService.GetUserOnMap(userId); await _mapService.UpdateUserOnMap(onMap, runInfo.toMap); return PoAction.Message("航行完成!", 100); } else { runInfo.position += spend; runInfo.upTime = TimeExtend.GetTimeStampSeconds; if (await _mapService.UpdateUserRunInfo(runInfo)) { string msg = await _mapService.GetUserRunEvent(userId); return PoAction.Ok(true, msg); } else { return PoAction.Message("航行错误,请重试!"); } } } #endregion #region npc相关 /// /// 获取NPC相关信息 /// /// /// [HttpGet] public async Task GetNpcInfo(int npcId) { string userId = StateHelper.userId; var data = await _mapService.GetNpcInfo(npcId); if (data == null) { return PoAction.Message("Npc不存在!"); } if (data.status != 1) { return PoAction.Message("Npc不存在!"); } var onMap = await _mapService.GetUserOnMapId(userId); if (data.mapId != onMap) { return PoAction.Message("Npc不存在!"); } List task = new List(); if (data.code == nameof(MapEnum.NpcCode.Task)) { task = await _taskService.GetTaskDataByNpc(userId, npcId); } return PoAction.Ok(new { data, task }); } /// /// 处理Npc事务 /// /// /// /// [HttpGet] public async Task HandleNpcEvent(int npcId, string code, string? parms) { string userId = StateHelper.userId; int areaId = StateHelper.areaId; var data = await _mapService.GetNpcInfo(npcId); if (data == null) { return PoAction.Message("Npc不存在!"); } if (data.status != 1) { return PoAction.Message("Npc不存在!"); } if (data.code != nameof(MapEnum.NpcCode.Event)) { return PoAction.Message("无法处理该业务!"); } if (data.bus.Any(it => it.code == code) == false) { return PoAction.Message("无法处理该业务!"); } var onMap = await _mapService.GetUserOnMap(userId); if (data.mapId != onMap.mapId) { return PoAction.Message("Npc不存在!"); } if (code == nameof(MapEnum.NpcEventCode.Magic_Event)) //魔城 { var magicService = App.GetService(); bool isWin = false; if (TimeAssist.GetHourNum > 221000) { var onMapUser = await _mapService.GetMapUser(GameConfig.GameMagicMapId, areaId, 1, [userId], 1); if (onMapUser.Count == 0) { string time = TimeAssist.GetDateTimeYMDString(0); if (await magicService.IsHaveWin(areaId, time)==false) { isWin = true; } } } await magicService.UpdateUserMagicEndTime(userId, isWin); await _mapService.UpdateUserOnMap(onMap, GameConfig.GameMagicInMapId); //设置地图 return PoAction.Ok(0, "魔城成功退出!"); } return PoAction.Message("非处理的游戏项!"); } #endregion #region 采集相关 /// /// 采集接口 /// /// /// [HttpGet] public async Task CollectMapRes(string resId) { var resInfo = await _mapService.GetMapResInfo(resId); if (resInfo == null) { return PoAction.Message("特产不存在!"); } var userId = StateHelper.userId; int area = StateHelper.areaId; var onMap = await _mapService.GetUserOnMap(userId); if (onMap.mapId != resInfo.mapId) { return PoAction.Message("特产不存在!"); } long lockTime = await _mapService.GetMapResLockTime(resId, area); if (lockTime > 0) { return PoAction.Message($"{lockTime}秒后可采集!"); } var skillInfo = await _skillService.GetUserSkillInfo(userId, nameof(SkillEnum.code.GATHER)); if (skillInfo == null) { return PoAction.Message("未学习采集术!"); } if (skillInfo.lev < resInfo.lev) { return PoAction.Message($"需要{resInfo.lev}级采集术才可以采集哦!"); } var myVigour = await _attrService.GetUserVigourInfo(userId); if (myVigour.vitality < resInfo.needVigour) { return PoAction.Message("活力不足!"); } if (await _attrService.UpdateUserVigour(userId, 0, (int)resInfo.needVigour)) { await _mapService.SetMapResLockTime(resId, area, (int)resInfo.gatherTime); if (RandomAssist.CheakRandom((int)resInfo.okRadio)) { //采集成功 string[] counts = resInfo.count.Split('-'); int onCount = RandomAssist.GetFormatedNumeric(Convert.ToInt32(counts[0]), Convert.ToInt32(counts[1]) + 1); await _goodsService.UpdateUserGoods(userId, 1, (int)resInfo.goodsId, onCount, "采集获得"); return PoAction.Ok(resInfo.gatherTime, $"成功采集{resInfo.resName}×{onCount}!"); } else { await _mapService.SetMapResLockTime(resId, area, (int)resInfo.gatherTime); return PoAction.Ok(resInfo.gatherTime, "抱歉,采集失败!提升技能会提高成功率哦!"); } } else { return PoAction.Message("采集失败,请稍后尝试!"); } } #endregion #region 地图物品 /// /// 拾取地图物品 /// /// /// [HttpGet] public async Task GetMapGoods(string mgId) { string userId = StateHelper.userId; var onMap = await _mapService.GetUserOnMapId(userId); var goods = await _fightService.GetFightGoodsInfo(onMap, mgId); if (goods == null) { return PoAction.Message("物品已被拾取!"); } if (await _weightService.CheckUserWeight(userId, goods.code, goods.par, goods.count)) { await _fightService.RemoveFightGoods(onMap, mgId); if (await GameBus.UpdateBag(userId, 1, goods.code, goods.par, goods.count, "城市地图拾取")) { return PoAction.Ok(true, $"成功拾取:{goods.name}×{goods.count}!"); } else { return PoAction.Message("拾取失败,请稍后尝试!"); } } else { return PoAction.Message("背包暂无空间!"); } } #endregion }