feat(backend/Game): Allow finishing

This commit is contained in:
2025-03-11 16:22:26 +00:00
parent ea97970da1
commit 2ec1b6d3ce
4 changed files with 136 additions and 5 deletions

View 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
}
}
}

View 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");
}
}
}

View File

@@ -39,9 +39,15 @@ namespace backend.Migrations
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")

View File

@@ -11,8 +11,10 @@ public class Game {
public DateTime StartedAt { get; set; }
public DateTime? FinishedAt { get; set; }
public int Fails { get; set; }
public int Hints { get; set; }
public ushort[]? Hand { get; set; }
public ushort[]? Deck { get; set; }
public ushort[]? Found { get; set; }
public void ShuffleDeck() {
if (Deck == null) return;
@@ -54,7 +56,12 @@ public class Game {
public bool GameIsFinished() {
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
if (finished) {
@@ -64,11 +71,14 @@ public class Game {
return finished;
}
// todo: refactor`
public SetCheckResult? IsSet(ushort[] indices, bool remove = true) {
if (GameIsFinished()) {
FinishedAt = DateTime.Now;
if (remove) {
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) {
@@ -98,7 +108,17 @@ public class Game {
// (From the top may cause the indices to be off by one)
if (remove) {
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) {