Get allocated key on install
This commit is contained in:
		
							parent
							
								
									654943f4fd
								
							
						
					
					
						commit
						5f0b69c2c9
					
				
					 3 changed files with 54 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -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 { }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<Key>($"/api/Keys/GetAllocated/{id}", request);
 | 
			
		||||
 | 
			
		||||
            if (response == null)
 | 
			
		||||
                return String.Empty;
 | 
			
		||||
 | 
			
		||||
            return response.Value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public string GetNewKey(Guid id)
 | 
			
		||||
        {
 | 
			
		||||
            var macAddress = GetMacAddress();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<Data.Models.Key> 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<Data.Models.Key> 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<Data.Models.Key> 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))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue