From 3e1acd26f28042b762b3f4b4a7e67910f9e78ef0 Mon Sep 17 00:00:00 2001 From: Michele Date: Thu, 23 Sep 2021 22:15:15 +0100 Subject: [PATCH 1/4] invite player fixes --- .../Commands/InvitePlayerToCampaignCommand.cs | 6 +++--- Sledgemapper.Api/Controllers/CampaignController.cs | 6 +++--- .../InvitePlayerToCampaignCommandHandler.cs | 4 ++-- Sledgemapper/IMapApi.cs | 12 ++++++------ Sledgemapper/UI/LoginRegisterWindow.Custom.cs | 1 + Sledgemapper/UI/MainWidget.Custom.cs | 2 +- Sledgemapper/UI/PlayerList.Custom.cs | 11 ++++++++--- Sledgemapper/UI/PlayerWindow.Custom.cs | 14 ++++++++++---- 8 files changed, 34 insertions(+), 22 deletions(-) diff --git a/Sledgemapper.Api/Commands/InvitePlayerToCampaignCommand.cs b/Sledgemapper.Api/Commands/InvitePlayerToCampaignCommand.cs index 8ecc338..de5aa7c 100644 --- a/Sledgemapper.Api/Commands/InvitePlayerToCampaignCommand.cs +++ b/Sledgemapper.Api/Commands/InvitePlayerToCampaignCommand.cs @@ -6,13 +6,13 @@ namespace Sledgemapper.Api.Commands public class InvitePlayerToCampaignCommand : IRequest { public double Timestamp { get; private set; } - public string CampaignName { get; private set; } + public Guid CampaignId { get; private set; } public string Email { get; private set; } public string UserId { get; private set; } - public InvitePlayerToCampaignCommand(string campaingName, string email, string userId) + public InvitePlayerToCampaignCommand(Guid campaignId, string email, string userId) { Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds(); - CampaignName = campaingName; + CampaignId = campaignId; UserId = userId; Email = email; } diff --git a/Sledgemapper.Api/Controllers/CampaignController.cs b/Sledgemapper.Api/Controllers/CampaignController.cs index a09e5af..77a6dc5 100644 --- a/Sledgemapper.Api/Controllers/CampaignController.cs +++ b/Sledgemapper.Api/Controllers/CampaignController.cs @@ -48,10 +48,10 @@ namespace Sledgemapper.Api.Controllers } [HttpPost] - [Route("{campaignName}/players/{email}")] - public async Task Invite(string campaignName, string email) + [Route("{campaignId}/players/{email}")] + public async Task Invite(Guid campaignId, string email) { - var result = await _mediator.Send(new InvitePlayerToCampaignCommand(campaignName, email, UserId)); + var result = await _mediator.Send(new InvitePlayerToCampaignCommand(campaignId, email, UserId)); return result; } diff --git a/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs b/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs index c6c6a28..b600ea9 100644 --- a/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs +++ b/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs @@ -26,8 +26,8 @@ namespace Sledgemapper.Api.Handlers { var user = await _dbcontext.Users.FindAsync(command.UserId); - var campaign = await _dbcontext.Campaigns.Where(campaign=>campaign.CampaignName==command.CampaignName && campaign.OwnerId==command.UserId).Include(campaign=>campaign.InvitedUsers).FirstAsync(); - var invitedUser = await _dbcontext.Users.FirstOrDefaultAsync(user=>user.Email==command.Email); + var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync(); + var invitedUser = await _dbcontext.Users.FirstOrDefaultAsync(user => user.Email == command.Email); _dbcontext.Attach(invitedUser); _dbcontext.Attach(campaign); campaign.InvitedUsers.Add(invitedUser); diff --git a/Sledgemapper/IMapApi.cs b/Sledgemapper/IMapApi.cs index 2c66283..b831739 100644 --- a/Sledgemapper/IMapApi.cs +++ b/Sledgemapper/IMapApi.cs @@ -42,15 +42,15 @@ namespace Sledgemapper Task> GetPlayers(Guid campaignName); - [Get("/campaign/{campaignName}/maps")] - Task> GetMaps(Guid campaignName); + [Get("/campaign/{campaignId}/maps")] + Task> GetMaps(Guid campaignId); - [Get("/map/{campaignName}/{mapName}")] - Task GetMap(Guid campaignName, Guid mapName); + [Get("/map/{campaignId}/{mapId}")] + Task GetMap(Guid campaignId, Guid mapId); - [Post("/campaign/{campaignName}/players/{email}")] - Task InvitePlayer(string campaignName, string email); + [Post("/campaign/{campaignId}/players/{email}")] + Task InvitePlayer(Guid campaignId, string email); diff --git a/Sledgemapper/UI/LoginRegisterWindow.Custom.cs b/Sledgemapper/UI/LoginRegisterWindow.Custom.cs index 73dee30..f565e68 100644 --- a/Sledgemapper/UI/LoginRegisterWindow.Custom.cs +++ b/Sledgemapper/UI/LoginRegisterWindow.Custom.cs @@ -100,6 +100,7 @@ namespace Sledgemapper.UI _authResponse = await CommunicationManager.Login(new AuthenticateModel { Username = TxtEmail.Text, + Email = TxtEmail.Text, Password = TxtPassword.Text }); successful = true; diff --git a/Sledgemapper/UI/MainWidget.Custom.cs b/Sledgemapper/UI/MainWidget.Custom.cs index d8d1ebb..f8379c6 100644 --- a/Sledgemapper/UI/MainWidget.Custom.cs +++ b/Sledgemapper/UI/MainWidget.Custom.cs @@ -297,7 +297,7 @@ namespace Sledgemapper.UI { if (!((MenuItem)sender).Enabled) return; - var content = new PlayerList(CommunicationManager); + var content = new PlayerList(CommunicationManager, Messenger); if (await content.LoadPlayers()) { content.ShowInModalWindow(Desktop, "Players"); diff --git a/Sledgemapper/UI/PlayerList.Custom.cs b/Sledgemapper/UI/PlayerList.Custom.cs index 84c238b..8fcdfd5 100644 --- a/Sledgemapper/UI/PlayerList.Custom.cs +++ b/Sledgemapper/UI/PlayerList.Custom.cs @@ -1,15 +1,20 @@ /* Generated by MyraPad at 28/08/2021 19:49:08 */ using System.Threading.Tasks; +using TinyMessenger; namespace Sledgemapper.UI { public partial class PlayerList { - protected readonly CommunicationManager CommunicationManager; + protected CommunicationManager CommunicationManager { get; } + protected TinyMessengerHub Messenger { get; } - public PlayerList(CommunicationManager communicationManager) + public PlayerList(CommunicationManager communicationManager, TinyMessenger.TinyMessengerHub messenger) { + CommunicationManager = communicationManager; + Messenger = messenger; + BuildUI(); CommunicationManager = communicationManager; BtnInvitePlayer.Click += (s, e) => @@ -38,7 +43,7 @@ namespace Sledgemapper.UI private void ShowAddPLayerWindow() { - new PlayerWindow(CommunicationManager).ShowInModalWindow(Desktop, "Invite player"); + new PlayerWindow(CommunicationManager, Messenger).ShowInModalWindow(Desktop, "Invite player"); } } } \ No newline at end of file diff --git a/Sledgemapper/UI/PlayerWindow.Custom.cs b/Sledgemapper/UI/PlayerWindow.Custom.cs index f314696..a81a12d 100644 --- a/Sledgemapper/UI/PlayerWindow.Custom.cs +++ b/Sledgemapper/UI/PlayerWindow.Custom.cs @@ -1,17 +1,21 @@ /* Generated by MyraPad at 28/08/2021 22:04:11 */ using Myra.Graphics2D.UI; using Sentry; +using Sledgemapper.Messages; using System; +using TinyMessenger; namespace Sledgemapper.UI { public partial class PlayerWindow { - protected readonly CommunicationManager CommunicationManager; + protected CommunicationManager CommunicationManager { get; } + protected TinyMessengerHub Messenger { get; } - public PlayerWindow(CommunicationManager communicationManager) + public PlayerWindow(CommunicationManager communicationManager, TinyMessenger.TinyMessengerHub messenger) { - CommunicationManager = communicationManager; + CommunicationManager = communicationManager; + Messenger = messenger; BuildUI(); BtnNewCampaign.Click += OnButtonInvitePlayerClicked; @@ -29,11 +33,13 @@ namespace Sledgemapper.UI var successful = false; try { - await CommunicationManager.Api.InvitePlayer(State.Instance.CampaignName, localContent.Content.TxtCampaign.Text); + await CommunicationManager.Api.InvitePlayer(State.Instance.CampaignId, localContent.Content.TxtCampaign.Text); } catch (Exception ex) { + Messenger.Publish(new ErrorMessage(this, "Error inviting player")); SentrySdk.CaptureException(ex); + } this.GetContainingWindow().Close(); } From 85e2f699bf31afedb9ac82cc2a389005a805ea4c Mon Sep 17 00:00:00 2001 From: Michele Scandura Date: Fri, 24 Sep 2021 10:21:44 +0100 Subject: [PATCH 2/4] general fixes --- .../Handlers/NewCampaignCommandHandler.cs | 4 ++-- Sledgemapper.Api/Sledgemapper.Api.csproj | 18 +++++++++--------- Sledgemapper/CommunicationManager.cs | 11 ++++++++++- Sledgemapper/Sledgemapper.cs | 1 + Sledgemapper/Sledgemapper.csproj | 2 +- Sledgemapper/UI/MainWidget.Custom.cs | 7 ++++++- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs b/Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs index 0c61334..f6b505e 100644 --- a/Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs +++ b/Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs @@ -23,7 +23,7 @@ namespace Sledgemapper.Api.Handlers { try { - var user = await _dbcontext.Users.FindAsync(notification.UserId, cancellationToken); + var user = await _dbcontext.Users.FindAsync(new object[] { notification.UserId }, cancellationToken); _dbcontext.Attach(user); var campaign = new Core.Entities.Campaign { @@ -39,7 +39,7 @@ namespace Sledgemapper.Api.Handlers } catch (Exception ex) { - + } return false; diff --git a/Sledgemapper.Api/Sledgemapper.Api.csproj b/Sledgemapper.Api/Sledgemapper.Api.csproj index 30d003d..a5dbc63 100644 --- a/Sledgemapper.Api/Sledgemapper.Api.csproj +++ b/Sledgemapper.Api/Sledgemapper.Api.csproj @@ -9,19 +9,19 @@ - - - - - + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + - - - + + + diff --git a/Sledgemapper/CommunicationManager.cs b/Sledgemapper/CommunicationManager.cs index 52bdd5d..c584097 100644 --- a/Sledgemapper/CommunicationManager.cs +++ b/Sledgemapper/CommunicationManager.cs @@ -301,7 +301,16 @@ namespace Sledgemapper Program.helper.SaveUnencryptedTokenCache(JsonSerializer.SerializeToUtf8Bytes(_authenticateResponse)); - + await Connection.StopAsync(); + + State.Instance.CampaignId=Guid.Empty; + State.Instance.CampaignName = string.Empty; + State.Instance.SessionId = Guid.Empty; + State.Instance.MapId = Guid.Empty; + State.Instance.MapName = string.Empty; + SessionData.SessionId = Guid.Empty; + SessionData.SessionName = string.Empty; + SessionData.Players.Clear(); return _authenticateResponse; } diff --git a/Sledgemapper/Sledgemapper.cs b/Sledgemapper/Sledgemapper.cs index fe3864d..00d7cf8 100644 --- a/Sledgemapper/Sledgemapper.cs +++ b/Sledgemapper/Sledgemapper.cs @@ -57,6 +57,7 @@ namespace Sledgemapper MyraEnvironment.Game = this; + _sessionData = new Session(); IsFixedTimeStep = true; TargetElapsedTime = TimeSpan.FromSeconds(1d / 30d); diff --git a/Sledgemapper/Sledgemapper.csproj b/Sledgemapper/Sledgemapper.csproj index 7cbb026..4789d7e 100644 --- a/Sledgemapper/Sledgemapper.csproj +++ b/Sledgemapper/Sledgemapper.csproj @@ -58,7 +58,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Sledgemapper/UI/MainWidget.Custom.cs b/Sledgemapper/UI/MainWidget.Custom.cs index f8379c6..1bb9990 100644 --- a/Sledgemapper/UI/MainWidget.Custom.cs +++ b/Sledgemapper/UI/MainWidget.Custom.cs @@ -229,7 +229,12 @@ namespace Sledgemapper.UI //MenuConnectJoin.Enabled = true; MenuCampaignOpen.Enabled = true; MenuCampaingNew.Enabled = true; - + MenuMapNew.Enabled = false; + MenuMapOpen.Enabled = false; + lblCampaign.Text = "n/a"; + lblMap.Text = "n/a"; + CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded; + CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted; lblUsername.Text = $"{obj.Initials}"; } From 5276055c4d43779aea48332a63739b4aefd686e9 Mon Sep 17 00:00:00 2001 From: Michele Scandura Date: Fri, 24 Sep 2021 11:00:24 +0100 Subject: [PATCH 3/4] fix player invitation --- .../GetCampaignPlayersCommandHandler.cs | 13 ++++++- .../InvitePlayerToCampaignCommandHandler.cs | 38 ++++++++++++------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Sledgemapper.Api/Handlers/GetCampaignPlayersCommandHandler.cs b/Sledgemapper.Api/Handlers/GetCampaignPlayersCommandHandler.cs index 23c801d..e915643 100644 --- a/Sledgemapper.Api/Handlers/GetCampaignPlayersCommandHandler.cs +++ b/Sledgemapper.Api/Handlers/GetCampaignPlayersCommandHandler.cs @@ -29,7 +29,18 @@ namespace Sledgemapper.Api.Handlers var user = await _dbcontext.Users.FindAsync(command.UserId); _dbcontext.Attach(user); - var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync(); + + var campaign = await _dbcontext + .Campaigns + .Where(campaign => campaign.CampaignId == command.CampaignId) + .Include(c => c.InvitedUsers) + + .Include(c => c.Owner) + .Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)).FirstAsync(); + + + + //var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync(); var players = campaign.InvitedUsers.Select(user => new Player { Initials = user.Initials, UserName = user.UserName , UserId = new Guid(user.Id)}).ToList(); return players; diff --git a/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs b/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs index b600ea9..aefdac0 100644 --- a/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs +++ b/Sledgemapper.Api/Handlers/InvitePlayerToCampaignCommandHandler.cs @@ -1,43 +1,53 @@ -using MediatR; -using Microsoft.EntityFrameworkCore; -using Sledgemapper.Api.Commands; -using Sledgemapper.Api.Infrastructure.Data; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediatR; +using Microsoft.EntityFrameworkCore; +using Sledgemapper.Api.Commands; +using Sledgemapper.Api.Infrastructure.Data; namespace Sledgemapper.Api.Handlers { public class InvitePlayerToCampaignCommandHandler : IRequestHandler { + private readonly SledgemapperDbContext _dbContext; private readonly IMediator _mediator; - private readonly SledgemapperDbContext _dbcontext; - public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) + public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbContext) { _mediator = mediator; - _dbcontext = dbcontext; + _dbContext = dbContext; } public async Task Handle(InvitePlayerToCampaignCommand command, CancellationToken cancellationToken) { try { - var user = await _dbcontext.Users.FindAsync(command.UserId); + var user = await _dbContext.Users.FindAsync(command.UserId); - var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync(); - var invitedUser = await _dbcontext.Users.FirstOrDefaultAsync(user => user.Email == command.Email); - _dbcontext.Attach(invitedUser); - _dbcontext.Attach(campaign); + var campaign = await _dbContext + .Campaigns + .Where(campaign => campaign.CampaignId == command.CampaignId) + .Include(c => c.InvitedUsers) + .Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)) + .FirstAsync(cancellationToken); + + + var invitedUser = + await _dbContext.Users.FirstOrDefaultAsync(iUser => iUser.Email == command.Email, + cancellationToken); + _dbContext.Attach(invitedUser); + _dbContext.Attach(campaign); campaign.InvitedUsers.Add(invitedUser); - _dbcontext.Campaigns.Update(campaign); - await _dbcontext.SaveChangesAsync(); + + await _dbContext.SaveChangesAsync(cancellationToken); return true; } catch (Exception ex) { } + return false; } } From 099fca8ceb8a01eb6682c0778beeadd8c767f05d Mon Sep 17 00:00:00 2001 From: Michele Scandura Date: Fri, 24 Sep 2021 11:02:22 +0100 Subject: [PATCH 4/4] small fixes --- Sledgemapper/UI/MainWidget.Custom.cs | 1 + Sledgemapper/UI/PlayerWindow.Generated.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Sledgemapper/UI/MainWidget.Custom.cs b/Sledgemapper/UI/MainWidget.Custom.cs index 1bb9990..c565054 100644 --- a/Sledgemapper/UI/MainWidget.Custom.cs +++ b/Sledgemapper/UI/MainWidget.Custom.cs @@ -231,6 +231,7 @@ namespace Sledgemapper.UI MenuCampaingNew.Enabled = true; MenuMapNew.Enabled = false; MenuMapOpen.Enabled = false; + MenuCampaignPlayers.Enabled = false; lblCampaign.Text = "n/a"; lblMap.Text = "n/a"; CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded; diff --git a/Sledgemapper/UI/PlayerWindow.Generated.cs b/Sledgemapper/UI/PlayerWindow.Generated.cs index 62d81ea..509d0b8 100644 --- a/Sledgemapper/UI/PlayerWindow.Generated.cs +++ b/Sledgemapper/UI/PlayerWindow.Generated.cs @@ -45,7 +45,7 @@ namespace Sledgemapper.UI grid1.Widgets.Add(TxtCampaign); BtnNewCampaign = new TextButton(); - BtnNewCampaign.Text = "New"; + BtnNewCampaign.Text = "Invite"; BtnNewCampaign.Padding = new Thickness(10, 5); BtnNewCampaign.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center; BtnNewCampaign.Id = "BtnNewCampaign";