From dcbd196fd3c54b85a42b80e5e5e8d1771bbf224f Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 19 Feb 2021 15:48:22 -0500 Subject: Get kill/clone working. --- index.html | 4 ++-- rules.mjs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 2d96b9d..66a62fa 100644 --- a/index.html +++ b/index.html @@ -73,10 +73,10 @@ Depending on the change to the codon: diff --git a/rules.mjs b/rules.mjs index 3cc2524..a87ba95 100644 --- a/rules.mjs +++ b/rules.mjs @@ -37,10 +37,10 @@ class NucleotideSelect { this.id = 'nucleotide-select' this._boundCloneHandler = this.handleClone.bind(this) + this.cloneButtons = document.querySelectorAll(`#${this.id} .clone`) } enter() { - this.cloneButtons = document.querySelectorAll(`#${this.id} .clone`) this.cloneButtons.forEach(button => { button.addEventListener('click', this._boundCloneHandler) button.disabled = false @@ -210,7 +210,6 @@ class SelectAminoAcid { } validSelection(aminoAcid) { - console.debug('aminoAcid:', aminoAcid, 'expected: ', this.expected) return aminoAcid == this.expected } @@ -235,12 +234,32 @@ class MarkAsLethal { this.isLethal = isLethal this.id = 'mark-as-lethal' + this._boundCloneHandler = this.handleClone.bind(this) + this._boundKillHandler = this.handleKill.bind(this) + this.cloneButtons = document.querySelectorAll(`#${this.id} .clone`) + this.killButtons = document.querySelectorAll(`#${this.id} .kill`) } enter() { + this.cloneButtons.forEach(button => { + button.addEventListener('click', this._boundCloneHandler) + button.disabled = false + }) + this.killButtons.forEach(button => { + button.addEventListener('click', this._boundKillHandler) + button.disabled = false + }) } exit() { + this.cloneButtons.forEach(button => { + button.removeEventListener('click', this._boundCloneHandler) + button.disabled = true + }) + this.killButtons.forEach(button => { + button.removeEventListener('click', this._boundKillHandler) + button.disabled = true + }) } get lethalHTML() { @@ -248,11 +267,39 @@ class MarkAsLethal { } get nonLethalHTML() { - return 'A change in amino acid is a lethal change.' + return 'If the amino acid doesn\'t change it is non-lethal.' + } + + handleClone(evt) { + if (this.isLethal) { + this.rules.error.innerHTML = this.lethalHTML + this.rules.next(new ShowError(this.rules, this)) + return + } + this.nextGoodState(this.rules.currentGenome) + } + + handleKill(evt) { + if (!this.isLethal) { + this.rules.error.innerHTML = this.nonLethalHTML + this.rules.next(new ShowError(this.rules, this)) + return + } + this.nextGoodState(this.rules.previousGenome) + } + + nextGoodState(genome) { + this.rules.iterations-- + if (this.rules.iterations === 0) { + this.rules.next(new PrintResults(this.rules)) + } else { + this.rules.genomeList.push(genome.clone()) + this.rules.next(new RollForNucleotide(this.rules)) + } } } -class DoNothing { +class PrintResults { constructor(rules) { this.rules = rules @@ -319,7 +366,7 @@ class Rules { this._debugStartAtPerformMutation(3) } else if (false) { this._debugStartAtSelectAminoAcid() - } else if (false) { + } else if (true) { this._debugStartAtSelectLethality() } else if (false) { this._debugStartWithError() @@ -388,6 +435,10 @@ class Rules { return this.genomeList.last } + get previousGenome() { + return this.genomeList.penultimate + } + showCurrent() { if (this.currentState.id === undefined) { return -- cgit v1.2.3