Fix some front-end bugs.

This commit is contained in:
AnduinXue
2023-12-13 12:28:39 +00:00
parent 60cb22cb22
commit 3b9f131016
4 changed files with 15 additions and 11 deletions
@@ -43,6 +43,10 @@ public class GamesController : Controller
{
await pusher.Listen(HttpContext.RequestAborted);
}
catch (TaskCanceledException)
{
// Ignore. This happens when the client closes the connection.
}
finally
{
await pusher.Close(HttpContext.RequestAborted);
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using Aiursoft.ChessServer.Data;
using Aiursoft.CSTools.Attributes;
using Microsoft.AspNetCore.Mvc;
namespace Aiursoft.ChessServer.Controllers;
@@ -23,10 +24,15 @@ public class PlayersController : ControllerBase
[HttpPut]
[Route("{id:guid}/new-name/{nickname}")]
public IActionResult ChangeNickname([Required]Guid id, [Required]string nickname)
public IActionResult ChangeNickname([Required]Guid id, [Required][MaxLength(20)][ValidDomainName]string nickname)
{
if (ModelState.IsValid == false)
{
return BadRequest();
}
var me = _database.GetOrAddPlayer(id);
me.NickName = nickname;
return Ok(me);
return Ok();
}
}
@@ -56,7 +56,7 @@ async function loadName() {
async function promptChangeName() {
const newName = prompt("Please enter your new name:");
if (newName) {
changeName(newName);
await changeName(newName);
await loadName();
}
}
@@ -1,10 +1,5 @@
const guid = function () {
// Create a new GUID:
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return crypto.randomUUID();
};
const getUserId = function () {
@@ -24,8 +19,7 @@ const getUserName = async function () {
const changeName = async function (newName) {
// call /players/{id}/{new-name} HTTP PUT API:
const response = await fetch(`/players/${getUserId()}/new-name/${newName}`, { method: 'PUT' });
return (await response.json()).nickName;
await fetch(`/players/${getUserId()}/new-name/${newName}`, { method: 'PUT' });
}
export { getUserName, changeName };