This commit is contained in:
Michele Scandura 2020-11-17 22:10:52 +00:00
parent 0a4b8ebeb2
commit e988f5c310
13 changed files with 135 additions and 72 deletions

View file

@ -17,11 +17,10 @@ using Sledgemapper.Helpers;
namespace SignalRChat.Hubs
{
[Authorize]
public class SledgemapperHub : Hub<ISledgemapperClient>
{
private static readonly ConcurrentDictionary<int, string> UserColors = new ConcurrentDictionary<int, string>();
private readonly MyDbContext _dbContext;
private readonly DataContext _datacontext;
@ -35,14 +34,14 @@ namespace SignalRChat.Hubs
// #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",
"#20f200",
"#FFCC00",
"#006666",
"#660029",
"#0000CC",
"#663399",
"#CC0099"};
"#e6194B",
"#f58231",
"#3cb44b",
"#000075",
"#911eb4",
"#800000",
"#808000",
"#469990"};
public async Task NewTile(string sessionName, Tile tile)
{
@ -147,37 +146,20 @@ namespace SignalRChat.Hubs
if (session != null)
{
// var newSession = new Session();
var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId };
_dbContext.SessionUsers.Add(userSession);
await _dbContext.SaveChangesAsync();
//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)).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, session.SessionName);
var user = _datacontext.Users.First(u => u.Id == userId);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).OrderBy(m => m.UserId).ToList();
var u = SessionUsers.FirstOrDefault(m => m.UserId == userId);
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = Colors[SessionUsers.IndexOf(u)] };
await Clients.Group(sessionName).NewPlayer(player);
var player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = UserColors[userId] };
await Clients.Group(session.SessionName).NewPlayer(player);
await Clients.Group(session.SessionName).RefreshPlayers();
var newSession = new Sledgemapper.Shared.Entities.Session
{
SessionName = sessionName,
SessionName = session.SessionName,
SessionId = session.SessionId
};
@ -202,7 +184,7 @@ namespace SignalRChat.Hubs
// }
}
public async Task UpdatePosition(string sessionName,int sessionId, Tile tile)
public async Task UpdatePosition(string sessionName, int sessionId, Tile tile)
{
var userId = int.Parse(Context.User.Identity.Name);
//var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName);
@ -235,6 +217,8 @@ namespace SignalRChat.Hubs
var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId };
_dbContext.UserConnections.Add(userConnection);
await _dbContext.SaveChangesAsync();
var availableColor = Colors.Where(m => !UserColors.Values.Contains(m)).First();
UserColors.AddOrUpdate(userId, availableColor, (key, oldValue) => availableColor);
await base.OnConnectedAsync();
}
@ -245,7 +229,7 @@ namespace SignalRChat.Hubs
{
_dbContext.UserConnections.Remove(userConnection);
}
var userSessions = _dbContext.SessionUsers.Where(m => m.UserId == userConnection.UserId).ToList();
{
foreach (var userSession in userSessions)