62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using Gameboard.ShogiUI.Sockets.Repositories;
|
|
using Gameboard.ShogiUI.Sockets.Repositories.RepositoryManagers;
|
|
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 IGameboardRepositoryManager manager;
|
|
private readonly IGameboardRepository repository;
|
|
|
|
public GameController(
|
|
IGameboardRepository repository,
|
|
IGameboardRepositoryManager manager)
|
|
{
|
|
this.manager = manager;
|
|
this.repository = repository;
|
|
}
|
|
|
|
[Route("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]
|
|
[Route("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();
|
|
}
|
|
}
|
|
}
|
|
}
|