diff --git a/src/index.js b/src/index.js index 3c37c27..de98a56 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ const { subscribe } = require('./ws/streams') mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, + useFindAndModify: false, }) const db = mongoose.connection diff --git a/src/models/matches.js b/src/models/matches.js index fc4b978..3474059 100644 --- a/src/models/matches.js +++ b/src/models/matches.js @@ -3,11 +3,11 @@ const { Schema, model } = require('mongoose') const gameScores = new Schema({ orange: { type: Number, - required: true, + default: 0, }, blue: { type: Number, - required: true, + default : 0, }, }) @@ -50,7 +50,6 @@ const matches = new Schema({ }, games: { type: [gameScores], - required: true, } }) diff --git a/src/models/rosters.js b/src/models/rosters.js index 5289397..6241d7b 100644 --- a/src/models/rosters.js +++ b/src/models/rosters.js @@ -3,23 +3,23 @@ const { Schema, model } = require('mongoose') const stats = new Schema({ goals: { type: Number, - required: true, + default: 0, }, assists: { type: Number, - required: true, + default: 0, }, saves: { type: Number, - required: true, + default: 0, }, shots: { type: Number, - required: true, + default: 0, }, demos: { type: Number, - required: true, + default: 0, }, }) @@ -32,10 +32,6 @@ const players = new Schema({ type: String, required: true, }, - stats: { - type: [stats], - required: true, - }, }) const rosters = new Schema({ @@ -49,8 +45,10 @@ const rosters = new Schema({ }, players: { type: [players], - required: true, }, + stats: { + type: stats, + } }) const Roster = model('Rosters', rosters) diff --git a/src/ws/crud.js b/src/ws/crud.js index 5acd3c7..bf5f9fb 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -33,10 +33,10 @@ const eventFns = { const rosterFns = { getAll: async () => { - return await rosters.find().populate('event').populate('match').exec() + return await rosters.find().exec() }, getById: async (id) => { - return await rosters.findById(id).populate('event').populate('match').exec() + return await rosters.findById(id).exec() }, update: async(id, data) => { return await rosters.findByIdAndUpdate(id, data).exec() @@ -49,10 +49,10 @@ const rosterFns = { const ev = new rosters(data) ev.save((err) => { if (err) - throw err; + throw err }) } catch (e) { - throw e; + throw e } } } diff --git a/src/ws/handler.js b/src/ws/handler.js index d741e05..43009bf 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -1,6 +1,7 @@ const { pop } = require('../uuids') const { channelEvents } = require('./channels') const crud = require('./crud') +const { recvUpdate } = require('./streams') const connections = {}; @@ -28,26 +29,32 @@ const handleMsg = async (msg, id) => { if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) { const channel = d.subscribe.split(':')[0] connections[id].events.push(d.subscribe) - connections[id].connection.send(JSON.stringify(await crud[channel].getAll())) + const dm = { + event: `${channel}:read`, + data: await crud[channel].getAll(), + } + connections[id].connection.send(JSON.stringify(dm)) } else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) { const ev = d.event.split(':') const channel = ev[0] const event = ev[1] console.log(`received event for ${channel} with data %s`, d.data) + let dm; switch (event) { case 'create': - const da = await crud[channel].create(d.data) - connections[id].connection.send(JSON.stringify(da)) + dm = await crud[channel].create(d.data) + recvUpdate(channel, dm) fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll()) break; case 'update': - const du = await crud[channel].update(d.data.id, d.data.data) - connections[id].connection.send(JSON.stringify(du)) + dm = await crud[channel].update(d.data.id, d.data.data) + recvUpdate(channel, dm) fanoutMsg(channel, await crud[channel].getAll()) break; case 'delete': await crud[channel].delete(d.data.id) + recvUpdate(channel, 'delete') fanoutMsg(channel, await crud[channel].getAll()) break; } @@ -60,7 +67,11 @@ const handleMsg = async (msg, id) => { const fanoutMsg = (channel, data) => { Object.keys(connections).forEach((k) => { if (connections[k].events.indexOf(`${channel}:read`) !== -1) { - connections[k].connection.send(JSON.stringify(data)) + const d = { + event: `${channel}:read`, + data, + } + connections[k].connection.send(JSON.stringify(d)) } }) } diff --git a/src/ws/streams.js b/src/ws/streams.js index a14b7a8..a65464c 100644 --- a/src/ws/streams.js +++ b/src/ws/streams.js @@ -30,11 +30,8 @@ const cModel = { } const recvUpdate = async (channel, data) => { - const streams = await crud['streams'].getAll() - const stream = streams.filter(x => x[cModel[channel]]._id === data._id)[0] - const c = connections.filter(x => x.streamid === stream._id) - Object.keys(c).forEach((k) => { - c[k].connection.send(JSON.stringify(stream)) + Object.keys(connections).forEach((k) => { + sendInitial(k) }) }