cleanup and fixes

This commit is contained in:
Michele Scandura 2021-09-20 16:30:42 +01:00
parent 4fd77a1f17
commit 77befef8e0
16 changed files with 78 additions and 230 deletions

View File

@ -7,6 +7,8 @@ using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Models;
using System.Collections.Generic;
using Sledgemapper.Api.Core.Entities;
namespace Sledgemapper.Api.Handlers
{
@ -32,13 +34,7 @@ namespace Sledgemapper.Api.Handlers
Dbcontext.Attach(user);
var campaign = await Dbcontext
.Campaigns
.Where(campaign => campaign.CampaignId == command.Campaign)
.Include(c => c.InvitedUsers)
.Include(c => c.Maps)
.Include(c => c.Owner)
.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)).FirstAsync();
var campaign = await GetCampaignForUser(command);
var maps = campaign.Maps.Any(s => s.SessionId == command.SessionId);
@ -48,6 +44,20 @@ namespace Sledgemapper.Api.Handlers
}
}
protected async Task<Campaign> GetCampaignForUser(TRequest command)
{
var user = await Dbcontext.Users.FindAsync(command.UserId);
Dbcontext.Attach(user);
var campaign = await Dbcontext
.Campaigns
.Where(campaign => campaign.CampaignId == command.Campaign)
.Include(c => c.InvitedUsers)
.Include(c => c.Maps)
.Include(c => c.Owner)
.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)).FirstAsync();
return campaign;
}
protected async Task<Session> SaveLog(TRequest command, string operation, string type, string data, CancellationToken cancellationToken)
{
var session = Dbcontext.Sessions.First(m => m.SessionId == command.SessionId);

View File

@ -16,7 +16,7 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewLineNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewLine(notification.Line);
await _hub.Clients.Groups(notification.Session.SessionId.ToString()).NewLine(notification.Line);
}
}
}

View File

@ -16,7 +16,7 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewNoteNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewNote(notification.Note);
await _hub.Clients.Groups(notification.Session.SessionId.ToString()).NewNote(notification.Note);
}
}
}

View File

@ -16,7 +16,7 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewRoomNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewRoom(notification.Room);
await _hub.Clients.Groups(notification.Session.SessionId.ToString()).NewRoom(notification.Room);
}
}
}

View File

@ -17,7 +17,7 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewTile(notification.Tile);
await _hub.Clients.Groups(notification.Session.SessionId.ToString()).NewTile(notification.Tile);
}
}
}

View File

@ -16,7 +16,7 @@ namespace Sledgemapper.Api.Handlers
public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.Session.SessionName).NewWall(notification.Wall);
await _hub.Clients.Groups(notification.Session.SessionId.ToString()).NewWall(notification.Wall);
}
}
}

View File

@ -9,30 +9,28 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class StartNewSessionHandler : IRequestHandler<NewSessionCommand, Guid>
public class StartNewSessionHandler : BaseCommandHandler<NewSessionCommand, Guid>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public StartNewSessionHandler(IMediator mediator, SledgemapperDbContext dbcontext)
public StartNewSessionHandler(IMediator mediator, SledgemapperDbContext dbcontext):base(mediator, dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<Guid> Handle(NewSessionCommand notification, CancellationToken cancellationToken)
public override async Task<Guid> Handle(NewSessionCommand command, CancellationToken cancellationToken)
{
var campaign = _dbcontext.Campaigns.First(c => c.CampaignId == notification.Campaign && c.OwnerId == notification.UserId.ToString());
var campaign = await GetCampaignForUser(command);
var session = new Session
{
SessionName = notification.SessionName,
OwnerUserId = notification.UserId,
SessionName = command.SessionName,
OwnerUserId = command.UserId,
CampaignId = campaign.CampaignId
};
_dbcontext.Sessions.Add(session);
await _dbcontext.SaveChangesAsync();
Dbcontext.Sessions.Add(session);
await Dbcontext.SaveChangesAsync();
return session.SessionId;
}
}

View File

@ -1,11 +1,16 @@
using System;
namespace Sledgemapper.Messages
{
public class MapOpenedMessage : TinyMessenger.TinyMessageBase
{
public string MapName { get; set; }
public MapOpenedMessage(object sender) : base(sender)
{
}
public string MapName { get; }
public Guid MapId { get; }
public MapOpenedMessage(object sender, string mapName, Guid mapId) : base(sender)
{
MapName = mapName;
MapId = mapId;
}
}
}

View File

@ -223,6 +223,8 @@ namespace Sledgemapper
_whiteRectangle = new Texture2D(GraphicsDevice, 1, 1);
_whiteRectangle.SetData(new[] { Color.White });
CenterOnTile(0, 0);
}
@ -581,7 +583,7 @@ namespace Sledgemapper
DrawTiles();
//DrawWalls();
DrawWalls();
DrawOverlays();

View File

@ -87,6 +87,11 @@ namespace Sledgemapper.UI
private async void OnMapOpenedMessage(MapOpenedMessage obj)
{
lblMap.Text = obj.MapName;
State.Instance.MapId = obj.MapId;
State.Instance.MapName = obj.MapName;
CommunicationManager.SessionData.SessionName = obj.MapName;
CommunicationManager.SessionData.SessionId = obj.MapId;
MenuConnectSync.Enabled = true;
MenuConnectUpload.Enabled = true;
CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;
@ -163,23 +168,7 @@ namespace Sledgemapper.UI
}
private void OnMenuConnectNewSelected(object sender, EventArgs e)
{
if (!((MenuItem)sender).Enabled)
{
return;
}
Window window = new()
{
Title = "New mapping session"
};
var content = new SessionWindow(CommunicationManager, Messenger);
window.Content = content;
window.ShowModal(Desktop);
}
private void UpdateConnectionState(HubConnection connection)
{
@ -302,16 +291,6 @@ namespace Sledgemapper.UI
_windowEditor.ShowModal(Desktop);
}
private void OnMenuConnectJoinSelected(object sender, EventArgs e)
{
if (!((MenuItem)sender).Enabled)
{
return;
}
new SessionWindow(CommunicationManager, Messenger).ShowInModalWindow(Desktop, "Join mapping session");
}
private void OnMenuViewShowCellNUmbersSelected(object sender, EventArgs e)
{
State.Instance.ShowCellNumbers = !State.Instance.ShowCellNumbers;
@ -475,7 +454,7 @@ namespace Sledgemapper.UI
return;
}
new MapWindow(CommunicationManager).ShowInModalWindow(Desktop, "New map");
new MapWindow(CommunicationManager, Messenger).ShowInModalWindow(Desktop, "New map");
}
//TODO Refactor

View File

@ -35,8 +35,8 @@ namespace Sledgemapper.UI
{
State.Instance.MapName = _selectedMap;
State.Instance.MapId = _selectedMapId;
// var map = CommunicationManager.Api.GetMap(State.Instance.CampaignId, State.Instance.MapId);
Messenger.Publish(new MapOpenedMessage(this) { MapName = State.Instance.MapName });
// var map = CommunicationManager.Api.GetMap(State.Instance.CampaignId, State.Instance.MapId);
Messenger.Publish(new MapOpenedMessage(this, _selectedMap, _selectedMapId));
this.GetContainingWindow().Close();
}
@ -71,7 +71,7 @@ namespace Sledgemapper.UI
private void OnMenuMapNew(object sender, EventArgs e)
{
new MapWindow(CommunicationManager).ShowInModalWindow(Desktop, "New map");
new MapWindow(CommunicationManager, Messenger).ShowInModalWindow(Desktop, "New map");
}
}
}

View File

@ -1,17 +1,22 @@
/* Generated by MyraPad at 02/09/2021 16:26:04 */
using Microsoft.AspNetCore.SignalR.Client;
using Myra.Graphics2D.UI;
using Sentry;
using Sledgemapper.Messages;
using System;
using TinyMessenger;
namespace Sledgemapper.UI
{
public partial class MapWindow
{
public CommunicationManager CommunicationManager { get; }
public TinyMessengerHub Messenger { get; }
public MapWindow(CommunicationManager communicationManager)
public MapWindow(CommunicationManager communicationManager, TinyMessengerHub messenger)
{
CommunicationManager= communicationManager;
Messenger = messenger;
BuildUI();
BtnNewCampaign.Click += OnButtonNewMapClicked;
}
@ -23,18 +28,27 @@ namespace Sledgemapper.UI
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as MapWindow;
var isValid = localContent.TxtCampaign.ValidateTextbox();
var localContent = ((TextButton)sender).GetParentContentInWindow<MapWindow>();// localWindow.Content as PlayerWindow;
var isValid = localContent.Content.TxtCampaign.ValidateTextbox();
if (!isValid)
{
return;
}
if (CommunicationManager.Connection.State != HubConnectionState.Connected)
{
Messenger.Publish(new SignalrConnectionUpdateMessage(this));
}
var successful = false;
Guid sessionGuid = Guid.Empty;
try
{
await CommunicationManager.Api.NewSession(State.Instance.CampaignId, localContent.TxtCampaign.Text);
sessionGuid = await CommunicationManager.Api.NewSession(State.Instance.CampaignId, localContent.Content.TxtCampaign.Text);
successful = sessionGuid != Guid.Empty;
}
catch (Exception ex)
@ -42,7 +56,11 @@ namespace Sledgemapper.UI
SentrySdk.CaptureException(ex);
}
localWindow.Close();
if (successful)
{
Messenger.Publish(new MapOpenedMessage(this, localContent.Content.TxtCampaign.Text, sessionGuid));
localContent.Window.Close();
}
}
}
}

View File

@ -1,74 +0,0 @@
/* Generated by MyraPad at 10/11/2020 10:59:36 */
using System;
using Microsoft.AspNetCore.SignalR.Client;
using Myra.Graphics2D.UI;
using Sentry;
using Sledgemapper.Messages;
using Sledgemapper.Shared.Entities;
using TinyMessenger;
using Session = Sledgemapper.Shared.Entities.Session;
namespace Sledgemapper.UI
{
public partial class SessionWindow
{
private readonly CommunicationManager CommunicationManager;
private readonly TinyMessengerHub Messenger;
public SessionWindow(CommunicationManager communicationManager, TinyMessengerHub messenger)
{
BuildUI();
CommunicationManager = communicationManager;
Messenger = messenger;
BtnLogin.Text = "Join";
BtnLogin.Click += OnButtonNewSessionClicked;
}
private async void OnButtonNewSessionClicked(object sender, EventArgs e)
{
var localContent = ((TextButton)sender).GetParentContentInWindow<SessionWindow>();// localWindow.Content as PlayerWindow;
var isValid = localContent.Content.TxtSession.ValidateTextbox();
if (!isValid)
{
return;
}
if (CommunicationManager.Connection.State != HubConnectionState.Connected)
{
Messenger.Publish(new SignalrConnectionUpdateMessage(this));
}
var successful = false;
try
{
var sessionGuid = await CommunicationManager.Api.NewSession(State.Instance.CampaignId, localContent.Content.TxtSession.Text);
if (sessionGuid != Guid.Empty)
{
//CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
State.Instance.SessionId = sessionGuid;
}
successful = true;
var result2 = await CommunicationManager.Connection?.InvokeAsync<Session>("JoinSession", localContent.Content.TxtSession.Text);
//CommunicationManager.SessionData.SessionId = result2.SessionId;
//CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
if (successful)
{
CommunicationManager.SessionData.SessionName = localContent.Content.TxtSession.Text;
CommunicationManager.SessionData.Map = CommunicationManager.SessionData.Map;
CommunicationManager.SessionData.Overlays = CommunicationManager.SessionData.Overlays;
CommunicationManager.SessionData.Walls = CommunicationManager.SessionData.Walls;
localContent.Window.Close();
}
}
}
}

View File

@ -1,62 +0,0 @@
/* Generated by MyraPad at 10/11/2020 10:59:36 */
using Myra.Graphics2D;
using Myra.Graphics2D.TextureAtlases;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.Brushes;
#if !STRIDE
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#else
using Stride.Core.Mathematics;
#endif
namespace Sledgemapper.UI
{
partial class SessionWindow: VerticalStackPanel
{
private void BuildUI()
{
var label1 = new Label();
label1.Text = "Session";
TxtSession = new TextBox();
TxtSession.GridColumn = 1;
TxtSession.Id = "TxtSession";
var grid1 = new Grid();
grid1.ColumnSpacing = 25;
grid1.RowSpacing = 10;
grid1.ColumnsProportions.Add(new Proportion
{
Type = Myra.Graphics2D.UI.ProportionType.Pixels,
Value = 60,
});
grid1.ColumnsProportions.Add(new Proportion
{
Type = Myra.Graphics2D.UI.ProportionType.Fill,
});
grid1.Widgets.Add(label1);
grid1.Widgets.Add(TxtSession);
BtnLogin = new TextButton();
BtnLogin.Text = "Join";
BtnLogin.Padding = new Thickness(10, 5);
BtnLogin.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
BtnLogin.Id = "BtnLogin";
Spacing = 16;
HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Center;
Width = 300;
Padding = new Thickness(10);
Widgets.Add(grid1);
Widgets.Add(BtnLogin);
}
public TextBox TxtSession;
public TextButton BtnLogin;
}
}

View File

@ -1,11 +0,0 @@
/* Generated by MyraPad at 10/11/2020 10:59:36 */
namespace Sledgemapper.UI
{
public partial class SessionWindow
{
public SessionWindow()
{
BuildUI();
}
}
}

View File

@ -1,17 +0,0 @@
<Project>
<Project.ExportOptions Namespace="Sledgemapper.UI" Class="SessionWindow" OutputPath="C:\dev\Map\Sledgemapper\UI" />
<VerticalStackPanel Spacing="16" HorizontalAlignment="Center" VerticalAlignment="Center" Width="300" Padding="10">
<Grid ColumnSpacing="25" RowSpacing="10">
<Grid.ColumnsProportions>
<Proportion Type="Pixels" Value="60" />
<Proportion Type="Fill" />
</Grid.ColumnsProportions>
<Label Text="Session" />
<TextBox GridColumn="1" Id="TxtSession" />
</Grid>
<TextButton Text="New" Padding="10, 5" HorizontalAlignment="Center" Id="BtnLogin" />
</VerticalStackPanel>
</Project>