86 lines
2.9 KiB
C#
86 lines
2.9 KiB
C#
using Gameboard.ShogiUI.Sockets.Managers;
|
|
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using Gameboard.ShogiUI.Sockets.ServiceModels.Api.Messages;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Gameboard.ShogiUI.Sockets.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class GameController : ControllerBase
|
|
{
|
|
private readonly IGameboardManager manager;
|
|
private readonly ISocketConnectionManager communicationManager;
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public GameController(
|
|
IGameboardRepository repository,
|
|
IGameboardManager manager,
|
|
ISocketConnectionManager communicationManager)
|
|
{
|
|
this.manager = manager;
|
|
this.communicationManager = communicationManager;
|
|
this.repository = repository;
|
|
}
|
|
|
|
[HttpPost("JoinCode")]
|
|
public async Task<IActionResult> PostGameInvitation([FromBody] PostGameInvitation request)
|
|
{
|
|
var userName = HttpContext.User.Claims.First(c => c.Type == "preferred_username").Value;
|
|
var isPlayer1 = await manager.IsPlayer1(request.SessionName, userName);
|
|
if (isPlayer1)
|
|
{
|
|
var code = await repository.PostJoinCode(request.SessionName, userName);
|
|
return new CreatedResult("", new PostGameInvitationResponse(code));
|
|
}
|
|
else
|
|
{
|
|
return new UnauthorizedResult();
|
|
}
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost("GuestJoinCode")]
|
|
public async Task<IActionResult> PostGuestGameInvitation([FromBody] PostGuestGameInvitation request)
|
|
{
|
|
|
|
var isGuest = manager.IsGuest(request.GuestId);
|
|
var isPlayer1 = manager.IsPlayer1(request.SessionName, request.GuestId);
|
|
if (isGuest && await isPlayer1)
|
|
{
|
|
var code = await repository.PostJoinCode(request.SessionName, request.GuestId);
|
|
return new CreatedResult("", new PostGameInvitationResponse(code));
|
|
}
|
|
else
|
|
{
|
|
return new UnauthorizedResult();
|
|
}
|
|
}
|
|
|
|
// TODO: Use JWT tokens for guests so they can authenticate and use API routes, too.
|
|
//[Route("")]
|
|
//public async Task<IActionResult> PostSession([FromBody] PostSession request)
|
|
//{
|
|
// var model = new Models.Session(request.Name, request.IsPrivate, request.Player1, request.Player2);
|
|
// var success = await repository.CreateSession(model);
|
|
// if (success)
|
|
// {
|
|
// var message = new ServiceModels.Socket.Messages.CreateGameResponse(ServiceModels.Socket.Types.ClientAction.CreateGame)
|
|
// {
|
|
// Game = model.ToServiceModel(),
|
|
// PlayerName =
|
|
// }
|
|
// var task = request.IsPrivate
|
|
// ? communicationManager.BroadcastToPlayers(response, userName)
|
|
// : communicationManager.BroadcastToAll(response);
|
|
// return new CreatedResult("", null);
|
|
// }
|
|
// return new ConflictResult();
|
|
//}
|
|
}
|
|
}
|