getting somewhere

This commit is contained in:
Michele 2020-11-16 00:33:02 +00:00
parent 8fdee0cb67
commit 573e9ea7bf
2 changed files with 29 additions and 9 deletions

View file

@ -30,6 +30,9 @@ namespace SignalRChat.Hubs
_dbContext = dbContext;
_datacontext = datacontext;
}
// other colors
// #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a
// private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
public List<string> Colors = new List<string>{"CC0000",
"CC3300",
@ -151,13 +154,16 @@ namespace SignalRChat.Hubs
var usersSession = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).Select(m => m.UserId).ToList();
var players = _datacontext.
Users.
Where(m => usersSession.Contains(m.Id)).
Select(p => new Player
Where(m => usersSession.Contains(m.Id)).ToList().
//Select((r, index) => new { Place = index, Name = r })
Select((p, index) => new Player
{
Initials = p.Initials,
UserId = userId,
Color = Colors[index],
Position = new Tile { X = 0, Y = 0 }
}).ToList();
await _dbContext.SaveChangesAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
@ -224,12 +230,26 @@ namespace SignalRChat.Hubs
await base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
public override async Task OnDisconnectedAsync(Exception exception)
{
_connectedPlayers.TryRemove(Context.ConnectionId, out var userId);
return base.OnDisconnectedAsync(exception);
}
var userConnection = _dbContext.UserConnections.FirstOrDefault(m => m.ConnectionId == Context.ConnectionId);
if (userConnection != null)
{
_dbContext.UserConnections.Remove(userConnection);
}
var userSessions = _dbContext.SessionUsers.Where(m => m.UserId == userConnection.UserId).ToList();
{
foreach (var userSession in userSessions)
{
var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId);
await Clients.Group(session.SessionName).PlayerUpdate(null); //send remove player
_dbContext.Sessions.Remove(session);
}
ConcurrentDictionary<string, int> _connectedPlayers = new ConcurrentDictionary<string, int>();
}
}
}
await _dbContext.SaveChangesAsync();
await base.OnDisconnectedAsync(exception);
}
}

Binary file not shown.