Fix games API
This commit is contained in:
@@ -31,26 +31,7 @@ public class GamesController : Controller
|
||||
public IActionResult GetInfo([FromRoute] int id)
|
||||
{
|
||||
var game = _database.GetOrAddGame(id);
|
||||
return Ok(new
|
||||
{
|
||||
Turn = game.Board.Turn.AsChar,
|
||||
Ended = game.Board.IsEndGame,
|
||||
End = game.Board.EndGame?.EndgameType,
|
||||
Won = game.Board.EndGame?.WonSide,
|
||||
game.Board.MoveIndex,
|
||||
game.Board.WhiteKingChecked,
|
||||
game.Board.BlackKingChecked,
|
||||
links = new Dictionary<string, string>
|
||||
{
|
||||
{ "ascii", $"games/{id}.ascii" },
|
||||
{ "fen", $"games/{id}.fen" },
|
||||
{ "pgn", $"games/{id}.pgn" },
|
||||
{ "html", $"games/{id}.html" },
|
||||
{ "websocket", $"games/{id}.ws" },
|
||||
{ "move-post", $"games/{id}/move/{{player}}/{{move_algebraic_notation}}" }
|
||||
},
|
||||
Listeners = game.Channel.GetListenerCount()
|
||||
});
|
||||
return Ok(new GameContext(game, id));
|
||||
}
|
||||
|
||||
[Route("{id:int}.ws")]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Aiursoft.Scanner.Abstractions;
|
||||
using System.Collections.Concurrent;
|
||||
using Aiursoft.ChessServer.Models;
|
||||
using Chess;
|
||||
|
||||
namespace Aiursoft.ChessServer.Data;
|
||||
|
||||
@@ -8,9 +9,9 @@ public class InMemoryDatabase : ISingletonDependency
|
||||
{
|
||||
private ConcurrentDictionary<int, Game> Games { get; } = new();
|
||||
|
||||
public Game[] GetActiveGames()
|
||||
{
|
||||
return Games.Values.ToArray();
|
||||
public GameContext[] GetActiveGames()
|
||||
{
|
||||
return Games.Select(g => new GameContext(g.Value, g.Key)).ToArray();
|
||||
}
|
||||
|
||||
public Game GetOrAddGame(int id)
|
||||
@@ -18,3 +19,37 @@ public class InMemoryDatabase : ISingletonDependency
|
||||
return Games.GetOrAdd(id, _ => new Game());
|
||||
}
|
||||
}
|
||||
|
||||
public class GameContext
|
||||
{
|
||||
public GameContext(Game game, int id)
|
||||
{
|
||||
Turn = game.Board.Turn.AsChar;
|
||||
Ended = game.Board.IsEndGame;
|
||||
End = game.Board.EndGame?.EndgameType;
|
||||
Won = game.Board.EndGame?.WonSide;
|
||||
MoveIndex = game.Board.MoveIndex;
|
||||
WhiteKingChecked = game.Board.WhiteKingChecked;
|
||||
BlackKingChecked = game.Board.BlackKingChecked;
|
||||
links = new Dictionary<string, string>
|
||||
{
|
||||
{ "ascii", $"games/{id}.ascii" },
|
||||
{ "fen", $"games/{id}.fen" },
|
||||
{ "pgn", $"games/{id}.pgn" },
|
||||
{ "html", $"games/{id}.html" },
|
||||
{ "websocket", $"games/{id}.ws" },
|
||||
{ "move-post", $"games/{id}/move/{{player}}/{{move_algebraic_notation}}" }
|
||||
};
|
||||
Listeners = game.Channel.GetListenerCount();
|
||||
}
|
||||
public Dictionary<string, string> links { get; internal set; }
|
||||
|
||||
public char Turn { get; internal set; }
|
||||
public bool Ended { get; internal set; }
|
||||
public EndgameType? End { get; internal set; }
|
||||
public PieceColor? Won { get; internal set; }
|
||||
public int MoveIndex { get; internal set; }
|
||||
public bool WhiteKingChecked { get; internal set; }
|
||||
public bool BlackKingChecked { get; internal set; }
|
||||
public int Listeners { get; internal set; }
|
||||
}
|
||||
Reference in New Issue
Block a user