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 4f0b16e..96676dd 100644 Binary files a/Sledgemapper.Api/sledgemapper.db and b/Sledgemapper.Api/sledgemapper.db differ 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)