rectoring

This commit is contained in:
Michele Scandura 2020-11-13 21:38:36 +00:00
parent 628fab2146
commit 1ae0ee5a8a
16 changed files with 135 additions and 62 deletions

View 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;
}
}
}

View file

@ -1,18 +0,0 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
namespace Sledgemapper.Api.Controllers
{
[Route("identity")]
[Authorize]
public class IdentityController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new JsonResult(from c in User.Claims select new { c.Type, c.Value });
}
}
}

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Handlers;
using Sledgemapper.Shared.Entities;
using System.Threading.Tasks;
@ -8,13 +9,22 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Controllers
{
[Authorize]
[Route("[controller]/{sessionName}")]
[Route("[controller]/{sessionName}")]
public class SessionController : ControllerBase
{
private readonly IMediator _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")]
public async Task Post(string sessionName, [FromBody]Tile tile)
{

View 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;
}
}
}

View file

@ -15,12 +15,15 @@ using Microsoft.AspNetCore.Authorization;
namespace SignalRChat.Hubs
{
[Authorize]
public class SledgemapperHub : Hub<ISledgemapperClient>
{
public SledgemapperHub() {
}
public SledgemapperHub()
{
}
private static Dictionary<string, Session> _sessions = new Dictionary<string, Session>();
public List<string> Colors = new List<string>{"CC0000",
"CC3300",
@ -113,6 +116,10 @@ namespace SignalRChat.Hubs
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();
session.Colors = new List<string>(Colors);
session.Colors.Shuffle();
@ -120,7 +127,7 @@ namespace SignalRChat.Hubs
session.Players.Add(player);
_sessions.Add(sessionName, session);
await Groups.AddToGroupAsync(Context.ConnectionId, sessionName);
return session;
}

View 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.

Binary file not shown.

View 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;
}
}

View file

@ -19,7 +19,7 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.Text;
using AutoMapper;
using Sledgemapper.Services;
using System.Security.Claims;
namespace SignalRChat
{
@ -79,6 +79,8 @@ namespace SignalRChat
// return unauthorized if user no longer exists
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;
}
};

Binary file not shown.

View file

@ -17,6 +17,7 @@ namespace Sledgemapper
public HubConnection Connection { get; private set; }
public Session SessionData;
private ChannelsQueue Queue = new ChannelsQueue();
private AuthenticateResponse _authenticateResponse;
public CommunicationManager(Session sessionData)
{
@ -24,12 +25,15 @@ namespace Sledgemapper
Connection = new HubConnectionBuilder()
.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")
.Build();
var httpClientHandler = new HttpClientHandler();
@ -58,7 +62,7 @@ namespace Sledgemapper
var p = SessionData.Players.FirstOrDefault(m => m.ConnectionId == player.ConnectionId);
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);
if (p is null)
{
SessionData.Players.Add(player);
SessionData.Players.Add(player);
}
else
{
p.Color = player.Color;
p.Position = player.Position;
p.Color = player.Color;
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)
{
await Policy

View file

@ -3,6 +3,7 @@ using Sledgemapper.Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
@ -27,5 +28,11 @@ namespace Sledgemapper
[Delete("/session/{sessionName}/overlay")]
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);
}
}

View file

@ -243,7 +243,8 @@ namespace Sledgemapper
{
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 visibleTilesY = GraphicsDevice.Viewport.Height / _state._tileSize + 1;
@ -518,20 +519,8 @@ namespace Sledgemapper
var successful = false;
try
{
var httpClientHandler = new HttpClientHandler();
#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
_authResponse = await _communicationManager.Login(new AuthenticateModel
{
Username = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text
@ -577,19 +566,7 @@ namespace Sledgemapper
var successful = false;
try
{
var httpClientHandler = new HttpClientHandler();
#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
var result = await _communicationManager.Register(new RegisterModel
{
Username = localContent.TxtEmail.Text,
Password = localContent.TxtPassword.Text,
@ -597,9 +574,9 @@ namespace Sledgemapper
LastName = localContent.TxtLastname.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,
Password = localContent.TxtPassword.Text

View file

@ -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.TextureAtlases;
using Myra.Graphics2D.UI;

View file

@ -23,7 +23,7 @@
<MenuItem Text="&amp;About" Id="MenuHelpAbout" />
</MenuItem>
</HorizontalMenu>
<VerticalSplitPane Width="200" Background="#666666FF" >
<VerticalSplitPane Width="200" Background="#666666FF">
<VerticalStackPanel>
<VerticalStackPanel.Proportions>
<Proportion Type="Fill" />