fix(SCOPONE-0008): complete iteration 1 remove ai lag
This commit is contained in:
@@ -3,7 +3,8 @@ import { Card, PlayerIndex, GameState, Difficulty } from '../game/types';
|
||||
import {
|
||||
createInitialState, applyMove, findCaptures, getScoreBreakdown, teamOf, calcPrimiera, getMatchOutcome
|
||||
} from '../game/engine';
|
||||
import { chooseMove, AIDecisionProgress } from '../game/ai';
|
||||
import { AIDecisionProgress } from '../game/ai';
|
||||
import { AIWorkerClient, AIWorkerClientLike } from '../game/ai-worker-client';
|
||||
import { CardTracker } from '../game/card-tracker';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -56,6 +57,7 @@ export class GameScene extends Phaser.Scene {
|
||||
// Difficulty & card tracker
|
||||
private difficulty: Difficulty = 'advanced';
|
||||
private tracker: CardTracker = new CardTracker();
|
||||
private aiClient: AIWorkerClientLike | null = null;
|
||||
|
||||
// Active player highlight
|
||||
private activeHighlightRect: Phaser.GameObjects.Graphics | null = null;
|
||||
@@ -111,6 +113,10 @@ export class GameScene extends Phaser.Scene {
|
||||
// Read difficulty from scene data (MenuScene passes it)
|
||||
this.difficulty = data?.difficulty ?? 'advanced';
|
||||
this.tracker = new CardTracker();
|
||||
this.aiClient?.dispose();
|
||||
this.aiClient = new AIWorkerClient();
|
||||
this.events.once(Phaser.Scenes.Events.SHUTDOWN, this.handleSceneShutdown, this);
|
||||
this.events.once(Phaser.Scenes.Events.DESTROY, this.handleSceneShutdown, this);
|
||||
|
||||
this.generateParticleTextures();
|
||||
this.drawBackground(W, H);
|
||||
@@ -603,21 +609,36 @@ export class GameScene extends Phaser.Scene {
|
||||
}
|
||||
}
|
||||
|
||||
private handleSceneShutdown(): void {
|
||||
this.aiClient?.dispose();
|
||||
this.aiClient = null;
|
||||
this.aiThinking = false;
|
||||
if (this.thinkBar) {
|
||||
this.hideThinkBar();
|
||||
}
|
||||
}
|
||||
|
||||
private async doAIMove(playerIdx: PlayerIndex): Promise<void> {
|
||||
const turnState = this.state;
|
||||
const aiClient = this.aiClient;
|
||||
|
||||
if (!aiClient) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const move = await chooseMove(
|
||||
const move = await aiClient.chooseMove(
|
||||
this.state,
|
||||
playerIdx,
|
||||
this.difficulty,
|
||||
this.tracker,
|
||||
(progress) => {
|
||||
if (!this.scene.isActive('GameScene') || this.state !== turnState) return;
|
||||
if (this.aiClient !== aiClient || !this.scene.isActive('GameScene') || this.state !== turnState) return;
|
||||
this.updateThinkBar(playerIdx, progress);
|
||||
}
|
||||
);
|
||||
|
||||
if (this.aiClient !== aiClient) return;
|
||||
if (!this.scene.isActive('GameScene')) return;
|
||||
if (this.state !== turnState || this.state.currentPlayer !== playerIdx || this.state.roundOver) return;
|
||||
|
||||
@@ -626,11 +647,11 @@ export class GameScene extends Phaser.Scene {
|
||||
this.executeMove(playerIdx, move.card, move.capture);
|
||||
} catch (error) {
|
||||
console.error('AI move failed', error);
|
||||
if (this.scene.isActive('GameScene') && this.state === turnState) {
|
||||
if (this.aiClient === aiClient && this.scene.isActive('GameScene') && this.state === turnState) {
|
||||
this.setStatus('Errore durante la mossa AI');
|
||||
}
|
||||
} finally {
|
||||
if (this.scene.isActive('GameScene') && this.state === turnState) {
|
||||
if (this.aiClient === aiClient && this.scene.isActive('GameScene') && this.state === turnState) {
|
||||
this.hideThinkBar();
|
||||
this.aiThinking = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user