mirror of
https://github.com/Wessel/nhl-setgame.git
synced 2026-07-28 19:21:20 +02:00
feat: Move hints to backend, clean up game service on frontend
This commit is contained in:
@@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using backend.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using System.Security.Claims;
|
||||
using DotNetEnv;
|
||||
|
||||
namespace backend.Controllers;
|
||||
|
||||
@@ -66,7 +67,7 @@ public class GamesController(GameContext context) : ControllerBase {
|
||||
}
|
||||
|
||||
var newGame = new Game {
|
||||
UserId = (long)user,
|
||||
UserId = user.Value,
|
||||
Deck = [..
|
||||
from shape in Enum.GetValues<CardShape>().Cast<CardShape>()
|
||||
from color in Enum.GetValues<CardColor>().Cast<CardColor>()
|
||||
@@ -148,8 +149,9 @@ public class GamesController(GameContext context) : ControllerBase {
|
||||
[Route("[action]/{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<List<int[]>>> SetsInHand(long id) {
|
||||
// Only show if user is admin (id < 1)
|
||||
var user = ParseUserId();
|
||||
if (user == null) {
|
||||
if (user == null || user > 0) {
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
@@ -169,4 +171,34 @@ public class GamesController(GameContext context) : ControllerBase {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("[action]/{id}")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult<List<ushort>>> Hint(long id) {
|
||||
var user = ParseUserId();
|
||||
if (user == null) {
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
var game = await _context.Games
|
||||
.Where(g => g.Id == id && g.UserId == user)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (game == null) {
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var sets = game.GetIndicesOfSet();
|
||||
|
||||
if (sets == null || sets.Count == 0) {
|
||||
return BadRequest("No sets found in hand.");
|
||||
}
|
||||
|
||||
game.Hints++;
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return sets[0].Take(2).Select(i => (ushort)i).ToList();
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ namespace backend.Migrations
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("Games");
|
||||
b.ToTable("Games", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("backend.Models.User", b =>
|
||||
@@ -94,7 +94,7 @@ namespace backend.Migrations
|
||||
b.HasIndex("Username")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Users");
|
||||
b.ToTable("Users", (string)null);
|
||||
});
|
||||
|
||||
modelBuilder.Entity("backend.Models.Game", b =>
|
||||
|
||||
@@ -5,7 +5,6 @@ public struct SetCheckResult {
|
||||
public bool? IsFinished { get; set; }
|
||||
public Game NewState { get; set; }
|
||||
}
|
||||
// todo: add check to routes if owner is user
|
||||
|
||||
public class Game {
|
||||
public long Id { get; set; }
|
||||
@@ -21,8 +20,8 @@ public class Game {
|
||||
public void ShuffleDeck() {
|
||||
if (Deck == null) return;
|
||||
|
||||
Random rand = new Random();
|
||||
Deck = Deck.OrderBy(_ => rand.Next()).ToArray();
|
||||
Random rand = new();
|
||||
Deck = [.. Deck.OrderBy(_ => rand.Next())];
|
||||
}
|
||||
|
||||
public void DealHand(int max = 12) {
|
||||
@@ -113,17 +112,14 @@ public class Game {
|
||||
foreach (var index in indices.OrderByDescending(i => i)) {
|
||||
var foundList = Found.ToList();
|
||||
foundList.Add(Hand[index]);
|
||||
Found = foundList.ToArray();
|
||||
Found = [.. foundList];
|
||||
|
||||
if (Hand.Length < 13)
|
||||
{
|
||||
if (Hand.Length < 13) {
|
||||
ReplaceCardInHand(index);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
var handList = Hand.ToList();
|
||||
handList.RemoveAt(index);
|
||||
Hand = handList.ToArray();
|
||||
Hand = [.. handList];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,20 +138,19 @@ public class Game {
|
||||
}
|
||||
|
||||
public List<int[]> GetIndicesOfSet() {
|
||||
if (Hand == null) return new List<int[]>();
|
||||
List<int[]> res = new List<int[]>();
|
||||
if (Hand == null) return [];
|
||||
List<int[]> res = [];
|
||||
|
||||
for (int i = 0; i < Hand.Length; i++) {
|
||||
for (int j = i + 1; j < Hand.Length; j++) {
|
||||
for (int k = j + 1; k < Hand.Length; k++) {
|
||||
var result = SetResult([(ushort)i, (ushort)j, (ushort)k]);
|
||||
for (int j = i + 1; j < Hand.Length; j++) {
|
||||
for (int k = j + 1; k < Hand.Length; k++) {
|
||||
var result = SetResult([(ushort)i, (ushort)j, (ushort)k]);
|
||||
|
||||
if (result == true) {
|
||||
Console.WriteLine($"Found set at {i}, {j}, {k}");
|
||||
res.Add([i, j, k]);
|
||||
}
|
||||
}
|
||||
if (result == true) {
|
||||
res.Add([i, j, k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user