Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions client/gameInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ export function gameInfo(model: PyChessModel): VNode {
])
)
}
if (model["simulId"]) {
sections.push(
h('section', [
h('div.tourney', [
h('a.icon.icon-target', { attrs: { href: '/simul/' + model["simulId"] } }, model["simulname"] || _("Simul"))
])
])
)
}
return h('div.game-info', sections);
}

Expand Down
3 changes: 3 additions & 0 deletions client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function initModel(el: HTMLElement) {
tournamentId : el.getAttribute("data-tournamentid") ?? "",
tournamentname : el.getAttribute("data-tournamentname") ?? "",
simulId : el.getAttribute("data-simulid") ?? "",
simulname : el.getAttribute("data-simulname") ?? "",
tournamentcreator: el.getAttribute("data-tournamentcreator") ?? "",
inviter : el.getAttribute("data-inviter") ?? "",
ply : parseInt(""+el.getAttribute("data-ply")),
Expand Down Expand Up @@ -117,6 +118,8 @@ function initModel(el: HTMLElement) {
puzzle: el.getAttribute("data-puzzle") ?? "",
blogs: el.getAttribute("data-blogs") ?? "",
corrGames: el.getAttribute("data-corrgames") ?? "",
simulGames: el.getAttribute("data-simulgames") ?? "",
simulHost: el.getAttribute("data-simulhost") === "True",
oauthUsernameSelection: el.getAttribute("data-oauth-id") ? {
oauth_id: el.getAttribute("data-oauth-id")!,
oauth_provider: el.getAttribute("data-oauth-provider")!,
Expand Down
1 change: 1 addition & 0 deletions client/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface MsgBoard {
by: string;
status: number;
pgn: string;
tp: string;
uci_usi: string;
result: string;
steps: Step[];
Expand Down
94 changes: 77 additions & 17 deletions client/nowPlaying.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { timeago } from './datetime';
import { getLastMoveFen, VARIANTS } from './variants';
import { displayUsername } from './user';

export type OngoingGamesMode = 'corr' | 'simul';

export interface Game {
gameId: string;
variant: string;
Expand All @@ -26,14 +28,43 @@ export interface Game {
fen: cg.FEN;
lastMove: string;
tp: string;
mins: number;
mins?: number;
date: string;
status?: number;
result?: string;
}

export function handleOngoingGameEvents(username: string, cgMap: {[gameId: string]: [Api, string]}) {
export interface OngoingGameUpdate {
gameId: string;
fen: cg.FEN;
lastMove: string;
tp: string;
date: string;
status?: number;
result?: string;
}

type OngoingGameEventOptions = {
mode?: OngoingGamesMode;
updateUnreadCounter?: boolean;
onUpdate?: (message: OngoingGameUpdate) => void;
};

export function handleOngoingGameEvents(
username: string,
cgMap: {[gameId: string]: [Api, string]},
options: OngoingGameEventOptions = {},
) {
const mode = options.mode ?? 'corr';
const updateUnreadCounter = options.updateUnreadCounter ?? (mode === 'corr');
const evtSource = new EventSource("/api/ongoing");
evtSource.onmessage = function(event) {
const message = JSON.parse(event.data);
const message = JSON.parse(event.data) as OngoingGameUpdate;

if (options.onUpdate) {
options.onUpdate(message);
}

if (!(message.gameId in cgMap)) return;

let cg, variantName;
Expand All @@ -46,14 +77,18 @@ export function handleOngoingGameEvents(username: string, cgMap: {[gameId: strin
fen: fen,
lastMove: lastMove,
});

const isMyTurn = message.tp === username;
patch(document.querySelector(`a[href='${message.gameId}'] .indicator`) as HTMLElement,
h('span.indicator', ''),
);
patch(document.querySelector(`a[href='${message.gameId}'] .indicator`) as HTMLElement,
corrClockIndicator(isMyTurn, message.date),
);
const noreadEl = document.querySelector('span.noread') as HTMLElement;
const indicatorEl = document.querySelector(`a[href='${message.gameId}'] .indicator`) as HTMLElement | null;
if (indicatorEl) {
patch(indicatorEl, gameIndicator(isMyTurn, message.date, mode));
}

if (!updateUnreadCounter) return;

const noreadEl = document.querySelector('span.noread') as HTMLElement | null;
if (!noreadEl) return;

const diff = isMyTurn ? 1 : -1;
const count = parseInt(noreadEl.dataset.count || '0') + diff;
patch(noreadEl, h('span.noread.data-count', {attrs: { 'data-count': count }}));
Expand All @@ -78,19 +113,44 @@ function corrClockIndicator(isMyTurn:boolean, date: string) {
return h('span.indicator', isMyTurn ? timer(date) : h('span', '\xa0')) //  
}

export function compareGames(username: string) {
function simulTurnIndicator(isMyTurn: boolean) {
return h('span.indicator', isMyTurn ? '●' : h('span', '\xa0'));
}

function gameIndicator(isMyTurn: boolean, date: string, mode: OngoingGamesMode) {
return mode === 'simul' ? simulTurnIndicator(isMyTurn) : corrClockIndicator(isMyTurn, date);
}

export function compareGames(username: string, mode: OngoingGamesMode = 'corr') {
return function(a: Game, b: Game) {
const aIsUserTurn = (a.tp === username);
const bIsUserTurn = (b.tp === username);
const aFinished = typeof a.status === 'number' && a.status >= 0;
const bFinished = typeof b.status === 'number' && b.status >= 0;
if (aFinished && !bFinished) return 1;
if (!aFinished && bFinished) return -1;

const aIsUserTurn = a.tp === username;
const bIsUserTurn = b.tp === username;
if (aIsUserTurn && !bIsUserTurn) return -1;
if (!aIsUserTurn && bIsUserTurn) return 1;
if (a.mins < b.mins) return -1;
if (a.mins > b.mins) return 1;

if (mode === 'simul') {
return a.gameId.localeCompare(b.gameId);
}

const aMins = typeof a.mins === 'number' ? a.mins : Number.POSITIVE_INFINITY;
const bMins = typeof b.mins === 'number' ? b.mins : Number.POSITIVE_INFINITY;
if (aMins < bMins) return -1;
if (aMins > bMins) return 1;
return 0;
};
}

export function gameViewPlaying(cgMap: {[gameId: string]: [Api, string]}, game: Game, username: string) {
export function gameViewPlaying(
cgMap: {[gameId: string]: [Api, string]},
game: Game,
username: string,
mode: OngoingGamesMode = 'corr',
) {
const variant = VARIANTS[game.variant];
const isMyTurn = game.tp === username;
const opp = (username === game.w) ? game.b : game.w;
Expand Down Expand Up @@ -121,7 +181,7 @@ export function gameViewPlaying(cgMap: {[gameId: string]: [Api, string]}, game:
}),
h('span.vstext', [
h('span', oppDisplay),
corrClockIndicator(isMyTurn, game.date),
gameIndicator(isMyTurn, game.date, mode),
]),
]);
}
Loading