using asap.core; using asap.playfab.async; using cfg; using game; using PlayFab.ClientModels; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using UnityEngine; namespace GameCore { public enum ELeaderboardState : byte { Success = 0, Failed = 1, Delay = 2 } public class GetLeaderboardsRequest { public string playerId { get; set; } public bool includeOther { get; set; } public int? Score { get; set; } } /// /// Leaderboard Model /// public class GetLeaderboardsResp { public ELeaderboardState state { get; set; } /// /// Event Id from table /// public int eventId { get; set; } /// /// Level of Leaderboard eg. T0 T1 T2 /// public int rankLevel { get; set; } /// /// /// Max players of this leaderboard /// public int capacity { get; set; } /// /// Num of current member in this leaderboard (Players + Bots) /// //public int Count => data?.Length ?? 0; /// /// Avaliable Rank Datas /// public RankData[] data { get; set; } } public class UpdateScoreRequest { public string playerId { get; set; } public float Score { get; set; } } public class UpdateScoreResp { public ELeaderboardState state { get; set; } public int rank { get; set; } public int preRank { get; set; } public double score { get; set; } public double preScore { get; set; } } public class GetRewardsRequest { public string playerId { get; set; } } public class GetRewardsResp { public bool success { get; set; } public List rewardDatas { get; set; } } //public class ManualUpdateRequest //{ // public int eventId { get; set; } //} public class RewardData { public int rankLevel { get; set; } public int dropId { get; set; } public int eventId { get; set; } public RankData[] ranks { get; set; } } public class RankData { /// /// Player PlayFab ID /// public string id { get; set; } /// /// Leaderboard ranking /// public int rank { get; set; } /// /// leaderboard score /// public float score { get; set; } /// /// Is this player myself /// public bool isMe { get; set; } public bool isBot { get; set; } public string displayName { get; set; } public string avatarUrl { get; set; } } public class EventRankData { } public class PFRankDataEvent { public bool isLocal; } public class LeadboardData { [Inject] public IUserService userService { get; set; } // [REMOVED] CustomServerMgr deleted public static int LV_MODELING = 100000; //public const string leadboardLevelName = "Level"; public const string leadboardLevelName = "LevelB"; const int RefreshSeconds = 300; bool isRefreshing = false; // [REMOVED] LeadboardItemData type no longer exists // public List LeadboardDataList = new List(); // public List LeadboardContinentDataList = new List(); public List LeadboardDataList = new List(); public List LeadboardContinentDataList = new List(); public GetLeaderboardsResp curEventRank; public RewardData eventRankReward; public cfg.RankInit lastEventRank; public cfg.FishingEvent fishingEvent; public cfg.RankTargets rankTarget; public int lastScore; public int lastAllScore; int MeRank = 0; public bool IsOpenleaderboard; public async Task SetLeadboardData(List names, List value) { List Statistics = new List(); for (int i = 0; i < names.Count; i++) { Statistics.Add(new StatisticUpdate() { StatisticName = names[i], Value = value[i] }); } UpdatePlayerStatisticsRequest request = new UpdatePlayerStatisticsRequest() { Statistics = Statistics, }; await PlayFabClientAsyncAPI.UpdatePlayerStatisticsAsync(request); } public async Task GetLeadboardData(string leaderboardNaem, int maxCount = 50) { GetLeaderboardRequest reques = new GetLeaderboardRequest() { StartPosition = 0, MaxResultsCount = maxCount, StatisticName = leaderboardNaem, ProfileConstraints = new PlayerProfileViewConstraints() { ShowAvatarUrl = true, ShowLocations = true, ShowDisplayName = true, } }; return await PlayFabClientAsyncAPI.GetLeaderboardAsync(reques); } public async void SetLvData() { isRefreshing = true; string localName = GContext.container.Resolve().ContinentCode + leadboardLevelName; string InfiniteBuildingLevel = GContext.container.Resolve().InfiniteBuildingLevel; int.TryParse(InfiniteBuildingLevel, out int InfiniteBuildingLevelInt); if (InfiniteBuildingLevelInt >= LV_MODELING) { InfiniteBuildingLevelInt = LV_MODELING - 1; } int maxLevel = GContext.container.Resolve().TbMapData.DataList[^1].LevelRequired; if (GContext.container.Resolve().lv > maxLevel) { GContext.container.Resolve().lv = maxLevel; } int level = GContext.container.Resolve().lv * LV_MODELING + InfiniteBuildingLevelInt; await SetLeadboardData(new List { leadboardLevelName, localName }, new List { level, level }); isRefreshing = false; } public async void AwaitSetLvData() { if (isRefreshing) { return; } isRefreshing = true; await Awaiters.Seconds(RefreshSeconds); SetLvData(); } public async void GetLevelLeadboardData(bool islocal = false,/* bool save = false,*/ string name = leadboardLevelName) { if (islocal) { name = GContext.container.Resolve().ContinentCode + leadboardLevelName; } var result = await GetLeadboardData(name); if (result != null) { // [REMOVED] LeadboardItemData type no longer exists List curList = new List(); var Leaderboard = result.Leaderboard; if (Leaderboard.Count > 0) { for (int i = 0; i < Leaderboard.Count; i++) { // LeadboardItemData item = new LeadboardItemData(); if (string.IsNullOrEmpty(Leaderboard[i].Profile.DisplayName)) { Leaderboard[i].Profile.DisplayName = userService.GetDefaultName(Leaderboard[i].PlayFabId); } // item.SetData(Leaderboard[i]); userService.SetPlayInfo(Leaderboard[i].PlayFabId, Leaderboard[i].Profile.DisplayName, Leaderboard[i].Profile.AvatarUrl); curList.Add(Leaderboard[i]); } if (islocal) { LeadboardContinentDataList = curList; } else { LeadboardDataList = curList; } GContext.Publish(new PFRankDataEvent() { isLocal = islocal }); } } } #region EventLeaderboard public async Task GetLeaderboard(int Score = 0) { GetLeaderboardsResp getLeaderboardsResp = null; GetLeaderboardsRequest getLeaderboardRequest = new GetLeaderboardsRequest() { playerId = userService.UserId, includeOther = true, Score = Score }; // [REMOVED] CustomServerMgr deleted return false; } public int GetMeRank() { return MeRank; } public List rankItemDatas { set; get; } public static string NextQueueKey = "EventRankNextQueueKey"; public async Task GetRewards() { if (lastEventRank != null && rankItemDatas == null && rankTarget == null) { int eventId = PlayerPrefs.GetInt(NextQueueKey + userService.CustomId, 0); if (GContext.container.Resolve().RankTargetCount.GetValueOrDefault(0) == eventId) { Debug.Log("Event Rank Rewards already got."); return; } int dropId = 0; GetRewardsRequest request = new GetRewardsRequest { playerId = userService.UserId }; // [REMOVED] CustomServerMgr deleted return; } } public async void UpdateScore(int score) { // [REMOVED] CustomServerMgr deleted return; } void GetRankPlayInfo(RankData[] RankDatas) { Dictionary robots = GContext.container.Resolve().TbRobot.DataMap; foreach (var item in RankDatas) { if (!item.isMe && !item.isBot) { if (!string.IsNullOrEmpty(item.avatarUrl) || !string.IsNullOrEmpty(item.displayName)) { userService.SetPlayInfo(item.id, item.displayName, item.avatarUrl); } else { userService.GetPlayInfo(item.id); } } else if (item.isBot) { int b = Convert.ToInt32(item.id, 16); if (robots.TryGetValue(b, out cfg.Robot robot)) { userService.SetPlayInfo(item.id, LocalizationMgr.GetText(robot.Name_l10n_key), robot.Avatar); } } } } #endregion } }