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, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
const db = mongoose.connection

@ -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,
}
})

@ -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)

@ -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
}
}
}

@ -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))
}
})
}

@ -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)
})
}

Loading…
Cancel
Save