add brackets

main v1.4.0
Ayush Mukherjee 4 years ago
parent f2445d666d
commit 1c27fa6758

@ -31,8 +31,7 @@ wss1.on('connection', (ws, req) => {
})
wss2.on('connection', (ws, req) => {
const id = req.id
subscribe(ws, id)
subscribe(ws, req.id, req.type)
})
const server = app.listen(port, () => {
@ -51,6 +50,15 @@ server.on('upgrade', (req, socket, head) => {
if (token != null || token != '' || token != "") {
wss2.handleUpgrade(req, socket, head, (socket) => {
req.id = token
req.type = 'stream'
wss2.emit('connection', socket, req)
})
}
} else if (pathname[1] === 'bracket') {
if (token != null || token != '' || token != "") {
wss2.handleUpgrade(req, socket, head, (socket) => {
req.id = token
req.type = 'bracket'
wss2.emit('connection', socket, req)
})
}

@ -0,0 +1,20 @@
const { Schema, model } = require('mongoose')
const streams = new Schema({
name: {
type: String,
required: true,
},
event: {
type: Schema.Types.ObjectId,
ref: 'Event',
},
matches: [{
type: Schema.Types.ObjectId,
ref: 'Matches',
}],
})
const Stream = model('Streams', streams)
module.exports = Stream

@ -19,7 +19,7 @@ const streams = new Schema({
}],
hosts: [{
type: Schema.Types.ObjectId,
ref: 'Hosts',
ref: 'Casters',
}],
})

@ -4,6 +4,7 @@ const matches = require('../models/matches')
const streams = require('../models/streams')
const casters = require('../models/casters')
const hosts = require('../models/hosts')
const brackets = require('../models/brackets')
const eventFns = {
getAll: async () => {
@ -170,6 +171,41 @@ const hostFns = {
}
}
const bracketFns = {
getAll: async () => {
return await brackets.find().exec()
},
getAllPop: async () => {
return await brackets.find().populate('event').populate({
path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }],
}).exec()
},
getById: async (id) => {
return await brackets.findById(id).populate('event').populate({
path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }],
}).exec()
},
update: async(id, data) => {
return await brackets.findByIdAndUpdate(id, data).exec()
},
delete: async (id) => {
return await brackets.findByIdAndDelete(id).exec()
},
create: async (data) => {
try {
const ev = new brackets(data)
ev.save((err) => {
if (err)
console.warn(err)
})
} catch (e) {
console.warn(e)
}
}
}
module.exports = {
'events': eventFns,
'rosters': rosterFns,
@ -177,4 +213,5 @@ module.exports = {
'streams': streamFns,
'casters': casterFns,
'hosts': hostFns,
'brackets': bracketFns,
}

@ -3,11 +3,12 @@ const crud = require('./crud')
const connections = {}
const subscribe = (ws, sid) => {
const subscribe = (ws, tid, type) => {
const id = v4()
connections[id] = {
connection: ws,
streamid: sid,
tid,
type,
}
ws.send(JSON.stringify({
@ -22,13 +23,6 @@ const subscribe = (ws, sid) => {
})
}
const cModel = {
'events': 'event',
'matches': 'matches',
'casters': 'casters',
'hosts': 'hosts',
}
const recvUpdate = async (channel, data) => {
Object.keys(connections).forEach((k) => {
sendInitial(k)
@ -38,11 +32,19 @@ const recvUpdate = async (channel, data) => {
const sendInitial = async (id) => {
try {
const c = connections[id]
const stream = await crud['streams'].getById(c.streamid)
if (c.type === 'stream') {
const stream = await crud['streams'].getById(c.tid)
c.connection.send(JSON.stringify({
event: 'streams:read',
data: stream,
}))
} else if (c.type === 'bracket') {
const bracket = await crud['brackets'].getById(c.tid)
c.connection.send(JSON.stringify({
event: 'brackets:read',
data: bracket,
}))
}
} catch (e) {
console.warn(e)
}

Loading…
Cancel
Save