From 64d093af95f4ab8ee5dc15bf2cc51b68c822c45c Mon Sep 17 00:00:00 2001 From: stonedDiscord Date: Sat, 26 Mar 2022 17:38:48 +0100 Subject: type the rest --- webAO/client.ts | 177 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 89 insertions(+), 88 deletions(-) diff --git a/webAO/client.ts b/webAO/client.ts index 7d1d1de..e7336ac 100644 --- a/webAO/client.ts +++ b/webAO/client.ts @@ -160,7 +160,7 @@ class Client extends EventEmitter { checkUpdater: any; _lastTimeICReceived: any; - constructor(address) { + constructor(address: string) { super(); if (mode !== 'replay') { this.serv = new WebSocket(`ws://${address}`); @@ -336,12 +336,12 @@ class Client extends EventEmitter { * @param {string} message the message to be sent * @param {string} side the name of the side in the background * @param {string} sfx_name the name of the sound effect - * @param {string} emote_modifier whether or not to zoom + * @param {number} emote_modifier whether or not to zoom * @param {number} sfx_delay the delay (in milliseconds) to play the sound effect - * @param {string} objection_modifier the number of the shout to play + * @param {number} objection_modifier the number of the shout to play * @param {string} evidence the filename of evidence to show - * @param {number} flip change to 1 to reverse sprite for position changes - * @param {number} realization screen flash effect + * @param {boolean} flip change to 1 to reverse sprite for position changes + * @param {boolean} realization screen flash effect * @param {number} text_color text color * @param {string} showname custom name to be displayed (optional) * @param {number} other_charid paired character (optional) @@ -356,24 +356,24 @@ class Client extends EventEmitter { message: string, side: string, sfx_name: string, - emote_modifier: string, - sfx_delay: string, - objection_modifier: string, - evidence: string, - flip: string, - realization: string, - text_color: string, + emote_modifier: number, + sfx_delay: number, + objection_modifier: number, + evidence: number, + flip: boolean, + realization: boolean, + text_color: number, showname: string, other_charid: string, self_hoffset: string, self_yoffset: string, - noninterrupting_preanim: string, - looping_sfx: string, - screenshake: string, + noninterrupting_preanim: boolean, + looping_sfx: boolean, + screenshake: boolean, frame_screenshake: string, frame_realization: string, frame_sfx: string, - additive: string, + additive: boolean, effect: string, ) { let extra_cccc = ''; @@ -425,7 +425,7 @@ class Client extends EventEmitter { * @param {string} evidence description * @param {string} evidence image filename */ - sendEE(id: string, name: string, desc: string, img: string) { + sendEE(id: number, name: string, desc: string, img: string) { this.sendServer(`EE#${id}#${escapeChat(encodeChat(name))}#${escapeChat(encodeChat(desc))}#${img}#%`); } @@ -433,7 +433,7 @@ class Client extends EventEmitter { * Sends delete evidence command. * @param {number} evidence id */ - sendDE(id) { + sendDE(id: number) { this.sendServer(`DE#${id}#%`); } @@ -442,7 +442,7 @@ class Client extends EventEmitter { * @param {number} side the position * @param {number} hp the health point */ - sendHP(side: string, hp: string) { + sendHP(side: number, hp: number) { this.sendServer(`HP#${side}#${hp}#%`); } @@ -533,7 +533,7 @@ class Client extends EventEmitter { (document.getElementById('ic_chat_name')).value = getCookie('ic_chat_name'); (document.getElementById('showname')).checked = Boolean(getCookie('showname')); - showname_click(0); + showname_click(null); (document.getElementById('client_callwords')).value = getCookie('callwords'); } @@ -542,7 +542,7 @@ class Client extends EventEmitter { * Requests to play as a specified character. * @param {number} character the character ID */ - sendCharacter(character: string) { + sendCharacter(character: number) { if (this.chars[character].name) { this.sendServer(`CC#${this.playerID}#${character}#web#%`); } } @@ -646,7 +646,7 @@ class Client extends EventEmitter { rtime = 0; } - setTimeout(() => onReplayGo(''), rtime); + setTimeout(() => onReplayGo(null), rtime); } } @@ -931,9 +931,9 @@ class Client extends EventEmitter { } const mute_select = document.getElementById('mute_select'); - mute_select.add(new Option(safeTags(chargs[0]), charid)); + mute_select.add(new Option(safeTags(chargs[0]), String(charid))); const pair_select = document.getElementById('pair_select'); - pair_select.add(new Option(safeTags(chargs[0]), charid)); + pair_select.add(new Option(safeTags(chargs[0]), String(charid))); // sometimes ini files lack important settings const default_options = { @@ -990,12 +990,12 @@ class Client extends EventEmitter { if (i % 2 === 0) { document.getElementById('client_loadingtext').innerHTML = `Loading Character ${args[1]}/${this.char_list_length}`; const chargs = args[i].split('&'); - const charid = args[i - 1]; + const charid = Number(args[i - 1]); setTimeout(() => this.handleCharacterInfo(chargs, charid), 500); } } // Request the next pack - this.sendServer(`AN#${(args[1] / 10) + 1}#%`); + this.sendServer(`AN#${(Number(args[1]) / 10) + 1}#%`); } /** @@ -1004,7 +1004,7 @@ class Client extends EventEmitter { * @param {Array} args packet arguments */ async handleSC(args: string[]) { - const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); // Add this so people can see characters loading on the screen. document.getElementById('client_loading').style.display = 'none'; @@ -1382,14 +1382,14 @@ class Client extends EventEmitter { let healthbox; if (args[1] === '1') { // Def hp - this.hp[0] = args[2]; + this.hp[0] = Number(args[2]); healthbox = document.getElementById('client_defense_hp'); } else { // Pro hp - this.hp[1] = args[2]; + this.hp[1] = Number(args[2]); healthbox = document.getElementById('client_prosecutor_hp'); } - healthbox.getElementsByClassName('health-bar')[0].style.width = `${percent_hp}%`; + (healthbox.getElementsByClassName('health-bar')[0]).style.width = `${percent_hp}%`; } /** @@ -1708,7 +1708,7 @@ class Client extends EventEmitter { iniedit_select.add(new Option(safeTags(me.name))); - cswap.forEach((inisw) => iniedit_select.add(new Option(safeTags(inisw)))); + cswap.forEach((inisw: string) => iniedit_select.add(new Option(safeTags(inisw)))); } } catch (err) { console.info("character doesn't have iniswaps"); @@ -1780,6 +1780,7 @@ class Viewport { startFirstTickCheck: boolean; startSecondTickCheck: boolean; startThirdTickCheck: boolean; + theme: string; constructor() { this.textnow = ''; @@ -1818,8 +1819,8 @@ class Viewport { // Allocate multiple blip audio channels to make blips less jittery const blipSelectors = document.getElementsByClassName('blipSound') this.blipChannels = [...blipSelectors]; - this.blipChannels.forEach((channel) => channel.volume = 0.5); - this.blipChannels.forEach((channel) => channel.onerror = opusCheck(channel)); + this.blipChannels.forEach((channel: HTMLAudioElement) => channel.volume = 0.5); + this.blipChannels.forEach((channel: HTMLAudioElement) => channel.onerror = opusCheck(channel)); this.currentBlipChannel = 0; this.sfxaudio = document.getElementById('client_sfxaudio'); @@ -1835,8 +1836,8 @@ class Viewport { const audioChannels = document.getElementsByClassName('audioChannel') this.music = [...audioChannels]; - this.music.forEach((channel) => channel.volume = 0.5); - this.music.forEach((channel) => channel.onerror = opusCheck(channel)); + this.music.forEach((channel: HTMLAudioElement) => channel.volume = 0.5); + this.music.forEach((channel: HTMLAudioElement) => channel.onerror = opusCheck(channel)); this.updater = null; this.testimonyUpdater = null; @@ -1858,7 +1859,7 @@ class Viewport { * @param {number} volume */ set musicVolume(volume: number) { - this.music.forEach((channel) => channel.volume = volume); + this.music.forEach((channel: HTMLAudioElement) => channel.volume = volume); } /** @@ -1891,18 +1892,18 @@ class Viewport { const view = document.getElementById('client_fullview'); - let bench; + let bench: HTMLImageElement; if ('def,pro,wit'.includes(position)) { - bench = document.getElementById(`client_${position}_bench`); + bench = document.getElementById(`client_${position}_bench`); } else { - bench = document.getElementById('client_bench_classic'); + bench = document.getElementById('client_bench_classic'); } - let court; + let court: HTMLImageElement; if ('def,pro,wit'.includes(position)) { - court = document.getElementById(`client_court_${position}`); + court = document.getElementById(`client_court_${position}`); } else { - court = document.getElementById('client_court_classic'); + court = document.getElementById('client_court_classic'); } const positions = { @@ -2072,7 +2073,7 @@ class Viewport { * TODO: the preanim logic, on the other hand, should probably be moved to tick() * @param {object} chatmsg the new chat message */ - async say(chatmsg) { + async say(chatmsg: any) { this.chatmsg = chatmsg; this.textnow = ''; @@ -2204,7 +2205,7 @@ class Viewport { // flip the paired character pairLayers.style.transform = this.chatmsg.other_flip === 1 ? 'scaleX(-1)' : 'scaleX(1)'; - this.blipChannels.forEach((channel) => channel.src = `${AO_HOST}sounds/general/sfx-blip${encodeURI(this.chatmsg.blips.toLowerCase())}.opus`); + this.blipChannels.forEach((channel: HTMLAudioElement) => channel.src = `${AO_HOST}sounds/general/sfx-blip${encodeURI(this.chatmsg.blips.toLowerCase())}.opus`); // process markup if (this.chatmsg.content.startsWith('~~')) { @@ -2233,7 +2234,7 @@ class Viewport { this.tick(); } - handleTextTick(charLayers) { + handleTextTick(charLayers: HTMLImageElement) { const chatBox = document.getElementById('client_chat'); const waitingBox = document.getElementById('client_chatwaiting'); const chatBoxInner = document.getElementById('client_inner_chat'); @@ -2303,12 +2304,12 @@ class Viewport { const shoutSprite = document.getElementById('client_shout'); const effectlayer = document.getElementById('client_fg'); const chatBoxInner = document.getElementById('client_inner_chat'); - let charLayers = document.getElementById('client_char'); - let pairLayers = document.getElementById('client_pair_char'); + let charLayers = document.getElementById('client_char'); + let pairLayers = document.getElementById('client_pair_char'); if ('def,pro,wit'.includes(this.chatmsg.side)) { - charLayers = document.getElementById(`client_${this.chatmsg.side}_char`); - pairLayers = document.getElementById(`client_${this.chatmsg.side}_pair_char`); + charLayers = document.getElementById(`client_${this.chatmsg.side}_char`); + pairLayers = document.getElementById(`client_${this.chatmsg.side}_pair_char`); } const charName = this.chatmsg.name.toLowerCase(); @@ -2429,7 +2430,7 @@ class Viewport { * Triggered when the Return key is pressed on the out-of-character chat input box. * @param {KeyboardEvent} event */ -export function onOOCEnter(event) { +export function onOOCEnter(event: KeyboardEvent) { if (event.keyCode === 13) { client.sendOOC((document.getElementById('client_oocinputbox')).value); (document.getElementById('client_oocinputbox')).value = ''; @@ -2441,7 +2442,7 @@ window.onOOCEnter = onOOCEnter; * Triggered when the user click replay GOOOOO * @param {KeyboardEvent} event */ -export function onReplayGo(_event) { +export function onReplayGo(_event: Event) { client.handleReplay(); } window.onReplayGo = onReplayGo; @@ -2450,24 +2451,24 @@ window.onReplayGo = onReplayGo; * Triggered when the Return key is pressed on the in-character chat input box. * @param {KeyboardEvent} event */ -export function onEnter(event) { +export function onEnter(event: KeyboardEvent) { if (event.keyCode === 13) { const mychar = client.character; const myemo = client.emote; const evi = client.evidence; - const flip = ((document.getElementById('button_flip').classList.contains('dark')) ? 1 : 0); - const flash = ((document.getElementById('button_flash').classList.contains('dark')) ? 1 : 0); - const screenshake = ((document.getElementById('button_shake').classList.contains('dark')) ? 1 : 0); - const noninterrupting_preanim = (((document.getElementById('check_nonint')).checked) ? 1 : 0); - const looping_sfx = (((document.getElementById('check_loopsfx')).checked) ? 1 : 0); - const color = (document.getElementById('textcolor')).value; + const flip = Boolean((document.getElementById('button_flip').classList.contains('dark'))); + const flash = Boolean((document.getElementById('button_flash').classList.contains('dark'))); + const screenshake = Boolean((document.getElementById('button_shake').classList.contains('dark'))); + const noninterrupting_preanim = Boolean(((document.getElementById('check_nonint')).checked)); + const looping_sfx = Boolean(((document.getElementById('check_loopsfx')).checked)); + const color = Number((document.getElementById('textcolor')).value); const showname = (document.getElementById('ic_chat_name')).value; const text = (document.getElementById('client_inputbox')).value; const pairchar = (document.getElementById('pair_select')).value; const pairoffset = (document.getElementById('pair_offset')).value; const pairyoffset = (document.getElementById('pair_y_offset')).value; const myrole = (document.getElementById('role_select')).value ? (document.getElementById('role_select')).value : mychar.side; - const additive = (((document.getElementById('check_additive')).checked) ? 1 : 0); + const additive = Boolean(((document.getElementById('check_additive')).checked)); const effect = (document.getElementById('effect_select')).value; let sfxname = '0'; @@ -2534,7 +2535,7 @@ function resetICParams() { } } -export function resetOffset(_event) { +export function resetOffset(_event: Event) { (document.getElementById('pair_offset')).value = '0'; (document.getElementById('pair_y_offset')).value = '0'; } @@ -2544,7 +2545,7 @@ window.resetOffset = resetOffset; * Triggered when the music search bar is changed * @param {MouseEvent} event */ -export function musiclist_filter(_event) { +export function musiclist_filter(_event: Event) { const musiclist_element = document.getElementById('client_musiclist'); const searchname = (document.getElementById('client_musicsearch')).value; @@ -2564,7 +2565,7 @@ window.musiclist_filter = musiclist_filter; * Triggered when an item on the music list is clicked. * @param {MouseEvent} event */ -export function musiclist_click(_event) { +export function musiclist_click(_event: Event) { const playtrack = (document.getElementById('client_musiclist')).value; client.sendMusicChange(playtrack); @@ -2581,7 +2582,7 @@ window.musiclist_click = musiclist_click; * Triggered when a character in the mute list is clicked * @param {MouseEvent} event */ -export function mutelist_click(_event) { +export function mutelist_click(_event: Event) { const mutelist = document.getElementById('mute_select'); const selected_character = mutelist.options[mutelist.selectedIndex]; @@ -2600,7 +2601,7 @@ window.mutelist_click = mutelist_click; * Triggered when the showname checkboc is clicked * @param {MouseEvent} event */ -export function showname_click(_event) { +export function showname_click(_event: Event) { setCookie('showname', String((document.getElementById('showname')).checked)); setCookie('ic_chat_name', (document.getElementById('ic_chat_name')).value); @@ -2612,9 +2613,9 @@ window.showname_click = showname_click; /** * Triggered when an item on the area list is clicked. - * @param {MouseEvent} event + * @param {HTMLElement} el */ -export function area_click(el) { +export function area_click(el: HTMLElement) { const area = client.areas[el.id.substr(4)].name; client.sendMusicChange(area); @@ -2629,8 +2630,8 @@ window.area_click = area_click; * Triggered by the music volume slider. */ export function changeMusicVolume() { - viewport.musicVolume = (document.getElementById('client_mvolume')).value; - setCookie('musicVolume', viewport.musicVolume); + viewport.musicVolume = Number((document.getElementById('client_mvolume')).value); + setCookie('musicVolume', String(viewport.musicVolume)); } window.changeMusicVolume = changeMusicVolume; @@ -2639,7 +2640,7 @@ window.changeMusicVolume = changeMusicVolume; */ export function changeBlipVolume() { const blipVolume = (document.getElementById('client_bvolume')).value; - viewport.blipChannels.forEach((channel) => channel.volume = blipVolume); + viewport.blipChannels.forEach((channel: HTMLAudioElement) => channel.volume = Number(blipVolume)); setCookie('blipVolume', blipVolume); } window.changeBlipVolume = changeBlipVolume; @@ -2685,7 +2686,7 @@ window.iniedit = iniedit; /** * Triggered by the pantilt checkbox */ -export async function switchPanTilt(addcheck) { +export async function switchPanTilt(addcheck: number) { const background = document.getElementById('client_fullview'); if (addcheck === 1) { (document.getElementById('client_pantilt')).checked = true; @@ -2737,7 +2738,7 @@ window.switchChatOffset = switchChatOffset; * Triggered when a character icon is clicked in the character selection menu. * @param {MouseEvent} event */ -export function changeCharacter(_event) { +export function changeCharacter(_event: Event) { document.getElementById('client_charselect').style.display = 'block'; document.getElementById('client_emo').innerHTML = ''; } @@ -2747,7 +2748,7 @@ window.changeCharacter = changeCharacter; * Triggered when there was an error loading a character sprite. * @param {HTMLImageElement} image the element containing the missing image */ -export function charError(image) { +export function charError(image: HTMLImageElement) { console.warn(`${image.src} is missing from webAO`); image.src = transparentPng; return true; @@ -2758,8 +2759,8 @@ window.charError = charError; * Triggered when there was an error loading a generic sprite. * @param {HTMLImageElement} image the element containing the missing image */ -export function imgError(image) { - image.onerror = ''; +export function imgError(image: HTMLImageElement) { + image.onerror = null; image.src = ''; // unload so the old sprite doesn't persist return true; } @@ -2769,7 +2770,7 @@ window.imgError = imgError; * Triggered when there was an error loading a sound * @param {HTMLAudioElement} image the element containing the missing sound */ -export function opusCheck(channel) { +export function opusCheck(channel: HTMLAudioElement) { const audio = channel.src if (audio === '') { return @@ -2805,7 +2806,7 @@ window.ReconnectButton = ReconnectButton; * @param {string} msg the string to be added * @param {string} name the name of the sender */ -function appendICLog(msg, showname = '', nameplate = '', time = new Date()) { +function appendICLog(msg: string, showname = '', nameplate = '', time = new Date()) { const entry = document.createElement('p'); const shownameField = document.createElement('span'); const nameplateField = document.createElement('span'); @@ -2849,10 +2850,10 @@ function appendICLog(msg, showname = '', nameplate = '', time = new Date()) { * check if the message contains an entry on our callword list * @param {string} message */ -export function checkCallword(message) { +export function checkCallword(message: string) { client.callwords.forEach(testCallword); - function testCallword(item) { + function testCallword(item: string) { if (item !== '' && message.toLowerCase().includes(item.toLowerCase())) { viewport.sfxaudio.pause(); viewport.sfxaudio.src = `${AO_HOST}sounds/general/sfx-gallery.opus`; @@ -2865,10 +2866,10 @@ export function checkCallword(message) { * Triggered when the music search bar is changed * @param {MouseEvent} event */ -export function chartable_filter(_event) { +export function chartable_filter(_event: Event) { const searchname = (document.getElementById('client_charactersearch')).value; - client.chars.forEach((character, charid) => { + client.chars.forEach((character: any, charid: number) => { const demothing = document.getElementById(`demo_${charid}`); if (character.name.toLowerCase().indexOf(searchname.toLowerCase()) === -1) { demothing.style.display = 'none'; @@ -2884,7 +2885,7 @@ window.chartable_filter = chartable_filter; * @param {number} ccharacter the character ID; if this is a large number, * then spectator is chosen instead. */ -export function pickChar(ccharacter) { +export function pickChar(ccharacter: number) { if (ccharacter === -1) { // Spectator document.getElementById('client_charselect').style.display = 'none'; @@ -2898,7 +2899,7 @@ window.pickChar = pickChar; * Highlights and selects an emotion for in-character chat. * @param {string} emo the new emotion to be selected */ -export function pickEmotion(emo) { +export function pickEmotion(emo: number) { try { if (client.selectedEmote !== -1) { document.getElementById(`emo_${client.selectedEmote}`).className = 'emote_button'; @@ -2972,7 +2973,7 @@ window.addEvidence = addEvidence; */ export function editEvidence() { const evidence_select = document.getElementById('evi_select'); - const id = parseInt(client.selectedEvidence) - 1; + const id = client.selectedEvidence - 1; client.sendEE( id, (document.getElementById('evi_name')).value, @@ -2989,7 +2990,7 @@ window.editEvidence = editEvidence; * Delete selected evidence. */ export function deleteEvidence() { - const id = parseInt(client.selectedEvidence) - 1; + const id = client.selectedEvidence - 1; client.sendDE(id); cancelEvidence(); } @@ -3201,7 +3202,7 @@ window.guilty = guilty; * Increment defense health point. */ export function addHPD() { - client.sendHP(1, String(parseInt(client.hp[0]) + 1)); + client.sendHP(1, (client.hp[0] + 1)); } window.addHPD = addHPD; @@ -3209,7 +3210,7 @@ window.addHPD = addHPD; * Decrement defense health point. */ export function redHPD() { - client.sendHP(1, String(parseInt(client.hp[0]) - 1)); + client.sendHP(1, (client.hp[0] - 1)); } window.redHPD = redHPD; @@ -3217,7 +3218,7 @@ window.redHPD = redHPD; * Increment prosecution health point. */ export function addHPP() { - client.sendHP(2, String(parseInt(client.hp[1]) + 1)); + client.sendHP(2, (client.hp[1] + 1)); } window.addHPP = addHPP; @@ -3225,7 +3226,7 @@ window.addHPP = addHPP; * Decrement prosecution health point. */ export function redHPP() { - client.sendHP(2, String(parseInt(client.hp[1]) - 1)); + client.sendHP(2, (client.hp[1] - 1)); } window.redHPP = redHPP; -- cgit