ExperimentBaseAreSameImages.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div>
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import ExperimentBase from '@/mixins/ExperimentBase'
  8. import { mapGetters } from 'vuex'
  9. import { rand } from '@/functions'
  10. import { EXPERIMENT as experimentMsgId } from '@/../config.messagesId'
  11. export default {
  12. name: 'ExperimentBaseAreSameImages',
  13. mixins: [ExperimentBase],
  14. data() {
  15. return {
  16. maxTestCount: null,
  17. testCount: 1,
  18. image1: null,
  19. image2: null
  20. }
  21. },
  22. computed: {
  23. ...mapGetters(['getHostURI'])
  24. },
  25. methods: {
  26. scrollToChoiceButtons() {
  27. const ele = document.querySelector('#choice')
  28. if (ele) ele.scrollIntoView({ behavior: 'smooth' })
  29. },
  30. // Get images links for a test
  31. async getTest(leftQuality, rightQuality) {
  32. const [image1, image2] = await Promise.all([this.getImage(leftQuality), this.getImage(rightQuality)])
  33. return { image1, image2 }
  34. },
  35. // Get a test with random qualities
  36. getRandomTest() {
  37. return this.getTest(
  38. this.qualities[rand(0, this.qualities.length - 1)],
  39. this.qualities[rand(0, this.qualities.length - 1)]
  40. )
  41. },
  42. // Get a test with random qualities
  43. getReferenceTest() {
  44. // Randomly choose which is the reference image (0 = left, 1 = right)
  45. const isReferenceLeft = rand(0, 1) === 0
  46. // Randomly choose a quality for the other image
  47. const randomQuality = this.qualities[rand(0, this.qualities.length - 1)]
  48. const res = [this.qualities[this.qualities.length - 1], randomQuality]
  49. this.referenceImagePosition = isReferenceLeft ? 'left' : 'right'
  50. const table = isReferenceLeft ? res : res.reverse()
  51. return this.getTest(table[0], table[1])
  52. },
  53. /** An action was triggered, load a new test and save progression
  54. * @param {Boolean} areTheSame Are the images the same
  55. * @param {Function} getTestFn Function to be called to get the next tests
  56. * @param {Function} additionalData Object to concat to log
  57. * @returns {void}
  58. */
  59. async areTheSameAction(areTheSame, getTestFn, additionalData) {
  60. this.loadingMessage = 'Loading new test...'
  61. this.loadingErrorMessage = null
  62. try {
  63. this.testCount++
  64. const obj = Object.assign({
  65. image1: this.image1,
  66. image2: this.image2,
  67. areTheSame,
  68. experimentName: this.experimentName,
  69. sceneName: this.sceneName,
  70. referenceImagePosition: this.referenceImagePosition || undefined
  71. }, additionalData || {})
  72. this.sendMessage({ msgId: experimentMsgId.DATA, msg: obj })
  73. const { image1, image2 } = await getTestFn()
  74. this.image1 = image1
  75. this.image2 = image2
  76. // Experiment end
  77. if (this.testCount > this.maxTestCount) return this.finishExperiment()
  78. }
  79. catch (err) {
  80. console.error('Failed to load new test', err)
  81. this.loadingErrorMessage = 'Failed to load new test. ' + err.message
  82. }
  83. finally {
  84. this.loadingMessage = null
  85. this.saveProgress()
  86. }
  87. },
  88. // Finish an experiment, sending full data to the server
  89. // Don't forget to surcharge this function when using this mixin to add more data
  90. finishExperiment() {
  91. const obj = {
  92. experimentName: this.experimentName,
  93. sceneName: this.sceneName
  94. }
  95. this.sendMessage({ msgId: experimentMsgId.VALIDATED, msg: obj })
  96. this.setExperimentFinished()
  97. this.$router.push(`/experiments/${this.experimentName}/${this.sceneName}/validated`)
  98. }
  99. }
  100. }
  101. </script>