change stats to per-team instead of per-player

main
Ayush Mukherjee 4 years ago
parent 99467f229c
commit 2064f0036f

@ -9,6 +9,7 @@ const { subscribe } = require('./ws/streams')
mongoose.connect(process.env.MONGO_URI, { mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true, useNewUrlParser: true,
useUnifiedTopology: true, useUnifiedTopology: true,
useFindAndModify: false,
}) })
const db = mongoose.connection const db = mongoose.connection

@ -3,11 +3,11 @@ const { Schema, model } = require('mongoose')
const gameScores = new Schema({ const gameScores = new Schema({
orange: { orange: {
type: Number, type: Number,
required: true, default: 0,
}, },
blue: { blue: {
type: Number, type: Number,
required: true, default : 0,
}, },
}) })
@ -50,7 +50,6 @@ const matches = new Schema({
}, },
games: { games: {
type: [gameScores], type: [gameScores],
required: true,
} }
}) })

@ -3,23 +3,23 @@ const { Schema, model } = require('mongoose')
const stats = new Schema({ const stats = new Schema({
goals: { goals: {
type: Number, type: Number,
required: true, default: 0,
}, },
assists: { assists: {
type: Number, type: Number,
required: true, default: 0,
}, },
saves: { saves: {
type: Number, type: Number,
required: true, default: 0,
}, },
shots: { shots: {
type: Number, type: Number,
required: true, default: 0,
}, },
demos: { demos: {
type: Number, type: Number,
required: true, default: 0,
}, },
}) })
@ -32,10 +32,6 @@ const players = new Schema({
type: String, type: String,
required: true, required: true,
}, },
stats: {
type: [stats],
required: true,
},
}) })
const rosters = new Schema({ const rosters = new Schema({
@ -49,8 +45,10 @@ const rosters = new Schema({
}, },
players: { players: {
type: [players], type: [players],
required: true,
}, },
stats: {
type: stats,
}
}) })
const Roster = model('Rosters', rosters) const Roster = model('Rosters', rosters)

@ -33,10 +33,10 @@ const eventFns = {
const rosterFns = { const rosterFns = {
getAll: async () => { getAll: async () => {
return await rosters.find().populate('event').populate('match').exec() return await rosters.find().exec()
}, },
getById: async (id) => { getById: async (id) => {
return await rosters.findById(id).populate('event').populate('match').exec() return await rosters.findById(id).exec()
}, },
update: async(id, data) => { update: async(id, data) => {
return await rosters.findByIdAndUpdate(id, data).exec() return await rosters.findByIdAndUpdate(id, data).exec()
@ -49,10 +49,10 @@ const rosterFns = {
const ev = new rosters(data) const ev = new rosters(data)
ev.save((err) => { ev.save((err) => {
if (err) if (err)
throw err; throw err
}) })
} catch (e) { } catch (e) {
throw e; throw e
} }
} }
} }

@ -1,6 +1,7 @@
const { pop } = require('../uuids') const { pop } = require('../uuids')
const { channelEvents } = require('./channels') const { channelEvents } = require('./channels')
const crud = require('./crud') const crud = require('./crud')
const { recvUpdate } = require('./streams')
const connections = {}; const connections = {};
@ -28,26 +29,32 @@ const handleMsg = async (msg, id) => {
if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) { if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) {
const channel = d.subscribe.split(':')[0] const channel = d.subscribe.split(':')[0]
connections[id].events.push(d.subscribe) 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) { } else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) {
const ev = d.event.split(':') const ev = d.event.split(':')
const channel = ev[0] const channel = ev[0]
const event = ev[1] const event = ev[1]
console.log(`received event for ${channel} with data %s`, d.data) console.log(`received event for ${channel} with data %s`, d.data)
let dm;
switch (event) { switch (event) {
case 'create': case 'create':
const da = await crud[channel].create(d.data) dm = await crud[channel].create(d.data)
connections[id].connection.send(JSON.stringify(da)) recvUpdate(channel, dm)
fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll())
fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll())
break; break;
case 'update': case 'update':
const du = await crud[channel].update(d.data.id, d.data.data) dm = await crud[channel].update(d.data.id, d.data.data)
connections[id].connection.send(JSON.stringify(du)) recvUpdate(channel, dm)
fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll())
break; break;
case 'delete': case 'delete':
await crud[channel].delete(d.data.id) await crud[channel].delete(d.data.id)
recvUpdate(channel, 'delete')
fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll())
break; break;
} }
@ -60,7 +67,11 @@ const handleMsg = async (msg, id) => {
const fanoutMsg = (channel, data) => { const fanoutMsg = (channel, data) => {
Object.keys(connections).forEach((k) => { Object.keys(connections).forEach((k) => {
if (connections[k].events.indexOf(`${channel}:read`) !== -1) { 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))
} }
}) })
} }

@ -30,11 +30,8 @@ const cModel = {
} }
const recvUpdate = async (channel, data) => { const recvUpdate = async (channel, data) => {
const streams = await crud['streams'].getAll() Object.keys(connections).forEach((k) => {
const stream = streams.filter(x => x[cModel[channel]]._id === data._id)[0] sendInitial(k)
const c = connections.filter(x => x.streamid === stream._id)
Object.keys(c).forEach((k) => {
c[k].connection.send(JSON.stringify(stream))
}) })
} }

Loading…
Cancel
Save