really good progress, need to fix tile upload
This commit is contained in:
parent
573e9ea7bf
commit
9ef43bb9f6
25 changed files with 280 additions and 112 deletions
107
Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs
Normal file
107
Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -7,41 +7,45 @@ using System.Text.Json;
|
|||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Handlers;
|
||||
using System.Linq;
|
||||
|
||||
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 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>
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
{
|
||||
Operation = "N",
|
||||
SessionName = notification.SessionName,
|
||||
SessionId = session.SessionId,
|
||||
Type = "T",
|
||||
Timestamp = notification.Timestamp,
|
||||
Object = jsonString
|
||||
Object = jsonString,
|
||||
UserId = notification.UserId
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue