This commit is contained in:
Michele Scandura 2020-11-18 11:09:26 +00:00
parent 1b910adf55
commit af441e772b
34 changed files with 279 additions and 292 deletions

View File

@ -3,26 +3,17 @@ using System;
namespace Sledgemapper.Api.Commands
{
public abstract class BaseCommand<T> : IRequest<T>
public abstract class BaseCommand<T> : IRequest<T>
{
public double Timestamp { get; private set; }
public string SessionName { get; private set; }
public int UserId { get; set; }
public int SessionId {get;set;}
public int UserId { get; private set; }
public BaseCommand(string sessionName, int userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionName = sessionName;
UserId=userId;
}
public BaseCommand(int sessionId, int userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionId = sessionId;
UserId=userId;
UserId = userId;
}
}
}

View File

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class DeleteOverlayCommand : BaseCommand<bool>
{
public Overlay Overlay { get; private set; }
public DeleteOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
{
Overlay = overlay;
}
}
}

View File

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class DeleteTileCommand : BaseCommand<bool>
{
public Tile Tile { get; private set; }
public DeleteTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
{
Tile = tile;
}
}
}

View File

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class DeleteWallCommand : BaseCommand<bool>
{
public Wall Wall { get; private set; }
public DeleteWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
}
}

View File

@ -1,18 +1,9 @@
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 class GetMapSnapshotCommand : IRequest<Session>
{
public string SessionName { get; private set; }
public GetMapSnapshotCommand(string sessionName)
@ -20,86 +11,4 @@ namespace Sledgemapper.Api.Commands
SessionName = sessionName;
}
}
public class GetMapSnapshotCommandHandler : IRequestHandler<GetMapSnapshotCommand, Sledgemapper.Shared.Entities.Session>
{
private readonly MyDbContext _dbcontext;
public GetMapSnapshotCommandHandler(MyDbContext dbcontext) { _dbcontext = dbcontext; }
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.OrderByDescending(s => s.Timestamp).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;
}
}
}
if (mapUpdates.Any())
{
var newSnapshot = new Snapshot
{
SessionId = session.SessionId,
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
}
return mapSession;
}
}
}

View File

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewOverlayCommand : BaseCommand<bool>
{
public Overlay Overlay { get; private set; }
public NewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
{
Overlay = overlay;
}
}
}

View File

@ -1,5 +1,3 @@
using MediatR;
namespace Sledgemapper.Api.Commands
{
public class NewSessionCommand : BaseCommand<bool>

View File

@ -0,0 +1,13 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewSnapshotCommand : BaseCommand<bool>
{
public Session Session { get; private set; }
public NewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
{
Session = session;
}
}
}

View File

@ -0,0 +1,13 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewTileCommand : BaseCommand<bool>
{
public Tile Tile { get; private set; }
public NewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
{
Tile = tile;
}
}
}

View File

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewWallCommand : BaseCommand<bool>
{
public Wall Wall { get; private set; }
public NewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
}
}

View File

@ -37,48 +37,49 @@ namespace Sledgemapper.Api.Controllers
public async Task Post(string sessionName, [FromBody] Session session)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Send(new SaveNewSnapshotCommand(sessionName, session, userId));
await _mediator.Send(new NewSnapshotCommand(sessionName, session, userId));
}
[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 NewTileCommand(sessionName, tile, userId));
}
[HttpPost("overlay")]
public async Task Post(string sessionName, [FromBody] Overlay overlay)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Publish(new NewOverlayNotification(sessionName, overlay,userId));
await _mediator.Publish(new NewOverlayCommand(sessionName, overlay, userId));
}
[HttpPost("wall")]
public async Task Post(string sessionName, [FromBody] Wall wall)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Publish(new NewWallNotification(sessionName, wall,userId));
await _mediator.Publish(new NewWallCommand(sessionName, wall, userId));
}
[HttpDelete("tile")]
public async Task Delete(string sessionName, [FromBody] Tile tile)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Publish(new DeleteTileNotification(sessionName, tile,userId));
await _mediator.Publish(new DeleteTileCommand(sessionName, tile, userId));
}
[HttpDelete("overlay")]
public async Task Delete(string sessionName, [FromBody] Overlay overlay)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Publish(new DeleteOverlayNotification(sessionName, overlay,userId));
await _mediator.Publish(new DeleteOverlayCommand(sessionName, overlay, userId));
}
[HttpDelete("wall")]
public async Task Delete(string sessionName, [FromBody] Wall wall)
{
var userId = int.Parse(HttpContext.User.Identity.Name);
await _mediator.Publish(new DeleteWallNotification(sessionName, wall,userId));
await _mediator.Publish(new DeleteWallCommand(sessionName, wall, userId));
}

View File

@ -1,6 +1,8 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Notifications;
using System.Linq;
using System.Text.Json;
using System.Threading;
@ -8,13 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteOverlay : INotificationHandler<DeleteOverlayNotification>
public class DeleteOverlayCommandHandler : IRequestHandler<DeleteOverlayCommand, bool>
{
private readonly MyDbContext _dbcontext;
public SaveDeleteOverlay(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteOverlayNotification notification, CancellationToken cancellationToken)
public DeleteOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteOverlayCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
@ -30,6 +34,8 @@ namespace Sledgemapper.Api.Handlers
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteOverlayNotification(session, notification.Overlay, notification.UserId));
return true;
}
}
}

View File

@ -1,4 +1,6 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Linq;
@ -8,13 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteTile : INotificationHandler<DeleteTileNotification>
public class DeleteTileCommandHandler : IRequestHandler<DeleteTileCommand,bool>
{
private readonly MyDbContext _dbcontext;
private readonly MyDbContext _dbcontext;
public SaveDeleteTile(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteTileNotification notification, CancellationToken cancellationToken)
public DeleteTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteTileCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
@ -29,6 +33,8 @@ namespace Sledgemapper.Api.Handlers
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteTileNotification(session, notification.Tile, notification.UserId));
return true;
}
}
}

View File

@ -1,6 +1,8 @@
using MediatR;
using Microsoft.AspNetCore.Http.Features;
using Sledgemapper.Api.Data;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Shared.Entities;
using System.Collections.Generic;
using System.Linq;
@ -11,13 +13,16 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteWall : INotificationHandler<DeleteWallNotification>
public class DeleteWallCommandHandler : IRequestHandler<DeleteWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
public SaveDeleteWall(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteWallNotification notification, CancellationToken cancellationToken)
public DeleteWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteWallCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
@ -32,6 +37,9 @@ namespace Sledgemapper.Api.Handlers
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteWallNotification(session, notification.Wall, notification.UserId));
return true;
}
}
}

View File

@ -0,0 +1,93 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Sledgemapper.Api.Models;
namespace Sledgemapper.Api.Commands
{
public class GetMapSnapshotCommandHandler : IRequestHandler<GetMapSnapshotCommand, Sledgemapper.Shared.Entities.Session>
{
private readonly MyDbContext _dbcontext;
public GetMapSnapshotCommandHandler(MyDbContext dbcontext) { _dbcontext = dbcontext; }
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.OrderByDescending(s => s.Timestamp).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;
}
}
}
if (mapUpdates.Any())
{
var newSnapshot = new Snapshot
{
SessionId = session.SessionId,
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
}
return mapSession;
}
}
}

View File

@ -1,5 +1,6 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Linq;
@ -9,25 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveNewOverlayCommand : BaseCommand<bool>
{
public Overlay Overlay { get; private set; }
public SaveNewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
{
Overlay = overlay;
}
}
public class SaveNewOverlayCommandHandler : IRequestHandler<SaveNewOverlayCommand, bool>
public class NewOverlayCommandHandler : IRequestHandler<NewOverlayCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public NewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewOverlayCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewOverlayCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);

View File

@ -1,34 +1,21 @@
using System.Transactions;
using System.Net.Mail;
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class SaveNewSnapshotCommand : BaseCommand<bool>
{
public Session Session { get; set; }
public SaveNewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
{
Session = session;
}
}
public class SaveNewSnapshotCommandHandler : IRequestHandler<SaveNewSnapshotCommand, bool>
public class NewSnapshotCommandHandler : IRequestHandler<NewSnapshotCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public NewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewSnapshotCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewSnapshotCommand notification, CancellationToken cancellationToken)
{
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);

View File

@ -1,34 +1,25 @@
using System.Transactions;
using System.Net.Mail;
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class SaveNewTileCommand : BaseCommand<bool>
{
public Tile Tile { get; set; }
public SaveNewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
{
Tile = tile;
}
}
public class SaveNewTileCommandHandler : IRequestHandler<SaveNewTileCommand, bool>
public class NewTileCommandHandler : IRequestHandler<NewTileCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public NewTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewTileCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewTileCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);

View File

@ -1,7 +1,8 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using System.Linq;
using System.Text.Json;
using System.Threading;
@ -9,25 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveNewWallCommand : BaseCommand<bool>
{
public Wall Wall { get; private set; }
public SaveNewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
}
public class SaveNewWallCommandHandler : IRequestHandler<SaveNewWallCommand, bool>
public class NewWallCommandHandler : IRequestHandler<NewWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public NewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewWallCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewWallCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View File

@ -1,4 +1,3 @@
using System.Security.AccessControl;
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Linq;
@ -6,11 +5,8 @@ using System.Threading.Tasks;
using System.Collections.Concurrent;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Clients;
using MediatR;
using System;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Authorization;
using Sledgemapper.Api.Models;
using Sledgemapper.Helpers;
@ -45,100 +41,34 @@ namespace SignalRChat.Hubs
public async Task NewTile(string sessionName, Tile tile)
{
// var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// var existingTile = _sessions[sessionName].Map.TryGetValue(tile.ToString(), out var t);
// if (existingTile)
// {
// _sessions[sessionName].Map.TryRemove(t.ToString(), out var rtile);
// }
// _sessions[sessionName].Map.TryAdd(tile.ToString(), tile);
// var jsonString = JsonSerializer.Serialize<Tile>(tile);
// _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
// {
// Operation = "N",
// SessionName = sessionName,
// Type = "T",
// Timestamp = timestamp,
// Object = jsonString
// });
// await _dbcontext.SaveChangesAsync();
await Clients.Group(sessionName).NewTile(tile);
}
public async Task NewWall(string sessionName, Wall tile)
{
// var existingTile = _sessions[sessionName].Walls.TryGetValue(tile.ToString(), out var t);
// if (existingTile)
// {
// _sessions[sessionName].Walls.TryRemove(t.ToString(), out var rtile);
// }
// _sessions[sessionName].Walls.TryAdd(tile.ToString(), tile);
await Clients.Group(sessionName).NewWall(tile);
}
public async Task NewOverlay(string sessionName, Overlay tile)
{
// var timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// var existingTile = _sessions[sessionName].Overlays.TryGetValue(tile.ToString(), out var t);
// if (existingTile)
// {
// _sessions[sessionName].Overlays.TryRemove(t.ToString(), out var rtile);
// }
// _sessions[sessionName].Overlays.TryAdd(tile.ToString(), tile);
// var jsonString = JsonSerializer.Serialize<Overlay>(tile);
// _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
// {
// Operation = "N",
// SessionName = sessionName,
// Type = "O",
// Timestamp = timestamp,
// Object = jsonString
// });
// await _dbcontext.SaveChangesAsync();
await Clients.Group(sessionName).NewOverlay(tile);
}
public async Task DeleteTile(string sessionName, Tile tile)
{
// _sessions[sessionName].Map.TryRemove(tile.ToString(), out var rtile);
await Clients.Group(sessionName).DeleteTile(tile);
}
public async Task DeleteWall(string sessionName, Wall tile)
{
//_sessions[sessionName].Walls.TryRemove(tile.ToString(), out var rtile);
await Clients.Group(sessionName).DeleteWall(tile);
}
public async Task DeleteOverlay(string sessionName, Overlay tile)
{
//_sessions[sessionName].Overlays.TryRemove(tile.ToString(), out var rtile);
await Clients.Group(sessionName).DeleteOverlay(tile);
}
// public async Task<Session> NewSession(string sessionName, string initials)
// {
// var userId = int.Parse(Context.User.Identity.Name);
// // var user = this.Context.GetHttpContext().;
// var session = new Session();
// session.Colors = new List<string>(Colors);
// session.Colors.Shuffle();
// var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[0], Initials = initials };
// session.Players.Add(player);
// _sessions.Add(sessionName, session);
// await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
// return session;
// }
public async Task<Sledgemapper.Shared.Entities.Session> JoinSession(string sessionName)
{
var session = _dbContext.Sessions.FirstOrDefault(s => s.SessionName == sessionName);
@ -169,51 +99,20 @@ namespace SignalRChat.Hubs
{
return null;
}
// if (_sessions.ContainsKey(sessionName))
// {
// var session = _sessions[sessionName];
// var player = new Player { Position = new Tile { X = 0, Y = 0 }, ConnectionId = Context.ConnectionId, Color = session.Colors[session.Players.Count], Initials = initials };
// session.Players.Add(player);
// await Clients.Group(sessionName).NewPlayer(player);
// return session;
// }
// else
// {
// return null;
// }
}
public async Task UpdatePosition(string sessionName, int sessionId, Tile tile)
{
var userId = int.Parse(Context.User.Identity.Name);
//var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionName == sessionName);
var SessionUsers = _dbContext.SessionUsers.Where(m => m.SessionId == 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<Session> Refresh(string sessionName)
// {
// return _sessions[sessionName];
// }
// public async Task Sync(string sessionName, Session map)
// {
// _sessions[sessionName].Map = map.Map;
// _sessions[sessionName].Overlays = map.Overlays;
// _sessions[sessionName].Walls = map.Walls;
// await Clients.Group(sessionName).UpdateMap(_sessions[sessionName]);
// }
public override async Task OnConnectedAsync()
{
var userId = int.Parse(Context.User.Identity.Name);
//var result = _connectedPlayers.AddOrUpdate(Context.ConnectionId, userId,(key, oldValue) => userId);
var userConnection = new UserConnection { ConnectionId = Context.ConnectionId, UserId = userId };
_dbContext.UserConnections.Add(userConnection);
await _dbContext.SaveChangesAsync();

View File

@ -2,7 +2,7 @@ using MediatR;
using Sledgemapper.Shared.Entities;
using System;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public abstract class BaseNotification : INotification
{

View File

@ -1,8 +1,8 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class DeleteOverlayNotification : BaseNotification
public class DeleteOverlayNotification : BaseNotification
{
public Overlay Overlay { get; private set; }
@ -10,5 +10,10 @@ namespace Sledgemapper.Api.Handlers
{
Overlay = overlay;
}
public DeleteOverlayNotification(Models.Session session, Overlay overlay, int userId) : base(session, userId)
{
Overlay = overlay;
}
}
}

View File

@ -1,6 +1,6 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class DeleteTileNotification : BaseNotification
{
@ -10,5 +10,9 @@ namespace Sledgemapper.Api.Handlers
{
Tile = tile;
}
public DeleteTileNotification(Models.Session session, Tile tile, int userId) : base(session, userId)
{
Tile = tile;
}
}
}

View File

@ -1,12 +1,17 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class DeleteWallNotification : BaseNotification
{
public Wall Wall { get; private set; }
public DeleteWallNotification(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
public DeleteWallNotification(Models.Session session, Wall wall, int userId) : base(session, userId)
{
Wall = wall;
}

View File

@ -1,6 +1,6 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class NewOverlayNotification : BaseNotification
{

View File

@ -1,4 +1,4 @@
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class NewSessionNotification : BaseNotification
{

View File

@ -3,7 +3,7 @@
using Sledgemapper.Api.Models;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class NewTileNotification : BaseNotification
{

View File

@ -1,6 +1,6 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Handlers
namespace Sledgemapper.Api.Notifications
{
public class NewWallNotification : BaseNotification
{