Compare commits

...

2 Commits

Author SHA1 Message Date
Michele Scandura
17d6cd28d6 fixing notes
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-20 16:49:49 +01:00
Michele Scandura
77befef8e0 cleanup and fixes 2021-09-20 16:30:42 +01:00
17 changed files with 105 additions and 307 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

@ -122,7 +122,7 @@ public class Map:Session
var newNote = new Note { X = selectedNote.X, Y = selectedNote.Y, Text = selectedNote.Text };
if (noteExists)
{
Walls.TryRemove(note.ToString(), out var _);
Notes.TryRemove(note.ToString(), out var _);
}
Notes.TryAdd(newNote.ToString(), newNote);

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

@ -24,6 +24,7 @@ namespace Sledgemapper
{
private readonly CommunicationManager _communicationManager;
private readonly State _state;
private readonly ArrayPool<Vector2> _vector2Pool;
private readonly GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private CachedContent _cachedContent;
@ -37,6 +38,13 @@ namespace Sledgemapper
private RenderTarget2D rendertarget;
private MainWidget _mainWidget;
private readonly TinyMessengerHub _messenger;
Effect outlineShader;
private Texture2D _transparentRedRectangle;
private Texture2D _whiteRectangle;
private Dictionary<string, Texture2D> _wallsContent;
private SpriteSheet _spriteSheet;
private SpriteSheet _rippleSpriteSheet;
Label _lblOverlayName;
public Sledgemapper()
{
@ -58,7 +66,7 @@ namespace Sledgemapper
_communicationManager.Connection.Reconnecting += OnHubReconnecting;
_communicationManager.Connection.Closed += OnHubDisconnected;
_state = State.Instance;
_vector2Pool = ArrayPool<Vector2>.Create();
_messenger = new TinyMessengerHub();
}
@ -76,6 +84,7 @@ namespace Sledgemapper
_mainWidget.lblConnectionStatus.Text = "Reconnecting";
await Task.Yield();
}
private async Task OnHubReconnected(string arg)
{
// ExceptionlessClient.Default.SubmitEvent(new Event { Message = "Hub reconnected", Type = "SignalR Client Events", Source = _settings.MachineName });
@ -118,7 +127,6 @@ namespace Sledgemapper
}
private void AddItemToToolGrid(Grid grid, EventHandler eventAction, Dictionary<string, Texture2D> tilesFolderContent)
{
var indexX = 0;
@ -165,9 +173,6 @@ namespace Sledgemapper
_lblOverlayName.Visible = false;
}
private SpriteSheet _rippleSpriteSheet;
Label _lblOverlayName;
private void OnTileButtonTouchEntered(object sender, EventArgs e)
{
var mouseState = Mouse.GetState().Position;
@ -188,7 +193,7 @@ namespace Sledgemapper
ResetRenderTarget();
_mainWidget = new MainWidget(_communicationManager, _messenger, Window);
_mainWidget = new MainWidget(_communicationManager, _messenger, Window);
@ -223,31 +228,19 @@ namespace Sledgemapper
_whiteRectangle = new Texture2D(GraphicsDevice, 1, 1);
_whiteRectangle.SetData(new[] { Color.White });
}
CenterOnTile(0, 0);
}
private void OnTxtOverlaySearchChange(object sender, ValueChangedEventArgs<string> e)
{
AddItemToToolGrid(_mainWidget.GridOverlays, OnOverlayButtonClicked, _spriteSheet, e.NewValue);
}
private void EditNote(Note note)
{
_state.SelectedNote = new Note { X = note.X, Y = note.Y, Text = note.Text };
var noteWindow = new NoteWindow();
Window window = new()
{
Title = $" Note on {note.X}:{note.Y}"
};
noteWindow.NoteText.Text = note.Text;
noteWindow.BtnOk.Click += OnButtonNoteOkClick;
noteWindow.BtnCancel.Click += OnButtonNoteCancelClick;
window.Content = noteWindow;
window.ShowModal(_desktop);
noteWindow.NoteText.SetKeyboardFocus();
new NoteWindow(_communicationManager, note).ShowInModalWindow(_desktop, $" Note on {note.X}:{note.Y}");
}
protected override void Update(GameTime gameTime)
@ -260,16 +253,21 @@ namespace Sledgemapper
var screenPosition = new Point(mouseState.Position.X - (int)_state.ViewportCenter.X, mouseState.Position.Y - (int)_state.ViewportCenter.Y);
_state.HoveredTile.X = screenPosition.X / _state.TileSize;
_state.HoveredTile.Y = screenPosition.Y / _state.TileSize;
if (screenPosition.X < 0)
if (!_desktop.ContextMenu?.Visible??true)
{
_state.HoveredTile.X--;
}
_state.HoveredTile.X = screenPosition.X / _state.TileSize;
_state.HoveredTile.Y = screenPosition.Y / _state.TileSize;
if (screenPosition.Y < 0)
{
_state.HoveredTile.Y--;
if (screenPosition.X < 0)
{
_state.HoveredTile.X--;
}
if (screenPosition.Y < 0)
{
_state.HoveredTile.Y--;
}
}
if (_state.InsertMode == InsertMode.Wall)
@ -581,7 +579,7 @@ namespace Sledgemapper
DrawTiles();
//DrawWalls();
DrawWalls();
DrawOverlays();
@ -1460,8 +1458,6 @@ namespace Sledgemapper
}
}
ArrayPool<Vector2> _vector2Pool;
private bool IsOffscreen(Tile position)
{
var boxTL = new Point(200 - _state.TileSize / 2, 75 - _state.TileSize / 2);
@ -1478,8 +1474,6 @@ namespace Sledgemapper
return false;
}
private int _borderWidth => (_state.TileSize / 6) % 2 == 0 ? (_state.TileSize / 6) : (_state.TileSize / 6) + 1;
private void DrawTiles()
{
for (var i = 0; i < _sessionData.Map.Values.Count; i++)
@ -1547,7 +1541,6 @@ namespace Sledgemapper
}
}
private void DrawRoom(Room tile)
{
var posX = tile.Start.X * _state.TileSize;
@ -1618,12 +1611,6 @@ namespace Sledgemapper
}
}
}
Effect outlineShader;
private Texture2D _transparentRedRectangle;
private Texture2D _whiteRectangle;
private Dictionary<string, Texture2D> _wallsContent;
private SpriteSheet _spriteSheet;
private void DrawDelete(Room tile)
{
@ -1723,47 +1710,12 @@ namespace Sledgemapper
}
}
private void OnButtonNoteOkClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var localContent = button.GetParentContentInWindow<NoteWindow>();
var note = new Note
{
X = _state.SelectedNote.X,
Y = _state.SelectedNote.Y,
Text = localContent.Content.NoteText.Text
};
_sessionData.NewNote(note);
localContent.Window.Close();
}
private void OnButtonNoteCancelClick(object sender, EventArgs e)
{
var button = ((TextButton)sender);
var content = button.GetParentContentInWindow<Widget>();
content.Window.Close();
}
private void OnContextMenuNewNoteClick(object sender, EventArgs e)
{
_desktop.HideContextMenu();
var noteWindow = new NoteWindow();
Window window = new()
{
Title = $" Note on {_state.SelectedTile.X}:{_state.SelectedTile.Y}"
};
noteWindow.BtnOk.Click += OnButtonNoteOkClick;
noteWindow.BtnCancel.Click += OnButtonNoteCancelClick;
window.Content = noteWindow;
window.ShowModal(_desktop);
noteWindow.NoteText.SetKeyboardFocus();
new NoteWindow(_communicationManager, new Note()).ShowInModalWindow(_desktop, $" Note on {_state.HoveredTile.X}:{_state.HoveredTile.Y}");
}
private void OnContextMenuDeleteNoteClick(object sender, EventArgs e)
{
_desktop.HideContextMenu();

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>