272 lines
10 KiB
C#
272 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using asap.core;
|
|
using tysdk;
|
|
|
|
namespace game
|
|
{
|
|
public class LoginTypeEvt
|
|
{
|
|
public TaskCompletionSource<EAccoutType> loginType = new TaskCompletionSource<EAccoutType>();
|
|
}
|
|
|
|
public class AggUserService : PlayFabUserService
|
|
{
|
|
public AggUserService() { }
|
|
internal AggUserService(PlayerDataStorage.PlayerKVControlledLoadCapability capability) : base(capability) { }
|
|
internal IReadOnlyList<PlayerDataStorage.PlayerKVIdentityChannel> KVLinkedAccounts { get; private set; } =
|
|
Array.Empty<PlayerDataStorage.PlayerKVIdentityChannel>();
|
|
internal override void ResetForKVLoad()
|
|
{
|
|
base.ResetForKVLoad();
|
|
KVLinkedAccounts=Array.Empty<PlayerDataStorage.PlayerKVIdentityChannel>();
|
|
}
|
|
internal override void ReplaceKVLinked(PlayerDataStorage.PlayerKVLoginState state)
|
|
{ KVLinkedAccounts=new List<PlayerDataStorage.PlayerKVIdentityChannel>(state.Linked).AsReadOnly(); }
|
|
protected override string NewPlayerName
|
|
{
|
|
get
|
|
{
|
|
if (TYSdkFacade.TYAccountInfo == null)
|
|
return base.NewPlayerName;
|
|
if (TYSdkFacade.TYAccountInfo.channel == EAccoutType.hwFacebook
|
|
|| TYSdkFacade.TYAccountInfo.channel == EAccoutType.hwGoogle)
|
|
if (!string.IsNullOrEmpty(TYSdkFacade.TYAccountInfo.userName))
|
|
return TYSdkFacade.TYAccountInfo.userName;
|
|
|
|
return base.NewPlayerName;
|
|
}
|
|
}
|
|
|
|
protected override string NewPlayerAvatar
|
|
{
|
|
get
|
|
{
|
|
if (TYSdkFacade.TYAccountInfo == null)
|
|
return base.NewPlayerAvatar;
|
|
if (TYSdkFacade.TYAccountInfo.channel == EAccoutType.hwFacebook
|
|
/*|| TYSdkFacade.TYAccountInfo.channel == EAccoutType.hwGoogle*/)
|
|
if (!string.IsNullOrEmpty(TYSdkFacade.TYAccountInfo.avatar))
|
|
return TYSdkFacade.TYAccountInfo.avatar;
|
|
|
|
return base.NewPlayerAvatar;
|
|
}
|
|
}
|
|
|
|
public override async Task<ELoginStep> Login(Action<int> progress, bool tryLoginByToken = true)
|
|
{
|
|
#if UNITY_EDITOR && !SERVICE_HOST
|
|
if(game.PlayerDataStorage.PlayerKVGameplayLoop.Enabled)
|
|
{
|
|
CustomId=game.PlayerDataStorage.PlayerKVGameplayLoop.Permit.PlayerId;
|
|
OnInitData+=InitData;
|
|
try { return await LoginWithBackend(progress,"kv-loop"); }
|
|
finally { OnInitData-=InitData; }
|
|
}
|
|
#endif
|
|
var backend=CaptureSaveBackend();
|
|
if(RejectSaveBackend(backend)) return ELoginStep.Api;
|
|
var generation = logoutGeneration;
|
|
if (IsLogin)
|
|
{
|
|
progress?.Invoke(3);
|
|
return ELoginStep.Done;
|
|
}
|
|
|
|
UnityEngine.Debug.Log("AggUserService Login");
|
|
|
|
|
|
LoginInfo loginInfo = new LoginInfo();
|
|
if (!TYSdkFacade.IsLoggedIn)
|
|
{
|
|
loginInfo = await TYSdkFacade.Instance.LoginBySns();
|
|
if (generation != logoutGeneration) return ELoginStep.Sdk;
|
|
|
|
if (!loginInfo.isSuccess && tryLoginByToken)
|
|
{
|
|
loginInfo = await TYSdkFacade.Instance.LoginByToken();
|
|
if (generation != logoutGeneration) return ELoginStep.Sdk;
|
|
}
|
|
|
|
if (!loginInfo.isSuccess)
|
|
{
|
|
var evt = new LoginTypeEvt();
|
|
GContext.Publish(evt);
|
|
var loginType = await evt.loginType.Task;
|
|
if (generation != logoutGeneration) return ELoginStep.Sdk;
|
|
loginInfo = await TYSdkFacade.Instance.Login(loginType);
|
|
if (generation != logoutGeneration) return ELoginStep.Sdk;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
loginInfo.isSuccess = true;
|
|
loginInfo.userId = TYSdkFacade.TYAccountInfo.userId;
|
|
}
|
|
|
|
if (!loginInfo.isSuccess) return ELoginStep.Sdk;
|
|
|
|
progress?.Invoke(1);
|
|
|
|
CustomId = loginInfo.StrUserId;
|
|
|
|
|
|
OnInitData += InitData;
|
|
|
|
ELoginStep step;
|
|
try { step = await LoginWithBackend(value => progress?.Invoke(value + 1),backend); }
|
|
finally { OnInitData -= InitData; }
|
|
if (generation != logoutGeneration) return ELoginStep.Sdk;
|
|
if (step != ELoginStep.Done) return step;
|
|
|
|
if (ChannelManager.IsRustoreStore)
|
|
{
|
|
GEvent.SetProp("sdk_channel", "ru_store");
|
|
}
|
|
|
|
GEvent.SetProps(new Dictionary<string, object>(){
|
|
{"pf_id", UserId},
|
|
{"CreateTime",CreateTime.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss")}
|
|
});
|
|
|
|
GEvent.SetUserProps(new Dictionary<string, object>(){
|
|
{"pf_id", UserId},
|
|
{"CreateTime",CreateTime.ToString("yyyy-MM-dd HH:mm:ss")}
|
|
});
|
|
|
|
AvatarToPF(TYSdkFacade.TYAccountInfo);
|
|
|
|
GEvent.OnLogin(UserId, CustomId);
|
|
if (config.Get<bool>("NewlyCreated", false))
|
|
{
|
|
using (var e = GEvent.CompleteRegistrationEvent())
|
|
{
|
|
e.AddContent("login_type", TYSdkFacade.TYAccountInfo.channel.ToString());
|
|
}
|
|
|
|
GEvent.SetProps(new Dictionary<string, object>(){
|
|
{"vip_level", 0}
|
|
});
|
|
|
|
GEvent.SetUserProps(new Dictionary<string, object>(){
|
|
{"vip_level", 0},
|
|
{"total_consumption", 0}
|
|
});
|
|
|
|
if (AFHelper.Instance.GetDLReferer(out var pid, out var uid))
|
|
{
|
|
UnityEngine.Debug.Log("[AggUserService] deeplink referer:" + pid + " " + uid);
|
|
|
|
using (var e = GEvent.GameEvent("deeplink_invited"))
|
|
{
|
|
e.AddContent("referrer_uid", uid);
|
|
e.AddContent("referrer_pid", pid);
|
|
}
|
|
|
|
var refererData = new Dictionary<string, object>()
|
|
{
|
|
{"refererPid", pid},
|
|
{"refererUid", uid},
|
|
{"playerUid", CustomId}
|
|
};
|
|
|
|
_ = customServerMgr.CustomServerPost("friend/RecoreReferer",
|
|
Newtonsoft.Json.JsonConvert.SerializeObject(refererData));
|
|
}
|
|
}
|
|
|
|
using (var e = GEvent.LoginEvent())
|
|
{
|
|
e.AddContent("login_type", TYSdkFacade.TYAccountInfo.channel.ToString());
|
|
e.AddContent("display_name", DisplayName);
|
|
e.AddContent("avatar_url", AvatarUrl);
|
|
e.AddContent("linkedAccout", string.Join('#', TYSdkFacade.TYAccountInfo.linkedAccout));
|
|
}
|
|
|
|
return step;
|
|
}
|
|
|
|
|
|
public override void Logout()
|
|
{
|
|
base.Logout();
|
|
if(IsControlledKVInstance) return;
|
|
TYSdkFacade.Instance.Logout();
|
|
}
|
|
|
|
public override async Task<bool> GetLinkState(EAccoutType accoutType)
|
|
{
|
|
return await TYSdkFacade.Instance.LinkCheck(accoutType);
|
|
}
|
|
|
|
public override async Task<(int code, int userid)> LinkAccount(EAccoutType accoutType)
|
|
{
|
|
var linkResult = await TYSdkFacade.Instance.LinkAccount(accoutType);
|
|
if (linkResult.code == 0)
|
|
{
|
|
var _accountInfo = TYSdkFacade.TYAccountInfo;
|
|
if (_accountInfo != null)
|
|
{
|
|
AvatarToPF(_accountInfo);
|
|
if (_accountInfo.channel == EAccoutType.hwFacebook)
|
|
{
|
|
_ = UpdateAvatarUrl(_accountInfo.avatar);
|
|
}
|
|
}
|
|
}
|
|
return (linkResult.code, linkResult.userId);
|
|
}
|
|
|
|
internal int KVNotificationCount { get; private set; }
|
|
internal override async Task<PlayerDataStorage.PlayerKVGameLoadResult> LoadKVForValidationAsync(PlayerDataStorage.PlayerKVGameLoadCapability capability)
|
|
{
|
|
EnterKVReceiver(ReceiveKVData);
|
|
try { return await base.LoadKVForValidationAsync(capability); }
|
|
finally { ExitKVReceiver(); }
|
|
}
|
|
void ReceiveKVData(IDictionary<string,string> data)
|
|
{
|
|
if(!IsControlledKVInstance || KVLoadingCopy==null || data.Count!=KVLoadingCopy.Count)
|
|
throw new InvalidOperationException("Controlled notification window invalid.");
|
|
KVNotificationCount++;
|
|
}
|
|
private void InitData(IDictionary<string, string> data)
|
|
{
|
|
#if UNITY_EDITOR && !SERVICE_HOST
|
|
// Editor loop has no channel SDK login. M31 already replaced account URLs and linked flags.
|
|
if(PlayerDataStorage.PlayerKVGameplayLoop.Enabled) return;
|
|
#endif
|
|
if (data.TryGetValue(EAccoutType.hwGoogle.ToString(), out var google) && google == "1")
|
|
{
|
|
TYSdkFacade.TYAccountInfo.linkedAccout.Add(EAccoutType.hwGoogle);
|
|
}
|
|
|
|
if (data.TryGetValue(EAccoutType.hwFacebook.ToString(), out var facebook) && facebook == "1")
|
|
{
|
|
TYSdkFacade.TYAccountInfo.linkedAccout.Add(EAccoutType.hwFacebook);
|
|
}
|
|
|
|
if (data.TryGetValue(EAccoutType.Apple.ToString(), out var apple) && apple == "1")
|
|
{
|
|
TYSdkFacade.TYAccountInfo.linkedAccout.Add(EAccoutType.Apple);
|
|
}
|
|
|
|
if (TYSdkFacade.TYAccountInfo.channel != EAccoutType.hwGuest && TYSdkFacade.TYAccountInfo.channel != EAccoutType.none)
|
|
{
|
|
var channel = TYSdkFacade.TYAccountInfo.channel.ToString();
|
|
if (!data.ContainsKey(channel))
|
|
{
|
|
data.Add(channel, "1");
|
|
PlayFabMgr.Instance.UpdateUserDataValue(channel, "1");
|
|
}
|
|
}
|
|
|
|
if (data.TryGetValue("EAccoutUrl", out var data_value))
|
|
{
|
|
EAccoutUrl = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(data_value);
|
|
}
|
|
}
|
|
}
|
|
}
|