Files
ft/Client/Assets/Scripts/Services/PlayerData/KvRuntimeSelection.cs

68 lines
1.9 KiB
C#

using System;
using System.Globalization;
namespace game.KV
{
/// <summary>
/// Immutable process-start selection with canonical positive numeric player identities.
/// </summary>
internal sealed class KvRuntimeSelection
{
internal bool IsKV
{
get;
}
internal Uri Api
{
get;
}
private KvRuntimeSelection(bool isKV, Uri api)
{
IsKV = isKV;
Api = api;
}
internal static string CompiledBackend
{
get
{
#if USE_KV
return "kv";
#else
return "playfab";
#endif
}
}
internal static KvRuntimeSelection CaptureForBuild(string api)
{
var backend = CompiledBackend;
if (backend == "playfab")
{
return new KvRuntimeSelection(false, null);
}
if (!Uri.TryCreate(api, UriKind.Absolute, out var address) ||
(address.Scheme != "http" && address.Scheme != "https") ||
!string.IsNullOrEmpty(address.UserInfo) || !string.IsNullOrEmpty(address.Query) ||
!string.IsNullOrEmpty(address.Fragment))
{
throw new ArgumentException("KV API address is invalid.", nameof(api));
}
return new KvRuntimeSelection(true, new Uri(address.AbsoluteUri.TrimEnd('/') + "/"));
}
internal bool TryAdmitPlayer(string requestedId, out string canonicalId)
{
canonicalId = null;
if (!IsKV || !long.TryParse(requestedId, NumberStyles.None, CultureInfo.InvariantCulture, out var id) ||
id <= 0)
{
return false;
}
canonicalId = id.ToString(CultureInfo.InvariantCulture);
return true;
}
}
}