112 lines
4.6 KiB
C#
112 lines
4.6 KiB
C#
using MediatR;
|
|
using Sledgemapper.Api.Infrastructure.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, Shared.Entities.Session>
|
|
{
|
|
private readonly SledgemapperDbContext _dbcontext;
|
|
|
|
public GetMapSnapshotCommandHandler(SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; }
|
|
|
|
public async Task<Shared.Entities.Session> Handle(GetMapSnapshotCommand notification, CancellationToken cancellationToken)
|
|
{
|
|
Snapshot snapshot;
|
|
double timestamp;
|
|
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<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;
|
|
case "N":
|
|
var note = JsonSerializer.Deserialize<Note>(mapUpdate.Object);
|
|
mapSession.NewNote(note);
|
|
break;
|
|
case "L":
|
|
var line = JsonSerializer.Deserialize<Line>(mapUpdate.Object);
|
|
mapSession.NewLine(line);
|
|
break;
|
|
case "R":
|
|
var room = JsonSerializer.Deserialize<Room>(mapUpdate.Object);
|
|
mapSession.NewRoom(room);
|
|
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;
|
|
case "N":
|
|
var note = JsonSerializer.Deserialize<Note>(mapUpdate.Object);
|
|
mapSession.DeleteNote(note);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (mapUpdates.Any())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|