getting somewhere
This commit is contained in:
parent
8fdee0cb67
commit
573e9ea7bf
2 changed files with 29 additions and 9 deletions
|
@ -30,6 +30,9 @@ namespace SignalRChat.Hubs
|
||||||
_dbContext = dbContext;
|
_dbContext = dbContext;
|
||||||
_datacontext = datacontext;
|
_datacontext = datacontext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// other colors
|
||||||
|
// #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a
|
||||||
// private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
|
// private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
|
||||||
public List<string> Colors = new List<string>{"CC0000",
|
public List<string> Colors = new List<string>{"CC0000",
|
||||||
"CC3300",
|
"CC3300",
|
||||||
|
@ -151,13 +154,16 @@ namespace SignalRChat.Hubs
|
||||||
var usersSession = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).Select(m => m.UserId).ToList();
|
var usersSession = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).Select(m => m.UserId).ToList();
|
||||||
var players = _datacontext.
|
var players = _datacontext.
|
||||||
Users.
|
Users.
|
||||||
Where(m => usersSession.Contains(m.Id)).
|
Where(m => usersSession.Contains(m.Id)).ToList().
|
||||||
Select(p => new Player
|
//Select((r, index) => new { Place = index, Name = r })
|
||||||
|
Select((p, index) => new Player
|
||||||
{
|
{
|
||||||
Initials = p.Initials,
|
Initials = p.Initials,
|
||||||
UserId = userId,
|
UserId = userId,
|
||||||
|
Color = Colors[index],
|
||||||
Position = new Tile { X = 0, Y = 0 }
|
Position = new Tile { X = 0, Y = 0 }
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
|
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
|
||||||
|
@ -224,12 +230,26 @@ namespace SignalRChat.Hubs
|
||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task OnDisconnectedAsync(Exception exception)
|
public override async Task OnDisconnectedAsync(Exception exception)
|
||||||
{
|
{
|
||||||
_connectedPlayers.TryRemove(Context.ConnectionId, out var userId);
|
var userConnection = _dbContext.UserConnections.FirstOrDefault(m => m.ConnectionId == Context.ConnectionId);
|
||||||
return base.OnDisconnectedAsync(exception);
|
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.
Loading…
Add table
Add a link
Reference in a new issue