ExperimentBaseExtracts.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <script>
  2. import ExperimentBase from '@/mixins/ExperimentBase.vue'
  3. import { mapGetters } from 'vuex'
  4. import { API_ROUTES, findNearestUpper, findNearestLower } from '@/functions'
  5. export default {
  6. mixins: [ExperimentBase],
  7. data() {
  8. return {
  9. // Updated when `setConfig` is called
  10. extractConfig: {
  11. x: 4,
  12. y: 4
  13. },
  14. extracts: []
  15. }
  16. },
  17. computed: {
  18. ...mapGetters(['getHostURI'])
  19. },
  20. methods: {
  21. // Load extracts from the API
  22. async getExtracts(quality = 'min') {
  23. const URI = `${this.getHostURI}${API_ROUTES.getImageExtracts(this.sceneName, quality, this.extractConfig.x, this.extractConfig.y)}`
  24. const { data } = await fetch(URI)
  25. .then(async res => {
  26. res.json = await res.json()
  27. return res
  28. })
  29. .then(res => {
  30. if (!res.ok) throw new Error(res.json.message + res.json.data ? `\n${res.json.data}` : '')
  31. return res.json
  32. })
  33. return data
  34. },
  35. // Config was updated, load extracts and save progression
  36. async setConfig(config) {
  37. if (!config) return
  38. this.loadingMessage = 'Loading configuration extracts...'
  39. this.loadingErrorMessage = null
  40. try {
  41. this.extractConfig.x = config.x
  42. this.extractConfig.y = config.y
  43. const data = await this.getExtracts()
  44. const hostURI = this.getHostURI
  45. this.extracts = data.extracts.map((url, i) => ({
  46. link: hostURI + url,
  47. quality: data.info.image.quality,
  48. zone: i + 1,
  49. index: i,
  50. nextQuality: findNearestUpper(data.info.image.quality, this.qualities),
  51. precQuality: findNearestLower(data.info.image.quality, this.qualities),
  52. loading: false
  53. }))
  54. }
  55. catch (err) {
  56. console.error('Failed to load new configuration', err)
  57. this.loadingErrorMessage = 'Failed to load new configuration. ' + err.message
  58. }
  59. finally {
  60. this.loadingMessage = null
  61. this.saveProgress()
  62. }
  63. },
  64. // An action was triggered, load extracts and save progression
  65. async extractAction(event, extractObj) {
  66. console.log(event, extractObj)
  67. const { index, nextQuality, precQuality, quality } = extractObj
  68. let newQuality
  69. if (event.button === 0) newQuality = precQuality // Left click
  70. if (event.button === 2) newQuality = nextQuality // Right click
  71. // Do not load a new extract if same quality
  72. if (newQuality === quality) return
  73. // Set loading state
  74. this.extracts[index].loading = true
  75. try {
  76. // Loading new extract
  77. const data = await this.getExtracts(newQuality)
  78. this.extracts[index].link = this.getHostURI + data.extracts[index]
  79. this.extracts[index].quality = data.info.image.quality
  80. this.extracts[index].nextQuality = findNearestUpper(data.info.image.quality, this.qualities)
  81. this.extracts[index].precQuality = findNearestLower(data.info.image.quality, this.qualities)
  82. this.extracts[index].loading = false
  83. }
  84. catch (err) {
  85. // TODO: toast message if fail
  86. console.error('Failed to load extract', err)
  87. }
  88. finally {
  89. this.extracts[index].loading = false
  90. this.saveProgress()
  91. }
  92. }
  93. }
  94. }
  95. </script>