projects.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // import qs from 'querystring'
  2. // import { REST } from 'api/rest-axios'
  3. import { GRAPHQL } from 'api/graphql-axios'
  4. import projectMod from 'store/modules/project'
  5. export default {
  6. namespaced: true,
  7. // initial state
  8. state: {
  9. projects: []
  10. },
  11. // getters
  12. getters: {
  13. getGridPos: (state) => (index) => {
  14. let popped = state.projects[index].grid.pop()
  15. console.log('getGridPos: popped', popped)
  16. return popped
  17. }
  18. },
  19. // mutations
  20. mutations: {
  21. setProjects (state, projects) {
  22. // record the retrieved data
  23. state.projects = projects
  24. console.log('setProjects', this)
  25. // register a new module only if doesn't exist
  26. let name
  27. projects.forEach((project, i) => {
  28. name = `project:${project.id}`
  29. if (!(this.state && this.state[name])) {
  30. projectMod.state = { ...project }
  31. this.registerModule(name, projectMod)
  32. } else {
  33. // re-use the already existing module
  34. console.log(`reusing module poject:${project.id}`)
  35. }
  36. })
  37. // randomly define building sizes
  38. let totalW = 0
  39. for (var i = 0; i < state.projects.length; i++) {
  40. let w = Math.round(21 + Math.random() * 15)
  41. totalW += w
  42. state.projects[i].size = {
  43. x: w,
  44. y: Math.round(100 + Math.random() * 250),
  45. z: Math.round(10 + Math.random() * 30)
  46. }
  47. }
  48. // positioning buildings on x regarding the widths
  49. // & setting up the window sizing
  50. // & setting up the content grid
  51. let margin = 5
  52. totalW += margin * (state.projects.length - 1)
  53. let wall, a
  54. let grid
  55. // fro each PROJECTS
  56. for (var j = 0; j < state.projects.length; j++) {
  57. // X POS
  58. let x = -1 * totalW / 2// + state.projects[0].size.x / 2
  59. for (var k = 0; k < j; k++) {
  60. x += state.projects[k].size.x
  61. }
  62. x += margin * j
  63. x += state.projects[j].size.x / 2
  64. state.projects[j].position = {
  65. x: x,
  66. y: -1 * state.projects[j].size.y / 2 + 10 + Math.random() * 30, // -10 + Math.random() * this.size.y / 2
  67. z: -10 + Math.random() * 10
  68. }
  69. // WINDOWS
  70. wall = {
  71. wallW: 0.001,
  72. // dig windows on face and back
  73. winW: 2 + Math.random() * 2,
  74. winH: 4 + Math.random() * 4,
  75. margin: 2,
  76. nbrWinX: 0,
  77. paddingX: 0,
  78. nbrWinY: 0,
  79. paddingY: 0,
  80. nbrWinZ: 0,
  81. paddingZ: 0
  82. }
  83. // removing windows on X until padding is enough
  84. a = 0
  85. wall.nbrWinX = Math.floor((state.projects[j].size.x - 2 * wall.margin) / wall.winW)
  86. while (wall.paddingX < 0.4) {
  87. wall.nbrWinX -= a
  88. wall.paddingX = (state.projects[j].size.x - 2 * wall.margin - wall.winW * wall.nbrWinX) / (wall.nbrWinX - 1)
  89. a++
  90. }
  91. // removing windows on Y until padding is enough
  92. a = 0
  93. wall.nbrWinY = Math.floor((state.projects[j].size.y - 2 * wall.margin) / wall.winH)
  94. while (wall.paddingY < 0.4) {
  95. wall.nbrWinY -= a
  96. wall.paddingY = (state.projects[j].size.y - 2 * wall.margin - wall.winH * wall.nbrWinY) / (wall.nbrWinY - 1)
  97. a++
  98. }
  99. state.projects[j].wall = { ...wall }
  100. // CONTENTS GRID
  101. a = 0
  102. wall.nbrWinZ = Math.floor((state.projects[j].size.z - 2 * wall.margin) / wall.winW)
  103. while (wall.paddingZ < 0.4) {
  104. wall.nbrWinZ -= a
  105. wall.paddingZ = (state.projects[j].size.z - 2 * wall.margin - wall.winW * wall.nbrWinZ) / (wall.nbrWinZ - 1)
  106. a++
  107. }
  108. grid = []
  109. for (var l = 0; l < wall.nbrWinZ * 2; l++) { // cols
  110. for (var m = 0; m < wall.nbrWinY * 2; m++) { // rows
  111. grid.push({
  112. z: margin + wall.winW / 2 * l,
  113. y: margin + wall.winH / 2 * m
  114. })
  115. }
  116. }
  117. // shuffle the grid
  118. for (let n = grid.length - 1; n > 0; n--) {
  119. const o = Math.floor(Math.random() * n)
  120. const temp = grid[n]
  121. grid[n] = grid[o]
  122. grid[o] = temp
  123. }
  124. state.projects[j].grid = [ ...grid ]
  125. }
  126. }
  127. },
  128. // actions
  129. actions: {
  130. // async get authors
  131. getProjects ({ dispatch, commit, state }) {
  132. // GRAPHQL
  133. GRAPHQL.post('', { query: `query {
  134. projects {
  135. id
  136. Weight
  137. Titre
  138. }
  139. }` })
  140. .then(({ data: { data: { projects } = null } }) => {
  141. console.log('graphql projects', projects)
  142. commit('setProjects', projects)
  143. })
  144. .catch((error) => {
  145. console.warn('Issue with getProjects', error)
  146. Promise.reject(error)
  147. })
  148. // REST
  149. // let params = {
  150. // _sort: `weight:ASC`
  151. // }
  152. // let q = qs.stringify(params)
  153. // REST.get(`projects?` + q, {})
  154. // .then(({ data }) => {
  155. // console.log('projects getProjects REST: data', data)
  156. // commit('setProjects', data)
  157. // })
  158. // .catch((error) => {
  159. // console.warn('Issue with getProjects', error)
  160. // Promise.reject(error)
  161. // })
  162. },
  163. build3dBuilding ({ dispatch, commit, state }) {
  164. }
  165. }
  166. }