From e998052cb2d8aea08b5af77c4611d08d9623c169 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 19 Feb 2021 14:13:25 -0500 Subject: First pass at lethal/non-lethal marking. --- index.html | 5 +++++ lethality-selector.mjs | 34 ++++++++++++++++++++++++++++ main.mjs | 3 ++- rules.mjs | 60 +++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 lethality-selector.mjs diff --git a/index.html b/index.html index 0661091..aa3a25b 100644 --- a/index.html +++ b/index.html @@ -130,6 +130,11 @@ + + diff --git a/lethality-selector.mjs b/lethality-selector.mjs new file mode 100644 index 0000000..9f5813d --- /dev/null +++ b/lethality-selector.mjs @@ -0,0 +1,34 @@ +class LethalitySelector { + constructor(elt) { + this.elt = elt + + for (const elt of this.elt.querySelectorAll('button')) { + elt.addEventListener('click', this.select.bind(this)) + } + } + + attach() { + this.elt.classList.remove('hidden') + } + + detach() { + this.elt.classList.add('hidden') + } + + get onItemSelected() { + if (this._onItemSelected !== undefined) { + return this._onItemSelected + } + return () => {} + } + + set onItemSelected(fn) { + this._onItemSelected = fn + } + + select(evt) { + this.onItemSelected(evt.originalTarget.id == 'lethal') + } +} + +export default LethalitySelector diff --git a/main.mjs b/main.mjs index 1af1c56..613eded 100644 --- a/main.mjs +++ b/main.mjs @@ -5,13 +5,14 @@ function init() { const die = document.querySelector('#die') const aminoAcidSelector = document.querySelector('#amino-acid-selector') const nucleotideSelector = document.querySelector('#nucleotide-selector') + const lethalitySelector = document.querySelector('#lethality-selector') const instructions = document.querySelector('#instructions') const cloneButton = document.querySelector('#clone') const remainingIterations = document.querySelector('#remaining-iterations') const printButton = document.querySelector('#print') const errors = document.querySelector('#errors') - const rules = new Rules(die, instructions, genomeList, aminoAcidSelector, nucleotideSelector, cloneButton, remainingIterations, printButton, errors) + const rules = new Rules(die, instructions, genomeList, aminoAcidSelector, nucleotideSelector, lethalitySelector, cloneButton, remainingIterations, printButton, errors) } init() diff --git a/rules.mjs b/rules.mjs index f3b0c8d..83c07bc 100644 --- a/rules.mjs +++ b/rules.mjs @@ -1,12 +1,13 @@ import { randomItem, ordinalSuffix } from './utils.mjs' import AminoAcid from './amino-acid.mjs' import AminoAcidSelector from './amino-acid-selector.mjs' -import Genome from './genome.mjs' -import Nucleotide from './nucleotide.mjs' import Die from './die.mjs' +import Error from './error.mjs' +import Genome from './genome.mjs' import GenomeList from './genome-list.mjs' +import LethalitySelector from './lethality-selector.mjs' +import Nucleotide from './nucleotide.mjs' import NucleotideSelector from './nucleotide-selector.mjs' -import Error from './error.mjs' class CloneNucleotide { constructor(rules) { @@ -229,29 +230,48 @@ class SelectAminoAcid { } const newAminoAcid = new AminoAcid(...this.codon.value.split('')) - window.aa = newAminoAcid - console.debug('new:', newAminoAcid) + const isLethal = this.codon.aminoAcid.value !== newAminoAcid.value this.codon.aminoAcid = newAminoAcid - this.rules.next(new MarkAsLethal(this.rules)) + this.rules.next(new MarkAsLethal(this.rules, isLethal)) } } class MarkAsLethal { - constructor(rules) { + constructor(rules, isLethal) { this.rules = rules + this.isLethal = isLethal this.id = 'mark-as-lethal' } enter() { - // Enable lethal/non-lethal selector. - - // Attach validator to selector and change state if possible. + this.rules.lethalitySelector.onItemSelected = this.handleItemSelected.bind(this) + this.rules.lethalitySelector.attach() } exit() { - // Disable lethal/non-lethal selector. + this.rules.lethalitySelector.detach() + } + get lethalHTML() { + return 'A change in amino acid is a lethal change.' + } + + get nonLethalHTML() { + return 'A change in amino acid is a lethal change.' + } + + handleItemSelected(isLethal) { + if (isLethal !== this.isLethal) { + if (this.isLethal) { + this.rules.error.innerHTML = this.lethalHTML + } else { + this.rules.error.innerHTML = this.nonLethalHTML + } + this.rules.next(new ShowError(this.rules, this)) + return + } + this.rules.iterations-- if (this.rules.isLastIteration) { this.rules.next(new DoNothing(this.rules)) @@ -308,12 +328,13 @@ class ShowError { } class Rules { - constructor(die, instructions, genomeList, aminoAcidSelector, nucleotideSelector, cloneButton, remainingIterations, printButton, errors) { + constructor(die, instructions, genomeList, aminoAcidSelector, nucleotideSelector, lethalitySelector, cloneButton, remainingIterations, printButton, errors) { this.die = new Die(die) this.instructions = instructions this.genomeList = new GenomeList(genomeList) this.aminoAcidSelector = new AminoAcidSelector(aminoAcidSelector) this.nucleotideSelector = new NucleotideSelector(nucleotideSelector) + this.lethalitySelector = new LethalitySelector(lethalitySelector) this.cloneButton = cloneButton this.remainingIterations = remainingIterations this.printButton = printButton @@ -327,8 +348,10 @@ class Rules { this._debugStartAtRollForMutation() } else if (false) { this._debugStartAtPerformMutation(3) - } else if (true) { + } else if (false) { this._debugStartAtSelectAminoAcid() + } else if (true) { + this._debugStartAtSelectLethality() } else if (false) { this._debugStartWithError() } else { @@ -387,6 +410,17 @@ class Rules { this.currentGenome.selectedNucleotide = nucleotide } + _debugStartAtSelectLethality() { + this.genomeList.push(this.currentGenome.clone()) + + this.currentState = new MarkAsLethal(this, true) + this.die.value = 15 + const nucleotide = this.currentGenome.nucleotides[15] + nucleotide.value = 'A' + this.currentGenome.selectedNucleotide = nucleotide + this.currentGenome.codons[5].aminoAcid = new AminoAcid('A', 'C', 'T') + } + _debugStartWithError() { this.currentState = new ShowError(this, new CloneNucleotide(this)) this.error.innerHTML = 'test an error' -- cgit v1.2.3