Before changing Piece[,] to Dictionary<string,Piece>
This commit is contained in:
@@ -1,22 +1,21 @@
|
||||
using Gameboard.ShogiUI.Sockets.Extensions;
|
||||
using Gameboard.ShogiUI.Sockets.ServiceModels.Socket.Types;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.WebSockets;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Gameboard.ShogiUI.Sockets.Models
|
||||
{
|
||||
public class Session
|
||||
{
|
||||
// TODO: Separate subscriptions to the Session from the Session.
|
||||
[JsonIgnore] public ConcurrentDictionary<string, WebSocket> Subscriptions { get; }
|
||||
public string Name { get; }
|
||||
public string Player1 { get; }
|
||||
public string? Player2 { get; }
|
||||
public string? Player2 { get; private set; }
|
||||
public bool IsPrivate { get; }
|
||||
|
||||
public Session(string name, bool isPrivate, string player1, string? player2 = null)
|
||||
public Shogi Shogi { get; }
|
||||
|
||||
public Session(string name, bool isPrivate, Shogi shogi, string player1, string? player2 = null)
|
||||
{
|
||||
Subscriptions = new ConcurrentDictionary<string, WebSocket>();
|
||||
|
||||
@@ -24,48 +23,12 @@ namespace Gameboard.ShogiUI.Sockets.Models
|
||||
Player1 = player1;
|
||||
Player2 = player2;
|
||||
IsPrivate = isPrivate;
|
||||
Shogi = shogi;
|
||||
}
|
||||
|
||||
public bool Subscribe(string playerName, WebSocket socket) => Subscriptions.TryAdd(playerName, socket);
|
||||
|
||||
public Task Broadcast(string message)
|
||||
public void SetPlayer2(string userName)
|
||||
{
|
||||
var tasks = new List<Task>(Subscriptions.Count);
|
||||
foreach (var kvp in Subscriptions)
|
||||
{
|
||||
var socket = kvp.Value;
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
public Task SendToPlayer1(string message)
|
||||
{
|
||||
if (Subscriptions.TryGetValue(Player1, out var socket))
|
||||
{
|
||||
return socket.SendTextAsync(message);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task SendToPlayer2(string message)
|
||||
{
|
||||
if (Player2 != null && Subscriptions.TryGetValue(Player2, out var socket))
|
||||
{
|
||||
return socket.SendTextAsync(message);
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Game ToServiceModel()
|
||||
{
|
||||
var players = new List<string>(2) { Player1 };
|
||||
if (!string.IsNullOrWhiteSpace(Player2)) players.Add(Player2);
|
||||
return new Game
|
||||
{
|
||||
GameName = Name,
|
||||
Players = players.ToArray()
|
||||
};
|
||||
Player2 = userName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user