Skip to content

Commit

Permalink
This commit aims to add:
Browse files Browse the repository at this point in the history
1. New Game Mechanics for hand drawing
2. A card pool provider on a per game-room basis
3. A black card player for each card czar turn
4. A replenishing hand function
5. General improvements and optimizations
  • Loading branch information
AluTheCrow committed Dec 2, 2022
1 parent ddb4e70 commit 903eabf
Show file tree
Hide file tree
Showing 19 changed files with 433 additions and 166 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,5 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd
/Properties/VersionInfo.cs
62 changes: 0 additions & 62 deletions Hubs/CrowGameHub.cs

This file was deleted.

43 changes: 17 additions & 26 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Server;
using Microsoft.Identity.Web;
using TheOmenDen.CrowsAgainstHumility.Services.Hubs;

#endregion
#region Bootstrap Logger
Log.Logger = new LoggerConfiguration()
Expand All @@ -52,21 +54,13 @@
{
var builder = WebApplication.CreateBuilder(args);

builder.Host
.ConfigureAppConfiguration((context, config) =>
{
config
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", true)
.AddEnvironmentVariables()
.AddAzureKeyVault(
new Uri(builder.Configuration["VaultUri"]),
new DefaultAzureCredential());

})
.UseDefaultServiceProvider(options => options.ValidateScopes = false)
.UseSerilog((context, services, configuration) => configuration
builder.Configuration.AddAzureKeyVault(
new Uri(builder.Configuration["VaultUri"]),
new DefaultAzureCredential());


builder.Host.UseDefaultServiceProvider(options => options.ValidateScopes = false)
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
Expand Down Expand Up @@ -122,7 +116,7 @@
.AddTwitch(options =>
{
options.ClientId = twitchStrings.ClientId;
options.ClientSecret = twitchStrings.Key;
options.ClientSecret = twitchStrings.Key;
options.SaveTokens = true;
})
.AddDiscord(options =>
Expand Down Expand Up @@ -256,7 +250,7 @@
builder.Services.AddScoped<IHostEnvironmentAuthenticationStateProvider>(sp =>
{
// this is safe because
// the `RevalidatingIdentityAuthenticationStateProvider` extends the `ServerAuthenticationStateProvider`
// the `RevalidatingIdentityAuthenticationStateProvider` extends the `ServerAuthenticationStateProvider`
var provider = (ServerAuthenticationStateProvider)sp.GetRequiredService<AuthenticationStateProvider>();
return provider;
});
Expand All @@ -283,15 +277,12 @@
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
endpoints.MapBlazorHub();
endpoints.MapHub<CawHub>(CawHub.HubUrl);
endpoints.MapHub<CrowGameHub>(CrowGameHub.HubUrl);
endpoints.MapFallbackToPage("/_Host");
});
app.MapControllers();
app.MapRazorPages();
app.MapBlazorHub();
app.MapHub<CawHub>(CawHub.HubUrl);
app.MapHub<CrowGameHub>(CrowGameHub.HubUrl);
app.MapFallbackToPage("/_Host");

await app.RunAsync();
}
Expand Down
3 changes: 0 additions & 3 deletions Properties/VersionInfo.cs

This file was deleted.

8 changes: 1 addition & 7 deletions TheOmenDen.CrowsAgainstHumility.Core/Models/GamePlay.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheOmenDen.CrowsAgainstHumility.Core.Models;
namespace TheOmenDen.CrowsAgainstHumility.Core.Models;
public sealed class GamePlay
{
public string HubConnectionId { get; set; }
Expand Down
18 changes: 11 additions & 7 deletions TheOmenDen.CrowsAgainstHumility.Core/Models/Player.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq;
using System.Reflection.Metadata.Ecma335;

namespace TheOmenDen.CrowsAgainstHumility.Core.Models;
namespace TheOmenDen.CrowsAgainstHumility.Core.Models;

public sealed class Player : IEquatable<Player>
{
private const int MaxHandSize = 10;

public Player() {}
public Player(String connectionId)
{
Expand All @@ -17,15 +15,21 @@ public Player(String connectionId)

public String ConnectionId { get; set; }

public int AwesomePoints { get; set; } = 2;

public string Name { get; set; } = String.Empty;

public String ConnectionGuid { get; }
public string Username { get; set; } = String.Empty;

public List<WhiteCard> Hand { get; } = new (MaxHandSize);

public Guid ConnectionGuid { get; }

public Guid Id { get; set;}

public bool IsConnected { get; set; }

public bool IsCardTsar { get; set; } = false;
public bool IsCardCzar { get; set; } = false;

public bool Equals(Player? other) => other is not null
&& (ReferenceEquals(this, other)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.Logging;
using TheOmenDen.CrowsAgainstHumility.Core.Models;
using TheOmenDen.CrowsAgainstHumility.Services.Rooms;
using TheOmenDen.Shared.Extensions;

namespace TheOmenDen.CrowsAgainstHumility.Services.CardPoolBuilding;
public static class BlackCardProvider
{
private static readonly object SyncObject = new ();

private static readonly Dictionary<String, List<BlackCard>> BlackCardToRoomDictionary = new(StringComparer.OrdinalIgnoreCase);

public static void AddRoomReference(CrowGameRoom room, IEnumerable<BlackCard> blackCards)
{
var blackCardList = blackCards.ToList();

if (!BlackCardToRoomDictionary.ContainsKey(room.RoomName))
{
BlackCardToRoomDictionary.Add(room.RoomName, blackCardList);
}

BlackCardToRoomDictionary[room.RoomName] = blackCardList;
}

public static BlackCard GetBlackCardForRound(String roomName)
{
if (!BlackCardToRoomDictionary.TryGetValue(roomName, out var blackCardPool))
{
throw new ArgumentOutOfRangeException(nameof(roomName));
}

var blackCardToPlay = blackCardPool.GetRandomElement();

lock (SyncObject)
{
RemoveUsedCardFromPool(roomName, blackCardToPlay);
}

return blackCardToPlay;
}

private static void RemoveUsedCardFromPool(String roomName, BlackCard blackCardToRemove) =>
_ = BlackCardToRoomDictionary.TryGetValue(roomName, out var blackCards)
&& blackCards.Remove(blackCardToRemove);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

using TheOmenDen.CrowsAgainstHumility.Core.Models;
using TheOmenDen.CrowsAgainstHumility.Services.Rooms;
using TheOmenDen.Shared.Extensions;

namespace TheOmenDen.CrowsAgainstHumility.Services.CardPoolBuilding;
public static class WhiteCardProvider
{
private static readonly Dictionary<String, IEnumerable<WhiteCard>> WhiteCardPoolByRoomDictionary = new(StringComparer.OrdinalIgnoreCase);
private const int MaxHandSize = 10;

public static void CreateCardPoolProviderForRoom(CrowGameRoom room, IEnumerable<WhiteCard> whiteCards)
{
if (WhiteCardPoolByRoomDictionary.ContainsKey(room.RoomName))
{
WhiteCardPoolByRoomDictionary.Add(room.RoomName, whiteCards);
}
}

public static bool RemoveCardPoolFromRoom(CrowGameRoom room) => WhiteCardPoolByRoomDictionary.Remove(room.RoomName);

public static void AddHandsToPlayers(CrowGameRoom room)
{
var cardPool = WhiteCardPoolByRoomDictionary[room.RoomName].ToArray();

foreach (var player in room.Players)
{
player.Hand.AddRange(cardPool.GetRandomElements(10));
}
}

public static void ReplenishPlayerHand(CrowGameRoom room)
{
var cardPool = WhiteCardPoolByRoomDictionary[room.RoomName].ToArray();

foreach (var playerHand in room.Players
.Where(p => !p.IsCardCzar)
.Select(p => p.Hand))
{
if (playerHand.Count >= MaxHandSize)
{
continue;
}

var lessThanHandSize = MaxHandSize - playerHand.Count;

playerHand.AddRange(cardPool.GetRandomElements(lessThanHandSize));
}
}
}
Loading

0 comments on commit 903eabf

Please sign in to comment.