funziona
This commit is contained in:
parent
0a4b8ebeb2
commit
e988f5c310
13 changed files with 135 additions and 72 deletions
|
@ -34,10 +34,10 @@ namespace Sledgemapper.Api.Commands
|
|||
double timestamp;
|
||||
Sledgemapper.Shared.Entities.Session mapSession;
|
||||
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
||||
snapshot = _dbcontext.Snapshots.FirstOrDefault(m => m.SessionId == session.SessionId);
|
||||
snapshot = _dbcontext.Snapshots.OrderByDescending(s => s.Timestamp).FirstOrDefault(m => m.SessionId == session.SessionId);
|
||||
if (snapshot is null)
|
||||
{
|
||||
|
||||
|
||||
timestamp = 0;
|
||||
mapSession = new Shared.Entities.Session();
|
||||
}
|
||||
|
@ -59,11 +59,11 @@ namespace Sledgemapper.Api.Commands
|
|||
mapSession.NewTile(tile, tile.ID);
|
||||
break;
|
||||
case "W":
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
mapSession.NewWall(wall, wall.ID);
|
||||
break;
|
||||
case "O":
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
mapSession.NewOverlay(overlay, overlay.ID);
|
||||
break;
|
||||
|
||||
|
@ -79,24 +79,29 @@ namespace Sledgemapper.Api.Commands
|
|||
mapSession.DeleteTile(tile);
|
||||
break;
|
||||
case "W":
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
|
||||
mapSession.DeleteWall(wall);
|
||||
break;
|
||||
case "O":
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
|
||||
mapSession.DeleteOverlay(overlay);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
var newSnapshot = new Snapshot{
|
||||
SessionId=session.SessionId,
|
||||
Timestamp=mapUpdates.Max(mapSession=>mapSession.Timestamp),
|
||||
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
|
||||
if (mapUpdates.Any())
|
||||
{
|
||||
var newSnapshot = new Snapshot
|
||||
{
|
||||
SessionId = session.SessionId,
|
||||
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
|
||||
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
|
||||
};
|
||||
await _dbcontext.Snapshots.AddAsync(newSnapshot);
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
await _dbcontext.Snapshots.AddAsync(newSnapshot);
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
|
||||
|
||||
return mapSession;
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace Sledgemapper.Api.Controllers
|
|||
{
|
||||
var userId = int.Parse(HttpContext.User.Identity.Name);
|
||||
await _mediator.Send(new SaveNewSnapshotCommand(sessionName, session, userId));
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("tile")]
|
||||
public async Task Post(string sessionName, [FromBody] Tile tile)
|
||||
|
|
|
@ -60,12 +60,16 @@ namespace Sledgemapper.Api.Data
|
|||
modelBuilder.Entity<UserConnection>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.UserConnectionId);
|
||||
entity.HasIndex(e => e.UserId);
|
||||
|
||||
});
|
||||
|
||||
modelBuilder.Entity<SessionUser>().ToTable("SessionUser", "dbo");
|
||||
modelBuilder.Entity<SessionUser>(entity =>
|
||||
{
|
||||
entity.HasKey(e => e.SessionUserId);
|
||||
entity.HasIndex(e => e.SessionId);
|
||||
|
||||
});
|
||||
|
||||
modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo");
|
||||
|
|
|
@ -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)
|
||||
|
|
Binary file not shown.
|
@ -49,7 +49,7 @@ namespace SignalRChat
|
|||
services.AddSignalR();
|
||||
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
services.AddMediatR(typeof(Startup));
|
||||
services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=sledgemapper.db"));
|
||||
services.AddDbContext<MyDbContext>(options => {options.UseSqlite("Data Source=sledgemapper.db"); options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);});
|
||||
|
||||
// services.AddEntityFrameworkSqlite().AddDbContext<MyDbContext>();
|
||||
// configure strongly typed settings objects
|
||||
|
|
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue