Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *Models*
- User
- - username: string
- - password: string
- - points: integer
- - guessed: boolean
- Room
- - word: string
- - color: string
- - status: 'joining'
- - round: { type: Sequelize.INTEGER, defaultValue: 0 }
- *Relations*
- User.belongsTo(Room) - user.roomId
- Room.hasMany(User) - room.users = [{ username, password, points }]
- *Routes*
- / - List of rooms, create room button/form?
- ```
- [name ][Create Room]
- <Rooms>
- - <name>
- - 2
- - 3
- ```
- /room/:id - a single room (gameplay)
- /login?
- /signup?
- *Endpoints*
- `POST /user` - signup
- `POST /login` - login
- `POST /room` - Room.create
- `const words = ['red', 'green', 'blue', 'yellow']`
- `PUT /room/join/:id`
- ```
- const room = await Room.findByPk(request.params.id)
- if (room.status === 'joining') {
- const { userId } = request.body
- const user = await User.findByPk(userId)
- const updated = await user.update({ roomId: request.params.id, points: 0 })
- } else {
- response.status(400).send('The game has already started')
- }
- ```
- `PUT /room/start/:id`
- ```
- const room = await Room.findByPk(request.params.id)
- if (room.status === 'joining') {
- const shuffled = shuffle(colors)
- await room.update({
- status: 'started',
- color: shuffled[0],
- word: shuffled[1]
- })
- }
- ```
- `PUT /room/guess/:id`
- ```
- const { userId, guess } = request.body
- const room = await Room.findByPk(request.params.id)
- const user = await User.findByPk(userId)
- if (room.status === 'started' || user.guessed) {
- const correct = guess === room.word
- const points = correct
- ? user.points + 1
- : user.points - 1
- const updatedUser = await User.update({ guessed: true, points })
- const updatedRoom = await Room.findByPk(request.params.id)
- const finished = correct || !updatedRoom.users.some(user => !user.guessed)
- if (finished) {
- const round = room.round + 1
- const shuffled = shuffle(colors)
- const updated = round > 6
- ? await room.update({
- color: shuffled[0],
- word: shuffled[1],
- })
- : await room.update({ status: 'done' })
- response.send(updated)
- }
- }
- ```
- *ALWAYS*
- ```
- const rooms = Room.findAll({ include: [User] })
- const data = JSON.stringify(rooms)
- stream.updateInit(data)
- stream.send(data)
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement