This commit is contained in:
Michele 2021-01-14 23:34:24 +00:00
parent 24b773d845
commit d3f8fd23f4
21 changed files with 297 additions and 49 deletions

View file

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

View file

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

View file

@ -60,6 +60,18 @@ namespace Sledgemapper.Api.Controllers
await _mediator.Send(new NewNoteCommand(sessionName, note, UserId));
}
[HttpPost("room")]
public async Task Post(string sessionName, [FromBody] Room room)
{
await _mediator.Send(new NewRoomCommand(sessionName, room, UserId));
}
[HttpPost("line")]
public async Task Post(string sessionName, [FromBody] Line line)
{
await _mediator.Send(new NewLineCommand(sessionName, line, UserId));
}
[HttpDelete("tile")]
public async Task Delete(string sessionName, [FromBody] Tile tile)
{

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Notifications;
namespace Sledgemapper.Api.Handlers
{
public class NewLineCommandHandler : IRequestHandler<NewLineCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewLineCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewLineCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Line>(notification.Line);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "L",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewLineNotification(session, notification.Line, notification.UserId));
return true;
}
}
}

View file

@ -0,0 +1,39 @@
using MediatR;
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;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewNoteCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Note>(notification.Note);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "N",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewNoteNotification(session, notification.Note, notification.UserId));
return true;
}
}
}

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Notifications;
namespace Sledgemapper.Api.Handlers
{
public class NewRoomCommandHandler : IRequestHandler<NewRoomCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewRoomCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewRoomCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Room>(notification.Room);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "R",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewRoomNotification(session, notification.Room, notification.UserId));
return true;
}
}
}

View file

@ -36,31 +36,4 @@ namespace Sledgemapper.Api.Handlers
return true;
}
}
public class NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewNoteCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Note>(notification.Note);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "N",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewNoteNotification(session, notification.Note, notification.UserId));
return true;
}
}
}

View file

@ -0,0 +1,22 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Hubs;
namespace Sledgemapper.Api.Handlers
{
public class SendNewLineMessage : INotificationHandler<NewLineNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
public SendNewLineMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
public async Task Handle(NewLineNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewLine(notification.Line);
}
}
}

View file

@ -0,0 +1,22 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Hubs;
namespace Sledgemapper.Api.Handlers
{
public class SendNewNoteMessage : INotificationHandler<NewNoteNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
public SendNewNoteMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
public async Task Handle(NewNoteNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewNote(notification.Note);
}
}
}

View file

@ -0,0 +1,22 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Hubs;
namespace Sledgemapper.Api.Handlers
{
public class SendNewRoomMessage : INotificationHandler<NewRoomNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
public SendNewRoomMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
public async Task Handle(NewRoomNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewRoom(notification.Room);
}
}
}

View file

@ -19,16 +19,4 @@ namespace Sledgemapper.Api.Handlers
await _hub.Clients.Groups(notification.Session.SessionName).NewWall(notification.Wall);
}
}
public class SendNewNoteMessage : INotificationHandler<NewNoteNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
public SendNewNoteMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
public async Task Handle(NewNoteNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewNote(notification.Note);
}
}
}

View file

@ -46,6 +46,16 @@ namespace Sledgemapper.Api.Hubs
await Clients.Group(sessionName).NewTile(tile);
}
public async Task NewRoom(string sessionName, Room room)
{
await Clients.Group(sessionName).NewRoom(room);
}
public async Task NewLine(string sessionName, Line line)
{
await Clients.Group(sessionName).NewLine(line);
}
public async Task NewWall(string sessionName, Wall tile)
{
await Clients.Group(sessionName).NewWall(tile);
@ -113,7 +123,7 @@ namespace Sledgemapper.Api.Hubs
var userId = int.Parse(Context.User.Identity.Name);
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 = UserColors[userId]};
var player = new Player { UserId = userId, Initials = user.Initials, Position = tile, Color = UserColors[userId] };
await Clients.Group(sessionName).PlayerUpdate(player);
}
@ -131,7 +141,7 @@ namespace Sledgemapper.Api.Hubs
public override async Task OnDisconnectedAsync(Exception exception)
{
var userConnection = _dbContext.UserConnections.FirstOrDefault(m => m.ConnectionId == Context.ConnectionId);
var userId=userConnection.UserId;
var userId = userConnection.UserId;
if (userConnection != null)
{
_dbContext.UserConnections.Remove(userConnection);
@ -142,8 +152,8 @@ namespace Sledgemapper.Api.Hubs
foreach (var userSession in userSessions)
{
var session = _dbContext.Sessions.FirstOrDefault(m => m.SessionId == userSession.SessionId);
await Clients.Group(session.SessionName).RemovePlayer(new Player{UserId=userId}); //send remove player
await Clients.Group(session.SessionName).RemovePlayer(new Player { UserId = userId }); //send remove player
_dbContext.SessionUsers.Remove(userSession);
}
}

View file

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Notifications
{
public class NewLineNotification : BaseNotification
{
public Line Line { get; private set; }
public NewLineNotification(Models.Session session, Line line, int userId) : base(session, userId)
{
Line = line;
}
}
}

View file

@ -0,0 +1,14 @@
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Notifications
{
public class NewRoomNotification : BaseNotification
{
public Room Room { get; private set; }
public NewRoomNotification(Models.Session session, Room room, int userId) : base(session, userId)
{
Room = room;
}
}
}

Binary file not shown.

Binary file not shown.

View file

@ -9,6 +9,7 @@ namespace Sledgemapper.Clients
Task NewWall(Wall wall);
Task NewOverlay(Overlay overlay);
Task NewNote(Note note);
Task NewRoom(Room room);
Task DeleteTile(Tile tile);
Task DeleteNote(Note note);
Task DeleteWall(Wall wall);
@ -18,5 +19,6 @@ namespace Sledgemapper.Clients
Task RemovePlayer(Player player);
Task UpdateMap(Session player);
Task RefreshPlayers();
Task NewLine(Line line);
}
}

View file

@ -201,7 +201,7 @@ namespace Sledgemapper.Shared.Entities
Lines.TryAdd(newLine.ToString(), newLine);
//TODO fix this
//OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newTile));
OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newLine));
}
public void NewRoom(Room line)
@ -221,7 +221,7 @@ namespace Sledgemapper.Shared.Entities
Rooms.TryAdd(newLine.ToString(), newLine);
//TODO fix this
//OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newTile));
OnRaiseMapEntityAddedEvent(new MapEntityAddedEventArgs(newLine));
}
}
}

View file

@ -95,6 +95,18 @@ namespace Sledgemapper
SessionData.Map.TryAdd(tile.ToString(), tile);
});
Connection.On<Room>("NewRoom", (room) =>
{
SessionData.Rooms.Remove(room.ToString(), out var _);
SessionData.Rooms.TryAdd(room.ToString(), room);
});
Connection.On<Line>("NewLine", (line) =>
{
SessionData.Lines.Remove(line.ToString(), out var _);
SessionData.Lines.TryAdd(line.ToString(), line);
});
Connection.On("RefreshPlayers", () =>
{
if (!string.IsNullOrWhiteSpace(SessionData.SessionName))
@ -166,7 +178,7 @@ namespace Sledgemapper
{
await Policy
.Handle<ApiException>(ex => ex.StatusCode == HttpStatusCode.RequestTimeout)
.RetryForeverAsync()
//.RetryAsync(Polly.RetrySyntax., async (exception, retryCount) => await Task.Delay(500))
.ExecuteAsync(async () => await call().ConfigureAwait(false))
@ -192,6 +204,12 @@ namespace Sledgemapper
case Note note:
Queue.Enqueue(async () => await Execute(async () => await Api.NewNote(note, SessionData.SessionName).ConfigureAwait(false)));
break;
case Room room:
Queue.Enqueue(async () => await Execute(async () => await Api.NewRoom(room, SessionData.SessionName).ConfigureAwait(false)));
break;
case Line line:
Queue.Enqueue(async () => await Execute(async () => await Api.NewLine(line, SessionData.SessionName).ConfigureAwait(false)));
break;
}
break;

View file

@ -49,5 +49,11 @@ namespace Sledgemapper
[Headers("Authorization")]
[Post("/users/authenticate")]
Task<AuthenticateResponse> Authenticate([Body] AuthenticateModel registerModel);
[Post("/session/{sessionName}/room")]
Task NewRoom(Room room, string sessionName);
[Post("/session/{sessionName}/line")]
Task NewLine(Line line, string sessionName);
}
}

View file

@ -607,9 +607,9 @@ namespace Sledgemapper
GraphicsDevice.SetRenderTarget(null);
Vector2 texelSize = new Vector2((float)(1 / (double)rendertarget.Width), (float)(1 / (double)rendertarget.Height));
outlineShader.Parameters["TexelSize"].SetValue(texelSize);
outlineShader.Parameters["BorderSize"].SetValue((int)(_state.TileSize / 100f *20f));
Vector2 texelSize = new Vector2((float)rendertarget.Width, (float)rendertarget.Height);
outlineShader.Parameters["ImageSize"].SetValue(texelSize);
//outlineShader.Parameters["BorderSize"].SetValue((int)(_state.TileSize / 100f *10f));
outlineShader.Parameters["R"].SetValue(_settings.OverlayTintColor.R / 255.0f);
outlineShader.Parameters["G"].SetValue(_settings.OverlayTintColor.G / 255.0f);