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) => { wss2.on('connection', (ws, req) => {
const id = req.id subscribe(ws, req.id, req.type)
subscribe(ws, id)
}) })
const server = app.listen(port, () => { const server = app.listen(port, () => {
@ -51,6 +50,15 @@ server.on('upgrade', (req, socket, head) => {
if (token != null || token != '' || token != "") { if (token != null || token != '' || token != "") {
wss2.handleUpgrade(req, socket, head, (socket) => { wss2.handleUpgrade(req, socket, head, (socket) => {
req.id = token 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) 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: [{ hosts: [{
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'Hosts', ref: 'Casters',
}], }],
}) })

@ -4,6 +4,7 @@ const matches = require('../models/matches')
const streams = require('../models/streams') const streams = require('../models/streams')
const casters = require('../models/casters') const casters = require('../models/casters')
const hosts = require('../models/hosts') const hosts = require('../models/hosts')
const brackets = require('../models/brackets')
const eventFns = { const eventFns = {
getAll: async () => { 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 = { module.exports = {
'events': eventFns, 'events': eventFns,
'rosters': rosterFns, 'rosters': rosterFns,
@ -177,4 +213,5 @@ module.exports = {
'streams': streamFns, 'streams': streamFns,
'casters': casterFns, 'casters': casterFns,
'hosts': hostFns, 'hosts': hostFns,
'brackets': bracketFns,
} }

@ -3,11 +3,12 @@ const crud = require('./crud')
const connections = {} const connections = {}
const subscribe = (ws, sid) => { const subscribe = (ws, tid, type) => {
const id = v4() const id = v4()
connections[id] = { connections[id] = {
connection: ws, connection: ws,
streamid: sid, tid,
type,
} }
ws.send(JSON.stringify({ 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) => { const recvUpdate = async (channel, data) => {
Object.keys(connections).forEach((k) => { Object.keys(connections).forEach((k) => {
sendInitial(k) sendInitial(k)
@ -38,11 +32,19 @@ const recvUpdate = async (channel, data) => {
const sendInitial = async (id) => { const sendInitial = async (id) => {
try { try {
const c = connections[id] 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({ c.connection.send(JSON.stringify({
event: 'streams:read', event: 'streams:read',
data: stream, 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) { } catch (e) {
console.warn(e) console.warn(e)
} }

Loading…
Cancel
Save