Merge branch 'master' of ssh://gitlab.aiursoft.cn:2202/aiursoft/chessserver

This commit is contained in:
xuef
2024-06-20 07:57:31 +00:00
21 changed files with 612 additions and 402 deletions
+2
View File
@@ -116,6 +116,8 @@ deploy_docker:
- lint
- test
script:
- if [ "$CI_COMMIT_REF_NAME" = "master" ]; then TAG="latest"; else TAG="$CI_COMMIT_REF_NAME"; fi
- echo building image hub.aiursoft.cn/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME:$TAG
- docker build . -t hub.aiursoft.cn/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME:latest
- docker push hub.aiursoft.cn/$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME:latest
rules:
+3 -1
View File
@@ -5,7 +5,9 @@
[![Test Coverage](https://gitlab.aiursoft.cn/aiursoft/ChessServer/badges/master/coverage.svg)](https://gitlab.aiursoft.cn/aiursoft/ChessServer/-/pipelines)
[![ManHours](https://manhours.aiursoft.cn/r/gitlab.aiursoft.cn/aiursoft/ChessServer.svg)](https://gitlab.aiursoft.cn/aiursoft/ChessServer/-/commits/master?ref_type=heads)
ChessServer is just a simple chess server for [Aiursoft](https://www.aiursoft.com) to test our new features.
ChessServer is just a simple chess server. Based on WebSocket. Can be played with sharing link with friends. No sign up required.
![overview](./screenshot.png)
## Try
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 237 KiB

@@ -17,4 +17,17 @@ public class ValidNickName : ValidationAttribute
{
return this.IsValid(value) ? ValidationResult.Success : new ValidationResult("The " + validationContext.DisplayName + " can only contain numbers, alphabet and underline.");
}
}
public class IsGuid : ValidationAttribute
{
public override bool IsValid(object? value)
{
return value is string input && Guid.TryParse(input, out _);
}
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
return this.IsValid(value) ? ValidationResult.Success : new ValidationResult("The " + validationContext.DisplayName + " is not a valid GUID.");
}
}
@@ -0,0 +1,166 @@
using System.ComponentModel.DataAnnotations;
using Aiursoft.AiurObserver.Extensions;
using Aiursoft.AiurObserver.WebSocket.Server;
using Aiursoft.ChessServer.Attributes;
using Aiursoft.ChessServer.Data;
using Aiursoft.ChessServer.Models;
using Aiursoft.ChessServer.Models.ViewModels;
using Aiursoft.CSTools.Services;
using Aiursoft.WebTools.Attributes;
using Microsoft.AspNetCore.Mvc;
namespace Aiursoft.ChessServer.Controllers;
[Route("challenge")]
public class ChallengesController (
Counter counter,
InMemoryDatabase database) : Controller
{
[Route("")]
[HttpGet]
public IActionResult Auto(Guid playerId)
{
// I created a challenge. Go to my challenge.
var myChallengeKey = database.GetMyOpenChallenge(playerId);
if (myChallengeKey != null)
{
return RedirectToAction(nameof(Challenge), new { id = (int)myChallengeKey });
}
// Exists a public challenge. Go to that challenge.
var otherChallenge = database.GetFirstPublicChallengeKey();
if (otherChallenge != null)
{
return RedirectToAction(nameof(Challenge), new { id = (int)otherChallenge });
}
// Create a new challenge.
return RedirectToAction(nameof(Create), new { playerId });
}
[HttpGet]
[Route("create")]
public IActionResult Create(Guid playerId)
{
var myChallengeKey = database.GetMyOpenChallenge(playerId);
if (myChallengeKey != null)
{
// This player already has a challenge. Redirect to that challenge.
return RedirectToAction(nameof(Challenge), new { id = myChallengeKey, playerId });
}
var model = new CreateChallengeViewModel();
return View(model);
}
[HttpPost]
[Route("create")]
public IActionResult Create(CreateChallengeViewModel model)
{
// Ensure single challenge can be created by a player.
var myChallengeKey = database.GetMyOpenChallenge(model.CreatorId);
if (myChallengeKey != null)
{
ModelState.AddModelError(nameof(model.CreatorId), "You already have a challenge!");
}
if (!ModelState.IsValid)
{
return View(model);
}
// Create a new challenge.
var player = database.GetOrAddPlayer(model.CreatorId);
var challenge = new Challenge(
creator: player,
message: model.Message,
roleRule: model.RoleRule,
timeLimit: model.TimeLimit,
permission: model.Permission);
var challengeId = counter.GetUniqueNo();
database.CreateChallenge(challengeId, challenge);
return RedirectToAction(nameof(Challenge), new { id = challengeId });
}
[HttpGet]
[Route("{id:int}")]
public IActionResult Challenge(int id)
{
var challenge = database.GetChallenge(id);
if (challenge == null)
{
// Challenge not found.
return NotFound();
}
if (challenge is AcceptedChallenge)
{
// Challenge already accepted.
return RedirectToAction(nameof(GamesController.GetHtml), "Games", new { id });
}
var model = new ChallengeViewModel
{
ChallengeId = id,
};
return View(model);
}
[HttpPost]
[Route("{id:int}/drop")]
public IActionResult Drop([FromRoute][Required]int id, [FromForm][Required][IsGuid]string playerId)
{
if (!ModelState.IsValid)
{
return RedirectToAction(nameof(Challenge), new { id });
}
var challenge = database.GetChallenge(id);
if (challenge != null && challenge.Creator.Id == Guid.Parse(playerId))
{
database.DeleteChallenge(id);
}
return RedirectToAction(nameof(Index), "Home");
}
[HttpPost]
[Route("accept/{id:int}")]
public async Task<IActionResult> Accept([FromRoute]int id, [FromQuery]Guid playerId)
{
try
{
await database.PatchChallengeAsAcceptedAsync(id, playerId);
}
catch (InvalidOperationException e)
{
return BadRequest(e.Message);
}
return Ok();
}
[EnforceWebSocket]
[Route("listen/{id:int}.ws")]
public async Task ListenChallenge(int id)
{
var pusher = await HttpContext.AcceptWebSocketClient();
var challenge = database.GetChallenge(id);
if (challenge == null)
{
return;
}
var outSub = challenge
.ChallengeChangedChannel
.Subscribe(t => pusher.Send(t, HttpContext.RequestAborted));
try
{
await pusher.Listen(HttpContext.RequestAborted);
}
catch (TaskCanceledException)
{
// Ignore. This happens when the client closes the connection.
}
finally
{
await pusher.Close(HttpContext.RequestAborted);
outSub.Unsubscribe();
}
}
}
@@ -4,6 +4,7 @@ using Aiursoft.ChessServer.Data;
using Aiursoft.ChessServer.Models.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Aiursoft.AiurObserver.Extensions;
using Aiursoft.ChessServer.Attributes;
using Aiursoft.WebTools.Attributes;
namespace Aiursoft.ChessServer.Controllers;
@@ -11,46 +12,73 @@ namespace Aiursoft.ChessServer.Controllers;
[Route("games")]
public class GamesController(InMemoryDatabase database) : Controller
{
[Route("")]
public IActionResult GetAll()
{
var games = database.GetActiveGames();
return Ok(games);
}
[Route("{id:int}.json")]
public IActionResult GetInfo([FromRoute] int id)
{
var game = database.GetOrAddGame(id);
return Ok(new GameContext(game, id));
var challenge = database.GetAcceptedChallenge(id);
if (challenge == null)
{
return NotFound();
}
return Ok(new ChallengeContext(challenge, id));
}
[Route("{id:int}.color")]
public IActionResult GetColor([FromRoute] int id, [FromQuery][IsGuid] string playerId)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var playerGuid = Guid.Parse(playerId);
var challenge = database.GetAcceptedChallenge(id);
if (challenge == null)
{
return NotFound();
}
return Ok(challenge.GetPlayerColor(playerGuid));
}
[Route("{id:int}.ws")]
[EnforceWebSocket]
public async Task GetWebSocket([FromRoute] int id, [FromQuery]string player)
public async Task GetWebSocket([FromRoute] int id, [FromQuery][IsGuid] string playerId)
{
if (!ModelState.IsValid)
{
return;
}
var playerGuid = Guid.Parse(playerId);
var challenge = database.GetAcceptedChallenge(id);
if (challenge == null)
{
return;
}
var pusher = await HttpContext.AcceptWebSocketClient();
var game = database.GetOrAddGame(id);
var outSub = game
var outSub = challenge.Game
.FenChangedChannel
.Subscribe(t => pusher.Send(t, HttpContext.RequestAborted));
var inSub = pusher
.Filter(t => !string.IsNullOrWhiteSpace(t))
.Subscribe(async move =>
{
lock (game.MovePieceLock)
{
if (!game.Board.IsEndGame &&
game.Board.IsValidMove(move) &&
game.Board.Turn.AsChar.ToString() == player)
lock (challenge.Game.MovePieceLock)
{
game.Board.Move(move);
if (!challenge.Game.Board.IsEndGame &&
challenge.Game.Board.IsValidMove(move) &&
challenge.GetTurnPlayer().Id == playerGuid)
{
challenge.Game.Board.Move(move);
}
}
}
await game.FenChangedChannel.BroadcastAsync(game.Board.ToFen());
});
await challenge.Game.FenChangedChannel.BroadcastAsync(challenge.Game.Board.ToFen());
});
try
{
await pusher.Listen(HttpContext.RequestAborted);
@@ -70,27 +98,48 @@ public class GamesController(InMemoryDatabase database) : Controller
[Route("{id:int}.ascii")]
public IActionResult GetAscii([FromRoute] int id)
{
var game = database.GetOrAddGame(id);
var game = database.GetAcceptedChallenge(id)?.Game;
if (game == null)
{
return NotFound();
}
return Ok(game.Board.ToAscii());
}
[Route("{id:int}.html")]
public IActionResult GetHtml([FromRoute] int id)
{
var game = database.GetAcceptedChallenge(id)?.Game;
if (game == null)
{
return NotFound();
}
return View(id);
}
[Route("{id:int}.fen")]
public IActionResult GetFen([FromRoute] int id)
{
var game = database.GetOrAddGame(id);
var game = database.GetAcceptedChallenge(id)?.Game;
if (game == null)
{
return NotFound();
}
return Ok(game.Board.ToFen());
}
[Route("{id:int}.pgn")]
public IActionResult GetPgn([FromRoute] int id)
{
var game = database.GetOrAddGame(id);
var game = database.GetAcceptedChallenge(id)?.Game;
if (game == null)
{
return NotFound();
}
return Ok(game.Board.ToPgn());
}
}
@@ -1,202 +1,28 @@
using Aiursoft.AiurObserver.Extensions;
using Aiursoft.AiurObserver.WebSocket.Server;
using Aiursoft.ChessServer.Data;
using Aiursoft.ChessServer.Models;
using Aiursoft.ChessServer.Models.ViewModels;
using Aiursoft.CSTools.Services;
using Aiursoft.WebTools.Attributes;
using Microsoft.AspNetCore.Mvc;
namespace Aiursoft.ChessServer.Controllers;
public class HomeController(
Counter counter,
InMemoryDatabase database) : Controller
public class HomeController(InMemoryDatabase database) : Controller
{
[HttpGet]
public IActionResult Index()
{
var model = new IndexViewModel
{
Challenges = database.GetPublicUnAcceptedChallenges()
};
return View(model);
}
/// <summary>
/// This action will auto redirect to a challenge.
///
/// If the player has a challenge, then go to that challenge.
///
/// If not, then go to a public challenge. If not, then create a new challenge.
/// </summary>
/// <param name="playerId"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Auto(Guid playerId)
{
// I created a challenge. Go to my challenge.
var myChallengeKey = database.GetMyChallengeKey(playerId);
if (myChallengeKey != null)
{
return RedirectToAction(nameof(Challenge), new { id = (int)myChallengeKey, playerId });
}
// Exists a public challenge. Go to that challenge.
var otherChallenge = database.GetFirstPublicChallengeKey();
if (otherChallenge != null)
{
return RedirectToAction(nameof(Challenge), new { id = (int)otherChallenge, playerId });
}
// Create a new challenge.
return RedirectToAction(nameof(Create), new { playerId });
}
/// <summary>
/// This action renders a page to create a new challenge.
///
/// However, if the player has a challenge, then redirect to that challenge.
/// </summary>
/// <param name="playerId"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Create(Guid playerId)
{
var myChallengeKey = database.GetMyChallengeKey(playerId);
if (myChallengeKey != null)
{
return RedirectToAction(nameof(Challenge), new { id = myChallengeKey, playerId });
}
var model = new CreateChallengeViewModel();
return View(model);
}
[HttpPost]
public IActionResult Create(CreateChallengeViewModel model)
{
// Ensure single challenge can be created by a player.
var myChallengeKey = database.GetMyChallengeKey(model.CreatorId);
if (myChallengeKey != null)
{
ModelState.AddModelError(nameof(model.CreatorId), "You already have a challenge!");
}
if (!ModelState.IsValid)
{
return View(model);
}
// Create a new challenge.
var player = database.GetOrAddPlayer(model.CreatorId);
var challenge = new Challenge(player)
{
RoleRule = model.RoleRule,
Message = model.Message,
Permission = model.Permission,
TimeLimit = model.TimeLimit,
};
var challengeId = counter.GetUniqueNo();
database.CreateChallenge(challengeId, challenge);
return RedirectToAction(nameof(Challenge), new { id = challengeId });
}
/// <summary>
/// This action renders a page to show the details of a challenge.
///
/// Will use JavaScript to call accept challenge API.
///
/// Will use WebSocket to listen to the challenge changes.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
public IActionResult Challenge(int id)
{
var challenge = database.GetChallenge(id);
if (challenge == null)
{
// Challenge not found.
return NotFound();
}
var model = new ChallengeViewModel
{
ChallengeId = id,
Challenges = database.GetPublicOpenChallenges()
};
return View(model);
}
[HttpPost]
public IActionResult DropChallenge(DropChallengeViewModel model)
[HttpGet]
public IActionResult Watch()
{
if (!ModelState.IsValid)
var model = new IndexViewModel
{
return RedirectToAction(nameof(Challenge), new { id = model.Id, playerId = model.PlayerId });
}
var challenge = database.GetChallenge(model.Id);
if (challenge != null && challenge.Creator.Id == model.PlayerId)
{
database.DeleteChallenge(model.Id);
}
return RedirectToAction(nameof(Index));
}
/// <summary>
/// This method accepts a challenge and updates the challenge's accepter.
/// </summary>
/// <param name="id">The ID of the challenge to accept.</param>
/// <param name="playerId">The ID of the player accepting the challenge.</param>
/// <returns>An IActionResult indicating the result of the operation.</returns>
[HttpPost]
public async Task<IActionResult> AcceptChallenge([FromRoute]int id, [FromQuery]Guid playerId)
{
var challenge = database.GetChallenge(id);
if (challenge == null)
{
return NotFound();
}
if (challenge.Accepter != null)
{
// Challenge already accepted.
return BadRequest("Challenge already accepted.");
}
if (challenge.Creator.Id == playerId)
{
// Cannot accept your own challenge.
return BadRequest("Cannot accept your own challenge!");
}
challenge.Accepter = database.GetOrAddPlayer(playerId);
var (newGameId, newGame) = database.AddNewGameAndGetId();
challenge.Game = newGame;
challenge.GameId = newGameId;
await challenge.ChallengeChangedChannel.BroadcastAsync("game-started-at-" + newGameId);
return Ok();
}
[Route("listen-challenge/{id:int}.ws")]
[EnforceWebSocket]
public async Task ListenChallenge(int id)
{
var pusher = await HttpContext.AcceptWebSocketClient();
var challenge = database.GetChallenge(id);
if (challenge == null)
{
return;
}
var outSub = challenge
.ChallengeChangedChannel
.Subscribe(t => pusher.Send(t, HttpContext.RequestAborted));
try
{
await pusher.Listen(HttpContext.RequestAborted);
}
catch (TaskCanceledException)
{
// Ignore. This happens when the client closes the connection.
}
finally
{
await pusher.Close(HttpContext.RequestAborted);
outSub.Unsubscribe();
}
Challenges = database.GetOnGoingOpenChallenges()
};
return View(nameof(Index), model);
}
}
@@ -17,11 +17,11 @@ public class PlayersController(InMemoryDatabase database) : ControllerBase
[HttpPut]
[Route("{id:guid}/new-name/{nickname}")]
public IActionResult ChangeNickname([Required]Guid id, [Required][MaxLength(20)][ValidNickName]string nickname)
public IActionResult ChangeNickname([Required]Guid id, [Required][MaxLength(40)][ValidNickName]string nickname)
{
if (ModelState.IsValid == false)
{
return BadRequest();
return BadRequest("Only numbers, alphabet and underline are allowed in nickname. Max length is 40.");
}
var me = database.GetOrAddPlayer(id);
@@ -1,45 +1,21 @@
using Aiursoft.Scanner.Abstractions;
using System.Collections.Concurrent;
using Aiursoft.ChessServer.Models;
using Aiursoft.ChessServer.Models.ViewModels;
namespace Aiursoft.ChessServer.Data;
public class InMemoryDatabase : ISingletonDependency
{
private ConcurrentDictionary<int, Game> Games { get; } = new();
private ConcurrentDictionary<Guid, Player> Players { get; } = new();
private ConcurrentDictionary<int, Challenge> Challenges { get; } = new();
public GameContext[] GetActiveGames()
{
return Games.Select(g => new GameContext(g.Value, g.Key)).ToArray();
}
public Game GetOrAddGame(int id)
{
lock (Games)
{
return Games.GetOrAdd(id, _ => new Game());
}
}
public (int gameId, Game game) AddNewGameAndGetId()
{
lock (Games)
{
for (var id = 1;; id++)
{
if (Games.ContainsKey(id)) continue;
var newGame = new Game();
Games.TryAdd(id, newGame);
return (id, newGame);
}
}
}
private IEnumerable<KeyValuePair<int, Challenge>> OpenChallenges =>
Challenges.Where(t => t.Value is not AcceptedChallenge);
private IEnumerable<KeyValuePair<int, Challenge>> OnGoingChallenges =>
Challenges.Where(t => t.Value is AcceptedChallenge);
public Player GetOrAddPlayer(Guid id)
{
lock (Players)
@@ -50,18 +26,42 @@ public class InMemoryDatabase : ISingletonDependency
});
}
}
public IReadOnlyCollection<KeyValuePair<int, Challenge>> GetPublicUnAcceptedChallenges()
public IReadOnlyCollection<KeyValuePair<int, Challenge>> GetPublicOpenChallenges()
{
lock (Challenges)
{
return Challenges
return OpenChallenges
.Where(t => t.Value.Permission == ChallengePermission.Public)
.Where(t => t.Value.Accepter == null)
.ToArray();
}
}
public IReadOnlyCollection<KeyValuePair<int, Challenge>> GetOnGoingOpenChallenges()
{
lock (Challenges)
{
return OnGoingChallenges
.Where(t => t.Value.Permission == ChallengePermission.Public)
.ToArray();
}
}
public int? GetMyOpenChallenge(Guid playerId)
{
lock (Challenges)
{
if (OpenChallenges.All(t => t.Value.Creator.Id != playerId))
{
return null;
}
return OpenChallenges
.FirstOrDefault(t => t.Value.Creator.Id == playerId)
.Key;
}
}
public Challenge? GetChallenge(int id)
{
lock (Challenges)
@@ -70,31 +70,54 @@ public class InMemoryDatabase : ISingletonDependency
}
}
public int? GetMyChallengeKey(Guid playerId)
public AcceptedChallenge? GetAcceptedChallenge(int id)
{
var challenge = GetChallenge(id);
return challenge as AcceptedChallenge;
}
public async Task PatchChallengeAsAcceptedAsync(int id, Guid accepter)
{
lock (Challenges)
{
if (Challenges.All(t => t.Value.Creator.Id != playerId))
var challenge = Challenges.GetValueOrDefault(id);
if (challenge == null)
{
return null;
throw new InvalidOperationException("Challenge not found!");
}
if (challenge.Creator.Id == accepter)
{
throw new InvalidOperationException("Cannot accept your own challenge!");
}
if (challenge is AcceptedChallenge)
{
throw new InvalidOperationException("Challenge already accepted!");
}
return Challenges
.FirstOrDefault(t => t.Value.Creator.Id == playerId)
.Key;
var newChallenge = new AcceptedChallenge(
creator: challenge.Creator,
accepter: GetOrAddPlayer(accepter),
message: challenge.Message,
roleRule: challenge.RoleRule,
timeLimit: challenge.TimeLimit,
permission: challenge.Permission,
challengeChangedChannel: challenge.ChallengeChangedChannel);
Challenges[id] = newChallenge;
}
await Challenges.GetValueOrDefault(id)!.ChallengeChangedChannel.BroadcastAsync("game-started");
}
public int? GetFirstPublicChallengeKey()
{
lock (Challenges)
{
if (Challenges.All(t => t.Value.Permission != ChallengePermission.Public))
if (OpenChallenges.All(t => t.Value.Permission != ChallengePermission.Public))
{
return null;
}
return Challenges
return OpenChallenges
.FirstOrDefault(t => t.Value.Permission == ChallengePermission.Public)
.Key;
}
+64 -11
View File
@@ -2,21 +2,21 @@ using Aiursoft.AiurObserver;
namespace Aiursoft.ChessServer.Models;
public class Challenge(Player creator)
public class Challenge(
Player creator,
string message,
RoleRule roleRule,
TimeSpan timeLimit,
ChallengePermission permission)
{
public Player Creator { get; set; } = creator;
public string Message { get; set; } = "A chess room.";
public Player? Accepter { get; set; }
public string Message { get; set; } = message;
public int? GameId { get; set; }
public Game? Game { get; set; }
public RoleRule RoleRule { get; set; } = roleRule;
public RoleRule RoleRule { get; set; } = RoleRule.Random;
public TimeSpan TimeLimit { get; set; } = timeLimit;
public TimeSpan TimeLimit { get; set; } = TimeSpan.FromMinutes(10);
public ChallengePermission Permission { get; set; } = ChallengePermission.Public;
public ChallengePermission Permission { get; set; } = permission;
// Possible messages:
// Player joined: p-joined-{player-nick-name}
@@ -25,5 +25,58 @@ public class Challenge(Player creator)
// Game started: game-started
// Creator transferred: creator-transferred-{new-owner-player-nick-name}
// Settings changed: settings-changed
public AsyncObservable<string> ChallengeChangedChannel { get; } = new();
public AsyncObservable<string> ChallengeChangedChannel { get; protected init; } = new();
}
public class AcceptedChallenge : Challenge
{
private readonly bool _creatorIsWhite;
public AcceptedChallenge(
Player creator,
Player accepter,
string message,
RoleRule roleRule,
TimeSpan timeLimit,
ChallengePermission permission,
AsyncObservable<string> challengeChangedChannel)
: base(creator, message, roleRule, timeLimit, permission)
{
Accepter = accepter;
Game = new Game();
ChallengeChangedChannel = challengeChangedChannel;
_creatorIsWhite = RoleRule switch
{
RoleRule.Random => new Random().Next(0, 2) == 0,
RoleRule.CreatorWhite => true,
RoleRule.AccepterWhite => false,
_ => _creatorIsWhite
};
}
public Player Accepter { get; set; }
public Game Game { get; set; }
public Player GetWhitePlayer() => _creatorIsWhite ? Creator : Accepter;
public Player GetBlackPlayer() => _creatorIsWhite ? Accepter : Creator;
public DateTime GameStartTime { get; set; } = DateTime.UtcNow;
public Player GetTurnPlayer()
{
return Game.Board.Turn.AsChar.ToString().Equals("w", StringComparison.CurrentCultureIgnoreCase)
? GetWhitePlayer() : GetBlackPlayer();
}
public string GetPlayerColor(Guid playerId)
{
// returns : w,b, or m (monitor)
if (GetWhitePlayer().Id == playerId)
{
return "w";
}
if (GetBlackPlayer().Id == playerId)
{
return "b";
}
return "m";
}
}
@@ -11,8 +11,7 @@ public class GameContext(Game game, int id)
{ "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}}" }
{ "websocket", $"games/{id}.ws" }
};
public char Turn { get; } = game.Board.Turn.AsChar;
@@ -23,4 +22,27 @@ public class GameContext(Game game, int id)
public bool WhiteKingChecked { get; } = game.Board.WhiteKingChecked;
public bool BlackKingChecked { get; } = game.Board.BlackKingChecked;
public int Listeners { get; } = game.FenChangedChannel.GetListenerCount();
}
}
public class ChallengeContext(AcceptedChallenge challenge, int id)
{
public GameContext Game { get; } = new(challenge.Game, id);
public string WhitePlayer { get; } = challenge.GetWhitePlayer().NickName;
public string BlackPlayer { get; } = challenge.GetBlackPlayer().NickName;
public string Creator { get; } = challenge.Creator.NickName;
public string Accepter { get; } = challenge.Accepter.NickName;
public string Message { get; } = challenge.Message;
public RoleRule RoleRule { get; } = challenge.RoleRule;
public TimeSpan TimeLimit { get; } = challenge.TimeLimit;
public ChallengePermission Permission { get; } = challenge.Permission;
public DateTime StartTime { get; } = challenge.GameStartTime;
}
@@ -1,14 +1,13 @@
@using Aiursoft.ChessServer.Controllers
@model Aiursoft.ChessServer.Models.ViewModels.ChallengeViewModel
@model Aiursoft.ChessServer.Models.ViewModels.ChallengeViewModel
<div class="jumbotron">
<div class="container">
<h1 class="display-4">Waiting for opponent joining...</h1>
<p class="lead">Please share the link of this room to your friend!</p>
@{
var link = $"{Context.Request.Scheme}://{Context.Request.Host}/Home/{nameof(HomeController.Challenge)}/{Model.ChallengeId}";
var link = $"{Context.Request.Scheme}://{Context.Request.Host}/challenge/{Model.ChallengeId}";
}
<form asp-controller="Home" asp-action="DropChallenge" asp-route-id="@Model.ChallengeId" method="post" class="d-inline" asp-antiforgery="false">
<form asp-controller="Challenges" asp-action="Drop" asp-route-id="@Model.ChallengeId" method="post" class="d-inline" asp-antiforgery="false">
<div asp-validation-summary="All" class="text-danger"></div>
<input type="hidden" name="playerId" value=""/>
@@ -48,23 +47,19 @@
// Listen to the channel
const wsScheme = window.location.protocol === "https:" ? "wss://" : "ws://";
const socket = new WebSocket(
`${wsScheme}${window.location.host}/listen-challenge/${@Model.ChallengeId}.ws`
`${wsScheme}${window.location.host}/challenge/listen/${@Model.ChallengeId}.ws`
);
socket.addEventListener('open', function (event) {
console.log('WebSocket is open now.');
// Accept the challenge
acceptChallenge(@Model.ChallengeId, getUserId());
socket.addEventListener('open', function () {
console.log('WebSocket is open now. Accepting challenge...');
acceptChallenge(@Model.ChallengeId);
});
socket.onmessage = function (event) {
// alert(event.data);
// May get data like: game-started-at-{newGameId}
if (event.data.startsWith('game-started-at-')) {
const gameId = event.data.replace('game-started-at-', '');
const gameIdNumber = parseInt(gameId);
if (gameIdNumber) {
// May get data like: game-started
if (event.data.startsWith('game-started')) {
const gameId = @Model.ChallengeId;
if (gameId) {
window.location.href = `/games/${gameId}.html`;
}
}
@@ -15,7 +15,7 @@
<div class="card mb-2 col-sm-12 px-1">
<div class="card-body">
<h5 class="card-title">Create a new room</h5>
<form asp-controller="Home" asp-action="Create" method="post" asp-antiforgery="false">
<form asp-controller="Challenges" asp-action="Create" method="post" asp-antiforgery="false">
<div asp-validation-summary="All" class="text-danger"></div>
<input type="hidden" asp-for="CreatorId"/>
<div class="form-group">
@@ -3,8 +3,27 @@
<div class="container mt-5">
<main role="main" class="pb-3">
<div class="row">
<vc:chess-board game-id="@Model"></vc:chess-board>
<div class="col-md-12">
<h5 id="versus-info" class="text-center text-wrap text-primary mt-1"></h5>
</div>
<div class="col-md-6">
@* Board *@
<vc:chess-board game-id="@Model"></vc:chess-board>
</div>
<div class="col-md-6">
@* Chat *@
</div>
</div>
</main>
</div>
@section scripts
{
<script type="module">
import { getAcceptedChallenge } from "/scripts/player.js";
const challenge = await getAcceptedChallenge(@Model);
const message = `${challenge.creator} VS ${challenge.accepter}`;
document.getElementById('versus-info').innerText = message;
</script>
}
@@ -1,10 +0,0 @@
@{
Layout = null;
}
<script type="module">
import { getUserId } from "/scripts/player.js";
const playerId = getUserId();
if (playerId) {
window.location.href = window.location.href.toLowerCase().replace("challengenoid", "Challenge") + `?playerId=${playerId}`;
}
</script>
@@ -7,9 +7,9 @@
<p class="lead">Join a room, or create a room!</p>
<p>
@* JS Will Append the player id to the link. *@
<a class="btn btn-success btn-lg mt-4" role="button" id="autoButton" asp-controller="Home" asp-action="Auto">Auto join</a>
<a class="btn btn-success btn-lg mt-4" role="button" id="autoButton" asp-controller="Challenges" asp-action="Auto">Auto join</a>
@* JS Will Append the player id to the link. *@
<a class="btn btn-secondary btn-lg mt-4" role="button" id="createButton" asp-controller="Home" asp-action="Create">Create a new room</a>
<a class="btn btn-secondary btn-lg mt-4" role="button" id="createButton" asp-controller="Challenges" asp-action="Create">Create a new room</a>
</p>
</div>
</div>
@@ -19,21 +19,23 @@
<div class="col-sm-12 px-1">
<div class="card mb-2 tests-card">
<div class="card-body">
<h5 class="card-title">Public games</h5>
<h5 class="card-title">Public open games</h5>
<div class="table-responsive">
<table class="table table-striped table-hover table-sm" id="logTable">
<tr>
<th>Room Name</th>
<th>Creator</th>
<th>Accepter</th>
<th>Action</th>
</tr>
@foreach (var item in Model.Challenges.Where(t => t.Value.Permission == ChallengePermission.Public))
@foreach (var item in Model.Challenges)
{
<tr>
<td>@item.Value.Message</td>
<td>@item.Value.Creator.NickName</td>
<td>@((item.Value as AcceptedChallenge)?.Accepter.NickName)
<td>
<a class="btn btn-sm btn-primary" asp-controller="Home" asp-action="Challenge" asp-route-id="@item.Key">Join</a>
<a class="btn btn-sm btn-primary" asp-controller="Challenges" asp-action="Challenge" asp-route-id="@item.Key">Join</a>
</td>
</tr>
}
@@ -1,21 +1,20 @@
@model Aiursoft.ChessServer.Views.Shared.Components.ChessBoard.ChessBoardModel
<div class="col-md-6">
<div id="board" style="width: 100%" class="w-100"></div>
</div>
<div class="col-md-12">
<p id="status" class="text-center text-wrap text-primary mt-1"></p>
</div>
<div id="board" style="width: 100%" class="w-100"></div>
<p id="status" class="text-center text-wrap text-primary mt-3"></p>
<p id="role" class="text-center text-wrap text-secondary mt-1"></p>
<script type="module">
import initGameBoard from "/scripts/chessboard.js";
import { getUserId, getPlayerColor } from "/scripts/player.js";
window.addEventListener('DOMContentLoaded', async () => {
// Ask the user to enter w or b:
const player = prompt("Please enter your color (w or b):");
initGameBoard(player, @Model.GameId);
//const player = prompt("Please enter your color (w or b):");
var playerId = getUserId();
var playerColor = await getPlayerColor(@Model.GameId);
initGameBoard(playerColor, playerId, @Model.GameId);
});
</script>
@@ -26,19 +26,22 @@
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" asp-controller="Home" asp-action="Index">Chess</a>
<li class="nav-item">
<a class="nav-link" asp-controller="Home" asp-action="Index">Rooms</a>
</li>
<li class="nav-item">
<a class="nav-link" asp-controller="Home" asp-action="Watch">Watch</a>
</li>
</ul>
<div class="form-inline">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" title="Manage" id="player-nick-name" href="#">未登录</a>
<a class="nav-link" title="Manage" id="player-nick-name" href="#">Player name</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://gitlab.aiursoft.cn/aiursoft/chessserver">
<i class="fab fa-gitlab"></i>
View on GitLab
Source Code
</a>
</li>
</ul>
@@ -1,6 +1,6 @@
import {Chess} from "../node_modules/chess.js/dist/esm/chess.js";
const initGameBoard = function (player, gameId) {
const initGameBoard = function (color, player, gameId) {
fetch(`/games/${gameId}.fen`)
.then(response => response.text())
.then(fen => {
@@ -8,11 +8,11 @@ const initGameBoard = function (player, gameId) {
let game = null;
const wsScheme = window.location.protocol === "https:" ? "wss://" : "ws://";
const socket = new WebSocket(
`${wsScheme}${window.location.host}/games/${gameId}.ws?player=${player}`
`${wsScheme}${window.location.host}/games/${gameId}.ws?playerId=${player}`
);
function onDragStart(source, piece, position, _) {
if (game.turn() !== player) {
if (game.turn() !== color) {
return false;
}
@@ -46,8 +46,6 @@ const initGameBoard = function (player, gameId) {
board.position(game.fen());
}
const statusControl = document.getElementById("status");
function updateStatusText() {
let status;
let moveColor = "White";
@@ -68,7 +66,7 @@ const initGameBoard = function (player, gameId) {
}
const config = {
orientation: player === "w" ? "white" : "black",
orientation: color === "w" ? "white" : color === "b" ? "black" : /*spectator*/ "white",
draggable: true,
dragoffBoard: "snapback",
position: fen,
@@ -77,6 +75,9 @@ const initGameBoard = function (player, gameId) {
onDrop: onDrop
};
board = ChessBoard("board", config);
const statusControl = document.getElementById("status");
const roleControl = document.getElementById("role");
roleControl.innerHTML = `You are ${color === "w" ? "White" : color === "b" ? "Black" : "Spectator"} player.`;
function refresh(newFen) {
game = new Chess(newFen);
@@ -18,13 +18,26 @@ const getUserName = async function () {
}
const changeName = async function (newName) {
// call /players/{id}/{new-name} HTTP PUT API:
await fetch(`/players/${getUserId()}/new-name/${newName}`, { method: 'PUT' });
}
const acceptChallenge = async function (challengeId, playerId) {
// call /home/AcceptChallenge/{challengeId}?playerId={playerId} HTTP POST API:
await fetch(`/home/AcceptChallenge/${challengeId}?playerId=${playerId}`, { method: 'POST' });
const response = await fetch(`/players/${getUserId()}/new-name/${newName}`, {method: 'PUT'});
if (!response.ok) {
alert(`Failed to change your name. ${await response.text()}`);
}
}
export { getUserId, getUserName, changeName, acceptChallenge };
const getAcceptedChallenge = async function (challengeId) {
const response = await fetch(`/games/${challengeId}.json`);
return await response.json();
}
const acceptChallenge = async function (challengeId) {
const playerId = getUserId();
await fetch(`/challenge/accept/${challengeId}?playerId=${playerId}`, {method: 'POST'});
}
const getPlayerColor = async function (challengeId) {
const playerId = getUserId();
const response = await fetch(`/games/${challengeId}.color?playerId=${playerId}`);
return await response.text();
}
export {getUserId, getUserName, changeName, getAcceptedChallenge, acceptChallenge, getPlayerColor};
+108 -76
View File
@@ -1,7 +1,11 @@
using System.Diagnostics;
using Aiursoft.AiurObserver;
using Aiursoft.AiurObserver.DefaultConsumers;
using Aiursoft.AiurObserver.WebSocket;
using Aiursoft.ChessServer.Data;
using Aiursoft.ChessServer.Models;
using Aiursoft.CSTools.Tools;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Aiursoft.WebTools.Extends;
@@ -27,7 +31,7 @@ public class BasicTests
[TestInitialize]
public async Task CreateServer()
{
_server = await AppAsync<Startup>(Array.Empty<string>(), port: _port);
_server = await AppAsync<Startup>([], port: _port);
await _server.StartAsync();
}
@@ -39,6 +43,24 @@ public class BasicTests
_server.Dispose();
}
private void CreateChallengeForTest(int id)
{
var db = _server!.Services.GetRequiredService<InMemoryDatabase>();
if (db.GetChallenge(id) != null)
{
return;
}
db.CreateChallenge(12345, new AcceptedChallenge(
creator: new Player(Guid.NewGuid()),
accepter: new Player(Guid.NewGuid()),
message: "Test",
roleRule: RoleRule.Random,
timeLimit: TimeSpan.FromMinutes(10),
permission: ChallengePermission.Public,
challengeChangedChannel: new AsyncObservable<string>()));
}
[TestMethod]
[DataRow("/")]
public async Task GetHome(string url)
@@ -55,24 +77,28 @@ public class BasicTests
[DataRow("/games/12345.pgn")]
public async Task GetChess(string url)
{
CreateChallengeForTest(12345);
var response = await _http.GetAsync(_endpointUrl + url);
response.EnsureSuccessStatusCode(); // Status Code 200-299
}
[TestMethod]
[DataRow("/games/1/move/w/e4")]
[DataRow("/games/2/move/w/d4")]
[DataRow("/games/3/move/w/Nf3")]
[DataRow("/games/4/move/w/Nc3")]
public async Task MoveChess(string url)
[DataRow("e4")]
[DataRow("d4")]
[DataRow("Nf3")]
[DataRow("Nc3")]
public async Task MoveChess(string action)
{
var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/{url.Split("/")[2]}.ws?player={url.Split("/")[4]}";
CreateChallengeForTest(12345);
var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/12345.ws?playerId={Guid.NewGuid()}";
var socket = await endPoint.ConnectAsWebSocketServer();
var socketStage = new MessageStageLast<string>();
socket.Subscribe(socketStage);
await Task.Factory.StartNew(() => socket.Listen());
await socket.Send(url.Split("/")[5]);
await socket.Send(action);
var waitMaxTime = new Stopwatch();
waitMaxTime.Start();
@@ -94,17 +120,21 @@ public class BasicTests
}
[TestMethod]
[DataRow("/games/5/move/w/O-O")]
[DataRow("/games/6/move/b/O-O-O")]
public async Task InvalidMoveChess(string url)
[DataRow("e4")]
[DataRow("d4")]
[DataRow("Nf3")]
[DataRow("Nc3")]
public async Task InvalidMoveChess(string action)
{
var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/{url.Split("/")[2]}.ws?player={url.Split("/")[4]}";
CreateChallengeForTest(12345);
var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/12345.ws?playerId={Guid.NewGuid()}";
var socket = await endPoint.ConnectAsWebSocketServer();
var socketStage = new MessageStageLast<string>();
socket.Subscribe(socketStage);
await Task.Factory.StartNew(() => socket.Listen());
await socket.Send(url.Split("/")[5]);
await socket.Send(action);
await Task.Delay(150);
// fen equal init
@@ -112,67 +142,69 @@ public class BasicTests
await socket.Close();
}
[TestMethod]
[DataRow(7)]
[DataRow(8)]
[DataRow(9)]
public async Task TestConnect(int gameId)
{
var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=w";
var socket = await endPoint.ConnectAsWebSocketServer();
var socketStage = new MessageStageLast<string>();
socket.Subscribe(socketStage);
await Task.Factory.StartNew(() => socket.Listen());
await socket.Send("e4");
await Task.Delay(150);
Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socketStage.Stage);
await socket.Close();
}
[TestMethod]
[DataRow(10)]
[DataRow(11)]
public async Task TestGameWithReconnection(int gameId)
{
var socket1 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=w")
.ConnectAsWebSocketServer();
var socket1Stage = new MessageStageLast<string>();
socket1.Subscribe(socket1Stage);
await Task.Factory.StartNew(() => socket1.Listen());
var socket2 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=b")
.ConnectAsWebSocketServer();
var socket2Stage = new MessageStageLast<string>();
socket2.Subscribe(socket2Stage);
await Task.Factory.StartNew(() => socket2.Listen());
await socket1.Send("e4");
await Task.Delay(150);
Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socket1Stage.Stage);
Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socket2Stage.Stage);
await socket2.Send("e5");
await Task.Delay(150);
Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket1Stage.Stage);
Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket2Stage.Stage);
await socket1.Close();
var socket3 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=w")
.ConnectAsWebSocketServer();
var socket3Stage = new MessageStageLast<string>();
socket3.Subscribe(socket3Stage);
await Task.Factory.StartNew(() => socket3.Listen());
await socket3.Send("Nf3");
await Task.Delay(150);
Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket1Stage.Stage);
Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2", socket2Stage.Stage);
Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2", socket3Stage.Stage);
await socket3.Close();
await socket2.Close();
}
// TODO Refactor tests!
// [TestMethod]
// [DataRow(7)]
// [DataRow(8)]
// [DataRow(9)]
// public async Task TestConnect(int gameId)
// {
// var endPoint = _endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?playerId={Guid.NewGuid()}";
// var socket = await endPoint.ConnectAsWebSocketServer();
// var socketStage = new MessageStageLast<string>();
// socket.Subscribe(socketStage);
// await Task.Factory.StartNew(() => socket.Listen());
//
// await socket.Send("e4");
// await Task.Delay(150);
// Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socketStage.Stage);
//
// await socket.Close();
// }
//
// [TestMethod]
// [DataRow(10)]
// [DataRow(11)]
// public async Task TestGameWithReconnection(int gameId)
// {
// var socket1 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=w")
// .ConnectAsWebSocketServer();
// var socket1Stage = new MessageStageLast<string>();
// socket1.Subscribe(socket1Stage);
// await Task.Factory.StartNew(() => socket1.Listen());
//
// var socket2 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=b")
// .ConnectAsWebSocketServer();
// var socket2Stage = new MessageStageLast<string>();
// socket2.Subscribe(socket2Stage);
// await Task.Factory.StartNew(() => socket2.Listen());
//
// await socket1.Send("e4");
// await Task.Delay(150);
// Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socket1Stage.Stage);
// Assert.AreEqual("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", socket2Stage.Stage);
//
// await socket2.Send("e5");
// await Task.Delay(150);
// Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket1Stage.Stage);
// Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket2Stage.Stage);
//
// await socket1.Close();
//
// var socket3 = await (_endpointUrl.Replace("http", "ws") + $"/games/{gameId}.ws?player=w")
// .ConnectAsWebSocketServer();
// var socket3Stage = new MessageStageLast<string>();
// socket3.Subscribe(socket3Stage);
// await Task.Factory.StartNew(() => socket3.Listen());
//
// await socket3.Send("Nf3");
// await Task.Delay(150);
// Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2", socket1Stage.Stage);
// Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2", socket2Stage.Stage);
// Assert.AreEqual("rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2", socket3Stage.Stage);
//
// await socket3.Close();
// await socket2.Close();
// }
}