From f75d85fdaf557bf54bbca26a1d02c359b314d567 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Wed, 14 Nov 2018 21:42:19 -0600 Subject: [PATCH] Reimplement "ignore nicknames" without CSS Honestly, the CSS solution was really cool. It was just incompatible with the new system. It also had enough other flaws (like, bad accessibility) that I don't feel _too_ bad about ripping it out. The new version is less code, anyway, although it does have the flaw of needing to replay the battle if you turn nicknames on or off. Fortunately, that should be a rare enough occurrence not to be a big deal. --- js/client-battle.js | 35 ++--------------------------------- src/battle-animations.ts | 17 +++++++++++------ src/battle-log.ts | 3 --- src/battle-text-parser.ts | 33 ++++++++++++--------------------- src/battle.ts | 8 +++++--- style/battle.css | 9 --------- 6 files changed, 30 insertions(+), 75 deletions(-) diff --git a/js/client-battle.js b/js/client-battle.js index 5549e52a1..2de868c64 100644 --- a/js/client-battle.js +++ b/js/client-battle.js @@ -1378,43 +1378,12 @@ this.battle.ignoreNicks = !!e.currentTarget.checked; Tools.prefs('ignorenicks', this.battle.ignoreNicks); this.battle.add('Nicknames ' + (this.battle.ignoreNicks ? '' : 'no longer ') + 'ignored.'); - this.toggleNicknames(this.battle.mySide); - this.toggleNicknames(this.battle.yourSide); - - var $log = $('.battle-log .inner'); - var $message = $('.battle .message'); - if (this.battle.ignoreNicks) { - $log.addClass('hidenicks'); - $message.addClass('hidenicks'); - } else { - $log.removeClass('hidenicks'); - $message.removeClass('hidenicks'); - } + this.battle.resetToCurrentTurn(); }, toggleIgnoreOpponent: function (e) { this.battle.ignoreOpponent = !!e.currentTarget.checked; this.battle.add('Opponent ' + (this.battle.ignoreOpponent ? '' : 'no longer ') + 'ignored.'); - var $log = $('.battle-log .inner'); - var $message = $('.battle .message'); - var $messages = $log.find('.chatmessage-' + this.battle.yourSide.id); - if (!$messages.length) return; - if (this.battle.ignoreOpponent) { - $messages.hide(); - $log.addClass('hidenicks'); - $message.addClass('hidenicks'); - } else { - $messages.show(); - $log.removeClass('hidenicks'); - $message.removeClass('hidenicks'); - } - }, - toggleNicknames: function (side) { - for (var i = 0; i < side.active.length; i++) { - if (!side.active[i]) continue; - side.active[i].statbarElem.html(side.getStatbarHTML(side.active[i], true)); - side.updateStatbar(side.active[i], true, true); - } - side.updateSidebar(); + this.battle.resetToCurrentTurn(); }, toggleRightPanelBattles: function (e) { Tools.prefs('rightpanelbattles', !!e.currentTarget.checked); diff --git a/src/battle-animations.ts b/src/battle-animations.ts index 7320f1fe2..f0c82b890 100644 --- a/src/battle-animations.ts +++ b/src/battle-animations.ts @@ -163,6 +163,17 @@ class BattleScene { $frame.addClass('battle'); this.$frame = $frame; this.log = new BattleLog($logFrame[0] as HTMLDivElement, this); + this.log.battleParser!.pokemonName = (pokemonId: string) => { + if (!pokemonId) return ''; + if (battle.ignoreNicks || battle.ignoreOpponent) { + const pokemon = battle.getPokemon(pokemonId); + if (pokemon) return pokemon.species; + } + if (!pokemonId.startsWith('p1') && !pokemonId.startsWith('p2')) return '???pokemon:' + pokemonId + '???'; + if (pokemonId.charAt(3) === ':') return pokemonId.slice(4).trim(); + else if (pokemonId.charAt(2) === ':') return pokemonId.slice(3).trim(); + return '???pokemon:' + pokemonId + '???'; + }; let numericId = 0; if (battle.id) { @@ -234,12 +245,6 @@ class BattleScene { this.$battle.append(this.$delay); this.$battle.append(this.$hiddenMessage); - if (this.battle.ignoreNicks) { - this.log.setHideNicks(true); - this.$messagebar.addClass('hidenicks'); - this.$hiddenMessage.addClass('hidenicks'); - } - if (!this.animating) { this.$battle.append('
seeking...
'); } diff --git a/src/battle-log.ts b/src/battle-log.ts index 004cefeec..632667329 100644 --- a/src/battle-log.ts +++ b/src/battle-log.ts @@ -60,9 +60,6 @@ class BattleLog { destroy() { this.elem.onscroll = null; } - setHideNicks(hideNicks: boolean) { - this.elem.className = this.className + (hideNicks ? ' hidenicks' : ''); - } add(args: Args, kwArgs?: KWArgs, preempt?: boolean) { if (kwArgs && kwArgs.silent) return; let divClass = 'chat'; diff --git a/src/battle-text-parser.ts b/src/battle-text-parser.ts index 391171ddf..114e09ead 100644 --- a/src/battle-text-parser.ts +++ b/src/battle-text-parser.ts @@ -43,38 +43,29 @@ class BattleTextParser { return input.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'); } + pokemonName = (pokemon: string) => { + if (!pokemon) return ''; + if (!pokemon.startsWith('p1') && !pokemon.startsWith('p2')) return '???pokemon:' + pokemon + '???'; + if (pokemon.charAt(3) === ':') return pokemon.slice(4).trim(); + else if (pokemon.charAt(2) === ':') return pokemon.slice(3).trim(); + return '???pokemon:' + pokemon + '???'; + }; + pokemon(pokemon: string) { if (!pokemon) return ''; let side; if (pokemon.startsWith('p1')) side = 0; else if (pokemon.startsWith('p2')) side = 1; else return '???pokemon:' + pokemon + '???'; - if (pokemon.charAt(3) === ':') pokemon = pokemon.slice(4).trim(); - else if (pokemon.charAt(2) === ':') pokemon = pokemon.slice(3).trim(); - else return '???pokemon:' + pokemon + '???'; + const name = this.pokemonName(pokemon); let template = BattleText.default[side === this.perspective ? 'pokemon' : 'opposingPokemon']; - return template.replace('[NICKNAME]', pokemon); - } - - pokemonName(pokemon: string) { - if (!pokemon) return ''; - if (!pokemon.startsWith('p1') && !pokemon.startsWith('p2')) return '???pokemon:' + pokemon + '???'; - if (pokemon.charAt(3) === ':') pokemon = pokemon.slice(4).trim(); - else if (pokemon.charAt(2) === ':') pokemon = pokemon.slice(3).trim(); - else return '???pokemon:' + pokemon + '???'; - return pokemon; + return template.replace('[NICKNAME]', name); } pokemonFull(pokemon: string, details: string) { - if (pokemon.startsWith('p1')) {} - else if (pokemon.startsWith('p2')) {} - else return '???pokemon:' + pokemon + '???'; - let nickname; - if (pokemon.charAt(3) === ':') nickname = pokemon.slice(4).trim(); - else if (pokemon.charAt(2) === ':') nickname = pokemon.slice(3).trim(); - else return '???pokemon:' + pokemon + '???'; + const nickname = this.pokemonName(pokemon); - let species = details.split(',')[0]; + const species = details.split(',')[0]; if (nickname === species) return [pokemon.slice(0, 2), `**${species}**`]; return [pokemon.slice(0, 2), `${nickname} (**${species}**)`]; } diff --git a/src/battle.ts b/src/battle.ts index 4c4e245aa..f612f93c9 100644 --- a/src/battle.ts +++ b/src/battle.ts @@ -1033,15 +1033,13 @@ class Battle { this.scene.log.add(args, kwArgs, preempt); } - switchSides(replay?: boolean) { + resetToCurrentTurn() { if (this.ended) { - this.setSidesSwitched(!this.sidesSwitched); this.reset(true); this.fastForwardTo(-1); } else { let turn = this.turn; let paused = this.paused; - this.setSidesSwitched(!this.sidesSwitched); this.reset(true); this.paused = paused; if (turn) this.fastForwardTo(turn); @@ -1052,6 +1050,10 @@ class Battle { } } } + switchSides() { + this.setSidesSwitched(!this.sidesSwitched); + this.resetToCurrentTurn(); + } setSidesSwitched(sidesSwitched: boolean) { this.sidesSwitched = sidesSwitched; if (this.sidesSwitched) { diff --git a/style/battle.css b/style/battle.css index e5f63b267..ba03df766 100644 --- a/style/battle.css +++ b/style/battle.css @@ -952,12 +952,3 @@ button.subtle { button.subtle:hover { text-decoration: underline; } -.hidenicks span.battle-nickname, -.hidenicks span.battle-nickname-foe { - font-size: 0; -} -.hidenicks span.battle-nickname::before, -.hidenicks span.battle-nickname-foe::before{ - content: attr(title); - font-size: 9pt; -}