Skip to content

Instantly share code, notes, and snippets.

View Ibro's full-sized avatar

Ibrahim Šuta Ibro

View GitHub Profile
@Ibro
Ibro / ChatHub.cs
Created December 27, 2022 00:43
SignalR ChatHub to talk to the clients
using Microsoft.AspNetCore.SignalR;
namespace BlazorSignalRChat;
// We're creating a Chat Hub - Hub is a SignalR class
// This class will be used to send messages to the client
// and receive messages from the client
public class ChatHub : Hub
{
// We will create a constant to be used from the client (Blazor page)
public static IHost SeedIfNeeded(this IHost host) {
using var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<IDbInitializer>>();
try {
logger.LogInformation("Checking if Database is created and if seed needs to be done.");
var dbInitializer = services.GetRequiredService<IDbInitializer>();
dbInitializer.Seed().GetAwaiter().GetResult();
services.AddScoped<IDbInitializer, DbInitializer>();
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.SeedIfNeeded()
.Run();
}
public interface IDbInitializer {
Task Seed();
}
public class DbInitializer: IDbInitializer {
private readonly ILogger < DbInitializer > _logger;
private readonly UserManager < IdentityUser > _userManager;
private readonly ApplicationDbContext _dbContext;
private readonly IWebHostEnvironment _environment;
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";
app.UseEndpoints(endpoints = >
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapHub < Connect4Hub > ("/connect4");
endpoints.MapFallbackToPage("/_Host");
});
public class GameBoard
{
public GamePiece[, ] Board { get; set; }
public GameBoard()
{
Board = new GamePiece[7, 6];
// Populate the Board with blank pieces
for (int i = 0; i <= 6; i++) {
public class GamePiece
{
public PieceColor Color;
public GamePiece() => Color = PieceColor.Blank;
public GamePiece(PieceColor color) => Color = color;
}
public enum PieceColor
{
Red,
Yellow,
Blank
}