import {InitiativeTrackerUtil, RenderableCollectionConditions} from "./initiativetracker-utils.js"; export class InitiativeTrackerPlayerUiV1 { constructor (view, playerName, serverToken) { this._view = view; this._playerName = playerName; this._serverToken = serverToken; this._clientPeer = new PeerVeClient(); } async pInit () { try { await this._clientPeer.pConnectToServer( this._serverToken, data => this._view.handleMessage(data), { label: this._playerName, serialization: "json", }, ); } catch (e) { JqueryUtil.doToast({ content: `Failed to create client! Are you sure the token was valid? (See the log for more details.)`, type: "danger", }); throw e; } } } export class InitiativeTrackerPlayerUiV0 { constructor (view, $iptServerToken, $btnGenClientToken, $iptClientToken) { this._view = view; this._$iptServerToken = $iptServerToken; this._$btnGenClientToken = $btnGenClientToken; this._$iptClientToken = $iptClientToken; } init () { this._$iptServerToken.keydown(evt => { this._$iptServerToken.removeClass("error-background"); if (evt.which === 13) this._$btnGenClientToken.click(); }); this._$btnGenClientToken.click(async () => { this._$iptServerToken.removeClass("error-background"); const serverToken = this._$iptServerToken.val(); if (PeerUtilV0.isValidToken(serverToken)) { try { this._$iptServerToken.attr("disabled", true); this._$btnGenClientToken.attr("disabled", true); const clientData = await PeerUtilV0.pInitialiseClient( serverToken, msg => this._view.handleMessage(msg), function (err) { if (!this.isClosed) { JqueryUtil.doToast({ content: `Server error:\n${err ? err.message || err : "(Unknown error)"}`, type: "danger", }); } }, ); if (!clientData) { this._$iptServerToken.attr("disabled", false); this._$btnGenClientToken.attr("disabled", false); JqueryUtil.doToast({ content: `Failed to create client. Are you sure the token was valid?`, type: "warning", }); } else { this._view.clientData = clientData; // -- This has no effect; the client doesn't error on sending when there's no connection -- // const livenessCheck = setInterval(async () => { // try { // await clientData.client.sendMessage({}) // } catch (e) { // JqueryUtil.doToast({ // content: `Could not reach server! You might need to reconnect.`, // type: "danger" // }); // clearInterval(livenessCheck); // } // }, 5000); this._$iptClientToken.val(clientData.textifiedSdp).attr("disabled", false); } } catch (e) { JqueryUtil.doToast({ content: `Failed to create client! Are you sure the token was valid? (See the log for more details.)`, type: "danger", }); setTimeout(() => { throw e; }); } } else this._$iptServerToken.addClass("error-background"); }); this._$iptClientToken.click(async () => { await MiscUtil.pCopyTextToClipboard(this._$iptClientToken.val()); JqueryUtil.showCopiedEffect(this._$iptClientToken); }); } } export class InitiativeTrackerPlayerMessageHandlerV1 { constructor (isCompact) { this._isCompact = isCompact; this._isUiInit = false; this._$meta = null; this._$head = null; this._$rows = null; } get isActive () { return this._isUiInit; } setElements ($meta, $head, $rows) { this._$meta = $meta; this._$head = $head; this._$rows = $rows; } initUi () {} // to be overridden as required handleMessage (msg) { const {data: {type, payload}} = msg; switch (type) { case "state": return this._handleMessage_state({payload: payload}); case "showImage": return this._handleMessage_showImage({payload: payload}); default: throw new Error(`Unhandled message type "${type}"!`); } } /* -------------------------------------------- */ _handleMessage_state ({payload}) { this.initUi(); const data = payload || {}; this._$meta.empty(); this._$head.empty(); this._$rows.empty(); if (data.round) { this._$meta.append(`