Skip to content

Instantly share code, notes, and snippets.

Avatar

Ibrahim Šuta Ibro

View GitHub Profile
View HostSeedExtension.cs
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();
View Startup.cs
services.AddScoped<IDbInitializer, DbInitializer>();
View Program.cs
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.SeedIfNeeded()
.Run();
}
View DbInitializer.cs
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;
View Connect4Hub.cs
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";
View Startup.cs
app.UseEndpoints(endpoints = >
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapHub < Connect4Hub > ("/connect4");
endpoints.MapFallbackToPage("/_Host");
});
View GameBoard.cs
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++) {
View GamePiece.cs
public class GamePiece
{
public PieceColor Color;
public GamePiece() => Color = PieceColor.Blank;
public GamePiece(PieceColor color) => Color = color;
}
View PieceColor.cs
public enum PieceColor
{
Red,
Yellow,
Blank
}
@Ibro
Ibro / Index.razor.cs
Created Apr 11, 2021
Connect4 - Live game with SignalR and Blazor
View Index.razor.cs
public class IndexComponent: ComponentBase
{
private IdentityUser _currentUser;
[Inject]
protected AuthenticationStateProvider AuthenticationStateProvider { get; set; }
[Inject]
protected UserManager < IdentityUser > UserManager { get; set; }