Skip to content

Commit

Permalink
Update chatCtrl to include round and game information in chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Carbrex committed Jun 17, 2024
1 parent 0d09633 commit a615fd0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
31 changes: 31 additions & 0 deletions ui/chat/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export default class ChatCtrl {
loaded: false,
enabled: prop(!!this.data.palantir),
};
console.log('site-study', site.analysis.study);
console.log('site-burl', site.analysis.study.baseUrl());
console.log('site-pos', site.analysis.study.position());
this.trans = site.trans(this.opts.i18n);
const noChat = site.storage.get('nochat');
this.vm = {
Expand Down Expand Up @@ -97,11 +100,36 @@ export default class ChatCtrl {
post = (text: string): boolean => {
text = text.trim();
if (!text) return false;
if (text.startsWith('<<<<')) return false;
if (text == 'You too!' && !this.data.lines.some(l => l.u != this.data.userId)) return false;
if (text.length > 140) {
alert('Max length: 140 chars. ' + text.length + ' chars used.');
return false;
}

if (site.analysis) {
// let roundId = 'static-round-id';
let roundId = site.analysis.study.relay.currentRound().id;
let roundSlug = site.analysis.study.relay.currentRound().slug;
// let gameId = 'static-game-id';
let gameId = site.analysis.study.currentChapter().id;
let moveNo = site.analysis.study.currentNode().ply;
// roundId = site.analysis.study.roundId();
// gameId = site.analysis.study.gameId();
// moveNo = site.analysis.study.moveNo();
console.log('site-study', site.analysis.study);
console.log('site-relay-tourShow', site.analysis.study.relay);
console.log('site-relay-tourShow', site.analysis.study.relay.tourShow);
console.log('site-round', site.analysis.study.relay.currentRound().id);
console.log('site-round', site.analysis.study.relay.currentRound().slug);
console.log('site-pos', site.analysis.study.position());
console.log('site-game', site.analysis.study.currentChapter().id);
console.log('site-shr-node', site.analysis.study.currentNode());
console.log('site-shr-node', site.analysis.study.currentNode().ply);
text = '<<<<' + roundSlug + '|' + roundId + '|' + gameId + '|' + moveNo + '>>>> ' + text;
}

console.log('chat post', text);
site.pubsub.emit('socket.send', 'talk', text);
return true;
};
Expand Down Expand Up @@ -131,6 +159,9 @@ export default class ChatCtrl {
onMessage = (line: Line) => {
this.data.lines.push(line);
const nb = this.data.lines.length;
console.log('chat message', line);
console.log('chat lines', nb);
console.log(this.data);
if (nb > this.maxLines) {
this.data.lines.splice(0, nb - this.maxLines + this.maxLinesDrop);
this.vm.domVersion++;
Expand Down
25 changes: 24 additions & 1 deletion ui/chat/src/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function (ctrl: ChatCtrl): Array<VNode | undefined> {
attrs: { role: 'log', 'aria-live': 'polite', 'aria-atomic': 'false' },
hook: {
insert(vnode) {
// console.log('vnode', vnode);
const $el = $(vnode.elm as HTMLElement).on('click', 'a.jump', (e: Event) => {
site.pubsub.emit('jump', (e.target as HTMLElement).getAttribute('data-ply'));
});
Expand Down Expand Up @@ -188,6 +189,24 @@ const userThunk = (name: string, title?: string, patron?: boolean, flair?: Flair
userLink({ name, title, patron, line: !!patron, flair });

function renderLine(ctrl: ChatCtrl, line: Line): VNode {
if (line.t.startsWith('<<<<')) {
// line.t looks like '<<<<roundSlug|roundId|gameId|moveNo>>>> text'
//text = '<<<<' + roundSlug+'|' roundId + '|' + gameId + '|' + moveNo + '>>>> ' + text;
const parts = line.t.match(/^<<<<([^|]+)\|([^|]+)\|([^|]+)\|([^|]+)>>>>\s(.+)$/);
if (parts) {
const [_, roundSlug, roundId, gameId, moveNo, text] = parts;
// console.log('_',_);
const broadcastSlug= site.analysis.study.relayData.tour.slug;
const broadcastURL = `/broadcast/${broadcastSlug}/${roundSlug}/${roundId}/${gameId}#${moveNo}`;
// const ply = `${roundId}/${gameId}/${moveNo}`;
// console.log('ply', ply);
line.t = text;
line.ply = broadcastURL;
console.log('line', broadcastURL);
}
}

// console.log('line', line);
const textNode = renderText(line.t, ctrl.opts.enhance);

if (line.u === 'lichess') return h('li.system', textNode);
Expand All @@ -204,6 +223,8 @@ function renderLine(ctrl: ChatCtrl, line: Line): VNode {
.match(enhance.userPattern)
?.find(mention => mention.trim().toLowerCase() == `@${ctrl.data.userId}`);

const plyy = line.ply ? h('a', { attrs: { 'href': line.ply } }, ' -->') : null;
// console.log('plyy', plyy);
return h(
'li',
{
Expand All @@ -214,7 +235,8 @@ function renderLine(ctrl: ChatCtrl, line: Line): VNode {
},
},
ctrl.moderation
? [line.u ? modLineAction() : null, userNode, ' ', textNode]
? [line.u ? modLineAction() : null, userNode, ' ', textNode,
plyy]
: [
myUserId && line.u && myUserId != line.u
? h('action.flag', {
Expand All @@ -224,6 +246,7 @@ function renderLine(ctrl: ChatCtrl, line: Line): VNode {
userNode,
' ',
textNode,
plyy
],
);
}
1 change: 1 addition & 0 deletions ui/chat/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Line {
p?: boolean; // patron
f?: Flair;
title?: string;
ply?: any;
}

export interface Permissions {
Expand Down

0 comments on commit a615fd0

Please sign in to comment.