feat: added web view 1v1

This commit is contained in:
shahdlala66
2026-04-17 23:20:16 +02:00
commit 1828fa3275
80 changed files with 11876 additions and 0 deletions
+119
View File
@@ -0,0 +1,119 @@
.game-shell {
height: 100dvh;
padding: clamp(0.75rem, 2vw, 1.5rem);
overflow: hidden;
}
.game-card {
max-width: 1100px;
height: 100%;
margin: 0 auto;
background: #F3C8A0;
border: 2px solid #5A2C28;
border-radius: 12px;
padding: clamp(0.75rem, 1.5vw, 1.25rem);
box-shadow: 0 8px 24px rgba(90, 44, 40, 0.2);
display: flex;
flex-direction: column;
min-height: 0;
}
header {
margin-bottom: 1rem;
}
h1,
h2 {
color: #5A2C28;
margin: 0 0 0.5rem;
}
.meta {
margin: 0;
}
.back-link {
display: inline-block;
margin-bottom: 0.5rem;
color: #5A2C28;
}
.top-section {
display: grid;
gap: 0.75rem;
margin-bottom: 0.75rem;
flex: 0 0 auto;
}
.state-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 0.75rem;
margin-bottom: 0.5rem;
}
.panel {
background: #B9C2DA;
border-radius: 10px;
border: 2px solid #5A2C28;
padding: 0.75rem;
}
.panel p {
margin: 0.3rem 0;
}
code {
display: block;
background: #E1EAA9;
border-radius: 8px;
padding: 0.5rem;
overflow-wrap: anywhere;
}
.move-form {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.75rem;
}
.board-section {
flex: 1 1 auto;
min-height: 0;
display: grid;
place-items: center;
padding: clamp(0.35rem, 1vw, 0.75rem);
border-radius: 10px;
border: 2px solid #5A2C28;
background: #B9DAD1;
overflow: hidden;
}
input {
border: 2px solid #5A2C28;
border-radius: 10px;
background: #B9DAD1;
padding: 0.6rem 0.75rem;
min-width: 180px;
}
button {
border: 2px solid #5A2C28;
border-radius: 10px;
background: #C19EF5;
color: #5A2C28;
padding: 0.6rem 1rem;
cursor: pointer;
}
button:hover {
background: #BA6D4B;
color: #F3C8A0;
}
.error {
margin-top: 0.5rem;
color: #5A2C28;
font-weight: 700;
}
+52
View File
@@ -0,0 +1,52 @@
<main class="game-shell">
<section class="game-card">
<header>
<a routerLink="/" class="back-link">Back</a>
<h1>1 vs 1 Game</h1>
<p class="meta">Game ID: <strong>{{ gameId }}</strong></p>
</header>
@if (loading) {
<p>Loading game state...</p>
} @else if (state) {
<section class="top-section">
<div class="state-grid">
<div class="panel">
<h2>Status</h2>
<p>Turn: <strong>{{ state.turn }}</strong></p>
<p>Status: <strong>{{ state.status }}</strong></p>
<p>FEN:</p>
<code>{{ state.fen }}</code>
</div>
<div class="panel">
<h2>Moves</h2>
<p>PGN: {{ state.pgn || 'No moves yet' }}</p>
<p>Played UCI: {{ state.moves.length ? state.moves.join(', ') : 'None' }}</p>
<p>Legal UCI: {{ legalMoveUciList.length ? legalMoveUciList.join(', ') : 'None' }}</p>
<p>Board: click your piece to highlight legal targets.</p>
</div>
</div>
<form class="move-form" (ngSubmit)="submitMove()">
<label for="uciMove">Play move (UCI)</label>
<input id="uciMove" name="uciMove" [(ngModel)]="moveInput" placeholder="e2e4" />
<button type="submit">Send Move</button>
</form>
</section>
<section class="board-section">
<app-chess-board
[fen]="state.fen"
[selectedSquare]="selectedSquare"
[highlightedSquares]="highlightedSquares"
(squareSelected)="onBoardSquareSelected($event)"
/>
</section>
}
@if (errorMessage) {
<p class="error">{{ errorMessage }}</p>
}
</section>
</main>
+250
View File
@@ -0,0 +1,250 @@
import { CommonModule } from '@angular/common';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { interval, startWith, Subscription, switchMap } from 'rxjs';
import { ChessBoardComponent } from '../../components/chess-board/chess-board.component';
import { GameFull, GameState, GameStreamEvent, LegalMove } from '../../models/game.models';
import { GameApiService } from '../../services/game-api.service';
@Component({
selector: 'app-game',
standalone: true,
imports: [CommonModule, FormsModule, RouterLink, ChessBoardComponent],
templateUrl: './game.component.html',
styleUrl: './game.component.css'
})
export class GameComponent implements OnInit, OnDestroy {
gameId = '';
game: GameFull | null = null;
errorMessage = '';
moveInput = '';
legalMoves: LegalMove[] = [];
loading = true;
selectedSquare: string | null = null;
highlightedSquares: string[] = [];
private selectedSquareMoves: LegalMove[] = [];
private streamSubscription: Subscription | null = null;
private pollSubscription: Subscription | null = null;
private routeSubscription: Subscription | null = null;
constructor(
private readonly route: ActivatedRoute,
private readonly gameApi: GameApiService
) {}
get state(): GameState | null {
return this.game?.state ?? null;
}
get legalMoveUciList(): string[] {
return this.legalMoves.map((move) => move.uci);
}
ngOnInit(): void {
this.routeSubscription = this.route.paramMap.subscribe((paramMap) => {
const id = paramMap.get('gameId');
if (!id) {
this.errorMessage = 'Missing gameId in route.';
this.loading = false;
return;
}
this.gameId = id;
this.loadGame();
});
}
ngOnDestroy(): void {
this.routeSubscription?.unsubscribe();
this.streamSubscription?.unsubscribe();
this.pollSubscription?.unsubscribe();
}
onBoardSquareSelected(square: string): void {
if (!this.state) {
return;
}
if (this.selectedSquare && this.highlightedSquares.includes(square)) {
const selectedMove = this.selectedSquareMoves.find((move) => move.to === square);
if (selectedMove) {
this.moveInput = selectedMove.uci;
this.submitMove();
}
return;
}
const piece = this.getPieceAtSquare(this.state.fen, square);
if (!piece || !this.isCurrentTurnPiece(piece)) {
this.clearSelection();
return;
}
this.errorMessage = '';
this.gameApi.getLegalMoves(this.gameId, square).subscribe({
next: (response) => {
this.selectedSquare = square;
this.selectedSquareMoves = response.moves;
this.highlightedSquares = response.moves.map((move) => move.to);
},
error: () => {
this.clearSelection();
this.errorMessage = 'Could not load legal moves for selected square.';
}
});
}
submitMove(): void {
const uci = this.moveInput.trim();
if (!uci) {
return;
}
this.errorMessage = '';
this.gameApi.makeMove(this.gameId, uci).subscribe({
next: (state) => {
if (this.game) {
this.game = { ...this.game, state };
}
this.moveInput = '';
this.clearSelection();
this.loadLegalMoves();
},
error: (error: { error?: { message?: string } }) => {
this.errorMessage = error.error?.message ?? 'Move rejected.';
}
});
}
private loadGame(): void {
this.loading = true;
this.errorMessage = '';
this.clearSelection();
this.streamSubscription?.unsubscribe();
this.pollSubscription?.unsubscribe();
this.gameApi.getGame(this.gameId).subscribe({
next: (game) => {
this.game = game;
this.loading = false;
this.loadLegalMoves();
this.startStream();
this.startPolling();
},
error: (error: { error?: { message?: string } }) => {
this.errorMessage = error.error?.message ?? `Could not load game ${this.gameId}.`;
this.loading = false;
}
});
}
private loadLegalMoves(): void {
this.gameApi.getLegalMoves(this.gameId).subscribe({
next: (response) => {
this.legalMoves = response.moves;
},
error: () => {
this.legalMoves = [];
}
});
}
private startStream(): void {
this.streamSubscription = this.gameApi.streamGame(this.gameId).subscribe({
next: (event) => this.applyStreamEvent(event),
error: () => {
this.errorMessage = 'Live stream disconnected.';
}
});
}
private startPolling(): void {
this.pollSubscription = interval(1500)
.pipe(
startWith(0),
switchMap(() => this.gameApi.getGame(this.gameId))
)
.subscribe({
next: (game) => {
const previousMoves = this.game?.state.moves.join(',') ?? '';
this.game = game;
if (previousMoves !== game.state.moves.join(',')) {
this.clearSelection();
this.loadLegalMoves();
}
}
});
}
private applyStreamEvent(event: GameStreamEvent): void {
if (event.type === 'gameFull') {
this.game = event.game;
this.clearSelection();
this.loadLegalMoves();
return;
}
if (event.type === 'gameState' && this.game) {
const moveCountBefore = this.game.state.moves.length;
this.game = { ...this.game, state: event.state };
if (event.state.moves.length !== moveCountBefore) {
this.clearSelection();
}
this.loadLegalMoves();
return;
}
if (event.type === 'error') {
this.errorMessage = event.error.message;
}
}
private clearSelection(): void {
this.selectedSquare = null;
this.selectedSquareMoves = [];
this.highlightedSquares = [];
}
private isCurrentTurnPiece(pieceCode: string): boolean {
if (!this.state) {
return false;
}
const isWhitePiece = pieceCode === pieceCode.toUpperCase();
return (this.state.turn === 'white' && isWhitePiece) || (this.state.turn === 'black' && !isWhitePiece);
}
private getPieceAtSquare(fen: string, targetSquare: string): string | null {
const placement = fen.split(' ')[0] ?? '';
const rows = placement.split('/');
if (rows.length !== 8 || targetSquare.length !== 2) {
return null;
}
const file = targetSquare.charCodeAt(0) - 97;
const rank = Number(targetSquare[1]);
const rowIndex = 8 - rank;
if (Number.isNaN(rank) || file < 0 || file > 7 || rowIndex < 0 || rowIndex > 7) {
return null;
}
let column = 0;
for (const symbol of rows[rowIndex]) {
if (symbol >= '1' && symbol <= '8') {
column += Number(symbol);
continue;
}
if (column === file) {
return symbol;
}
column += 1;
}
return null;
}
}
@@ -0,0 +1,69 @@
.welcome-shell {
min-height: 100vh;
display: grid;
place-items: center;
padding: 2rem;
}
.welcome-card {
width: min(900px, 100%);
border-radius: 12px;
border: 2px solid #5A2C28;
background: #F3C8A0;
padding: 2rem;
box-shadow: 0 8px 24px rgba(90, 44, 40, 0.2);
}
h1 {
margin: 0 0 0.25rem;
color: #5A2C28;
}
p {
margin: 0 0 1.25rem;
}
.mode-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 1rem;
}
.mode {
border: 2px solid #5A2C28;
border-radius: 10px;
padding: 1rem;
text-align: left;
display: grid;
gap: 0.25rem;
}
.mode span {
font-size: 1.15rem;
color: #5A2C28;
}
.mode small {
color: #5A2C28;
opacity: 0.9;
}
.mode-active {
background: #B9DAD1;
cursor: pointer;
}
.mode-active:hover:enabled {
background: #B9C2DA;
}
.mode-disabled {
background: #E1EAA9;
opacity: 0.75;
}
.error {
color: #5A2C28;
font-weight: 700;
margin-top: 1rem;
}
@@ -0,0 +1,27 @@
<main class="welcome-shell">
<section class="welcome-card">
<h1>Welcome to NowChess</h1>
<p>Pick a mode to begin.</p>
<div class="mode-grid">
<button type="button" class="mode mode-disabled" disabled>
<span>Bot</span>
<small>Coming soon</small>
</button>
<button type="button" class="mode mode-active" (click)="startOneVsOne()" [disabled]="creating">
<span>1 vs 1</span>
<small>{{ creating ? 'Creating game...' : 'Start now' }}</small>
</button>
<button type="button" class="mode mode-disabled" disabled>
<span>Future Technique</span>
<small>Placeholder</small>
</button>
</div>
@if (errorMessage) {
<p class="error">{{ errorMessage }}</p>
}
</section>
</main>
@@ -0,0 +1,43 @@
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { finalize } from 'rxjs';
import { GameApiService } from '../../services/game-api.service';
@Component({
selector: 'app-welcome',
standalone: true,
imports: [CommonModule],
templateUrl: './welcome.component.html',
styleUrl: './welcome.component.css'
})
export class WelcomeComponent {
creating = false;
errorMessage = '';
constructor(
private readonly router: Router,
private readonly gameApi: GameApiService
) {}
startOneVsOne(): void {
if (this.creating) {
return;
}
this.errorMessage = '';
this.creating = true;
this.gameApi
.createGame()
.pipe(finalize(() => (this.creating = false)))
.subscribe({
next: (game) => {
void this.router.navigate(['/game', game.gameId]);
},
error: (error: { error?: { message?: string } }) => {
this.errorMessage = error.error?.message ?? 'Unable to create a game.';
}
});
}
}