Skip to content

Commit

Permalink
refactor:use analyse result rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
allanjoseph98 committed Dec 18, 2024
1 parent 06d76fb commit e08c9c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
29 changes: 14 additions & 15 deletions ui/analyse/src/plugins/analyse.nvui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type AnalyseController from '../ctrl';
import { makeConfig as makeCgConfig } from '../ground';
import type { AnalyseData } from '../interfaces';
import type { Player } from 'game';
import viewStatus from 'game/view/status';
import {
type MoveStyle,
renderSan,
Expand Down Expand Up @@ -44,6 +43,7 @@ import { setupPosition } from 'chessops/variant';
import { plyToTurn } from 'chess';
import { Chessground as makeChessground } from 'chessground';
import { pubsub } from 'common/pubsub';
import { renderResult } from '../view/components';

const throttled = (sound: string) => throttle(100, () => site.sound.play(sound));
const selectSound = throttled('select');
Expand Down Expand Up @@ -103,7 +103,7 @@ export function initModule(ctrl: AnalyseController) {
: []),
h('h2', 'Pieces'),
h('div.pieces', renderPieces(ctrl.chessground.state.pieces, style)),
...renderResult(ctrl),
...renderAriaResult(ctrl),
h('h2', 'Current position'),
h(
'p.position.lastMove',
Expand Down Expand Up @@ -314,19 +314,18 @@ function renderBestMove(ctrl: AnalyseController, style: MoveStyle): string {
return '';
}

function renderResult(ctrl: AnalyseController): VNode[] {
if (ctrl.data.game.status.id >= 30) {
const winner = ctrl.data.game.winner;
const result = winner === 'white' ? '1-0' : winner === 'black' ? '0-1' : '½-½';
return [
h('h2', 'Game status'),
h('div.status', { attrs: { role: 'status', 'aria-live': 'assertive', 'aria-atomic': 'true' } }, [
h('div.result', result),
h('div.status', viewStatus(ctrl)),
]),
];
}
return [];
function renderAriaResult(ctrl: AnalyseController): VNode[] {
const result = renderResult(ctrl);
return result.length
? [
h('h2', 'Game status'),
h(
'div.status',
{ attrs: { role: 'status', 'aria-live': 'assertive', 'aria-atomic': 'true' } },
result,
),
]
: result;
}

function renderCurrentLine(ctrl: AnalyseController, style: MoveStyle): VNodeChildren {
Expand Down
43 changes: 17 additions & 26 deletions ui/analyse/src/view/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,36 +341,27 @@ export function renderControls(ctrl: AnalyseCtrl) {
);
}

function renderMoveList(ctrl: AnalyseCtrl, deps?: typeof studyDeps, concealOf?: ConcealOf) {
function renderResult(ctrl: AnalyseCtrl, deps?: typeof studyDeps): VNode[] {
const render = (result: string, status: VNodeKids) => [h('div.result', result), h('div.status', status)];
if (ctrl.data.game.status.id >= 30) {
let result;
switch (ctrl.data.game.winner) {
case 'white':
result = '1-0';
break;
case 'black':
result = '0-1';
break;
default:
result = '½-½';
}
return render(result, statusView(ctrl));
} else if (ctrl.study) {
const result = deps?.findTag(ctrl.study.data.chapter.tags, 'result');
if (!result || result === '*') return [];
if (result === '1-0') return render(result, [i18n.site.whiteIsVictorious]);
if (result === '0-1') return render(result, [i18n.site.blackIsVictorious]);
return render('½-½', [i18n.site.draw]);
}
return [];
export function renderResult(ctrl: AnalyseCtrl, deps?: typeof studyDeps): VNode[] {
const render = (result: string, status: VNodeKids) => [h('div.result', result), h('div.status', status)];
if (ctrl.data.game.status.id >= 30) {
const winner = ctrl.data.game.winner;
const result = winner === 'white' ? '1-0' : winner === 'black' ? '0-1' : '½-½';
return render(result, statusView(ctrl));
} else if (ctrl.study) {
const result = deps?.findTag(ctrl.study.data.chapter.tags, 'result');
if (!result || result === '*') return [];
if (result === '1-0') return render(result, [i18n.site.whiteIsVictorious]);
if (result === '0-1') return render(result, [i18n.site.blackIsVictorious]);
return render('½-½', [i18n.site.draw]);
}
return h('div.analyse__moves.areplay', [
return [];
}

const renderMoveList = (ctrl: AnalyseCtrl, deps?: typeof studyDeps, concealOf?: ConcealOf): VNode =>
h('div.analyse__moves.areplay', [
h(`div.areplay__v${ctrl.treeVersion}`, [renderTreeView(ctrl, concealOf), ...renderResult(ctrl)]),
!ctrl.practice && !deps?.gbEdit.running(ctrl) && renderNextChapter(ctrl),
]);
}

export const renderMaterialDiffs = (ctrl: AnalyseCtrl): [VNode, VNode] =>
materialView.renderMaterialDiffs(
Expand Down

0 comments on commit e08c9c7

Please sign in to comment.