controllers/game/h_i_p_s_controller.js

  1. import { Controller } from "@hotwired/stimulus"
  2. import { AES } from "crypto-es/lib/aes"
  3. import { Utf8 } from 'crypto-es/lib/core.js'
  4. /**
  5. * @class Game.HIPSController
  6. * @classdesc Stimulus controller for /game/hiding-in-plain-sight.
  7. * @extends Controller
  8. */
  9. export default class HIPSController extends Controller {
  10. /**
  11. * @property {Function} answer - targets the text field input
  12. * @property {Function} error - targets the error message
  13. * @property {Function} unlock - targets the alert
  14. * @memberof Game.HIPSController
  15. * @static
  16. */
  17. static targets = [ "answer", "error", "unlock" ]
  18. /**
  19. * If the answer matches `128aes`, the string in the alert is decrypted to reveal the path to the
  20. * next puzzle. Otherwise an error message is displayed.
  21. *
  22. * @instance
  23. * @memberof Game.HIPSController
  24. * @returns {void} N/A
  25. * */
  26. submit() {
  27. if (this.answerTarget.value === "128aes") {
  28. this.errorTarget.style.display = "none"
  29. const bytes = AES.decrypt(this.unlockTarget.innerHTML, "128aes")
  30. this.unlockTarget.innerHTML = bytes.toString(Utf8)
  31. } else {
  32. this.errorTarget.style.display = "block"
  33. this.errorTarget.innerHTML = `${this.answerTarget.value} is incorrect`
  34. }
  35. }
  36. }