mirror of
https://github.com/Wessel/nhl-setgame.git
synced 2026-06-08 14:18:05 +02:00
feat(backend/Game): Allow finishing
This commit is contained in:
66
backend/backend/Migrations/20250311160942_HintsnFound.Designer.cs
generated
Normal file
66
backend/backend/Migrations/20250311160942_HintsnFound.Designer.cs
generated
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
using backend.Models;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace backend.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(GameContext))]
|
||||||
|
[Migration("20250311160942_HintsnFound")]
|
||||||
|
partial class HintsnFound
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "9.0.2")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("backend.Models.Game", b =>
|
||||||
|
{
|
||||||
|
b.Property<long>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("bigint");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
|
||||||
|
|
||||||
|
b.PrimitiveCollection<int[]>("Deck")
|
||||||
|
.HasColumnType("integer[]");
|
||||||
|
|
||||||
|
b.Property<int>("Fails")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b.Property<DateTime?>("FinishedAt")
|
||||||
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
|
b.PrimitiveCollection<int[]>("Found")
|
||||||
|
.HasColumnType("integer[]");
|
||||||
|
|
||||||
|
b.PrimitiveCollection<int[]>("Hand")
|
||||||
|
.HasColumnType("integer[]");
|
||||||
|
|
||||||
|
b.Property<int>("Hints")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b.Property<DateTime>("StartedAt")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("timestamp with time zone")
|
||||||
|
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Games");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
backend/backend/Migrations/20250311160942_HintsnFound.cs
Normal file
39
backend/backend/Migrations/20250311160942_HintsnFound.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace backend.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class HintsnFound : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<int[]>(
|
||||||
|
name: "Found",
|
||||||
|
table: "Games",
|
||||||
|
type: "integer[]",
|
||||||
|
nullable: true);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "Hints",
|
||||||
|
table: "Games",
|
||||||
|
type: "integer",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Found",
|
||||||
|
table: "Games");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "Hints",
|
||||||
|
table: "Games");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,9 +39,15 @@ namespace backend.Migrations
|
|||||||
b.Property<DateTime?>("FinishedAt")
|
b.Property<DateTime?>("FinishedAt")
|
||||||
.HasColumnType("timestamp with time zone");
|
.HasColumnType("timestamp with time zone");
|
||||||
|
|
||||||
|
b.PrimitiveCollection<int[]>("Found")
|
||||||
|
.HasColumnType("integer[]");
|
||||||
|
|
||||||
b.PrimitiveCollection<int[]>("Hand")
|
b.PrimitiveCollection<int[]>("Hand")
|
||||||
.HasColumnType("integer[]");
|
.HasColumnType("integer[]");
|
||||||
|
|
||||||
|
b.Property<int>("Hints")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
b.Property<DateTime>("StartedAt")
|
b.Property<DateTime>("StartedAt")
|
||||||
.ValueGeneratedOnAdd()
|
.ValueGeneratedOnAdd()
|
||||||
.HasColumnType("timestamp with time zone")
|
.HasColumnType("timestamp with time zone")
|
||||||
|
|||||||
@@ -11,8 +11,10 @@ public class Game {
|
|||||||
public DateTime StartedAt { get; set; }
|
public DateTime StartedAt { get; set; }
|
||||||
public DateTime? FinishedAt { get; set; }
|
public DateTime? FinishedAt { get; set; }
|
||||||
public int Fails { get; set; }
|
public int Fails { get; set; }
|
||||||
|
public int Hints { get; set; }
|
||||||
public ushort[]? Hand { get; set; }
|
public ushort[]? Hand { get; set; }
|
||||||
public ushort[]? Deck { get; set; }
|
public ushort[]? Deck { get; set; }
|
||||||
|
public ushort[]? Found { get; set; }
|
||||||
|
|
||||||
public void ShuffleDeck() {
|
public void ShuffleDeck() {
|
||||||
if (Deck == null) return;
|
if (Deck == null) return;
|
||||||
@@ -54,7 +56,12 @@ public class Game {
|
|||||||
public bool GameIsFinished() {
|
public bool GameIsFinished() {
|
||||||
if (Deck == null || Hand == null) return false;
|
if (Deck == null || Hand == null) return false;
|
||||||
|
|
||||||
var finished = Deck.Length == 0 && Hand.All(card => card == 0);
|
var finished = Deck.Length == 0 && Hand.Length < 12;
|
||||||
|
|
||||||
|
// Check if no sets can be made with the remaining cards
|
||||||
|
if (!finished && GetIndicesOfSet().Count == 0) {
|
||||||
|
finished = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Empty hand if game is finished for optimal database space usage
|
// Empty hand if game is finished for optimal database space usage
|
||||||
if (finished) {
|
if (finished) {
|
||||||
@@ -64,11 +71,14 @@ public class Game {
|
|||||||
return finished;
|
return finished;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo: refactor`
|
||||||
public SetCheckResult? IsSet(ushort[] indices, bool remove = true) {
|
public SetCheckResult? IsSet(ushort[] indices, bool remove = true) {
|
||||||
if (GameIsFinished()) {
|
if (remove) {
|
||||||
FinishedAt = DateTime.Now;
|
if (GameIsFinished()) {
|
||||||
|
FinishedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
return new SetCheckResult { IsFinished = true, NewState = this };
|
return new SetCheckResult { IsFinished = true, NewState = this };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indices.Length != 3 || Hand == null) {
|
if (indices.Length != 3 || Hand == null) {
|
||||||
@@ -98,7 +108,17 @@ public class Game {
|
|||||||
// (From the top may cause the indices to be off by one)
|
// (From the top may cause the indices to be off by one)
|
||||||
if (remove) {
|
if (remove) {
|
||||||
foreach (var index in indices.OrderByDescending(i => i)) {
|
foreach (var index in indices.OrderByDescending(i => i)) {
|
||||||
ReplaceCardInHand(index);
|
Console.WriteLine(Hand.Length);
|
||||||
|
if (Hand.Length < 13)
|
||||||
|
{
|
||||||
|
ReplaceCardInHand(index);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var handList = Hand.ToList();
|
||||||
|
handList.RemoveAt(index);
|
||||||
|
Hand = handList.ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (GetIndicesOfSet().Count < 1 && Deck?.Length > 0) {
|
while (GetIndicesOfSet().Count < 1 && Deck?.Length > 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user