Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. *Models*
  2.  
  3. User
  4. - username: string
  5. - password: string
  6. - points: integer
  7. - guessed: boolean
  8.  
  9. Room
  10. - word: string
  11. - color: string
  12. - status: 'joining'
  13. - round: { type: Sequelize.INTEGER, defaultValue: 0 }
  14.  
  15. *Relations*
  16. User.belongsTo(Room) - user.roomId
  17. Room.hasMany(User) - room.users = [{ username, password, points }]
  18.  
  19. *Routes*
  20. / - List of rooms, create room button/form?
  21.  
  22. ```
  23. [name ][Create Room]
  24.  
  25. <Rooms>
  26. - <name>
  27. - 2
  28. - 3
  29. ```
  30.  
  31.  
  32. /room/:id - a single room (gameplay)
  33. /login?
  34. /signup?
  35.  
  36. *Endpoints*
  37. `POST /user` - signup
  38. `POST /login` - login
  39. `POST /room` - Room.create
  40.  
  41. `const words = ['red', 'green', 'blue', 'yellow']`
  42.  
  43. `PUT /room/join/:id`
  44. ```
  45. const room = await Room.findByPk(request.params.id)
  46. if (room.status === 'joining') {
  47. const { userId } = request.body
  48. const user = await User.findByPk(userId)
  49. const updated = await user.update({ roomId: request.params.id, points: 0 })
  50. } else {
  51. response.status(400).send('The game has already started')
  52. }
  53. ```
  54.  
  55. `PUT /room/start/:id`
  56. ```
  57. const room = await Room.findByPk(request.params.id)
  58. if (room.status === 'joining') {
  59. const shuffled = shuffle(colors)
  60. await room.update({
  61. status: 'started',
  62. color: shuffled[0],
  63. word: shuffled[1]
  64. })
  65. }
  66. ```
  67.  
  68. `PUT /room/guess/:id`
  69. ```
  70. const { userId, guess } = request.body
  71. const room = await Room.findByPk(request.params.id)
  72. const user = await User.findByPk(userId)
  73.  
  74. if (room.status === 'started' || user.guessed) {
  75.  
  76. const correct = guess === room.word
  77. const points = correct
  78. ? user.points + 1
  79. : user.points - 1
  80.  
  81. const updatedUser = await User.update({ guessed: true, points })
  82. const updatedRoom = await Room.findByPk(request.params.id)
  83.  
  84. const finished = correct || !updatedRoom.users.some(user => !user.guessed)
  85. if (finished) {
  86. const round = room.round + 1
  87. const shuffled = shuffle(colors)
  88.  
  89. const updated = round > 6
  90. ? await room.update({
  91. color: shuffled[0],
  92. word: shuffled[1],
  93. })
  94. : await room.update({ status: 'done' })
  95.  
  96. response.send(updated)
  97. }
  98. }
  99. ```
  100.  
  101.  
  102. *ALWAYS*
  103. ```
  104. const rooms = Room.findAll({ include: [User] })
  105. const data = JSON.stringify(rooms)
  106. stream.updateInit(data)
  107. stream.send(data)
  108. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement