diff --git a/src/index.js b/src/index.js index d503026..97c47be 100644 --- a/src/index.js +++ b/src/index.js @@ -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) }) } diff --git a/src/models/brackets.js b/src/models/brackets.js new file mode 100644 index 0000000..9e4fe4f --- /dev/null +++ b/src/models/brackets.js @@ -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 \ No newline at end of file diff --git a/src/models/streams.js b/src/models/streams.js index 2fbf63b..afd6dc2 100644 --- a/src/models/streams.js +++ b/src/models/streams.js @@ -19,7 +19,7 @@ const streams = new Schema({ }], hosts: [{ type: Schema.Types.ObjectId, - ref: 'Hosts', + ref: 'Casters', }], }) diff --git a/src/ws/crud.js b/src/ws/crud.js index 65339aa..9174b0a 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -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, } \ No newline at end of file diff --git a/src/ws/streams.js b/src/ws/streams.js index a1b38a6..ba7ad83 100644 --- a/src/ws/streams.js +++ b/src/ws/streams.js @@ -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) - c.connection.send(JSON.stringify({ - event: 'streams:read', - data: stream, - })) + 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) }