Merge branch 'refactoring' of https://git.michelescandura.com/michele/Map into refactoring
This commit is contained in:
commit
ea9cc32534
15 changed files with 135 additions and 44 deletions
15
Sledgemapper.Api/Commands/NewSessionCommand.cs
Normal file
15
Sledgemapper.Api/Commands/NewSessionCommand.cs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace Sledgemapper.Api.Commands
|
||||||
|
{
|
||||||
|
public class NewSessionCommand : IRequest<bool>
|
||||||
|
{
|
||||||
|
public string SessionName { get; set; }
|
||||||
|
public int UserId { get; }
|
||||||
|
public NewSessionCommand(string sessionName, int userId)
|
||||||
|
{
|
||||||
|
SessionName = sessionName;
|
||||||
|
UserId = userId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Sledgemapper.Api.Commands;
|
||||||
using Sledgemapper.Api.Handlers;
|
using Sledgemapper.Api.Handlers;
|
||||||
using Sledgemapper.Shared.Entities;
|
using Sledgemapper.Shared.Entities;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -8,13 +9,22 @@ using System.Threading.Tasks;
|
||||||
namespace Sledgemapper.Api.Controllers
|
namespace Sledgemapper.Api.Controllers
|
||||||
{
|
{
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[Route("[controller]/{sessionName}")]
|
[Route("[controller]/{sessionName}")]
|
||||||
public class SessionController : ControllerBase
|
public class SessionController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
|
||||||
public SessionController(IMediator mediator) { _mediator = mediator; }
|
public SessionController(IMediator mediator) { _mediator = mediator; }
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task Post(string sessionName)
|
||||||
|
{
|
||||||
|
var userId = int.Parse(HttpContext.User.Identity.Name);
|
||||||
|
|
||||||
|
var result = await _mediator.Send(new NewSessionCommand(sessionName, userId));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost("tile")]
|
[HttpPost("tile")]
|
||||||
public async Task Post(string sessionName, [FromBody]Tile tile)
|
public async Task Post(string sessionName, [FromBody]Tile tile)
|
||||||
{
|
{
|
||||||
|
|
32
Sledgemapper.Api/Handlers/StartNewSessionHandler.cs
Normal file
32
Sledgemapper.Api/Handlers/StartNewSessionHandler.cs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
using MediatR;
|
||||||
|
using Sledgemapper.Api.Commands;
|
||||||
|
using Sledgemapper.Api.Data;
|
||||||
|
using Sledgemapper.Api.Models;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Sledgemapper.Api.Handlers
|
||||||
|
{
|
||||||
|
public class StartNewSessionHandler : IRequestHandler<NewSessionCommand, bool>
|
||||||
|
{
|
||||||
|
private readonly IMediator _mediator;
|
||||||
|
private readonly MyDbContext _dbcontext;
|
||||||
|
|
||||||
|
public StartNewSessionHandler(IMediator mediator, MyDbContext dbcontext)
|
||||||
|
{
|
||||||
|
_mediator = mediator;
|
||||||
|
_dbcontext = dbcontext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Handle(NewSessionCommand notification, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// _dbcontext.MapLogs.Add(new Session
|
||||||
|
// {
|
||||||
|
// SessionName = notification.SessionName,
|
||||||
|
// OwnerUserId = notification.UserId
|
||||||
|
// });
|
||||||
|
await _dbcontext.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,12 +15,15 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
|
|
||||||
namespace SignalRChat.Hubs
|
namespace SignalRChat.Hubs
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public class SledgemapperHub : Hub<ISledgemapperClient>
|
public class SledgemapperHub : Hub<ISledgemapperClient>
|
||||||
{
|
{
|
||||||
public SledgemapperHub() {
|
public SledgemapperHub()
|
||||||
|
{
|
||||||
}
|
|
||||||
|
}
|
||||||
private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
|
private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
|
||||||
public List<string> Colors = new List<string>{"CC0000",
|
public List<string> Colors = new List<string>{"CC0000",
|
||||||
"CC3300",
|
"CC3300",
|
||||||
|
@ -113,6 +116,10 @@ namespace SignalRChat.Hubs
|
||||||
|
|
||||||
public async Task<Session> NewSession(string sessionName, string initials)
|
public async Task<Session> NewSession(string sessionName, string initials)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
var userId = int.Parse(Context.User.Identity.Name);
|
||||||
|
|
||||||
|
// var user = this.Context.GetHttpContext().;
|
||||||
var session = new Session();
|
var session = new Session();
|
||||||
session.Colors = new List<string>(Colors);
|
session.Colors = new List<string>(Colors);
|
||||||
session.Colors.Shuffle();
|
session.Colors.Shuffle();
|
||||||
|
@ -120,7 +127,7 @@ namespace SignalRChat.Hubs
|
||||||
session.Players.Add(player);
|
session.Players.Add(player);
|
||||||
_sessions.Add(sessionName, session);
|
_sessions.Add(sessionName, session);
|
||||||
|
|
||||||
|
|
||||||
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
|
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
17
Sledgemapper.Api/Models/Session.cs
Normal file
17
Sledgemapper.Api/Models/Session.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Sledgemapper.Api.Models
|
||||||
|
{
|
||||||
|
public class Session
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int SessionId { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public string SessionName{get;set;}
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int OwnerUserId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
8
Sledgemapper.Api/Notifications/NewSessionNotification.cs
Normal file
8
Sledgemapper.Api/Notifications/NewSessionNotification.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace Sledgemapper.Api.Handlers
|
||||||
|
{
|
||||||
|
public class NewSessionNotification : BaseNotification
|
||||||
|
{
|
||||||
|
public int UserId { get; }
|
||||||
|
public NewSessionNotification(string sessionName, int userId) : base(sessionName) => UserId = userId;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Sledgemapper.Services;
|
using Sledgemapper.Services;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace SignalRChat
|
namespace SignalRChat
|
||||||
{
|
{
|
||||||
|
@ -79,6 +79,8 @@ namespace SignalRChat
|
||||||
// return unauthorized if user no longer exists
|
// return unauthorized if user no longer exists
|
||||||
context.Fail("Unauthorized");
|
context.Fail("Unauthorized");
|
||||||
}
|
}
|
||||||
|
context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.Name, user.Username));
|
||||||
|
context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Binary file not shown.
|
@ -17,6 +17,7 @@ namespace Sledgemapper
|
||||||
public HubConnection Connection { get; private set; }
|
public HubConnection Connection { get; private set; }
|
||||||
public Session SessionData;
|
public Session SessionData;
|
||||||
private ChannelsQueue Queue = new ChannelsQueue();
|
private ChannelsQueue Queue = new ChannelsQueue();
|
||||||
|
private AuthenticateResponse _authenticateResponse;
|
||||||
|
|
||||||
public CommunicationManager(Session sessionData)
|
public CommunicationManager(Session sessionData)
|
||||||
{
|
{
|
||||||
|
@ -24,12 +25,15 @@ namespace Sledgemapper
|
||||||
Connection = new HubConnectionBuilder()
|
Connection = new HubConnectionBuilder()
|
||||||
.WithAutomaticReconnect()
|
.WithAutomaticReconnect()
|
||||||
|
|
||||||
.WithUrl("http://localhost:5000/SledgemapperHub")
|
.WithUrl("http://localhost:5000/SledgemapperHub", options =>
|
||||||
|
{
|
||||||
|
options.AccessTokenProvider = () => Task.FromResult(_authenticateResponse.Token);
|
||||||
|
})
|
||||||
|
|
||||||
// .WithUrl("http://hub.michelescandura.com:5000/SledgemapperHub")
|
// .WithUrl("http://hub.michelescandura.com:5000/SledgemapperHub")
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var httpClientHandler = new HttpClientHandler();
|
var httpClientHandler = new HttpClientHandler();
|
||||||
|
|
||||||
|
@ -58,7 +62,7 @@ namespace Sledgemapper
|
||||||
var p = SessionData.Players.FirstOrDefault(m => m.ConnectionId == player.ConnectionId);
|
var p = SessionData.Players.FirstOrDefault(m => m.ConnectionId == player.ConnectionId);
|
||||||
if (p != null)
|
if (p != null)
|
||||||
{
|
{
|
||||||
p.Position = player.Position;
|
p.Position = player.Position;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -100,16 +104,28 @@ namespace Sledgemapper
|
||||||
var p = SessionData.Players.FirstOrDefault(m => m.ConnectionId == player.ConnectionId);
|
var p = SessionData.Players.FirstOrDefault(m => m.ConnectionId == player.ConnectionId);
|
||||||
if (p is null)
|
if (p is null)
|
||||||
{
|
{
|
||||||
SessionData.Players.Add(player);
|
SessionData.Players.Add(player);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
p.Color = player.Color;
|
p.Color = player.Color;
|
||||||
p.Position = player.Position;
|
p.Position = player.Position;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> Register(RegisterModel registerModel)
|
||||||
|
{
|
||||||
|
var result = await Api.Register(registerModel);
|
||||||
|
return result.IsSuccessStatusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<AuthenticateResponse> Login(AuthenticateModel authenticateModel)
|
||||||
|
{
|
||||||
|
_authenticateResponse = await Api.Authenticate(authenticateModel);
|
||||||
|
return _authenticateResponse;
|
||||||
|
}
|
||||||
|
|
||||||
private async Task Execute(Func<Task> call)
|
private async Task Execute(Func<Task> call)
|
||||||
{
|
{
|
||||||
await Policy
|
await Policy
|
||||||
|
|
|
@ -3,6 +3,7 @@ using Sledgemapper.Shared.Entities;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -27,5 +28,11 @@ namespace Sledgemapper
|
||||||
|
|
||||||
[Delete("/session/{sessionName}/overlay")]
|
[Delete("/session/{sessionName}/overlay")]
|
||||||
Task DeleteOverlay([Body] Overlay overlay, string sessionName);
|
Task DeleteOverlay([Body] Overlay overlay, string sessionName);
|
||||||
|
|
||||||
|
[Post("/users/register")]
|
||||||
|
Task<HttpResponseMessage> Register([Body] RegisterModel registerModel);
|
||||||
|
|
||||||
|
[Post("/users/authenticate")]
|
||||||
|
Task<AuthenticateResponse> Authenticate([Body] AuthenticateModel registerModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
|
@ -243,7 +243,8 @@ namespace Sledgemapper
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
GraphicsDevice.Clear(new Color(24,118,157));
|
//GraphicsDevice.Clear(new Color(24,118,157));
|
||||||
|
GraphicsDevice.Clear(Color.LightGray);
|
||||||
|
|
||||||
var visibleTilesX = GraphicsDevice.Viewport.Width / _state._tileSize + 1;
|
var visibleTilesX = GraphicsDevice.Viewport.Width / _state._tileSize + 1;
|
||||||
var visibleTilesY = GraphicsDevice.Viewport.Height / _state._tileSize + 1;
|
var visibleTilesY = GraphicsDevice.Viewport.Height / _state._tileSize + 1;
|
||||||
|
@ -518,20 +519,8 @@ namespace Sledgemapper
|
||||||
var successful = false;
|
var successful = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var httpClientHandler = new HttpClientHandler();
|
|
||||||
|
_authResponse = await _communicationManager.Login(new AuthenticateModel
|
||||||
#if DEBUG
|
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback =
|
|
||||||
(message, certificate, chain, sslPolicyErrors) => true;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
var identiyApi = RestService.For<IIdentityApi>(
|
|
||||||
new HttpClient(httpClientHandler)
|
|
||||||
{
|
|
||||||
BaseAddress = new Uri("http://localhost:4000")
|
|
||||||
});
|
|
||||||
|
|
||||||
_authResponse = await identiyApi.Authenticate(new AuthenticateModel
|
|
||||||
{
|
{
|
||||||
Username = localContent.TxtEmail.Text,
|
Username = localContent.TxtEmail.Text,
|
||||||
Password = localContent.TxtPassword.Text
|
Password = localContent.TxtPassword.Text
|
||||||
|
@ -577,19 +566,7 @@ namespace Sledgemapper
|
||||||
var successful = false;
|
var successful = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var httpClientHandler = new HttpClientHandler();
|
var result = await _communicationManager.Register(new RegisterModel
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback =
|
|
||||||
(message, certificate, chain, sslPolicyErrors) => true;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
var identiyApi = RestService.For<IIdentityApi>(
|
|
||||||
new HttpClient(httpClientHandler)
|
|
||||||
{
|
|
||||||
BaseAddress = new Uri("http://localhost:4000")
|
|
||||||
});
|
|
||||||
var result = await identiyApi.Register(new RegisterModel
|
|
||||||
{
|
{
|
||||||
Username = localContent.TxtEmail.Text,
|
Username = localContent.TxtEmail.Text,
|
||||||
Password = localContent.TxtPassword.Text,
|
Password = localContent.TxtPassword.Text,
|
||||||
|
@ -597,9 +574,9 @@ namespace Sledgemapper
|
||||||
LastName = localContent.TxtLastname.Text,
|
LastName = localContent.TxtLastname.Text,
|
||||||
Initials = localContent.TxtInitials.Text
|
Initials = localContent.TxtInitials.Text
|
||||||
});
|
});
|
||||||
if (result.IsSuccessStatusCode)
|
if (result)
|
||||||
{
|
{
|
||||||
_authResponse = await identiyApi.Authenticate(new AuthenticateModel
|
_authResponse = await _communicationManager.Login(new AuthenticateModel
|
||||||
{
|
{
|
||||||
Username = localContent.TxtEmail.Text,
|
Username = localContent.TxtEmail.Text,
|
||||||
Password = localContent.TxtPassword.Text
|
Password = localContent.TxtPassword.Text
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Generated by MyraPad at 10/11/2020 15:49:08 */
|
/* Generated by MyraPad at 11/11/2020 09:47:53 */
|
||||||
using Myra.Graphics2D;
|
using Myra.Graphics2D;
|
||||||
using Myra.Graphics2D.TextureAtlases;
|
using Myra.Graphics2D.TextureAtlases;
|
||||||
using Myra.Graphics2D.UI;
|
using Myra.Graphics2D.UI;
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
<MenuItem Text="&About" Id="MenuHelpAbout" />
|
<MenuItem Text="&About" Id="MenuHelpAbout" />
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</HorizontalMenu>
|
</HorizontalMenu>
|
||||||
<VerticalSplitPane Width="200" Background="#666666FF" >
|
<VerticalSplitPane Width="200" Background="#666666FF">
|
||||||
<VerticalStackPanel>
|
<VerticalStackPanel>
|
||||||
<VerticalStackPanel.Proportions>
|
<VerticalStackPanel.Proportions>
|
||||||
<Proportion Type="Fill" />
|
<Proportion Type="Fill" />
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue