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/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 c6c6a28..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.CampaignName==command.CampaignName && 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; } } 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/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/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/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..c565054 100644 --- a/Sledgemapper/UI/MainWidget.Custom.cs +++ b/Sledgemapper/UI/MainWidget.Custom.cs @@ -229,7 +229,13 @@ namespace Sledgemapper.UI //MenuConnectJoin.Enabled = true; MenuCampaignOpen.Enabled = true; MenuCampaingNew.Enabled = true; - + MenuMapNew.Enabled = false; + MenuMapOpen.Enabled = false; + MenuCampaignPlayers.Enabled = false; + lblCampaign.Text = "n/a"; + lblMap.Text = "n/a"; + CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded; + CommunicationManager.SessionData.MapEntityDeleted -= OnMapEntityDeleted; lblUsername.Text = $"{obj.Initials}"; } @@ -297,7 +303,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(); } 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";