备份CatanBuilding瘦身独立工程
This commit is contained in:
448
Assets/Scripts/DataCenter/DataCenter.LeadboardData.cs
Normal file
448
Assets/Scripts/DataCenter/DataCenter.LeadboardData.cs
Normal file
@@ -0,0 +1,448 @@
|
||||
using asap.core;
|
||||
using asap.playfab.async;
|
||||
using game;
|
||||
using PlayFab.ClientModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using tysdk;
|
||||
|
||||
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; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Leaderboard Model
|
||||
/// </summary>
|
||||
public class GetLeaderboardsResp
|
||||
{
|
||||
public ELeaderboardState state { get; set; }
|
||||
/// <summary>
|
||||
/// Event Id from table
|
||||
/// </summary>
|
||||
public int eventId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Level of Leaderboard eg. T0 T1 T2
|
||||
/// </summary>
|
||||
public int rankLevel { get; set; }
|
||||
///
|
||||
/// <summary>
|
||||
/// Max players of this leaderboard
|
||||
/// </summary>
|
||||
public int capacity { get; set; }
|
||||
/// <summary>
|
||||
/// Num of current member in this leaderboard (Players + Bots)
|
||||
/// </summary>
|
||||
//public int Count => data?.Length ?? 0;
|
||||
|
||||
/// <summary>
|
||||
/// Avaliable Rank Datas
|
||||
/// </summary>
|
||||
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<RewardData> 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
|
||||
{
|
||||
/// <summary>
|
||||
/// Player PlayFab ID
|
||||
/// </summary>
|
||||
public string id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Leaderboard ranking
|
||||
/// </summary>
|
||||
public int rank { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// leaderboard score
|
||||
/// </summary>
|
||||
public float score { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Is this player myself
|
||||
/// </summary>
|
||||
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; }
|
||||
[Inject]
|
||||
public ICustomServerMgr customServerMgr { get; set; }
|
||||
|
||||
public static int LV_MODELING = 100000;
|
||||
//public const string leadboardLevelName = "Level";
|
||||
public const string leadboardLevelName = "LevelB";
|
||||
const int RefreshSeconds = 300;
|
||||
bool isRefreshing = false;
|
||||
public List<LeadboardItemData> LeadboardDataList = new List<LeadboardItemData>();
|
||||
public List<LeadboardItemData> LeadboardContinentDataList = new List<LeadboardItemData>();
|
||||
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<string> names, List<int> value)
|
||||
{
|
||||
List<StatisticUpdate> Statistics = new List<StatisticUpdate>();
|
||||
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<GetLeaderboardResult> 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<IUserService>().ContinentCode + leadboardLevelName;
|
||||
string InfiniteBuildingLevel = GContext.container.Resolve<PlayerData>().InfiniteBuildingLevel;
|
||||
int.TryParse(InfiniteBuildingLevel, out int InfiniteBuildingLevelInt);
|
||||
int level = GContext.container.Resolve<PlayerData>().lv * LV_MODELING + InfiniteBuildingLevelInt;
|
||||
await SetLeadboardData(new List<string> { leadboardLevelName, localName }, new List<int> { 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<IUserService>().ContinentCode + leadboardLevelName;
|
||||
}
|
||||
|
||||
var result = await GetLeadboardData(name);
|
||||
if (result != null)
|
||||
{
|
||||
List<LeadboardItemData> curList = new List<LeadboardItemData>();
|
||||
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(item);
|
||||
}
|
||||
if (islocal)
|
||||
{
|
||||
LeadboardContinentDataList = curList;
|
||||
}
|
||||
else
|
||||
{
|
||||
LeadboardDataList = curList;
|
||||
}
|
||||
GContext.Publish(new PFRankDataEvent() { isLocal = islocal });
|
||||
}
|
||||
}
|
||||
}
|
||||
#region EventLeaderboard
|
||||
|
||||
public async Task<bool> GetLeaderboard(int Score = 0)
|
||||
{
|
||||
GetLeaderboardsResp getLeaderboardsResp = null;
|
||||
GetLeaderboardsRequest getLeaderboardRequest = new GetLeaderboardsRequest()
|
||||
{
|
||||
playerId = userService.UserId,
|
||||
includeOther = true,
|
||||
Score = Score
|
||||
};
|
||||
string json = await customServerMgr.EventRequest("GetLeaderboards", getLeaderboardRequest);
|
||||
if (!string.IsNullOrEmpty(json) && json != "[]")
|
||||
{
|
||||
getLeaderboardsResp = Newtonsoft.Json.JsonConvert.DeserializeObject<GetLeaderboardsResp>(json);
|
||||
}
|
||||
if (getLeaderboardsResp != null && getLeaderboardsResp.state == ELeaderboardState.Success)
|
||||
{
|
||||
curEventRank = getLeaderboardsResp;
|
||||
GetRankPlayInfo(curEventRank.data);
|
||||
}
|
||||
else if (curEventRank == null || curEventRank.data == null)
|
||||
{
|
||||
curEventRank = new GetLeaderboardsResp();
|
||||
}
|
||||
|
||||
if (curEventRank.data == null)
|
||||
{
|
||||
curEventRank.data = new RankData[] { new RankData { id = userService.UserId, isMe = true, rank = 1 } };
|
||||
MeRank = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (curEventRank.data.Length > 1)
|
||||
{
|
||||
for (int i = 0; i < curEventRank.data.Length; i++)
|
||||
{
|
||||
if (curEventRank.data[i].isMe)
|
||||
{
|
||||
MeRank = curEventRank.data[i].rank;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MeRank = 0;
|
||||
}
|
||||
}
|
||||
GContext.Publish(new EventRankData());
|
||||
return curEventRank != null;
|
||||
}
|
||||
public int GetMeRank()
|
||||
{
|
||||
return MeRank;
|
||||
}
|
||||
public List<ItemData> rankItemDatas { set; get; }
|
||||
public static string NextQueueKey = "EventRankNextQueueKey";
|
||||
public async Task GetRewards()
|
||||
{
|
||||
if (lastEventRank != null && rankItemDatas == null)
|
||||
{
|
||||
int dropId = 0;
|
||||
GetRewardsRequest request = new GetRewardsRequest { playerId = userService.UserId };
|
||||
string json = await customServerMgr.EventRequest("GetRewards", request);
|
||||
|
||||
GetRewardsResp response = null;
|
||||
if (!string.IsNullOrEmpty(json) && json != "[]")
|
||||
{
|
||||
response = Newtonsoft.Json.JsonConvert.DeserializeObject<GetRewardsResp>(json);
|
||||
}
|
||||
if (response != null && response.success && response.rewardDatas != null && response.rewardDatas.Count > 0)
|
||||
{
|
||||
dropId = response.rewardDatas[0].dropId;
|
||||
eventRankReward = response.rewardDatas[0];
|
||||
RankData[] ranks = response.rewardDatas[0].ranks;
|
||||
GetRankPlayInfo(ranks);
|
||||
RankData rankData = ranks.Where(x => x.isMe).FirstOrDefault();
|
||||
float score = rankData.score;
|
||||
var _tables = GContext.container.Resolve<cfg.Tables>();
|
||||
var fishingEvent1 = _tables.TbFishingEvent.GetOrDefault(response.rewardDatas[0].eventId);
|
||||
if (fishingEvent1 != null)
|
||||
{
|
||||
fishingEvent = fishingEvent1;
|
||||
}
|
||||
var lastEventRank1 = _tables.TbRankInit.GetOrDefault(fishingEvent.RedirectID);
|
||||
if (lastEventRank1 != null)
|
||||
{
|
||||
lastEventRank = lastEventRank1;
|
||||
}
|
||||
if (lastEventRank.UnlockPoint > score)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rankTarget = _tables.TbRankTargets.GetOrDefault(lastEventRank.TargetList[0]);
|
||||
if (rankTarget != null)
|
||||
{
|
||||
lastAllScore = (int)score;
|
||||
lastScore = lastAllScore;
|
||||
while (rankTarget != null && rankTarget.NextTarget > 0 && rankTarget.TokenRequired <= lastScore)
|
||||
{
|
||||
lastScore -= rankTarget.TokenRequired;
|
||||
rankTarget = _tables.TbRankTargets.GetOrDefault(rankTarget.NextTarget);
|
||||
}
|
||||
}
|
||||
|
||||
if (dropId > 0)
|
||||
{
|
||||
var fishingStage = GContext.container.ResolveStage("FishingStage") as FishingStage;
|
||||
if (fishingStage != null)
|
||||
{
|
||||
fishingStage.IsNextQueue = true;
|
||||
fishingStage.NextQueueKey = NextQueueKey;
|
||||
}
|
||||
rankItemDatas = GContext.container.Resolve<PlayerItemData>().AddItemByDrop(dropId, true);
|
||||
if (fishingStage != null)
|
||||
{
|
||||
fishingStage.IsNextQueue = false;
|
||||
fishingStage.NextQueueKey = string.Empty;
|
||||
}
|
||||
#if AGG
|
||||
using (var e = GEvent.GameEvent("last_ranking"))
|
||||
{
|
||||
int playerCount = ranks.Where(x => !x.isBot).Count();
|
||||
int isBotCount = ranks.Length - playerCount;
|
||||
e.AddContent("ranking_tier", response.rewardDatas[0].rankLevel)
|
||||
.AddContent("ranking_points", lastAllScore)
|
||||
.AddContent("get_reward_user", playerCount)
|
||||
.AddContent("get_reward_robot", isBotCount)
|
||||
.AddContent("rank", rankData.rank)
|
||||
.AddContent("dropId", dropId);
|
||||
if (rankItemDatas != null)
|
||||
{
|
||||
for (int i = 0; i < rankItemDatas.Count; i++)
|
||||
{
|
||||
if (rankItemDatas[i].id == 1001)
|
||||
{
|
||||
e.AddContent("reward_hook", rankItemDatas[i].count);
|
||||
}
|
||||
else if (rankItemDatas[i].id == 1002)
|
||||
{
|
||||
e.AddContent("reward_cash", rankItemDatas[i].count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
GContext.container.Resolve<IFaceUIService>().AddFaceUIData(fishingEvent.FaceID, fishingEvent.RedirectID, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public async void UpdateScore(int score)
|
||||
{
|
||||
UpdateScoreResp data = null;
|
||||
UpdateScoreRequest request = new UpdateScoreRequest { playerId = userService.UserId, Score = score };
|
||||
string json = await customServerMgr.EventRequest("UpdateScore", request);
|
||||
if (!string.IsNullOrEmpty(json) && json != "[]")
|
||||
{
|
||||
data = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdateScoreResp>(json);
|
||||
}
|
||||
if (data != null && data.state == ELeaderboardState.Success)
|
||||
{
|
||||
MeRank = data.rank;
|
||||
//更新主界面排名显示
|
||||
}
|
||||
else /*if (data.state == ELeaderboardState.Delay)*/
|
||||
{
|
||||
MeRank = 0;
|
||||
curEventRank = new GetLeaderboardsResp();
|
||||
curEventRank.data = new RankData[] { new RankData { isMe = true, rank = 1 } };
|
||||
}
|
||||
|
||||
GContext.Publish(new EventRankData());
|
||||
}
|
||||
void GetRankPlayInfo(RankData[] RankDatas)
|
||||
{
|
||||
Dictionary<int, cfg.Robot> robots = GContext.container.Resolve<cfg.Tables>().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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user