before deleting Rules
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@@ -16,27 +17,24 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
/// <summary>
|
||||
/// Key is userName
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, Guid> Tokens;
|
||||
private readonly ConcurrentDictionary<string, Guid> Tokens;
|
||||
|
||||
public SocketTokenManager()
|
||||
{
|
||||
Tokens = new Dictionary<string, Guid>();
|
||||
Tokens = new ConcurrentDictionary<string, Guid>();
|
||||
}
|
||||
|
||||
public Guid GenerateToken(string userName)
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
Tokens.Remove(userName, out _);
|
||||
|
||||
if (Tokens.ContainsKey(userName))
|
||||
{
|
||||
Tokens.Remove(userName);
|
||||
}
|
||||
Tokens.Add(userName, guid);
|
||||
var guid = Guid.NewGuid();
|
||||
Tokens.TryAdd(userName, guid);
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
Tokens.Remove(userName);
|
||||
Tokens.Remove(userName, out _);
|
||||
});
|
||||
|
||||
return guid;
|
||||
@@ -45,13 +43,12 @@ namespace Gameboard.ShogiUI.Sockets.Managers
|
||||
/// <returns>User name associated to the guid or null.</returns>
|
||||
public string GetUsername(Guid guid)
|
||||
{
|
||||
if (Tokens.ContainsValue(guid))
|
||||
var userName = Tokens.FirstOrDefault(kvp => kvp.Value == guid).Key;
|
||||
if (userName != null)
|
||||
{
|
||||
var username = Tokens.First(kvp => kvp.Value == guid).Key;
|
||||
Tokens.Remove(username);
|
||||
return username;
|
||||
Tokens.Remove(userName, out _);
|
||||
}
|
||||
return null;
|
||||
return userName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user