117 lines
3.8 KiB
C#
117 lines
3.8 KiB
C#
using MediatR;
|
|
using Microsoft.AspNetCore.Http.Features;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using SignalRChat.Hubs;
|
|
using Sledgemapper.Api.Data;
|
|
using Sledgemapper.Clients;
|
|
using Sledgemapper.Shared.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sledgemapper.Api.Handlers
|
|
{
|
|
public abstract class BaseNotification : INotification
|
|
{
|
|
public double Timestamp { get; private set; }
|
|
public string SessionName { get; private set; }
|
|
|
|
public BaseNotification(string sessionName)
|
|
{
|
|
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
|
SessionName = sessionName;
|
|
}
|
|
}
|
|
|
|
public class NewTileNotification : BaseNotification
|
|
{
|
|
public Tile Tile { get; private set; }
|
|
|
|
public NewTileNotification(string sessionName, Tile tile) : base(sessionName)
|
|
{
|
|
Tile = tile;
|
|
}
|
|
}
|
|
|
|
public class NewOverlayNotification : BaseNotification
|
|
{
|
|
public Overlay Overlay { get; private set; }
|
|
|
|
public NewOverlayNotification(string sessionName, Overlay overlay) : base(sessionName)
|
|
{
|
|
Overlay = overlay;
|
|
}
|
|
}
|
|
|
|
public class SendNewTileMessage : INotificationHandler<NewTileNotification>
|
|
{
|
|
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
|
|
|
public SendNewTileMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
|
|
|
public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
|
|
{
|
|
await _hub.Clients.Groups(notification.SessionName).NewTile(notification.Tile);
|
|
}
|
|
}
|
|
|
|
public class SaveNewTile : INotificationHandler<NewTileNotification>
|
|
{
|
|
private readonly MyDbContext _dbcontext;
|
|
|
|
public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext;
|
|
|
|
public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
|
|
{
|
|
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
|
|
|
|
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
|
{
|
|
Operation = "N",
|
|
SessionName = notification.SessionName,
|
|
Type = "T",
|
|
Timestamp = notification.Timestamp,
|
|
Object = jsonString
|
|
});
|
|
await _dbcontext.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public class SendNewOverlayMessage : INotificationHandler<NewOverlayNotification>
|
|
{
|
|
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
|
|
|
|
public SendNewOverlayMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
|
|
|
|
public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken)
|
|
{
|
|
await _hub.Clients.Groups(notification.SessionName).NewOverlay(notification.Overlay);
|
|
}
|
|
}
|
|
|
|
public class SaveNewOverlay : INotificationHandler<NewOverlayNotification>
|
|
{
|
|
private readonly MyDbContext _dbcontext;
|
|
|
|
public SaveNewOverlay(MyDbContext dbcontext) => _dbcontext = dbcontext;
|
|
|
|
public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken)
|
|
{
|
|
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
|
|
|
|
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
|
{
|
|
Operation = "N",
|
|
SessionName = notification.SessionName,
|
|
Type = "O",
|
|
Timestamp = notification.Timestamp,
|
|
Object = jsonString
|
|
});
|
|
|
|
await _dbcontext.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|