Skip to content

Instantly share code, notes, and snippets.

@Ibro
Created May 24, 2021 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ibro/5dc058ca88060403f97e9d37a338252d to your computer and use it in GitHub Desktop.
Save Ibro/5dc058ca88060403f97e9d37a338252d to your computer and use it in GitHub Desktop.
public class Connect4Hub: Hub {
private readonly ILogger < Connect4Hub > _logger;
public const string OnGameReady = "OnGameReady";
public const string NotifySecondPlayer = "NotifySecondPlayer";
public const string NotifyGameHost = "NotifyGameHost";
public const string NotifyOnGameOver = "NotifyOnGameOver";
public const string NotifyOnDraw = "NotifyOnDraw";
public const string NotifyOnPlayerLeft = "NotifyOnPlayerLeft";
private const string Connect4Group = "Connect4";
public Connect4Hub(ILogger < Connect4Hub > logger) {
_logger = logger;
}
public async Task JoinedQueue() {
await Groups.AddToGroupAsync(Context.ConnectionId, Connect4Group);
}
public async Task GameReady(GameBoard board) {
await Clients.Group(Connect4Group).SendAsync(OnGameReady, board);
}
public async Task OnPlayerDisconnected(Guid userId) {
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Connect4Group);
_logger.LogWarning("User left");
await Clients.Group(Connect4Group).SendAsync(NotifyOnPlayerLeft, userId);
}
public async Task OnOpponentMoveReceived(GameBoard board) {
await Clients.Group(Connect4Group).SendAsync(NotifyGameHost, board);
}
public async Task OnHostMoveReceived(GameBoard board) {
await Clients.Group(Connect4Group).SendAsync(NotifySecondPlayer, board);
}
public async Task OnGameOver(GameBoard board, WinningPlay winningPlay) {
await Clients.Group(Connect4Group).SendAsync(NotifyOnGameOver, board, winningPlay);
}
public async Task OnDraw(GameBoard board) {
await Clients.Group(Connect4Group).SendAsync(NotifyOnDraw, board);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment