Skip to content

Commit

Permalink
#1645 #1500 and adding anons-as-test-users mode cmdline switch
Browse files Browse the repository at this point in the history
  • Loading branch information
nnickoloff1234 committed Jan 6, 2025
1 parent bf0bb14 commit 4f44a63
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 152 deletions.
49 changes: 43 additions & 6 deletions client/bug/chat.bug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { h, VNode } from "snabbdom";
import { _ } from '@/i18n';
import { patch } from "@/document";
import {RoundControllerBughouse} from "@/bug/roundCtrl.bug";
import {selectMove} from "@/bug/movelist.bug";
import {formatChatMessageTime, getLocalMoveNum, selectMove} from "@/bug/movelist.bug";
import {StepChat} from "@/messages";

export function renderBugChatPresets(sendMessage: (s:string)=>void): VNode {
return h('div#chatpresets', [
Expand Down Expand Up @@ -34,12 +35,48 @@ export function renderBugChatPresets(sendMessage: (s:string)=>void): VNode {
]);
}

export function chatMessageBug (container: HTMLElement, user: string, message: string, /*chatType: string,*/ localTime?: string, ply?: number, ctrl?: RoundControllerBughouse) {
export function chatMessageBug (ply: number, ctrl: RoundControllerBughouse, x: StepChat) {

//TODO: first lines and very last copied from chat.ts
const chatDiv = document.getElementById('bugroundchat-messages') as HTMLElement;
// You must add border widths, padding and margins to the right.
// Only scroll the chat on a new message if the user is at the very bottom of the chat
const isBottom = chatDiv.scrollHeight - (chatDiv.scrollTop + chatDiv.offsetHeight) < 80;
const container = document.getElementById('messages') as HTMLElement;

const step = ctrl?.steps[ply!]!;
const boardName = step.turnColor === 'black' ? step.boardName?.toUpperCase() : step.boardName;
const lastMoveSan = ply === 0? "": getLocalMoveNum(step) + '' + boardName + "." + step.san!;

const message = x.message
const m = message.replace('!bug!','');
patch(container, h('div#messages', [ h("li.message",
[h("div.time", localTime), h("user", h("a", { attrs: {href: "/@/" + user} }, user)),
h('div.bugchat.'+m,{ attrs: {"title": ctrl?.steps[ply!].san!}, on: { click: () => {onchatclick(ply, ctrl)}}}, [])
]) ]));

const user = x.username

const time = formatChatMessageTime(x);

const san = h("div.time.bugchatpointer", {attrs: {"title": time }, on: {
click: () => {
onchatclick(ply, ctrl)
}}}, lastMoveSan);

if (message.startsWith("!bug!")) {

patch(container, h('div#messages', [h("li.message",
[san, h("user", h("a", {attrs: {href: "/@/" + user}}, user)),
h('div.bugchat.' + m, {
attrs: {"title": lastMoveSan}, on: {
click: () => {
onchatclick(ply, ctrl)
}
}
}, [])
])]));
} else {
patch(container, h('div#messages', [ h("li.message", [san, h("user", h("a", { attrs: {href: "/@/" + user} }, user)), h("t.bugchatpointer", { attrs: {"title": ctrl?.steps[ply!].san!}, on: { click: () => { onchatclick(ply, ctrl) }}}, message)]) ]));
}

if (isBottom) setTimeout(() => {chatDiv.scrollTop = chatDiv.scrollHeight;}, 200);
}

export function onchatclick(ply: number | undefined, ctrl?: RoundControllerBughouse) {
Expand Down
34 changes: 17 additions & 17 deletions client/bug/movelist.bug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import AnalysisControllerBughouse from './analysisCtrl.bug';
import { result } from '../result'
import { patch } from '../document';
import { RoundControllerBughouse } from "./roundCtrl.bug";
import { Step } from "../messages";
import {Step, StepChat} from "../messages";

export function selectMove (ctrl: AnalysisControllerBughouse | RoundControllerBughouse, ply: number, plyVari = 0): void {
let plyMax = ctrl.steps.length - 1;
Expand Down Expand Up @@ -97,22 +97,13 @@ export function updateMovelist (ctrl: AnalysisControllerBughouse | RoundControll
const moves: VNode[] = [];
const prevPly = ctrl.steps[plyFrom-1];
let lastColIdx = plyFrom ===1? 0: prevPly.boardName === 'a'? prevPly.turnColor === 'white'/*black made the move*/? 2: 1: prevPly.turnColor === 'white'/*black made the move*/? 4: 3;
let plyA: number = 0;
let plyB: number = 0;
let didWeRenderVariSectionAfterLastMove = false;
let didWeRenderChatSectionAfterLastMove = false;

// in round mode we only call this for last move, so we need to reconstruct actual per-board ply from history
for (let ply = 1; ply < plyFrom; ply++) {
ctrl.steps[ply].boardName === 'a'? plyA++ : plyB++;
}

for (let ply = plyFrom; ply < plyTo; ply++) {
const move = ctrl.steps[ply].san;
if (move === null) continue;

ctrl.steps[ply].boardName === 'a'? plyA++ : plyB++;

const colIdx = ctrl.steps[ply].boardName === 'a'? ctrl.steps[ply].turnColor === 'black'/*meaning move was made by white and now black's turn*/? 1 : 2 : ctrl.steps[ply].turnColor === 'black'? 3 : 4 ;

if (didWeRenderVariSectionAfterLastMove) {
Expand All @@ -139,10 +130,7 @@ export function updateMovelist (ctrl: AnalysisControllerBughouse | RoundControll
if (ctrl.steps[ply].chat) {
const chatMessages: VNode[] = [];
for (let x of ctrl.steps[ply].chat!) {
const min = Math.floor(x.time/60000);
const sec = Math.floor((x.time - min*60000)/1000);
const millis = x.time - min*60000 - sec*1000;
const time = min+":"+(sec.toString().padStart(2, '0'))+"."+(millis.toString().padStart(3, '0'));
const time = formatChatMessageTime(x)
const m = x.message.replace('!bug!','');
const v = h("li.message",
[h("div.time", time), h("user", h("a", { attrs: {href: "/@/" + x.username} }, x.username)),
Expand All @@ -157,7 +145,7 @@ export function updateMovelist (ctrl: AnalysisControllerBughouse | RoundControll
didWeRenderChatSectionAfterLastMove = true;
}

moves.push(h('move-bug.counter', Math.floor(ctrl.steps[ply].boardName === 'a'? (plyA + 1) / 2 : (plyB + 1) / 2 ) ) );
moves.push(h('move-bug.counter', getLocalMoveNum(ctrl.steps[ply])));

const el = h('move-bug', {
class: { active: ((ply === plyTo - 1) && activate), haschat: !!ctrl.steps[ply].chat },
Expand All @@ -173,8 +161,8 @@ export function updateMovelist (ctrl: AnalysisControllerBughouse | RoundControll

// if (ply % 2 !== 0) moves.push(h('move-bug', '...'));

let plyAVari = plyA;
let plyBVari = plyB;
let plyAVari = ctrl.steps[ply].plyA!;
let plyBVari = ctrl.steps[ply].plyB!;

moves.push(h('vari#vari' + ctrl.plyVari,
variMoves?
Expand Down Expand Up @@ -220,6 +208,18 @@ export function updateMovelist (ctrl: AnalysisControllerBughouse | RoundControll
}
}

export function getLocalMoveNum(step: Step) {
return Math.floor(step.boardName === 'a'? (step.plyA! + 1) / 2 : (step.plyB! + 1) / 2 );
}

export function formatChatMessageTime(x: StepChat) {
const min = Math.floor(x.time/60000);
const sec = Math.floor((x.time - min*60000)/1000);
const millis = x.time - min*60000 - sec*1000;
const time = min+":"+(sec.toString().padStart(2, '0'))+"."+(millis.toString().padStart(3, '0'));
return time;
}

export function updateResult (ctrl: AnalysisControllerBughouse | RoundControllerBughouse) {
if (ctrl.status < 0) return;

Expand Down
Loading

0 comments on commit 4f44a63

Please sign in to comment.