|
|
@ -1,6 +1,224 @@
|
|
|
|
namespace WebApi.Controllers
|
|
|
|
using DTOs;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
using OrderCriterias;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WebApi.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class LobbyController
|
|
|
|
public class LobbyController : ControllerBase
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
private Unit unity;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly Logger<LobbyController> logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public LobbyController(Unit unit, Logger<LobbyController> logger)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.unity = unit;
|
|
|
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// add a lobby
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="lobby">the lobby to add</param>
|
|
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
|
|
/// status code :
|
|
|
|
|
|
|
|
/// 200 if the lobby is added
|
|
|
|
|
|
|
|
/// 202 if the lobby is added
|
|
|
|
|
|
|
|
/// 208 if the lobby was already in the database
|
|
|
|
|
|
|
|
/// 500 if there was an internal error that doesn't allow to continue
|
|
|
|
|
|
|
|
/// return content :
|
|
|
|
|
|
|
|
/// no content when status code = 200
|
|
|
|
|
|
|
|
/// lobby added when status code = 202
|
|
|
|
|
|
|
|
/// lobby already in the database that equals the one we wanted to add when status code = 208
|
|
|
|
|
|
|
|
/// no content when status code = 500
|
|
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
|
|
public async Task<IActionResult> PostLobby(LobbyDto lobby)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int count = unity.getNbLobbies(); // count : number of elements before opperation
|
|
|
|
|
|
|
|
var tmp = await unity.addLobby(lobby); // tmp : lobby recieve by the addLobby method
|
|
|
|
|
|
|
|
if (unity.getNbLobbies() == count) // <=> not added
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (tmp.Name == lobby.Name)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// it was already in the database
|
|
|
|
|
|
|
|
logger.LogInformation(message: $"want to add a lobby already in the database : {tmp.ToString()}");
|
|
|
|
|
|
|
|
return StatusCode(208, tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// we recieve a lobby already in the database
|
|
|
|
|
|
|
|
// that should be equal to the lobby that we
|
|
|
|
|
|
|
|
// wanted to add but it isn't
|
|
|
|
|
|
|
|
logger.LogCritical(message: "controller's add fail (already in the database) but\nlobby" +
|
|
|
|
|
|
|
|
$" recieve isn't the same as the one we wanted to add.\n" +
|
|
|
|
|
|
|
|
$"Lobby recieve : {tmp.ToString()}\nlobby to add : {lobby.ToString()}");
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// added
|
|
|
|
|
|
|
|
if (tmp.Name == lobby.Name)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogTrace(message: $"lobby added : {tmp.ToString()}");
|
|
|
|
|
|
|
|
// the lobby has been added and we recieved him
|
|
|
|
|
|
|
|
return StatusCode(202, tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// the lobby may be added but we do not recieved him
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (unity.getLobby(lobby.Name, lobby.IdCreator) != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// he is added
|
|
|
|
|
|
|
|
logger.LogError(message: $"lobby added but not recieved");
|
|
|
|
|
|
|
|
return Ok(); // not 202 to make a difference between 2 cases
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// he is not added
|
|
|
|
|
|
|
|
logger.LogCritical(message: "lobby that we wanted to add not added\nand we have added another one");
|
|
|
|
|
|
|
|
if (unity.getLobby(lobby.Name, lobby.IdCreator) == null) // <=> not added
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.LogError(message: "this case should not append");
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// get a part of all lobbies
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="page">the actual page</param>
|
|
|
|
|
|
|
|
/// <param name="count">number of T element in a page</param>
|
|
|
|
|
|
|
|
/// <param name="orderCriteria">the order criteria</param>
|
|
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
|
|
/// status code :
|
|
|
|
|
|
|
|
/// 200 if we got a set
|
|
|
|
|
|
|
|
/// 204 if we got an empty set
|
|
|
|
|
|
|
|
/// 400 if we got nothing
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// return content :
|
|
|
|
|
|
|
|
/// a tuple of the number of page and
|
|
|
|
|
|
|
|
/// all lobbies in the database for
|
|
|
|
|
|
|
|
/// the page nb if status code = 200
|
|
|
|
|
|
|
|
/// the number of page for count element if status code = 204
|
|
|
|
|
|
|
|
/// nothing if status code = 400
|
|
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetSomeLobbies(int page, int count = 10, LobbyOrderCriteria orderCriteria = LobbyOrderCriteria.ById)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var tmp = await unity.getLobbies(page, count, orderCriteria);
|
|
|
|
|
|
|
|
if (tmp.lobbies == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogInformation(message: "get lobby : bad request (page or/and count incorrect)");
|
|
|
|
|
|
|
|
return BadRequest(tmp.nbPages);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (tmp.lobbies.Count() == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogWarning(message: $"get lobby : no content. number of element : {unity.getNbLobbies()}, page wanted : {page}, number of elements in a page : {count}");
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogTrace(message: $"get lobbies : page = {page}, count = {count}, order criteria = {orderCriteria switch
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
LobbyOrderCriteria.ById => "byId",
|
|
|
|
|
|
|
|
LobbyOrderCriteria.ByName => "byName",
|
|
|
|
|
|
|
|
_ => "none"
|
|
|
|
|
|
|
|
}}");
|
|
|
|
|
|
|
|
return Ok(tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// delete a lobby
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="id">the id of the lobby to delete</param>
|
|
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
|
|
/// status code :
|
|
|
|
|
|
|
|
/// 200 if the lobby is removed
|
|
|
|
|
|
|
|
/// 204 if the lobby is removed
|
|
|
|
|
|
|
|
/// 400 if the id is incorrect
|
|
|
|
|
|
|
|
/// 500 if there was an internal error that doesn't allow to continue
|
|
|
|
|
|
|
|
/// return content :
|
|
|
|
|
|
|
|
/// aministrator deleted when status code = 200
|
|
|
|
|
|
|
|
/// no content when status code = 204
|
|
|
|
|
|
|
|
/// no content when status code = 400
|
|
|
|
|
|
|
|
/// no content when status code = 500
|
|
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
|
|
public async Task<IActionResult> DeleteLobby(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (id < 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogError("want to delete a lobby with an id less than 0");
|
|
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int count = unity.getNbLobbies(); // count : number of elements before opperation
|
|
|
|
|
|
|
|
var tmp = await unity.removeLobby(id);
|
|
|
|
|
|
|
|
if (tmp == null) // we don't recieve the lobby
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (unity.getNbLobbies() == count) // he is not deleted
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (unity.getLobby(id) != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogCritical(message: "remove lobby fail : lobby not removed and not recieved");
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogInformation(message: "trying to remove a lobby with an id who don't exist");
|
|
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else // he may be deleted
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (unity.getLobby(id) == null) // he must be deleted
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogError(message: "lobby removed but not returned");
|
|
|
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else // he is not deleted
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
logger.LogCritical(message: "remove lobby fail : lobby to remove not remove\ninstead, anotherone is deleted and we recieved nothing");
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unity.getNbLobbies() == count)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// <=> we have recieved a lobby which should be deleted
|
|
|
|
|
|
|
|
// but since we have the same number of lobby than
|
|
|
|
|
|
|
|
// before deletion, it isn't deleted
|
|
|
|
|
|
|
|
logger.LogCritical(message: $"lobby \"{tmp.ToString()}\"should be delete but it isn't");
|
|
|
|
|
|
|
|
return StatusCode(500);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.LogTrace(message: $"lobby removed {tmp.ToString()}");
|
|
|
|
|
|
|
|
return Ok(tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// get a lobby
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="id">the id of the lobby</param>
|
|
|
|
|
|
|
|
/// <returns>
|
|
|
|
|
|
|
|
/// status code :
|
|
|
|
|
|
|
|
/// 200 when we got a lobby
|
|
|
|
|
|
|
|
/// 204 when we got null
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// return content :
|
|
|
|
|
|
|
|
/// the lobby that correspond to the id when status code = 200
|
|
|
|
|
|
|
|
/// nothing when status code 204
|
|
|
|
|
|
|
|
/// </returns>
|
|
|
|
|
|
|
|
public async Task<IActionResult> GetLobby(int id)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var tmp = await unity.getLobby(id);
|
|
|
|
|
|
|
|
if (tmp == null) return NoContent();
|
|
|
|
|
|
|
|
return Ok(tmp);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|