diff --git a/LANCommander.Playnite.Extension/InstallController.cs b/LANCommander.Playnite.Extension/InstallController.cs index e1f725c..d4510de 100644 --- a/LANCommander.Playnite.Extension/InstallController.cs +++ b/LANCommander.Playnite.Extension/InstallController.cs @@ -62,6 +62,10 @@ namespace LANCommander.PlaynitePlugin { PowerShellRuntime.RunScript(PlayniteGame, ScriptType.Install); PowerShellRuntime.RunScript(PlayniteGame, ScriptType.NameChange, Plugin.Settings.PlayerName); + + var key = Plugin.LANCommander.GetAllocatedKey(game.Id); + + PowerShellRuntime.RunScript(PlayniteGame, ScriptType.KeyChange, $"\"{key}\""); } catch { } diff --git a/LANCommander.Playnite.Extension/LANCommanderClient.cs b/LANCommander.Playnite.Extension/LANCommanderClient.cs index ee90c5d..6be18c5 100644 --- a/LANCommander.Playnite.Extension/LANCommanderClient.cs +++ b/LANCommander.Playnite.Extension/LANCommanderClient.cs @@ -152,6 +152,26 @@ namespace LANCommander.PlaynitePlugin return response.Value; } + public string GetAllocatedKey(Guid id) + { + var macAddress = GetMacAddress(); + + var request = new KeyRequest() + { + GameId = id, + MacAddress = macAddress, + ComputerName = Environment.MachineName, + IpAddress = GetIpAddress(), + }; + + var response = PostRequest($"/api/Keys/GetAllocated/{id}", request); + + if (response == null) + return String.Empty; + + return response.Value; + } + public string GetNewKey(Guid id) { var macAddress = GetMacAddress(); diff --git a/LANCommander/Controllers/Api/KeysController.cs b/LANCommander/Controllers/Api/KeysController.cs index ab3ad97..55f4c71 100644 --- a/LANCommander/Controllers/Api/KeysController.cs +++ b/LANCommander/Controllers/Api/KeysController.cs @@ -26,14 +26,42 @@ namespace LANCommander.Controllers.Api return KeyService.Get(k => k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && k.ClaimedByMacAddress == keyRequest.MacAddress).First(); } + public async Task GetAllocated(Guid id, KeyRequest keyRequest) + { + var existing = KeyService.Get(k => k.Game.Id == id && k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && k.ClaimedByMacAddress == keyRequest.MacAddress).FirstOrDefault(); + + if (existing != null) + return existing; + else + return await AllocateNewKey(id, keyRequest); + } + [HttpPost("Allocate/{id}")] public async Task Allocate(Guid id, KeyRequest keyRequest) { - var existing = KeyService.Get(k => k.Game.Id == id && k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && keyRequest.MacAddress == keyRequest.MacAddress).FirstOrDefault(); + var existing = KeyService.Get(k => k.Game.Id == id && k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && keyRequest.MacAddress == keyRequest.MacAddress).FirstOrDefault(); - if (existing != null) + var availableKey = KeyService.Get(k => k.Game.Id == id) + .Where(k => + (k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && String.IsNullOrWhiteSpace(k.ClaimedByMacAddress)) + || + (k.AllocationMethod == Data.Models.KeyAllocationMethod.UserAccount && k.ClaimedByUser == null)) + .FirstOrDefault(); + + if (availableKey == null && existing != null) + return existing; + else if (availableKey == null) + return null; + else + { await KeyService.Release(existing.Id); + return await KeyService.Allocate(availableKey, keyRequest.MacAddress); + } + } + + private async Task AllocateNewKey(Guid id, KeyRequest keyRequest) + { var availableKey = KeyService.Get(k => k.Game.Id == id) .Where(k => (k.AllocationMethod == Data.Models.KeyAllocationMethod.MacAddress && String.IsNullOrWhiteSpace(k.ClaimedByMacAddress))