From 9ef43bb9f68e0063a1583a97c15df0042abba9f5 Mon Sep 17 00:00:00 2001 From: Michele Scandura Date: Tue, 17 Nov 2020 00:01:21 +0000 Subject: [PATCH] really good progress, need to fix tile upload --- .../Commands/GetMapSnapshotCommand.cs | 107 ++++++++++++++++++ .../Commands/SaveNewTileCommand.cs | 28 +++-- .../Controllers/SessionController.cs | 26 +++-- Sledgemapper.Api/Data/MyDbContext.cs | 7 ++ .../Handlers/SaveDeleteOverlay.cs | 7 +- Sledgemapper.Api/Handlers/SaveDeleteTile.cs | 7 +- Sledgemapper.Api/Handlers/SaveDeleteWall.cs | 6 +- Sledgemapper.Api/Handlers/SaveNewOverlay.cs | 7 +- Sledgemapper.Api/Handlers/SaveNewTile.cs | 57 +++++----- Sledgemapper.Api/Handlers/SaveNewWall.cs | 8 +- Sledgemapper.Api/Hubs/SledgemapperHub.cs | 66 ++++++----- Sledgemapper.Api/Models/MapLog.cs | 4 +- Sledgemapper.Api/Models/Snapshot.cs | 4 + .../Notifications/BaseNotification.cs | 4 +- .../DeleteOverlayNotification.cs | 2 +- .../Notifications/DeleteTileNotification.cs | 2 +- .../Notifications/DeleteWallNotification.cs | 2 +- .../Notifications/NewOverlayNotification.cs | 2 +- .../Notifications/NewSessionNotification.cs | 5 +- .../Notifications/NewTileNotification.cs | 2 +- .../Notifications/NewWallNotification.cs | 2 +- Sledgemapper.Api/sledgemapper.db | Bin 28672 -> 32768 bytes Sledgemapper/CommunicationManager.cs | 22 ++-- Sledgemapper/IMapApi.cs | 3 + Sledgemapper/Sledgemapper.cs | 12 +- 25 files changed, 280 insertions(+), 112 deletions(-) create mode 100644 Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs diff --git a/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs b/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs new file mode 100644 index 0000000..8ffce12 --- /dev/null +++ b/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs @@ -0,0 +1,107 @@ +using System.Transactions; +using System.Net.Mail; +using MediatR; +using Sledgemapper.Api.Data; +using Sledgemapper.Shared.Entities; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Sledgemapper.Api.Handlers; +using System.Linq; +using Sledgemapper.Api.Models; + +namespace Sledgemapper.Api.Commands +{ + public class GetMapSnapshotCommand : IRequest + { + public string SessionName { get; private set; } + public GetMapSnapshotCommand(string sessionName) + { + SessionName = sessionName; + } + } + + public class GetMapSnapshotCommandHandler : IRequestHandler + { + private readonly MyDbContext _dbcontext; + private readonly IMediator _mediator; + + public GetMapSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } + + public async Task Handle(GetMapSnapshotCommand notification, CancellationToken cancellationToken) + { + Snapshot snapshot; + 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); + if (snapshot is null) + { + + timestamp = 0; + mapSession = new Shared.Entities.Session(); + } + else + { + mapSession = JsonSerializer.Deserialize(snapshot.Object); + timestamp = snapshot.Timestamp; + } + + var mapUpdates = _dbcontext.MapLogs.Where(m => m.SessionId == session.SessionId && m.Timestamp > timestamp).OrderBy(m => m.Timestamp).ToList(); + foreach (var mapUpdate in mapUpdates) + { + if (mapUpdate.Operation == "N") + { + switch (mapUpdate.Type) + { + case "T": + var tile = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.NewTile(tile, tile.ID); + break; + case "W": + var wall = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.NewWall(wall, wall.ID); + break; + case "O": + var overlay = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.NewOverlay(overlay, overlay.ID); + break; + + } + + } + else if (mapUpdate.Operation == "D") + { + switch (mapUpdate.Type) + { + case "T": + var tile = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.DeleteTile(tile); + break; + case "W": + var wall = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.DeleteWall(wall); + break; + case "O": + var overlay = JsonSerializer.Deserialize(mapUpdate.Object); + mapSession.DeleteOverlay(overlay); + break; + } + } + } +var newSnapshot = new Snapshot{ + SessionId=session.SessionId, + Timestamp=mapUpdates.Max(mapSession=>mapSession.Timestamp), + Object = JsonSerializer.Serialize(mapSession) + +}; +await _dbcontext.Snapshots.AddAsync(newSnapshot); +await _dbcontext.SaveChangesAsync(); + + + return mapSession; + } + + + } +} diff --git a/Sledgemapper.Api/Commands/SaveNewTileCommand.cs b/Sledgemapper.Api/Commands/SaveNewTileCommand.cs index 8e8f13e..ec187ca 100644 --- a/Sledgemapper.Api/Commands/SaveNewTileCommand.cs +++ b/Sledgemapper.Api/Commands/SaveNewTileCommand.cs @@ -7,41 +7,45 @@ using System.Text.Json; using System.Threading; using System.Threading.Tasks; using Sledgemapper.Api.Handlers; +using System.Linq; namespace Sledgemapper.Api.Commands { -public class SaveNewTileCommand:BaseCommand -{ - public Tile Tile {get;set;} - public SaveNewTileCommand(string sessionName, Tile tile, int userId):base(sessionName, userId){ - Tile=tile; + public class SaveNewTileCommand : BaseCommand + { + public Tile Tile { get; set; } + public SaveNewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId) + { + Tile = tile; + } } -} public class SaveNewTileCommandHandler : IRequestHandler { private readonly MyDbContext _dbcontext; -private readonly IMediator _mediator; + private readonly IMediator _mediator; - public SaveNewTileCommandHandler(IMediator mediator, MyDbContext dbcontext){ _dbcontext = dbcontext; _mediator= mediator;} + public SaveNewTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } public async Task Handle(SaveNewTileCommand notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Tile); + var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "N", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "T", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId = notification.UserId }); await _dbcontext.SaveChangesAsync(); - await _mediator.Publish(new NewTileNotification(notification.SessionName, notification.Tile)); + await _mediator.Publish(new NewTileNotification(notification.SessionName, notification.Tile, notification.UserId)); return true; } - + } } diff --git a/Sledgemapper.Api/Controllers/SessionController.cs b/Sledgemapper.Api/Controllers/SessionController.cs index 9f6352e..4348310 100644 --- a/Sledgemapper.Api/Controllers/SessionController.cs +++ b/Sledgemapper.Api/Controllers/SessionController.cs @@ -25,41 +25,53 @@ namespace Sledgemapper.Api.Controllers return result; } + [HttpGet] + public async Task Get(string sessionName) + { + var userId = int.Parse(HttpContext.User.Identity.Name); + var result = await _mediator.Send(new GetMapSnapshotCommand(sessionName)); + return result; + } + [HttpPost("tile")] public async Task Post(string sessionName, [FromBody] Tile tile) { var userId = int.Parse(HttpContext.User.Identity.Name); - await _mediator.Send(new SaveNewTileCommand(sessionName, tile, userId)); - } + await _mediator.Send(new SaveNewTileCommand(sessionName, tile, userId)); } [HttpPost("overlay")] public async Task Post(string sessionName, [FromBody] Overlay overlay) { - await _mediator.Publish(new NewOverlayNotification(sessionName, overlay)); + var userId = int.Parse(HttpContext.User.Identity.Name); + await _mediator.Publish(new NewOverlayNotification(sessionName, overlay,userId)); } [HttpPost("wall")] public async Task Post(string sessionName, [FromBody] Wall wall) { - await _mediator.Publish(new NewWallNotification(sessionName, wall)); + var userId = int.Parse(HttpContext.User.Identity.Name); + await _mediator.Publish(new NewWallNotification(sessionName, wall,userId)); } [HttpDelete("tile")] public async Task Delete(string sessionName, [FromBody] Tile tile) { - await _mediator.Publish(new DeleteTileNotification(sessionName, tile)); + var userId = int.Parse(HttpContext.User.Identity.Name); + await _mediator.Publish(new DeleteTileNotification(sessionName, tile,userId)); } [HttpDelete("overlay")] public async Task Delete(string sessionName, [FromBody] Overlay overlay) { - await _mediator.Publish(new DeleteOverlayNotification(sessionName, overlay)); + var userId = int.Parse(HttpContext.User.Identity.Name); + await _mediator.Publish(new DeleteOverlayNotification(sessionName, overlay,userId)); } [HttpDelete("wall")] public async Task Delete(string sessionName, [FromBody] Wall wall) { - await _mediator.Publish(new DeleteWallNotification(sessionName, wall)); + var userId = int.Parse(HttpContext.User.Identity.Name); + await _mediator.Publish(new DeleteWallNotification(sessionName, wall,userId)); } diff --git a/Sledgemapper.Api/Data/MyDbContext.cs b/Sledgemapper.Api/Data/MyDbContext.cs index 653eda4..ea909df 100644 --- a/Sledgemapper.Api/Data/MyDbContext.cs +++ b/Sledgemapper.Api/Data/MyDbContext.cs @@ -19,6 +19,7 @@ namespace Sledgemapper.Api.Data public DbSet Sessions { get; set; } public DbSet UserConnections { get; set; } public DbSet SessionUsers { get; set; } + public DbSet Snapshots { get; set; } public MyDbContext(DbContextOptions options) : base(options) { @@ -67,6 +68,12 @@ namespace Sledgemapper.Api.Data entity.HasKey(e => e.SessionUserId); }); + modelBuilder.Entity().ToTable("Snapshot", "dbo"); + modelBuilder.Entity(entity => + { + entity.HasKey(e => e.SnapshotId); + }); + base.OnModelCreating(modelBuilder); } } diff --git a/Sledgemapper.Api/Handlers/SaveDeleteOverlay.cs b/Sledgemapper.Api/Handlers/SaveDeleteOverlay.cs index 52ee3a9..e2475ca 100644 --- a/Sledgemapper.Api/Handlers/SaveDeleteOverlay.cs +++ b/Sledgemapper.Api/Handlers/SaveDeleteOverlay.cs @@ -1,6 +1,7 @@ using MediatR; using Sledgemapper.Api.Data; using Sledgemapper.Shared.Entities; +using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(DeleteOverlayNotification notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Overlay); + var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "D", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "O", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId=notification.UserId }); await _dbcontext.SaveChangesAsync(); diff --git a/Sledgemapper.Api/Handlers/SaveDeleteTile.cs b/Sledgemapper.Api/Handlers/SaveDeleteTile.cs index be143ec..ebcccac 100644 --- a/Sledgemapper.Api/Handlers/SaveDeleteTile.cs +++ b/Sledgemapper.Api/Handlers/SaveDeleteTile.cs @@ -1,6 +1,7 @@ using MediatR; using Sledgemapper.Api.Data; using Sledgemapper.Shared.Entities; +using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(DeleteTileNotification notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Tile); + var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "D", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "T", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId = notification.UserId }); await _dbcontext.SaveChangesAsync(); } diff --git a/Sledgemapper.Api/Handlers/SaveDeleteWall.cs b/Sledgemapper.Api/Handlers/SaveDeleteWall.cs index 3d4b96d..70820e9 100644 --- a/Sledgemapper.Api/Handlers/SaveDeleteWall.cs +++ b/Sledgemapper.Api/Handlers/SaveDeleteWall.cs @@ -20,14 +20,16 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(DeleteWallNotification notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Wall); + var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "D", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "W", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId = notification.UserId }); await _dbcontext.SaveChangesAsync(); } diff --git a/Sledgemapper.Api/Handlers/SaveNewOverlay.cs b/Sledgemapper.Api/Handlers/SaveNewOverlay.cs index b2980e4..e965d4f 100644 --- a/Sledgemapper.Api/Handlers/SaveNewOverlay.cs +++ b/Sledgemapper.Api/Handlers/SaveNewOverlay.cs @@ -1,6 +1,7 @@ using MediatR; using Sledgemapper.Api.Data; using Sledgemapper.Shared.Entities; +using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Overlay); + var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "N", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "O", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId = notification.UserId }); await _dbcontext.SaveChangesAsync(); diff --git a/Sledgemapper.Api/Handlers/SaveNewTile.cs b/Sledgemapper.Api/Handlers/SaveNewTile.cs index 968e846..842e85e 100644 --- a/Sledgemapper.Api/Handlers/SaveNewTile.cs +++ b/Sledgemapper.Api/Handlers/SaveNewTile.cs @@ -1,31 +1,34 @@ -using MediatR; -using Sledgemapper.Api.Data; -using Sledgemapper.Shared.Entities; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; +// using MediatR; +// using Sledgemapper.Api.Data; +// using Sledgemapper.Shared.Entities; +// using System.Linq; +// using System.Text.Json; +// using System.Threading; +// using System.Threading.Tasks; -namespace Sledgemapper.Api.Handlers -{ - public class SaveNewTile : INotificationHandler - { - private readonly MyDbContext _dbcontext; +// namespace Sledgemapper.Api.Handlers +// { +// public class SaveNewTile : INotificationHandler +// { +// private readonly MyDbContext _dbcontext; - public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext; +// public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext; - public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken) - { - var jsonString = JsonSerializer.Serialize(notification.Tile); +// public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken) +// { +// var jsonString = JsonSerializer.Serialize(notification.Tile); +// var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); - _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog - { - Operation = "N", - SessionName = notification.SessionName, - Type = "T", - Timestamp = notification.Timestamp, - Object = jsonString - }); - await _dbcontext.SaveChangesAsync(); - } - } -} +// _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog +// { +// Operation = "N", +// SessionId = session.SessionId, +// Type = "T", +// Timestamp = notification.Timestamp, +// Object = jsonString, +// UserId=notification.UserId +// }); +// await _dbcontext.SaveChangesAsync(); +// } +// } +// } diff --git a/Sledgemapper.Api/Handlers/SaveNewWall.cs b/Sledgemapper.Api/Handlers/SaveNewWall.cs index 828fd0f..c426346 100644 --- a/Sledgemapper.Api/Handlers/SaveNewWall.cs +++ b/Sledgemapper.Api/Handlers/SaveNewWall.cs @@ -1,6 +1,7 @@ using MediatR; using Sledgemapper.Api.Data; using Sledgemapper.Shared.Entities; +using System.Linq; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -16,14 +17,15 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Wall); - + var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "N", - SessionName = notification.SessionName, + SessionId = session.SessionId, Type = "W", Timestamp = notification.Timestamp, - Object = jsonString + Object = jsonString, + UserId = notification.UserId, }); await _dbcontext.SaveChangesAsync(); } diff --git a/Sledgemapper.Api/Hubs/SledgemapperHub.cs b/Sledgemapper.Api/Hubs/SledgemapperHub.cs index 5fd8c99..d9c2bc3 100644 --- a/Sledgemapper.Api/Hubs/SledgemapperHub.cs +++ b/Sledgemapper.Api/Hubs/SledgemapperHub.cs @@ -34,15 +34,15 @@ namespace SignalRChat.Hubs // other colors // #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a // private static Dictionary _sessions = new Dictionary(); - public List Colors = new List{"CC0000", - "CC3300", - "FFCC00", - "009900", - "006666", - "0066FF", - "0000CC", - "663399", - "CC0099"}; + public List Colors = new List{ + "#CC0000", + "#20f200", + "#FFCC00", + "#006666", + "#660029", + "#0000CC", + "#663399", + "#CC0099"}; public async Task NewTile(string sessionName, Tile tile) { @@ -140,7 +140,7 @@ namespace SignalRChat.Hubs // return session; // } - public async Task JoinSession(string sessionName, string initials) + public async Task JoinSession(string sessionName) { var session = _dbContext.Sessions.FirstOrDefault(s => s.SessionName == sessionName); var userId = int.Parse(Context.User.Identity.Name); @@ -151,25 +151,30 @@ namespace SignalRChat.Hubs 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(); + // 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, 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 player = new Player { UserId = userId, Initials = user.Initials, Position = new Tile { X = 0, Y = 0 }, Color = Colors[SessionUsers.IndexOf(SessionUsers.FirstOrDefault(m => m.UserId == userId))] }; + + await Clients.Group(sessionName).NewPlayer(player); var newSession = new Sledgemapper.Shared.Entities.Session { - Players = players, SessionName = sessionName }; @@ -196,12 +201,12 @@ namespace SignalRChat.Hubs public async Task UpdatePosition(string sessionName, Tile tile) { - // var userId = int.Parse(Context.User.Identity.Name); - // var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName); - - // var player = _sessions[sessionName].Players.First(m => m.ConnectionId == Context.ConnectionId); - // player.Position = tile; - // await Clients.Group(sessionName).PlayerUpdate(player); + var userId = int.Parse(Context.User.Identity.Name); + var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName); + var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == session.SessionId).OrderBy(m => m.UserId).ToList(); + var user = _datacontext.Users.First(u => u.Id == userId); + var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = Colors[SessionUsers.IndexOf(SessionUsers.FirstOrDefault(m => m.UserId == userId))] }; + await Clients.Group(sessionName).PlayerUpdate(player); } // public async Task Refresh(string sessionName) @@ -243,7 +248,7 @@ namespace SignalRChat.Hubs { var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId); await Clients.Group(session.SessionName).PlayerUpdate(null); //send remove player - _dbContext.Sessions.Remove(session); + _dbContext.SessionUsers.Remove(userSession); } } @@ -252,4 +257,5 @@ namespace SignalRChat.Hubs await base.OnDisconnectedAsync(exception); } - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/Sledgemapper.Api/Models/MapLog.cs b/Sledgemapper.Api/Models/MapLog.cs index 331b711..30ecfa1 100644 --- a/Sledgemapper.Api/Models/MapLog.cs +++ b/Sledgemapper.Api/Models/MapLog.cs @@ -10,10 +10,10 @@ namespace Sledgemapper.Api.Models public int MapLogId { get; set; } [Required] - public string User{get;set;} + public int UserId{get;set;} [Required] - public string SessionName { get; set; } + public int SessionId { get; set; } [Required] [MaxLength(1)] diff --git a/Sledgemapper.Api/Models/Snapshot.cs b/Sledgemapper.Api/Models/Snapshot.cs index 0b83e97..39d0721 100644 --- a/Sledgemapper.Api/Models/Snapshot.cs +++ b/Sledgemapper.Api/Models/Snapshot.cs @@ -9,6 +9,10 @@ namespace Sledgemapper.Api.Models [Key] public int SnapshotId { get; set; } + [Required] + public int SessionId { get; set; } + + [Required] public string Object { get; set; } diff --git a/Sledgemapper.Api/Notifications/BaseNotification.cs b/Sledgemapper.Api/Notifications/BaseNotification.cs index eebcb5e..0a82f1c 100644 --- a/Sledgemapper.Api/Notifications/BaseNotification.cs +++ b/Sledgemapper.Api/Notifications/BaseNotification.cs @@ -7,11 +7,13 @@ namespace Sledgemapper.Api.Handlers { public double Timestamp { get; private set; } public string SessionName { get; private set; } + public int UserId { get; private set; } - public BaseNotification(string sessionName) + public BaseNotification(string sessionName, int userId) { Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); SessionName = sessionName; + UserId = userId; } } } diff --git a/Sledgemapper.Api/Notifications/DeleteOverlayNotification.cs b/Sledgemapper.Api/Notifications/DeleteOverlayNotification.cs index 5417131..4c5a707 100644 --- a/Sledgemapper.Api/Notifications/DeleteOverlayNotification.cs +++ b/Sledgemapper.Api/Notifications/DeleteOverlayNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Overlay Overlay { get; private set; } - public DeleteOverlayNotification(string sessionName, Overlay overlay) : base(sessionName) + public DeleteOverlayNotification(string sessionName, Overlay overlay, int userId) : base(sessionName, userId) { Overlay = overlay; } diff --git a/Sledgemapper.Api/Notifications/DeleteTileNotification.cs b/Sledgemapper.Api/Notifications/DeleteTileNotification.cs index 302a093..e134df0 100644 --- a/Sledgemapper.Api/Notifications/DeleteTileNotification.cs +++ b/Sledgemapper.Api/Notifications/DeleteTileNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Tile Tile { get; private set; } - public DeleteTileNotification(string sessionName, Tile tile) : base(sessionName) + public DeleteTileNotification(string sessionName, Tile tile, int userId) : base(sessionName, userId) { Tile = tile; } diff --git a/Sledgemapper.Api/Notifications/DeleteWallNotification.cs b/Sledgemapper.Api/Notifications/DeleteWallNotification.cs index 6dcfa4d..515ce46 100644 --- a/Sledgemapper.Api/Notifications/DeleteWallNotification.cs +++ b/Sledgemapper.Api/Notifications/DeleteWallNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Wall Wall { get; private set; } - public DeleteWallNotification(string sessionName, Wall wall) : base(sessionName) + public DeleteWallNotification(string sessionName, Wall wall, int userId) : base(sessionName, userId) { Wall = wall; } diff --git a/Sledgemapper.Api/Notifications/NewOverlayNotification.cs b/Sledgemapper.Api/Notifications/NewOverlayNotification.cs index 6162d05..fe39ab7 100644 --- a/Sledgemapper.Api/Notifications/NewOverlayNotification.cs +++ b/Sledgemapper.Api/Notifications/NewOverlayNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Overlay Overlay { get; private set; } - public NewOverlayNotification(string sessionName, Overlay overlay) : base(sessionName) + public NewOverlayNotification(string sessionName, Overlay overlay, int userId) : base(sessionName, userId) { Overlay = overlay; } diff --git a/Sledgemapper.Api/Notifications/NewSessionNotification.cs b/Sledgemapper.Api/Notifications/NewSessionNotification.cs index 31aa937..060d51d 100644 --- a/Sledgemapper.Api/Notifications/NewSessionNotification.cs +++ b/Sledgemapper.Api/Notifications/NewSessionNotification.cs @@ -2,7 +2,8 @@ namespace Sledgemapper.Api.Handlers { public class NewSessionNotification : BaseNotification { - public int UserId { get; } - public NewSessionNotification(string sessionName, int userId) : base(sessionName) => UserId = userId; + + public NewSessionNotification(string sessionName, int userId) : base(sessionName, userId) + {} } } \ No newline at end of file diff --git a/Sledgemapper.Api/Notifications/NewTileNotification.cs b/Sledgemapper.Api/Notifications/NewTileNotification.cs index 9ab7c82..433bcd1 100644 --- a/Sledgemapper.Api/Notifications/NewTileNotification.cs +++ b/Sledgemapper.Api/Notifications/NewTileNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Tile Tile { get; private set; } - public NewTileNotification(string sessionName, Tile tile) : base(sessionName) + public NewTileNotification(string sessionName, Tile tile, int userId) : base(sessionName, userId) { Tile = tile; } diff --git a/Sledgemapper.Api/Notifications/NewWallNotification.cs b/Sledgemapper.Api/Notifications/NewWallNotification.cs index f6ecf00..8409cc7 100644 --- a/Sledgemapper.Api/Notifications/NewWallNotification.cs +++ b/Sledgemapper.Api/Notifications/NewWallNotification.cs @@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers { public Wall Wall { get; private set; } - public NewWallNotification(string sessionName, Wall wall) : base(sessionName) + public NewWallNotification(string sessionName, Wall wall, int userId) : base(sessionName, userId) { Wall = wall; } diff --git a/Sledgemapper.Api/sledgemapper.db b/Sledgemapper.Api/sledgemapper.db index 4f0b16e3294220c3f3916a0163d97d64c6c896a6..96676dd6ee9eed605ce081b81c56605c29c3b1b6 100644 GIT binary patch literal 32768 zcmeI4|8Emz9LMi^*Du$ueGtEtZ4S-{Av82(qubb;n5>L)SLoWU>tKbLrDHd@+ApKm z3f&wELrDCIL=yf1f0M)ne~=#}(HIh<3DIaYQTz=|6cQ7SM#Ud|uGh0`dmY@;Xpqm7 zwfEff-1B~(_v?A?y8HHwkA>$85EXOacpM{S@?CTfW1DT*?YkBxk^U?CA!TOmt& zRlHf$MD6P3og~+4Pf)IFu9uyc+TL?~WWPyPApij&00e*l5C8%|00{h_2*_5ed$5x} zy=6W-Bb^LQPK`^lJfF*I%SbXKRg!GOv0xw`Dbv56obitGzc@>stJ=A#iFR|;Z& zhZa@ju;=uG#p?EW=#|bwayl(d$Wnedm(5DY3Z&-RHEX5Pcwlch$m7~fBJ8yDB-Evc zW-w`9JSYTXd?Xs@BNO2;KOBvW$72DK&g0R8Q}s%tvCv2$mf#Nt6MSGI9t}lE%_G4` z+`Fm@Jz*`s>Ok2l9-JigR_b=zJ*RVKtGlaaA(pV*Y`qDm|p9s`-jer%1=tInpxY^BGAlBr^+`Cxatg<2cD$-CMWPE1c?B9ajqW zN~KEFT<5#G(JJfJM7?%4xUX*B8URW=^(t0+b&RpPJ3Hw!rPUrQ{#ada4ZifGn|OM7 zKjrD zeLwdXcZHkdHru|my=fb={${;wowjbW+_an}A`pN85C8%|;4vT|VjIQL2rZe}NPG!T zVt-G+7bmd4&x=F*upbxZ(^7XY_TpHsp#G})yS=#jkDb4JnJ#{D>i0$QxdtVoL6nG| z0j1^Z8IfEw^s#Ah`nM(aDZ zO~j)C;_|(O*x4GTyZ6P81|^la!8Uyy65Cs&^udSXrUs?;^qo9=Np!bHY1e@GjHYDL z`cCDv#wh4%-0w1{wu?`&;{s~+*m)+jB#D{gF1Qv1#zr$wLmgmEQ3 zcZ>VQ4Xsfsjf=>*lHMpNeJ!|%5~m!;YlqXG=7$0uq15^B1!rr7t_W?0g>-PI&V430 zS|fB(uGj%~BZ9d#LfZtk zUT8fBfYj?)U|J&d+9!g^u#g_3m&*R2mk>f00e*l z5C8%|00;m9AOHk_01)`^2sq447pFaf<1jIuwDvrRon_kT%7Y4|eRi&#ff#11wu$m^ z0=toBJhT$4K2gLf_y1XRn?iq}pV3_sfdB-601yBIKmZ5;0U!VbfB+Bx0zd!=Jdy;= zEK4D7(ab1|q}ik_PMB$Baa=wBzeAxv(J$l~fIE+*L1;7(00KY&2mk>f00e*l5C8%| z00;m9AV5*d1pu03l?wng$0!#7XwJlv3jiFaod46VO%(Zq00e*l5C8%|00;m9AOHk_ z01yBIk0F5-iMCMm`4`OWk}`Q7_AlWc-&CKnRA%}yJPV2!_w-has}q(On3RVU@2?go z(=HgBXNVNqo6`x;X})W5P{uKl0lYV8J!FMILfWI8RA`bjkUij+?$ zOO>^dd?=ZopTYjwWLlQIc$8#Gd6IebMK2!CrE^-m@~S|2`bK}Ym`+#bUQ+M>U!c%! zbOT*R7aqf*4wiuc5C8%|00;m9AOHk_01yBIKmZ8*>jXN;t$J=SwJ>w+$bq@x`90}) zX{3KV6z)IJ9q1tU@43O~?qaAfHk=>no$v*xq*zIq*gFvDAUE~7!NlR=>7K!eFd9ou zrlLhTT%64(0v*bo{=uG^#XXY)zQxQ~F;+;-B?^ZIV)B@sIAqZe6#5C>N57H?1Rwwe tfB+Bx0zd!=00AHX1b_e#00KbZ(IsGJ5WPrV0YG$;QNB-@Scbg+|2IScO-TR% delta 765 zcmYjPOKTHR6rOu0Gs&Gab6P=46TwF}7A)l1X2i|p(PWyYNo>-zaZzYHH1UzNnMYzb zt)RHmmRb7`ikpZa6gL(Ph){K--Kf}&2!aTruDsI{^lr|_d3@)b@7&Ezy!jDro%T4H ze~;U~aH*en7Z~xGxaN7#_r(3&UFe)W^IpIjZ(R{akkh&&WDsY)7S1Ey`Xh8ZkNN~m zE+XgP&=9(vvtEdIty_JA)`&Q4-2)Rd{hfi;cd)&6PV~TMhJ{@Wuiyvlb_V5_8#sb^JAxv;DCFnv9=HyYf&F6wW27beL_yV8c0N%h$*rTjF zFvqzZ3?dxoJTg2_4RTvhvqZsv0pBP;vtWoclD(Wzu;b;V0nAyQ>*qPdNK2e40X a@)Z1pJw9%P>!GltheB)*1+kMozVKh$*StFb diff --git a/Sledgemapper/CommunicationManager.cs b/Sledgemapper/CommunicationManager.cs index 7e889de..c7c89b8 100644 --- a/Sledgemapper/CommunicationManager.cs +++ b/Sledgemapper/CommunicationManager.cs @@ -58,6 +58,10 @@ namespace Sledgemapper { p.Position = player.Position; } + else + { + SessionData.Players.Add(player); + } }); Connection.On("DeleteTile", (tile) => @@ -87,7 +91,7 @@ namespace Sledgemapper SessionData.Walls.TryAdd(tile.ToString(), tile); }); - Connection.On("NewOverlay", (tile) => + Connection.On("NewOverlay", (tile) => { SessionData.Overlays.Remove(tile.ToString(), out var _); SessionData.Overlays.TryAdd(tile.ToString(), tile); @@ -115,13 +119,13 @@ namespace Sledgemapper public async Task Register(RegisterModel registerModel) { - var result = await Api.Register(registerModel); + var result = await Api.Register(registerModel).ConfigureAwait(false); return result.IsSuccessStatusCode; } public async Task Login(AuthenticateModel authenticateModel) { - _authenticateResponse = await Api.Authenticate(authenticateModel); + _authenticateResponse = await Api.Authenticate(authenticateModel).ConfigureAwait(false); return _authenticateResponse; } @@ -143,13 +147,13 @@ namespace Sledgemapper switch (entity) { case Tile tile: - Queue.Enqueue(async () => await Execute(async () => await Api.NewTile(tile, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.NewTile(tile, SessionData.SessionName).ConfigureAwait(false))); break; case Overlay overlay: - Queue.Enqueue(async () => await Execute(async () => await Api.NewOverlay(overlay, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.NewOverlay(overlay, SessionData.SessionName).ConfigureAwait(false))); break; case Wall wall: - Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName).ConfigureAwait(false))); break; } break; @@ -158,13 +162,13 @@ namespace Sledgemapper switch (entity) { case Tile tile: - Queue.Enqueue(async () => await Execute(async () => await Api.DeleteTile(tile, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.DeleteTile(tile, SessionData.SessionName).ConfigureAwait(false))); break; case Overlay overlay: - Queue.Enqueue(async () => await Execute(async () => await Api.DeleteOverlay(overlay, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.DeleteOverlay(overlay, SessionData.SessionName).ConfigureAwait(false))); break; case Wall wall: - Queue.Enqueue(async () => await Execute(async () => await Api.DeleteWall(wall, SessionData.SessionName))); + Queue.Enqueue(async () => await Execute(async () => await Api.DeleteWall(wall, SessionData.SessionName).ConfigureAwait(false))); break; } break; diff --git a/Sledgemapper/IMapApi.cs b/Sledgemapper/IMapApi.cs index eafac08..4d7e268 100644 --- a/Sledgemapper/IMapApi.cs +++ b/Sledgemapper/IMapApi.cs @@ -12,6 +12,9 @@ namespace Sledgemapper [Headers("Authorization: Bearer")] public interface IMapApi { + [Get("/session/{sessionName}")] + Task Session(string sessionName); + [Post("/session/{sessionName}")] Task NewSession(string sessionName); diff --git a/Sledgemapper/Sledgemapper.cs b/Sledgemapper/Sledgemapper.cs index 80072b2..295ff34 100644 --- a/Sledgemapper/Sledgemapper.cs +++ b/Sledgemapper/Sledgemapper.cs @@ -347,7 +347,7 @@ namespace Sledgemapper { foreach (var player in _sessionData.Players.Copy()) { - var hexs = player.Color.Split(2).ToArray(); + var hexs = player.Color.TrimStart('#').Split(2).ToArray(); var color = new Color(int.Parse(hexs[0], System.Globalization.NumberStyles.HexNumber), int.Parse(hexs[1], System.Globalization.NumberStyles.HexNumber), int.Parse(hexs[2], System.Globalization.NumberStyles.HexNumber)); @@ -426,13 +426,13 @@ namespace Sledgemapper var successful = false; try { - var result = await _communicationManager.Connection?.InvokeAsync("JoinSession", localContent.TxtSession.Text, _authResponse.Initials); + var result = await _communicationManager.Connection?.InvokeAsync("JoinSession", localContent.TxtSession.Text); if (result != null) { _sessionData.Map = result.Map; _sessionData.Walls = result.Walls; _sessionData.Overlays = result.Overlays; - _sessionData.Players = result.Players; + _sessionData.MapEntityAdded += OnMapEntityAdded; _sessionData.MapEntityDeleted += OnMapEntityDeleted; } @@ -486,7 +486,7 @@ namespace Sledgemapper } successful = result; - var result2 = await _communicationManager.Connection?.InvokeAsync("JoinSession", localContent.TxtSession.Text, _authResponse.Initials); + var result2 = await _communicationManager.Connection?.InvokeAsync("JoinSession", localContent.TxtSession.Text); } catch (Exception ex) @@ -688,7 +688,9 @@ namespace Sledgemapper private async void OnMenuConnectSyncSelected(object sender, EventArgs e) { - await _communicationManager.Connection?.InvokeAsync("Sync", _sessionData.SessionName, _sessionData); + // await _communicationManager.Connection?.InvokeAsync("Sync", _sessionData.SessionName, _sessionData); + var serverMap = await _communicationManager.Api.Session(_sessionData.SessionName); + } private void OnMenuConnectNewSelected(object sender, EventArgs e)