wiring up map updates
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michele Scandura 2021-09-16 16:34:44 +01:00
parent e18cbb157c
commit 155cb4ea9a
13 changed files with 99 additions and 58 deletions

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using MediatR;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class GetCampaignMapsCommand : IRequest<List<Session>>
{
public double Timestamp { get; private set; }
public string CampaignName { get; private set; }
public string UserId { get; private set; }
public GetCampaignMapsCommand(string campaingName, string userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
CampaignName = campaingName;
UserId = userId;
}
}
}

View File

@ -10,6 +10,7 @@ namespace Sledgemapper.Api.Commands
public double Timestamp { get; private set; }
public string CampaignName { get; private set; }
public string UserId { get; private set; }
public GetCampaignPlayersCommand(string campaingName, string userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
@ -17,18 +18,4 @@ namespace Sledgemapper.Api.Commands
UserId = userId;
}
}
public class GetCampaignMapsCommand : IRequest<List<Session>>
{
public double Timestamp { get; private set; }
public string CampaignName { get; private set; }
public string UserId { get; private set; }
public GetCampaignMapsCommand(string campaingName, string userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
CampaignName = campaingName;
UserId = userId;
}
}
}

View File

@ -6,7 +6,7 @@ namespace Sledgemapper.Api.Commands
{
public Overlay Overlay { get; private set; }
public NewOverlayCommand(string sessionName, Overlay overlay, string userId) : base(sessionName, userId)
public NewOverlayCommand(string campaignId, string mapId, Overlay overlay, string userId) : base(campaignId, mapId, userId)
{
Overlay = overlay;
}

View File

@ -40,7 +40,7 @@ namespace Sledgemapper.Api.Controllers
[HttpPost("overlay")]
public async Task Post(string campaign, string mapName, [FromBody] Overlay overlay)
{
await _mediator.Send(new NewOverlayCommand(mapName, overlay, UserId));
await _mediator.Send(new NewOverlayCommand(campaign, mapName, overlay, UserId));
}
[HttpPost("wall")]

View File

@ -0,0 +1,43 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using Sledgemapper.Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class GetCampaignMapsCommandHandler : IRequestHandler<GetCampaignMapsCommand, List<Session>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignMapsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Session>> Handle(GetCampaignMapsCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignName == command.CampaignName && campaign.OwnerId == command.UserId).Include(campaign => campaign.Maps).FirstAsync();
var maps = campaign.Maps.Select(session => new Session { SessionName = session.SessionName, SessionId = session.SessionId }).ToList();
return maps;
}
catch (Exception ex)
{
}
return null;
}
}
}

View File

@ -40,36 +40,4 @@ namespace Sledgemapper.Api.Handlers
return null;
}
}
public class GetCampaignMapsCommandHandler : IRequestHandler<GetCampaignMapsCommand, List<Session>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignMapsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Session>> Handle(GetCampaignMapsCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignName == command.CampaignName && campaign.OwnerId == command.UserId).Include(campaign => campaign.Maps).FirstAsync();
var maps = campaign.Maps.Select(session => new Session { SessionName = session.SessionName, SessionId = session.SessionId }).ToList();
return maps;
}
catch (Exception ex)
{
}
return null;
}
}
}

View File

@ -7,6 +7,8 @@ using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
namespace Sledgemapper.Api.Handlers
{
@ -18,21 +20,32 @@ namespace Sledgemapper.Api.Handlers
public NewOverlayCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewOverlayCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewOverlayCommand command, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize(notification.Overlay);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == new Guid(command.Campaign) && campaign.OwnerId == command.UserId).Include(campaign => campaign.Maps).FirstAsync();
var maps = campaign.Maps.Any(s => s.SessionId == int.Parse(command.SessionName));
if (!maps)
{
throw new Exception("Unauthorized");
}
var jsonString = JsonSerializer.Serialize(command.Overlay);
var session = _dbcontext.Sessions.First(m => m.SessionId == int.Parse(command.SessionName));
_dbcontext.MapLogs.Add(new Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "O",
Timestamp = notification.Timestamp,
Timestamp = command.Timestamp,
Object = jsonString,
UserId = notification.UserId,
UserId = command.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewOverlayNotification(session, notification.Overlay, notification.UserId));
await _mediator.Publish(new NewOverlayNotification(session, command.Overlay, command.UserId));
return true;
}
}

View File

@ -199,7 +199,7 @@ namespace Sledgemapper
Queue.Enqueue(async () => await Execute(async () => await Api.NewTile(tile, SessionData.SessionName).ConfigureAwait(false)));
break;
case Overlay overlay:
Queue.Enqueue(async () => await Execute(async () => await Api.NewOverlay(overlay, SessionData.SessionName).ConfigureAwait(false)));
Queue.Enqueue(async () => await Execute(async () => await Api.NewOverlay(overlay,State.Instance.CampaignId.ToString(), State.Instance.MapId.ToString()).ConfigureAwait(false)));
break;
case Wall wall:
Queue.Enqueue(async () => await Execute(async () => await Api.NewWall(wall, SessionData.SessionName).ConfigureAwait(false)));

View File

@ -20,8 +20,8 @@ namespace Sledgemapper
[Post("/session/{sessionName}/tile")]
Task NewTile([Body] Tile tile, string sessionName);
[Post("/session/{sessionName}/overlay")]
Task NewOverlay([Body] Overlay overlay, string sessionName);
[Post("/map/{campaignId}/{mapId}/overlay")]
Task NewOverlay([Body] Overlay overlay, string campaignId, string mapId);
[Post("/session/{sessionName}/note")]
Task NewNote([Body] Note note, string sessionName);

View File

@ -60,6 +60,8 @@ namespace Sledgemapper
public bool ShowCellNumbers { get; set; }
public Vector3 ViewportCenter { get; set; }
public string MapName { get; internal set; }
public Guid CampaignId { get; internal set; }
public int MapId { get; internal set; }
//public State()
//{

View File

@ -57,6 +57,7 @@ namespace Sledgemapper.UI
var item = sender as ListItem;
State.Instance.CampaignName = item.ItemName.Text;
State.Instance.CampaignId = (Guid)item.Tag;
var list = item.Parent as Grid;
for (var i = 0; i < list.ChildrenCount; i++)
{
@ -74,7 +75,7 @@ namespace Sledgemapper.UI
{
var item = new ListItem();
item.ItemName.Text = campaign.Name;
item.Tag = campaign.Id;
item.TouchUp += OnCampaignSelected;
StackCampaignsList.AddChild(item);
}

View File

@ -73,6 +73,7 @@ namespace Sledgemapper.UI
private void OnCampaignSelectedMessage(CampaignSelectedMessage obj)
{
lblCampaign.Text = State.Instance.CampaignName;
MenuMapNew.Enabled = true;
MenuMapOpen.Enabled = true;
MenuCampaignPlayers.Enabled = true;

View File

@ -2,6 +2,7 @@
using Myra.Graphics2D.Brushes;
using Myra.Graphics2D.UI;
using Sledgemapper.Messages;
using Sledgemapper.Shared.Entities;
using System;
using System.Threading.Tasks;
using TinyMessenger;
@ -14,6 +15,7 @@ namespace Sledgemapper.UI
private TinyMessengerHub Messenger { get; }
private string _selectedMap;
private int _selectedMapId;
public MapList(CommunicationManager communicationManager, TinyMessengerHub messenger)
{
CommunicationManager = communicationManager;
@ -32,6 +34,7 @@ namespace Sledgemapper.UI
private void BtnLoadCampaign_Click(object sender, EventArgs e)
{
State.Instance.MapName = _selectedMap;
State.Instance.MapId = _selectedMapId;
var map = CommunicationManager.Api.GetMap(State.Instance.CampaignName, State.Instance.MapName);
Messenger.Publish(new MapOpenedMessage(this) { MapName = State.Instance.MapName });
this.GetContainingWindow().Close();
@ -44,6 +47,7 @@ namespace Sledgemapper.UI
{
var item = new ListItem();
item.ItemName.Text = campaign.SessionName;
item.Tag = campaign.SessionId;
item.TouchUp += OnMapSelected;
StackCampaignsList.AddChild(item);
}
@ -62,6 +66,7 @@ namespace Sledgemapper.UI
}
item.Background = new SolidBrush(Settings.Instance.OverlayTintColor);
_selectedMap = item.ItemName.Text;
_selectedMapId = (int)item.Tag;
}
private void OnMenuMapNew(object sender, EventArgs e)