really good progress, need to fix tile upload

This commit is contained in:
Michele Scandura 2020-11-17 00:01:21 +00:00
parent 573e9ea7bf
commit 9ef43bb9f6
25 changed files with 280 additions and 112 deletions

View file

@ -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<Sledgemapper.Shared.Entities.Session>
{
public string SessionName { get; private set; }
public GetMapSnapshotCommand(string sessionName)
{
SessionName = sessionName;
}
}
public class GetMapSnapshotCommandHandler : IRequestHandler<GetMapSnapshotCommand, Sledgemapper.Shared.Entities.Session>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public GetMapSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<Sledgemapper.Shared.Entities.Session> 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<Sledgemapper.Shared.Entities.Session>(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<Tile>(mapUpdate.Object);
mapSession.NewTile(tile, tile.ID);
break;
case "W":
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
mapSession.NewWall(wall, wall.ID);
break;
case "O":
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
mapSession.NewOverlay(overlay, overlay.ID);
break;
}
}
else if (mapUpdate.Operation == "D")
{
switch (mapUpdate.Type)
{
case "T":
var tile = JsonSerializer.Deserialize<Tile>(mapUpdate.Object);
mapSession.DeleteTile(tile);
break;
case "W":
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
mapSession.DeleteWall(wall);
break;
case "O":
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)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
return mapSession;
}
}
}

View file

@ -7,41 +7,45 @@ using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Sledgemapper.Api.Handlers; using Sledgemapper.Api.Handlers;
using System.Linq;
namespace Sledgemapper.Api.Commands namespace Sledgemapper.Api.Commands
{ {
public class SaveNewTileCommand:BaseCommand<bool> public class SaveNewTileCommand : BaseCommand<bool>
{ {
public Tile Tile {get;set;} public Tile Tile { get; set; }
public SaveNewTileCommand(string sessionName, Tile tile, int userId):base(sessionName, userId){ public SaveNewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
Tile=tile; {
Tile = tile;
}
} }
}
public class SaveNewTileCommandHandler : IRequestHandler<SaveNewTileCommand, bool> public class SaveNewTileCommandHandler : IRequestHandler<SaveNewTileCommand, bool>
{ {
private readonly MyDbContext _dbcontext; 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<bool> Handle(SaveNewTileCommand notification, CancellationToken cancellationToken) public async Task<bool> Handle(SaveNewTileCommand notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile); var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "N", Operation = "N",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "T", Type = "T",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId = notification.UserId
}); });
await _dbcontext.SaveChangesAsync(); 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; return true;
} }
} }
} }

View file

@ -25,41 +25,53 @@ namespace Sledgemapper.Api.Controllers
return result; return result;
} }
[HttpGet]
public async Task<Session> Get(string sessionName)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
var result = await _mediator.Send(new GetMapSnapshotCommand(sessionName));
return result;
}
[HttpPost("tile")] [HttpPost("tile")]
public async Task Post(string sessionName, [FromBody] Tile tile) public async Task Post(string sessionName, [FromBody] Tile tile)
{ {
var userId = int.Parse(HttpContext.User.Identity.Name); 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")] [HttpPost("overlay")]
public async Task Post(string sessionName, [FromBody] Overlay 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")] [HttpPost("wall")]
public async Task Post(string sessionName, [FromBody] Wall 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")] [HttpDelete("tile")]
public async Task Delete(string sessionName, [FromBody] Tile 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")] [HttpDelete("overlay")]
public async Task Delete(string sessionName, [FromBody] Overlay 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")] [HttpDelete("wall")]
public async Task Delete(string sessionName, [FromBody] Wall 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));
} }

View file

@ -19,6 +19,7 @@ namespace Sledgemapper.Api.Data
public DbSet<Session> Sessions { get; set; } public DbSet<Session> Sessions { get; set; }
public DbSet<UserConnection> UserConnections { get; set; } public DbSet<UserConnection> UserConnections { get; set; }
public DbSet<SessionUser> SessionUsers { get; set; } public DbSet<SessionUser> SessionUsers { get; set; }
public DbSet<Snapshot> Snapshots { get; set; }
public MyDbContext(DbContextOptions options) : base(options) public MyDbContext(DbContextOptions options) : base(options)
{ {
@ -67,6 +68,12 @@ namespace Sledgemapper.Api.Data
entity.HasKey(e => e.SessionUserId); entity.HasKey(e => e.SessionUserId);
}); });
modelBuilder.Entity<Snapshot>().ToTable("Snapshot", "dbo");
modelBuilder.Entity<Snapshot>(entity =>
{
entity.HasKey(e => e.SnapshotId);
});
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }
} }

View file

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using Sledgemapper.Api.Data; using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities; using Sledgemapper.Shared.Entities;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(DeleteOverlayNotification notification, CancellationToken cancellationToken) public async Task Handle(DeleteOverlayNotification notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay); var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "D", Operation = "D",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "O", Type = "O",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId=notification.UserId
}); });
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();

View file

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using Sledgemapper.Api.Data; using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities; using Sledgemapper.Shared.Entities;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(DeleteTileNotification notification, CancellationToken cancellationToken) public async Task Handle(DeleteTileNotification notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile); var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "D", Operation = "D",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "T", Type = "T",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId = notification.UserId
}); });
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();
} }

View file

@ -20,14 +20,16 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(DeleteWallNotification notification, CancellationToken cancellationToken) public async Task Handle(DeleteWallNotification notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall); var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "D", Operation = "D",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "W", Type = "W",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId = notification.UserId
}); });
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();
} }

View file

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using Sledgemapper.Api.Data; using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities; using Sledgemapper.Shared.Entities;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,14 +17,16 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken) public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay); var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "N", Operation = "N",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "O", Type = "O",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId = notification.UserId
}); });
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();

View file

@ -1,31 +1,34 @@
using MediatR; // using MediatR;
using Sledgemapper.Api.Data; // using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities; // using Sledgemapper.Shared.Entities;
using System.Text.Json; // using System.Linq;
using System.Threading; // using System.Text.Json;
using System.Threading.Tasks; // using System.Threading;
// using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers // namespace Sledgemapper.Api.Handlers
{ // {
public class SaveNewTile : INotificationHandler<NewTileNotification> // public class SaveNewTile : INotificationHandler<NewTileNotification>
{ // {
private readonly MyDbContext _dbcontext; // private readonly MyDbContext _dbcontext;
public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext; // public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext;
public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken) // public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
{ // {
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile); // var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
// var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog // _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ // {
Operation = "N", // Operation = "N",
SessionName = notification.SessionName, // SessionId = session.SessionId,
Type = "T", // Type = "T",
Timestamp = notification.Timestamp, // Timestamp = notification.Timestamp,
Object = jsonString // Object = jsonString,
}); // UserId=notification.UserId
await _dbcontext.SaveChangesAsync(); // });
} // await _dbcontext.SaveChangesAsync();
} // }
} // }
// }

View file

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using Sledgemapper.Api.Data; using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities; using Sledgemapper.Shared.Entities;
using System.Linq;
using System.Text.Json; using System.Text.Json;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -16,14 +17,15 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken) public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken)
{ {
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall); var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{ {
Operation = "N", Operation = "N",
SessionName = notification.SessionName, SessionId = session.SessionId,
Type = "W", Type = "W",
Timestamp = notification.Timestamp, Timestamp = notification.Timestamp,
Object = jsonString Object = jsonString,
UserId = notification.UserId,
}); });
await _dbcontext.SaveChangesAsync(); await _dbcontext.SaveChangesAsync();
} }

View file

@ -34,15 +34,15 @@ namespace SignalRChat.Hubs
// other colors // other colors
// #cca300, #20f200, #004011, #00e6d6, #005c73, #0057d9, #d900ca, #660029, #d9003a // #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>{
"CC3300", "#CC0000",
"FFCC00", "#20f200",
"009900", "#FFCC00",
"006666", "#006666",
"0066FF", "#660029",
"0000CC", "#0000CC",
"663399", "#663399",
"CC0099"}; "#CC0099"};
public async Task NewTile(string sessionName, Tile tile) public async Task NewTile(string sessionName, Tile tile)
{ {
@ -140,7 +140,7 @@ namespace SignalRChat.Hubs
// return session; // return session;
// } // }
public async Task<Sledgemapper.Shared.Entities.Session> JoinSession(string sessionName, string initials) public async Task<Sledgemapper.Shared.Entities.Session> JoinSession(string sessionName)
{ {
var session = _dbContext.Sessions.FirstOrDefault(s => s.SessionName == sessionName); var session = _dbContext.Sessions.FirstOrDefault(s => s.SessionName == sessionName);
var userId = int.Parse(Context.User.Identity.Name); var userId = int.Parse(Context.User.Identity.Name);
@ -151,25 +151,30 @@ namespace SignalRChat.Hubs
var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId }; var userSession = new SessionUser { SessionId = session.SessionId, UserId = userId };
_dbContext.SessionUsers.Add(userSession); _dbContext.SessionUsers.Add(userSession);
await _dbContext.SaveChangesAsync(); await _dbContext.SaveChangesAsync();
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)).ToList(). // Where(m => usersSession.Contains(m.Id)).ToList().
//Select((r, index) => new { Place = index, Name = r }) // //Select((r, index) => new { Place = index, Name = r })
Select((p, index) => new Player // Select((p, index) => new Player
{ // {
Initials = p.Initials, // Initials = p.Initials,
UserId = userId, // UserId = userId,
Color = Colors[index], // 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);
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 var newSession = new Sledgemapper.Shared.Entities.Session
{ {
Players = players,
SessionName = sessionName SessionName = sessionName
}; };
@ -196,12 +201,12 @@ namespace SignalRChat.Hubs
public async Task UpdatePosition(string sessionName, Tile tile) public async Task UpdatePosition(string sessionName, Tile tile)
{ {
// var userId = int.Parse(Context.User.Identity.Name); var userId = int.Parse(Context.User.Identity.Name);
// var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName); 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 player = _sessions[sessionName].Players.First(m => m.ConnectionId == Context.ConnectionId); var user = _datacontext.Users.First(u => u.Id == userId);
// player.Position = tile; 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); await Clients.Group(sessionName).PlayerUpdate(player);
} }
// public async Task<Session> Refresh(string sessionName) // public async Task<Session> Refresh(string sessionName)
@ -243,7 +248,7 @@ namespace SignalRChat.Hubs
{ {
var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId); var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId);
await Clients.Group(session.SessionName).PlayerUpdate(null); //send remove player 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); await base.OnDisconnectedAsync(exception);
} }
} }
}

View file

@ -10,10 +10,10 @@ namespace Sledgemapper.Api.Models
public int MapLogId { get; set; } public int MapLogId { get; set; }
[Required] [Required]
public string User{get;set;} public int UserId{get;set;}
[Required] [Required]
public string SessionName { get; set; } public int SessionId { get; set; }
[Required] [Required]
[MaxLength(1)] [MaxLength(1)]

View file

@ -9,6 +9,10 @@ namespace Sledgemapper.Api.Models
[Key] [Key]
public int SnapshotId { get; set; } public int SnapshotId { get; set; }
[Required]
public int SessionId { get; set; }
[Required] [Required]
public string Object { get; set; } public string Object { get; set; }

View file

@ -7,11 +7,13 @@ namespace Sledgemapper.Api.Handlers
{ {
public double Timestamp { get; private set; } public double Timestamp { get; private set; }
public string SessionName { 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(); Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionName = sessionName; SessionName = sessionName;
UserId = userId;
} }
} }
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Overlay Overlay { get; private set; } 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; Overlay = overlay;
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Tile Tile { get; private set; } 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; Tile = tile;
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Wall Wall { get; private set; } 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; Wall = wall;
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Overlay Overlay { get; private set; } 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; Overlay = overlay;
} }

View file

@ -2,7 +2,8 @@ namespace Sledgemapper.Api.Handlers
{ {
public class NewSessionNotification : BaseNotification 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)
{}
} }
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Tile Tile { get; private set; } 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; Tile = tile;
} }

View file

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Handlers
{ {
public Wall Wall { get; private set; } 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; Wall = wall;
} }

Binary file not shown.

View file

@ -58,6 +58,10 @@ namespace Sledgemapper
{ {
p.Position = player.Position; p.Position = player.Position;
} }
else
{
SessionData.Players.Add(player);
}
}); });
Connection.On<Tile>("DeleteTile", (tile) => Connection.On<Tile>("DeleteTile", (tile) =>
@ -87,7 +91,7 @@ namespace Sledgemapper
SessionData.Walls.TryAdd(tile.ToString(), tile); SessionData.Walls.TryAdd(tile.ToString(), tile);
}); });
Connection.On<Overlay>("NewOverlay", (tile) => Connection.On<Overlay>("NewOverlay", (tile) =>
{ {
SessionData.Overlays.Remove(tile.ToString(), out var _); SessionData.Overlays.Remove(tile.ToString(), out var _);
SessionData.Overlays.TryAdd(tile.ToString(), tile); SessionData.Overlays.TryAdd(tile.ToString(), tile);
@ -115,13 +119,13 @@ namespace Sledgemapper
public async Task<bool> Register(RegisterModel registerModel) public async Task<bool> Register(RegisterModel registerModel)
{ {
var result = await Api.Register(registerModel); var result = await Api.Register(registerModel).ConfigureAwait(false);
return result.IsSuccessStatusCode; return result.IsSuccessStatusCode;
} }
public async Task<AuthenticateResponse> Login(AuthenticateModel authenticateModel) public async Task<AuthenticateResponse> Login(AuthenticateModel authenticateModel)
{ {
_authenticateResponse = await Api.Authenticate(authenticateModel); _authenticateResponse = await Api.Authenticate(authenticateModel).ConfigureAwait(false);
return _authenticateResponse; return _authenticateResponse;
} }
@ -143,13 +147,13 @@ namespace Sledgemapper
switch (entity) switch (entity)
{ {
case Tile tile: 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; break;
case Overlay overlay: 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; break;
case Wall wall: 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;
} }
break; break;
@ -158,13 +162,13 @@ namespace Sledgemapper
switch (entity) switch (entity)
{ {
case Tile tile: 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; break;
case Overlay overlay: 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; break;
case Wall wall: 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;
} }
break; break;

View file

@ -12,6 +12,9 @@ namespace Sledgemapper
[Headers("Authorization: Bearer")] [Headers("Authorization: Bearer")]
public interface IMapApi public interface IMapApi
{ {
[Get("/session/{sessionName}")]
Task<Session> Session(string sessionName);
[Post("/session/{sessionName}")] [Post("/session/{sessionName}")]
Task<bool> NewSession(string sessionName); Task<bool> NewSession(string sessionName);

View file

@ -347,7 +347,7 @@ namespace Sledgemapper
{ {
foreach (var player in _sessionData.Players.Copy()) 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), var color = new Color(int.Parse(hexs[0], System.Globalization.NumberStyles.HexNumber),
int.Parse(hexs[1], System.Globalization.NumberStyles.HexNumber), int.Parse(hexs[1], System.Globalization.NumberStyles.HexNumber),
int.Parse(hexs[2], System.Globalization.NumberStyles.HexNumber)); int.Parse(hexs[2], System.Globalization.NumberStyles.HexNumber));
@ -426,13 +426,13 @@ namespace Sledgemapper
var successful = false; var successful = false;
try try
{ {
var result = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text, _authResponse.Initials); var result = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text);
if (result != null) if (result != null)
{ {
_sessionData.Map = result.Map; _sessionData.Map = result.Map;
_sessionData.Walls = result.Walls; _sessionData.Walls = result.Walls;
_sessionData.Overlays = result.Overlays; _sessionData.Overlays = result.Overlays;
_sessionData.Players = result.Players;
_sessionData.MapEntityAdded += OnMapEntityAdded; _sessionData.MapEntityAdded += OnMapEntityAdded;
_sessionData.MapEntityDeleted += OnMapEntityDeleted; _sessionData.MapEntityDeleted += OnMapEntityDeleted;
} }
@ -486,7 +486,7 @@ namespace Sledgemapper
} }
successful = result; successful = result;
var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text, _authResponse.Initials); var result2 = await _communicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.TxtSession.Text);
} }
catch (Exception ex) catch (Exception ex)
@ -688,7 +688,9 @@ namespace Sledgemapper
private async void OnMenuConnectSyncSelected(object sender, EventArgs e) 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) private void OnMenuConnectNewSelected(object sender, EventArgs e)