Files
MinFt/Client/Assets/Scripts/DataCenter/DataCenter.SmallGame.cs
2026-04-27 12:07:32 +08:00

568 lines
22 KiB
C#

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;
namespace GameCore
{
public class ChangeSmallGameTargetEvent
{
public OpponentItemData opponentItemData;
public System.Threading.Tasks.TaskCompletionSource<bool> ts;
public bool random;
}
public class SmallGameData
{
private cfg.Tables _tables;
IUserService userService;
public SmallGameData(cfg.Tables tables, IUserService userService)
{
this._tables = tables;
this.userService = userService;
BottleCashParamBase = _tables.TbGlobalConfig.BottleCashParamBase;
BottleCashParamDiv = _tables.TbGlobalConfig.BottleCashParamDiv;
BottleCashParamLevel = _tables.TbGlobalConfig.BottleCashParamLevel;
}
public void Init()
{
campData = GContext.container.Resolve<CampDataMM>();
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 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<OpponentItemData> opponentItemDatas = new List<OpponentItemData>();
public List<OpponentItemData> opponentFriendItemDatas = new List<OpponentItemData>();
public List<OpponentItemData> opponentNotFriendItemDatas = new List<OpponentItemData>();
public List<OpponentItemData> opponentFriend = new List<OpponentItemData>();
Dictionary<string, OpponentItemData> opponentFriendDic = new Dictionary<string, OpponentItemData>();
public Dictionary<EnemyType, List<OpponentItemData>> opponentEnemy = new Dictionary<EnemyType, List<OpponentItemData>>();
List<OpponentItemData> opponentItemDatasPre = new List<OpponentItemData>();
List<OpponentItemData> opponentFriendItemDatasPre = new List<OpponentItemData>();
List<OpponentItemData> opponentNotFriendItemDatasPre = new List<OpponentItemData>();
EnemyType curEnemyType = EnemyType.Aquarium;
public Dictionary<string, float> 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<OpponentItemData> GetOpponentEnemy()
{
return opponentEnemy.GetValueOrDefault(curEnemyType);
}
public OpponentItemData GetBankHeistOpponent(EnemyType enemyType)
{
this.curEnemyType = enemyType;
if (opponentItemDatasPre == null || opponentItemDatasPre.Count == 0)
{
ClearOpponent();
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<ILoadResourceService>();
RobotSetMap();
int mapId;
if (nextOpponent.isRobot)
{
mapId = nextRobotMapId;
}
else
{
mapId = nextRobotMapId; // removed: Aquarium - fallback to robot map
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.EnqueueBundleSilently(mapData.FishRes);
}
}
string bombPrefab = _tables.TbConstruction.GetOrDefault(nextOpponent.BombID)?.BombPrefab;
if (!string.IsNullOrEmpty(bombPrefab))
{
loadResourceService.EnqueueBundleSilently(new List<string>() { bombPrefab });
}
}
}
void RobotSetMap()
{
int curMapId = nextRobotMapId;
var mapDatas = _tables.TbMapData.DataList;
var maxMapID = GContext.container.Resolve<PlayerData>().lastMapId;
//非当前地图
var mapIDs = new List<int>();
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<int> LoadAquariumResource(string playFabId)
{
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<OpponentItemData>();
opponentNotFriendItemDatasPre = new List<OpponentItemData>();
opponentItemDatasPre = new List<OpponentItemData>();
powerData = new Dictionary<string, float>();
opponentFriend = new List<OpponentItemData>();
opponentFriendDic = new Dictionary<string, OpponentItemData>();
}
public async Task LoadData()
{
ClearOpponent();
try
{
await GetLeaderboardData(curEnemyType);
}
catch (Exception ex)
{
Debug.LogError(ex.Message);
}
GetRobotData();
}
int GetConstructionID(int level)
{
List<Construction> _dataList = _tables.TbConstruction.DataList;
int id = _dataList[0].ID;
for (int j = _dataList.Count - 1; j >= 0; j--)
{
if (_dataList[j].InitialLevel <= level)
{
id = _dataList[j].ID;
break;
}
}
return id;
}
public async void SetEnemy(EnemyType enemyType, long glod)
{
if (!opponentData.isRobot && opponentData.playFabID.Length > 10)
{
SetEnemyIDRequest setEnemyCampIDRequest = new SetEnemyIDRequest()
{
enemyType = enemyType,
playerID = userService.UserId,
enemyID = opponentData.playFabID,
content = glod.ToString()
};
// [REMOVED] CustomServerMgr deleted
// var customServerMgr = GContext.container.Resolve<ICustomServerMgr>();
// string json = await customServerMgr.BombRequest("SetEnemyID", setEnemyCampIDRequest);
// if (!string.IsNullOrEmpty(json) && json != "[]")
// {
// SetEnemyIDResponse setCampIDResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<SetEnemyIDResponse>(json);
// }
}
}
//向服务器请求仇人列表
async Task GetEnemyData(EnemyType enemyType)
{
GetEnemyIDRequest getEnemyCampIDRequest = new GetEnemyIDRequest()
{
playerID = userService.UserId,
enemyType = enemyType,
};
// [REMOVED] CustomServerMgr deleted
// var customServerMgr = GContext.container.Resolve<ICustomServerMgr>();
// string json = await customServerMgr.BombRequest("GetEnemyID", getEnemyCampIDRequest);
return;
/*
{
GetEnemyIDResponse getCampIDResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<GetEnemyIDResponse>(json);
if (getCampIDResponse.enemyIDs == null)
{
return;
}
List<EventPopupData> eventPopupDatas = new List<EventPopupData>();
//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<OpponentItemData> opponentItemDatas = new List<OpponentItemData>();
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 (getCampIDResponse.levels != null)
{
if (getCampIDResponse.levels.TryGetValue(opponentItemData.playFabID, out int level))
{
opponentItemData.BombID = GetConstructionID(level);
}
}
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<cfg.Robot>(_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<FishingEventData>();
if (!_fishingEventData.IsOpenRank())
{
return;
}
LeadboardData leadboardData = GContext.container.Resolve<LeadboardData>();
if (leadboardData.curEventRank == null)
{
return;
}
Dictionary<int, cfg.Robot> robots = GContext.container.Resolve<cfg.Tables>().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<string, OpponentItemData> playerList = new Dictionary<string, OpponentItemData>();
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<string, OpponentItemData> playerList)
{
List<string> playFabIds = playerList.Values.Select(x => x.playFabID).ToList();
GetCampIDRequest getCampIDRequest = new GetCampIDRequest()
{
playerIDs = playFabIds
};
// [REMOVED] CustomServerMgr deleted
// var customServerMgr = GContext.container.Resolve<ICustomServerMgr>();
// string json = await customServerMgr.BombRequest("GetCampID", getCampIDRequest);
return;
}
#endregion
}
}