diff --git a/Source/Services/RPSLS.Game.Multiplayer/Config/MultiplayerSettings.cs b/Source/Services/RPSLS.Game.Multiplayer/Config/MultiplayerSettings.cs index ed3f82e..acdfed9 100644 --- a/Source/Services/RPSLS.Game.Multiplayer/Config/MultiplayerSettings.cs +++ b/Source/Services/RPSLS.Game.Multiplayer/Config/MultiplayerSettings.cs @@ -10,5 +10,6 @@ public TokenSettings Token { get; set; } = new TokenSettings(); public int GameStatusUpdateDelay { get; set; } = 1000; public int GameStatusMaxWait { get; set; } = 60; + public int EntityTokenExpirationMinutes { get; set; } = 60; } } diff --git a/Source/Services/RPSLS.Game.Multiplayer/Models/Token.cs b/Source/Services/RPSLS.Game.Multiplayer/Models/Token.cs new file mode 100644 index 0000000..9d7855b --- /dev/null +++ b/Source/Services/RPSLS.Game.Multiplayer/Models/Token.cs @@ -0,0 +1,28 @@ +using System; + +namespace RPSLS.Game.Multiplayer.Models +{ + public class Token + { + private readonly int _maxTokenLifeMinutes; + private readonly DateTime _creationTimeStamp; + + public Token(string value, int maxTokenLifeMinutes) + { + Value = value; + _maxTokenLifeMinutes = maxTokenLifeMinutes; + _creationTimeStamp = DateTime.UtcNow; + } + + public string Value { get; private set; } + + public bool IsExpired + { + get + { + var tokenLife = DateTime.UtcNow.Subtract(_creationTimeStamp); + return tokenLife.Minutes > _maxTokenLifeMinutes; + } + } + } +} diff --git a/Source/Services/RPSLS.Game.Multiplayer/Services/PlayFabService.cs b/Source/Services/RPSLS.Game.Multiplayer/Services/PlayFabService.cs index 9562884..a978649 100644 --- a/Source/Services/RPSLS.Game.Multiplayer/Services/PlayFabService.cs +++ b/Source/Services/RPSLS.Game.Multiplayer/Services/PlayFabService.cs @@ -22,6 +22,8 @@ namespace RPSLS.Game.Multiplayer.Services private readonly ILogger _logger; private readonly MultiplayerSettings _settings; + private Token _entityToken = null; + public bool HasCredentials { get => !string.IsNullOrWhiteSpace(_settings.Title) && !string.IsNullOrWhiteSpace(_settings.SecretKey); } public PlayFabService(ILogger logger, IOptions settings) @@ -43,6 +45,13 @@ namespace RPSLS.Game.Multiplayer.Services public async Task GetEntityToken(string userTitleId = null) { + if (_entityToken?.Value != null && !_entityToken.IsExpired) + { + return _entityToken.Value; + } + + PlayFabAuthenticationAPI.ForgetAllCredentials(); + var tokenRequestBuilder = new GetEntityTokenRequestBuilder(); if (!string.IsNullOrWhiteSpace(userTitleId)) { @@ -52,7 +61,10 @@ namespace RPSLS.Game.Multiplayer.Services var entityTokenResult = await Call( PlayFabAuthenticationAPI.GetEntityTokenAsync, tokenRequestBuilder); - return entityTokenResult.EntityToken; + + _entityToken = new Token(entityTokenResult.EntityToken, _settings.EntityTokenExpirationMinutes); + + return _entityToken.Value; } public async Task CreateTicket(string username, string token) @@ -154,7 +166,7 @@ namespace RPSLS.Game.Multiplayer.Services .WithStats(WinsStat) .WithLimits(0, _settings.Leaderboard.Top)); - var players = new List(); + var players = new List(); foreach (var entry in leaderboardResult.Leaderboard) { var isTwitterUser = !(entry.DisplayName?.StartsWith("$") ?? false);