using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using asap.core; using cfg; using game; using LitJson; using Newtonsoft.Json; using UnityEngine; using static EventPartnerData; namespace GameCore { public class ChangeSmallGameTargetEvent { public OpponentItemData opponentItemData; public System.Threading.Tasks.TaskCompletionSource ts; public bool random; } public class SmallGameData { private cfg.Tables _tables; IUserService userService; ICustomServerMgr customServerMgr; public SmallGameData(cfg.Tables tables, IUserService userService, ICustomServerMgr customServerMgr) { this._tables = tables; this.userService = userService; this.customServerMgr = customServerMgr; BottleCashParamBase = _tables.TbGlobalConfig.BottleCashParamBase; BottleCashParamDiv = _tables.TbGlobalConfig.BottleCashParamDiv; BottleCashParamLevel = _tables.TbGlobalConfig.BottleCashParamLevel; } public void Init() { campData = GContext.container.Resolve(); LoginRandomOpponent(); } CampDataMM campData; int _heistGameCount = 0; int _bombGameCount = 0; int _fishHuntGameCount = 0; public int HeistGameCount => _heistGameCount; public int FishHuntGameCount => _fishHuntGameCount; public int BombGameCount => _bombGameCount; public int AlbumEventID; //public UnityEngine.Vector3 image_extra_pos { set; get; } public void SetHeistGameCount(int value) { _heistGameCount = value; } public void SaveHeistGameCount() { _heistGameCount++; PlayFabMgr.Instance.UpdateUserDataValue("HeistGameCount", _heistGameCount.ToString()); } public void SetBombGameCount(int value) { _bombGameCount = value; } public void SaveBombGameCount() { _bombGameCount++; PlayFabMgr.Instance.UpdateUserDataValue("BombGameCount", _bombGameCount.ToString()); } public void SetFishHuntGameCount(int value) { _fishHuntGameCount = value; } public void SaveFishHuntGameCount() { _fishHuntGameCount++; PlayFabMgr.Instance.UpdateUserDataValue("FishHuntGameCount", _fishHuntGameCount.ToString()); } #region 小游戏仇人 public List opponentItemDatas = new List(); public List opponentFriendItemDatas = new List(); public List opponentNotFriendItemDatas = new List(); public List opponentFriend = new List(); Dictionary opponentFriendDic = new Dictionary(); public Dictionary> opponentEnemy = new Dictionary>(); List opponentItemDatasPre = new List(); List opponentFriendItemDatasPre = new List(); List opponentNotFriendItemDatasPre = new List(); EnemyType curEnemyType = EnemyType.Aquarium; public Dictionary powerData; public OpponentItemData opponentData; OpponentItemData nextOpponent; int GMbobmName = 0; int BottleCashParamBase; int BottleCashParamDiv; int BottleCashParamLevel; public int RobotMapId; public int nextRobotMapId; public void GMSetBomb(string bobmName) { GMbobmName = int.Parse(bobmName); } public async void LoginRandomOpponent() { GetRobotData(); RandomOpponent(); await LoadData(); RandomOpponent(); } void RandomOpponent() { Synchrodata(); nextOpponent = NextOpponent(); LoadNextOpponentResource(); } public List GetOpponentEnemy() { return opponentEnemy.GetValueOrDefault(curEnemyType); } public OpponentItemData GetBankHeistOpponent(EnemyType enemyType) { this.curEnemyType = enemyType; if (opponentItemDatasPre == null || opponentItemDatasPre.Count == 0) { ClearOpponent(); GetFriendData(); GetRobotData(); } opponentData = RandomItem(); LoadData(); GetEnemyData(curEnemyType); if (GMbobmName > 0) { opponentData.BombID = GMbobmName; } return opponentData; } void Synchrodata() { if (opponentItemDatasPre != null && opponentItemDatasPre.Count > 0) { opponentItemDatas = opponentItemDatasPre; } if (opponentFriendItemDatasPre != null && opponentFriendItemDatasPre.Count > 0) { opponentFriendItemDatas = opponentFriendItemDatasPre; } if (opponentNotFriendItemDatasPre != null && opponentNotFriendItemDatasPre.Count > 0) { opponentNotFriendItemDatas = opponentNotFriendItemDatasPre; } } public OpponentItemData RandomItem() { OpponentItemData target = nextOpponent; Synchrodata(); nextOpponent = NextOpponent(); RobotMapId = nextRobotMapId; LoadNextOpponentResource(); return target; } async void LoadNextOpponentResource() { if (nextOpponent != null) { ILoadResourceService loadResourceService = GContext.container.Resolve(); RobotSetMap(); int mapId; if (nextOpponent.isRobot) { mapId = nextRobotMapId; } else { mapId = await LoadAquariumResource(nextOpponent.playFabID); if (mapId > 0) { nextRobotMapId = mapId; } else { mapId = nextRobotMapId; } } Debug.Log($"nextOpponent Next MapID {mapId}"); if (mapId > 0) { MapDownloadRes mapData = _tables.TbMapDownloadRes.GetOrDefault(mapId); if (mapData != null) { loadResourceService.Load(mapData.FishRes); } } string bombPrefab = _tables.TbConstruction.GetOrDefault(nextOpponent.BombID)?.BombPrefab; if (!string.IsNullOrEmpty(bombPrefab)) { loadResourceService.Load(new List() { bombPrefab }); } } } void RobotSetMap() { int curMapId = nextRobotMapId; var mapDatas = _tables.TbMapData.DataList; var maxMapID = GContext.container.Resolve().lastMapId; //非当前地图 var mapIDs = new List(); for (int i = 0; i < mapDatas.Count; i++) { if (mapDatas[i].ID <= maxMapID) { if (mapDatas[i].ID != curMapId) { mapIDs.Add(mapDatas[i].ID); } } else { break; } } if (mapIDs.Count == 0) { nextRobotMapId = maxMapID; } else { nextRobotMapId = mapIDs[UnityEngine.Random.Range(0, mapIDs.Count)]; } } async Task LoadAquariumResource(string playFabId) { GetAquariumDataRequest request = new GetAquariumDataRequest(); request.playFabId = playFabId; string result = await customServerMgr.CustomServerPost("aquarium/GetAquariumData", Newtonsoft.Json.JsonConvert.SerializeObject(request)); if (result != null) { var resp = JsonConvert.DeserializeObject(result); if (resp.code == 0) { var data = resp.data; List slotDatas = data.slotDatas; if (slotDatas == null) { return 0; } var time = ZZTimeHelper.UtcNow(); List aquariumResultDatas = data.aquariumResultDatas; for (int i = 0; i < slotDatas.Count; i++) { AquariumSlotData slotData = slotDatas[i]; slotData.index = i; var HatchTime = slotData.FishHatchTime(); if (HatchTime < time) { if (!slotData.IsResult() && aquariumResultDatas.Count > i && !aquariumResultDatas[i].IsResult()) { return resp.data.mapId; } } } } } return 0; } public OpponentItemData NextOpponent() { int friendW = Math.Min(100, opponentFriendItemDatas.Count * 20); if (opponentFriendItemDatas.Count == 0) { friendW = 0; } int noFriendW = 70; if (opponentNotFriendItemDatas.Count == 0) { noFriendW = 0; } int robotW = 30; int allW = friendW + noFriendW + robotW; int w = UnityEngine.Random.Range(0, allW); OpponentItemData target; if (friendW > 0 && opponentFriendItemDatas.Count > 0 && w < friendW) { target = opponentFriendItemDatas[UnityEngine.Random.Range(0, opponentFriendItemDatas.Count)]; target.target_group = 1; } else if (noFriendW > 0 && opponentNotFriendItemDatas.Count > 0 && w < friendW + noFriendW) { target = opponentNotFriendItemDatas[UnityEngine.Random.Range(0, opponentNotFriendItemDatas.Count)]; target.target_group = 2; } else { if (opponentItemDatas == null || opponentItemDatas.Count == 0) { GetRobotData(); opponentItemDatas = opponentItemDatasPre; Debug.LogError("opponentItemDatas Count == 0"); } target = opponentItemDatas[UnityEngine.Random.Range(0, opponentItemDatas.Count)]; target.target_group = 3; } return target; } void ClearOpponent() { Synchrodata(); opponentFriendItemDatasPre = new List(); opponentNotFriendItemDatasPre = new List(); opponentItemDatasPre = new List(); powerData = new Dictionary(); opponentFriend = new List(); opponentFriendDic = new Dictionary(); } public async Task LoadData() { ClearOpponent(); GetFriendData(); try { await GetActiveNotFriendData(); await GetLeaderboardData(curEnemyType); } catch (Exception ex) { Debug.LogError(ex.Message); } GetRobotData(); } async Task GetActiveNotFriendData() { var request = new EventBuildRecommendPlayerRequest(); int lv = GContext.container.Resolve().lv; int minLevel = (int)(lv * 0.5f); if (minLevel < 0) { minLevel = 0; } request.MinLevel = minLevel; request.MaxLevel = lv * 2; request.Exclude = new List(); string json = await customServerMgr.CustomServerPost($"eventpartner/recommend", Newtonsoft.Json.JsonConvert.SerializeObject(request)); if (!string.IsNullOrEmpty(json) && json != "[]") { EventBuildRecommendPlayerResp resp = Newtonsoft.Json.JsonConvert.DeserializeObject(json); if (resp.State == EEventBuildState.Success && resp.Players != null) { List _dataList = _tables.TbConstruction.DataList; for (int i = 0; i < resp.Players.Count; i++) { if (resp.Players[i].PlayFabId == userService.UserId) { continue; } OpponentItemData opponentItemData = new OpponentItemData(); opponentItemData.playFabID = resp.Players[i].PlayFabId; opponentItemData.target_source = 2; userService.SetPlayInfo(resp.Players[i].PlayFabId, resp.Players[i].DisplayName, resp.Players[i].AvatarUrl); userService.SetPlayerLv(resp.Players[i].PlayFabId, resp.Players[i].Level); int level = resp.Players[i].Level; int id = _dataList[0].ID; for (int j = _dataList.Count - 1; j >= 0; j--) { if (_dataList[j].InitialLevel <= level) { id = _dataList[j].ID; break; } } opponentItemData.BombID = id; opponentItemData.power = (float)Math.Log10(BottleCashParamBase + level / BottleCashParamLevel) / BottleCashParamDiv; powerData[opponentItemData.playFabID] = opponentItemData.power; //opponentItemDatasPre.Add(opponentItemData); opponentNotFriendItemDatasPre.Add(opponentItemData); } } } } void GetFriendData() { var FriendList = GContext.container.Resolve().FriendList; opponentFriendItemDatasPre.Clear(); DateTime dateTime = ZZTimeHelper.UtcNow().Date.AddDays(-1); if (FriendList != null && FriendList.Count > 0) { List _dataList = _tables.TbConstruction.DataList; for (int i = 0; i < FriendList.Count; i++) { if (FriendList[i].playFabId == userService.UserId) { continue; } OpponentItemData opponentItemData = new OpponentItemData(); opponentItemData.playFabID = FriendList[i].playFabId; opponentItemData.target_source = 1; //if (FriendList[i].LastLogin != null && FriendList[i].LastLogin > dateTime) //{ opponentFriendItemDatasPre.Add(opponentItemData); //} int level = FriendList[i].value / LeadboardData.LV_MODELING; int id = _dataList[0].ID; for (int j = _dataList.Count - 1; j >= 0; j--) { if (_dataList[j].InitialLevel <= level) { id = _dataList[j].ID; break; } } opponentItemData.BombID = id; opponentItemData.power = (float)Math.Log10(BottleCashParamBase + level / BottleCashParamLevel) / BottleCashParamDiv; powerData[opponentItemData.playFabID] = opponentItemData.power; //opponentItemDatasPre.Add(opponentItemData); opponentFriend.Add(opponentItemData); opponentFriendDic[opponentItemData.playFabID] = opponentItemData; } } } public async void SetEnemy(EnemyType enemyType, long glod) { if (!opponentData.isRobot) { SetEnemyIDRequest setEnemyCampIDRequest = new SetEnemyIDRequest() { enemyType = enemyType, playerID = userService.UserId, albumEventID = AlbumEventID, enemyID = opponentData.playFabID, content = glod.ToString() }; var customServerMgr = GContext.container.Resolve(); string json = await customServerMgr.BombRequest("SetEnemyID", setEnemyCampIDRequest); if (!string.IsNullOrEmpty(json) && json != "[]") { SetEnemyIDResponse setCampIDResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(json); } } } //向服务器请求仇人列表 async Task GetEnemyData(EnemyType enemyType) { GetEnemyIDRequest getEnemyCampIDRequest = new GetEnemyIDRequest() { playerID = userService.UserId, enemyType = enemyType, albumEventID = AlbumEventID, }; var customServerMgr = GContext.container.Resolve(); string json = await customServerMgr.BombRequest("GetEnemyID", getEnemyCampIDRequest); if (!string.IsNullOrEmpty(json) && json != "[]") { GetEnemyIDResponse getCampIDResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(json); if (getCampIDResponse.enemyIDs == null) { return; } List eventPopupDatas = GContext.container.Resolve().eventPopupDatas; //DateTime dateTime = ZZTimeHelper.UtcNow().UtcNowOffset(); //EventPopupData eventPopupData; int count; ReportDataType reportDataType = ReportDataType.Bomb; switch (enemyType) { case EnemyType.Bomb: reportDataType = ReportDataType.Bomb; break; case EnemyType.Heist: reportDataType = ReportDataType.Heist; break; case EnemyType.Aquarium: reportDataType = ReportDataType.Aquarium; break; } List opponentItemDatas = new List(); opponentEnemy[enemyType] = opponentItemDatas; eventPopupDatas = eventPopupDatas.Where(x => (int)(x.type) == (int)reportDataType).ToList(); for (int i = 0; i < getCampIDResponse.enemyIDs.Count; i++) { if (getCampIDResponse.enemyIDs[i] == userService.UserId) { continue; } OpponentItemData opponentItemData = new OpponentItemData(); opponentItemData.playFabID = getCampIDResponse.enemyIDs[i]; //if (string.IsNullOrEmpty(getCampIDResponse.avatar[i]) && string.IsNullOrEmpty(getCampIDResponse.displayName[i])) //{ // userService.GetPlayInfo(opponentItemData.playFabID); //} //else //{ // userService.SetPlayInfo(opponentItemData.playFabID, getCampIDResponse.displayName[i], getCampIDResponse.avatar[i]); //} if (getCampIDResponse.enemyType == EnemyType.Bomb) { opponentItemData.BombID = getCampIDResponse.campID[i]; } if (powerData.ContainsKey(opponentItemData.playFabID)) { opponentItemData.power = powerData[opponentItemData.playFabID]; } else { opponentItemData.power = 1; } count = eventPopupDatas.Where(x => x.playerID == opponentItemData.playFabID).Count(); if (reportDataType == ReportDataType.Aquarium) { if (count > 1) { opponentItemData.info = LocalizationMgr.GetFormatTextValue("UI_EventBankHeistOpponentPopupPanel_3", count); } else { opponentItemData.info = LocalizationMgr.GetFormatTextValue("UI_EventBankHeistOpponentPopupPanel_5", 1); } } else { if (count > 1) { opponentItemData.info = LocalizationMgr.GetFormatTextValue("UI_EventBankHeistOpponentPopupPanel_6", count); } else { opponentItemData.info = LocalizationMgr.GetFormatTextValue("UI_EventBankHeistOpponentPopupPanel_7", 1); } } if (opponentFriendDic.TryGetValue(opponentItemData.playFabID, out OpponentItemData friendData)) { friendData.info = opponentItemData.info; } opponentItemData.target_source = 4; opponentItemDatas.Add(opponentItemData); //opponentNotFriendItemDatasPre.Add(opponentItemData); } } } void GetRobotData() { int count = opponentItemDatasPre.Count; int allCount = 20; if (count >= 20) { allCount = count + 1; } var robots = new List(_tables.TbRobot.DataList); cfg.Robot robot; int myID = campData.Id; cfg.TbConstruction tbConstruction = _tables.TbConstruction; int constructionFirstId = tbConstruction.DataList[0].ID % 1000; int constructionLastId = tbConstruction.DataList[^1].ID; int campId; for (int i = count; i < allCount; i++) { robot = robots[UnityEngine.Random.Range(0, robots.Count)]; robots.Remove(robot); OpponentItemData opponentItemData = new OpponentItemData(); opponentItemData.playFabID = robot.ID.ToString("X"); opponentItemData.target_source = -1; userService.SetPlayInfo(opponentItemData.playFabID, LocalizationMgr.GetText(robot.Name_l10n_key), robot.Avatar); //opponentItemData.playName = robot.Name; //opponentItemData.url = robot.Avatar; opponentItemData.isRobot = true; campId = robot.ConstructionOffset + myID; if (campId < constructionFirstId) { campId = constructionFirstId; } if (campId > constructionLastId) { campId = constructionLastId; } opponentItemData.BombID = campId; opponentItemData.power = (float)Math.Log10(BottleCashParamBase - i) / BottleCashParamDiv; opponentItemDatasPre.Add(opponentItemData); } } async Task GetLeaderboardData(EnemyType enemyType) { FishingEventData _fishingEventData = GContext.container.Resolve(); if (!_fishingEventData.IsOpenRank()) { return; } LeadboardData leadboardData = GContext.container.Resolve(); if (leadboardData.curEventRank == null) { return; } Dictionary robots = GContext.container.Resolve().TbRobot.DataMap; GetLeaderboardsResp curEventRank = leadboardData.curEventRank; int myID = campData.Id; cfg.TbConstruction tbConstruction = _tables.TbConstruction; int constructionFirstId = tbConstruction.DataList[0].ID % 1000; int constructionLastId = tbConstruction.DataList[^1].ID; int campId = myID; Dictionary playerList = new Dictionary(); for (int i = 0; i < curEventRank.data.Length; i++) { if (curEventRank.data[i].isMe || curEventRank.data[i].id == userService.UserId) { continue; } if (curEventRank.data[i].id == null || powerData.ContainsKey(curEventRank.data[i].id)) { continue; } OpponentItemData opponentItemData = new OpponentItemData(); opponentItemData.playFabID = curEventRank.data[i].id; if (curEventRank.data[i].isBot) { int b = Convert.ToInt32(opponentItemData.playFabID, 16); if (robots.TryGetValue(b, out cfg.Robot robot)) { campId = robot.ConstructionOffset + myID; } if (campId < constructionFirstId) { campId = constructionFirstId; } if (campId > constructionLastId) { campId = constructionLastId; } opponentItemData.isRobot = true; opponentItemData.target_source = -1; opponentItemDatasPre.Add(opponentItemData); } else { playerList[opponentItemData.playFabID] = opponentItemData; opponentItemData.target_source = 3; userService.GetPlayInfo(opponentItemData.playFabID); opponentNotFriendItemDatasPre.Add(opponentItemData); } opponentItemData.BombID = campId; opponentItemData.power = (float)Math.Log10(BottleCashParamBase - curEventRank.data[i].rank) / BottleCashParamDiv; powerData[opponentItemData.playFabID] = opponentItemData.power; } if (playerList.Count > 0 && enemyType == EnemyType.Bomb) { await GetCampID(playerList); } } async Task GetCampID(Dictionary playerList) { List playFabIds = playerList.Values.Select(x => x.playFabID).ToList(); GetCampIDRequest getCampIDRequest = new GetCampIDRequest() { playerIDs = playFabIds }; var customServerMgr = GContext.container.Resolve(); string json = await customServerMgr.BombRequest("GetCampID", getCampIDRequest); if (!string.IsNullOrEmpty(json) && json != "[]") { GetCampIDResponse getCampIDResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(json); if (getCampIDResponse != null && getCampIDResponse.campIDs != null && getCampIDResponse.campIDs.Count > 0) { foreach (var item in getCampIDResponse.campIDs) { playerList[item.Key].BombID = item.Value; } } } } #endregion } }