This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddScoped<IDbInitializer, DbInitializer>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void Main(string[] args) | |
{ | |
CreateHostBuilder(args) | |
.Build() | |
.SeedIfNeeded() | |
.Run(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseEndpoints(endpoints = > | |
{ | |
endpoints.MapControllers(); | |
endpoints.MapBlazorHub(); | |
endpoints.MapHub < Connect4Hub > ("/connect4"); | |
endpoints.MapFallbackToPage("/_Host"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GamePiece | |
{ | |
public PieceColor Color; | |
public GamePiece() => Color = PieceColor.Blank; | |
public GamePiece(PieceColor color) => Color = color; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum PieceColor | |
{ | |
Red, | |
Yellow, | |
Blank | |
} |
NewerOlder