Move from the hand.
This commit is contained in:
@@ -88,6 +88,9 @@ public class SessionsController : ControllerBase
|
||||
var session = await sessionRepository.ReadSession(name);
|
||||
if (session == null) return this.NotFound();
|
||||
|
||||
var players = await queryRespository.GetUsersForSession(session.Name);
|
||||
if (players == null) return this.NotFound();
|
||||
|
||||
return new ReadSessionResponse
|
||||
{
|
||||
Session = new Session
|
||||
@@ -100,8 +103,8 @@ public class SessionsController : ControllerBase
|
||||
PlayerInCheck = session.Board.BoardState.InCheck?.ToContract(),
|
||||
WhoseTurn = session.Board.BoardState.WhoseTurn.ToContract()
|
||||
},
|
||||
Player1 = session.Player1,
|
||||
Player2 = session.Player2,
|
||||
Player1 = players.Value.Player1,
|
||||
Player2 = players.Value.Player2,
|
||||
SessionName = session.Name
|
||||
}
|
||||
};
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace Shogi.Api.Managers;
|
||||
|
||||
public interface ISocketConnectionManager
|
||||
{
|
||||
Task BroadcastToAll(ISocketResponse response);
|
||||
Task BroadcastToAll(ISocketMessage response);
|
||||
void Subscribe(WebSocket socket, string playerName);
|
||||
void Unsubscribe(string playerName);
|
||||
Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames);
|
||||
Task BroadcastToPlayers(ISocketMessage response, params string?[] playerNames);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,7 +45,7 @@ public class SocketConnectionManager : ISocketConnectionManager
|
||||
connections.TryRemove(playerName, out _);
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames)
|
||||
public async Task BroadcastToPlayers(ISocketMessage response, params string?[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
@@ -59,7 +59,7 @@ public class SocketConnectionManager : ISocketConnectionManager
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
public Task BroadcastToAll(ISocketResponse response)
|
||||
public Task BroadcastToAll(ISocketMessage response)
|
||||
{
|
||||
var message = Serialize(response);
|
||||
logger.LogInformation("Broadcasting:\n{0}\nDone Broadcasting.", message);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Dapper;
|
||||
using Shogi.Contracts.Api;
|
||||
using Shogi.Contracts.Types;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace Shogi.Api.Repositories;
|
||||
@@ -32,9 +33,33 @@ public class QueryRepository : IQueryRespository
|
||||
AllOtherSessions = otherSessions.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="ValueTuple"/> with Item1 as player 1 and Item2 as player 2.</returns>
|
||||
public async Task<(User Player1, User? Player2)?> GetUsersForSession(string sessionName)
|
||||
{
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
var results = await connection.QueryAsync<(string Player1Name, string Player1DisplayName, string Player2Name, string Player2DisplayName)>(
|
||||
"session.ReadUsersBySession",
|
||||
new { SessionName = sessionName },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
if (results.Any())
|
||||
{
|
||||
var (Player1Name, Player1DisplayName, Player2Name, Player2DisplayName) = results.First();
|
||||
var p1 = new User(Player1Name, Player1DisplayName);
|
||||
var p2 = Player2Name != null
|
||||
? new User(Player2Name, Player2DisplayName)
|
||||
: null;
|
||||
return (p1, p2);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IQueryRespository
|
||||
{
|
||||
Task<(User Player1, User? Player2)?> GetUsersForSession(string sessionName);
|
||||
Task<ReadSessionsPlayerCountResponse> ReadSessionPlayerCount(string playerName);
|
||||
}
|
||||
@@ -73,6 +73,15 @@ public class SessionRepository : ISessionRepository
|
||||
|
||||
public async Task CreateMove(string sessionName, MovePieceCommand command)
|
||||
{
|
||||
var yep = new
|
||||
{
|
||||
command.To,
|
||||
command.From,
|
||||
command.IsPromotion,
|
||||
command.PieceFromHand,
|
||||
SessionName = sessionName
|
||||
};
|
||||
|
||||
using var connection = new SqlConnection(connectionString);
|
||||
await connection.ExecuteAsync(
|
||||
"session.CreateMove",
|
||||
@@ -81,7 +90,7 @@ public class SessionRepository : ISessionRepository
|
||||
command.To,
|
||||
command.From,
|
||||
command.IsPromotion,
|
||||
command.PieceFromHand,
|
||||
PieceFromHand = command.PieceFromHand.ToString(),
|
||||
SessionName = sessionName
|
||||
},
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace Shogi.Api.Services
|
||||
var message = await socket.ReceiveTextAsync();
|
||||
if (string.IsNullOrWhiteSpace(message)) continue;
|
||||
logger.LogInformation("Request \n{0}\n", message);
|
||||
var request = JsonSerializer.Deserialize<ISocketRequest>(message);
|
||||
var request = JsonSerializer.Deserialize<ISocketMessage>(message);
|
||||
if (request == null || !Enum.IsDefined(typeof(SocketAction), request.Action))
|
||||
{
|
||||
await socket.SendTextAsync("Error: Action not recognized.");
|
||||
|
||||
Reference in New Issue
Block a user