Compare commits

..

No commits in common. 'main' and 'v1.0.0' have entirely different histories.
main ... v1.0.0

3505
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,6 +1,6 @@
{ {
"name": "nuke", "name": "nuke",
"version": "1.5.0", "version": "1.0.0",
"description": "APL Esports' Nuke Server", "description": "APL Esports' Nuke Server",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -11,19 +11,17 @@
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"axios": "^1", "axios": "^0.21.1",
"bufferutil": "^4.0.8",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^8", "dotenv": "^8.2.0",
"express": "^4", "express": "^4.17.1",
"helmet": "^7", "helmet": "^4.1.0",
"mongoose": "^8", "mongoose": "^5.11.19",
"morgan": "^1.10", "morgan": "^1.10.0",
"utf-8-validate": "^6.0.3", "uuid": "^8.3.2",
"uuid": "^9", "ws": "^7.4.4"
"ws": "^8"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^3.0" "nodemon": "^2.0.4"
} }
} }

@ -23,7 +23,6 @@ router.post('/', async (req, res) => {
push(id) push(id)
res.json({ res.json({
session: id, session: id,
name: r.data.entries[0].name,
}) })
} else { } else {
res.status(403).json({ res.status(403).json({

@ -4,32 +4,26 @@ const url = require('url')
const app = require('./app') const app = require('./app')
const { get } = require('./uuids') const { get } = require('./uuids')
const handler = require('./ws/handler') const handler = require('./ws/handler')
const { subscribe } = require('./ws/streams')
mongoose.connect(process.env.MONGO_URI) mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
const db = mongoose.connection const db = mongoose.connection
db.once('open', () => console.log('Connected to mongodb instance!')) db.once('open', () => console.log('Connected to mongodb instance!'))
const port = process.env.PORT || 5000; const port = process.env.PORT || 5000;
wss1 = new WebSocket.Server({ wss = new WebSocket.Server({
noServer: true,
})
wss2 = new WebSocket.Server({
noServer: true, noServer: true,
}) })
wss1.on('connection', (ws, req) => { wss.on('connection', (ws, req) => {
const id = req.id const id = req.id
handler(ws, id) handler(ws, id)
}) })
wss2.on('connection', (ws, req) => {
subscribe(ws, req.id, req.type)
})
const server = app.listen(port, () => { const server = app.listen(port, () => {
console.log(`Express listening: http://localhost:${port}`) console.log(`Express listening: http://localhost:${port}`)
}) })
@ -38,25 +32,9 @@ server.on('upgrade', (req, socket, head) => {
const pathname = url.parse(req.url).pathname.split('/') const pathname = url.parse(req.url).pathname.split('/')
const token = pathname[2] const token = pathname[2]
if (pathname[1] === 'ws' && get(token)) { if (pathname[1] === 'ws' && get(token)) {
wss1.handleUpgrade(req, socket, head, (socket) => { wss.handleUpgrade(req, socket, head, (socket) => {
req.id = token req.id = token
wss1.emit('connection', socket, req) wss.emit('connection', socket, req);
}) })
} else if (pathname[1] === 'stream') {
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)
})
}
} }
}) })

@ -1,26 +0,0 @@
const { Schema, model } = require('mongoose')
const brackets = new Schema({
name: {
type: String,
required: true,
},
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
event: {
type: Schema.Types.ObjectId,
ref: 'Event',
},
matches: [{
type: Schema.Types.ObjectId,
ref: 'Matches',
}],
})
const Bracket = model('Brackets', brackets)
module.exports = Bracket

@ -9,19 +9,14 @@ const casters = new Schema({
type: String, type: String,
required: true, required: true,
}, },
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
twitter: { twitter: {
type: String, type: String,
required: true, required: true,
}, },
image: { image: {
type: String, type: String,
} required: true,
},
}); });
const Caster = model('Casters', casters) const Caster = model('Casters', casters)

@ -6,18 +6,8 @@ const events = new mongoose.Schema({
required: true, required: true,
unique: true, unique: true,
}, },
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
streams: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Streams',
}]
}); });
const Event = mongoose.model('Event', events) const Event = mongoose.model('Events', events)
module.exports = Event module.exports = Event

@ -15,7 +15,8 @@ const hosts = new Schema({
}, },
image: { image: {
type: String, type: String,
} required: true,
},
}); });
const Host = model('Hosts', hosts) const Host = model('Hosts', hosts)

@ -3,29 +3,25 @@ const { Schema, model } = require('mongoose')
const gameScores = new Schema({ const gameScores = new Schema({
orange: { orange: {
type: Number, type: Number,
default: 0, required: true,
}, },
blue: { blue: {
type: Number, type: Number,
default : 0, required: true,
}, },
}) })
const matches = new Schema({ const matches = new Schema({
orange: { orange: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'Rosters', refs: 'Rosters',
required: true, required: true,
}, },
blue: { blue: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'Rosters', refs: 'Rosters',
required: true, required: true,
}, },
current: {
type: Boolean,
default: false,
},
started: { started: {
type: Boolean, type: Boolean,
required: true, required: true,
@ -34,20 +30,10 @@ const matches = new Schema({
type: Boolean, type: Boolean,
required: true, required: true,
}, },
reset: {
type: Boolean,
default: false,
},
type: { type: {
type: String, type: String,
required: true, required: true,
}, },
type_arabic: {
type: String,
},
type_jpn: {
type: String,
},
bestOf: { bestOf: {
type: String, type: String,
required: true, required: true,
@ -55,26 +41,17 @@ const matches = new Schema({
series: { series: {
orange: { orange: {
type: Number, type: Number,
default: 0, required: true,
},
blue: {
type: Number,
default: 0,
},
},
resets: {
orange: {
type: Number,
default: 0,
}, },
blue: { blue: {
type: Number, type: Number,
default: 0, required: true,
}, },
}, },
games: { games: {
type: [gameScores], type: [gameScores],
} required: true,
},
}) })
const Match = model('Matches', matches) const Match = model('Matches', matches)

@ -3,23 +3,23 @@ const { Schema, model } = require('mongoose')
const stats = new Schema({ const stats = new Schema({
goals: { goals: {
type: Number, type: Number,
default: 0, required: true,
}, },
assists: { assists: {
type: Number, type: Number,
default: 0, required: true,
}, },
saves: { saves: {
type: Number, type: Number,
default: 0, required: true,
}, },
shots: { shots: {
type: Number, type: Number,
default: 0, required: true,
}, },
demos: { demos: {
type: Number, type: Number,
default: 0, required: true,
}, },
}) })
@ -28,28 +28,25 @@ const players = new Schema({
type: String, type: String,
required: true, required: true,
}, },
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
account: { account: {
type: String, type: String,
required: true, required: true,
}, },
stats: {
type: [stats],
required: true,
},
}) })
const rosters = new Schema({ const rosters = new Schema({
name: { event: {
type: String, type: Schema.Types.ObjectId,
ref: 'Events',
required: true, required: true,
}, },
name_arabic: { name: {
type: String,
},
name_jpn: {
type: String, type: String,
required: true,
}, },
logo: { logo: {
type: String, type: String,
@ -57,10 +54,8 @@ 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)

@ -1,67 +0,0 @@
const { Schema, model } = require('mongoose')
const stats = new Schema({
name: {
type: String,
required: true,
},
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
image: {
type: String,
required: true,
},
image_full: {
type: Boolean,
default: false,
required: true,
},
win_name: {
type: String,
required: true,
},
win: {
type: String,
required: true,
},
goals_name: {
type: String,
required: true,
},
goals: {
type: String,
required: true,
},
shots_name: {
type: String,
required: true,
},
shots: {
type: String,
required: true,
},
saves_name: {
type: String,
required: true,
},
saves: {
type: String,
required: true,
},
assists_name: {
type: String,
required: true,
},
assists: {
type: String,
required: true,
},
})
const Stat = model('Stats', stats)
module.exports = Stat

@ -5,32 +5,16 @@ const streams = new Schema({
type: String, type: String,
required: true, required: true,
}, },
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
event: { event: {
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'Event', refs: 'Events',
requried: true,
},
matches: {
type: [Schema.Types.ObjectId],
refs: 'matches',
requried: true,
}, },
matches: [{
type: Schema.Types.ObjectId,
ref: 'Matches',
}],
casters: [{
type: Schema.Types.ObjectId,
ref: 'Casters',
}],
hosts: [{
type: Schema.Types.ObjectId,
ref: 'Casters',
}],
stats: [{
type: Schema.Types.ObjectId,
ref: 'Stats'
}],
}) })
const Stream = model('Streams', streams) const Stream = model('Streams', streams)

@ -2,11 +2,9 @@ const channels = [
'rosters', 'rosters',
'matches', 'matches',
'streams', 'streams',
'stats',
'events', 'events',
'casters', 'casters',
'hosts', 'hosts',
'brackets',
]; ];
const events = [ const events = [
@ -16,9 +14,7 @@ const events = [
'delete', 'delete',
]; ];
const channelEvents = [ const channelEvents = [];
'streams:full',
];
channels.forEach((c) => { channels.forEach((c) => {
events.forEach((e) => { events.forEach((e) => {

@ -1,18 +1,16 @@
const events = require('../models/events') const events = require('../models/events')
const rosters = require('../models/rosters') const rosters = require('../models/rosters')
const matches = require('../models/matches') const matches = require('../models/matches')
const stats = require('../models/stats')
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 () => {
return await events.find().populate('streams').exec() return await events.find().exec()
}, },
getById: async (id) => { getById: async (id) => {
return await events.findById(id).populate('streams').exec() return await events.findById(id).exec()
}, },
update: async(id, data) => { update: async(id, data) => {
return await events.findByIdAndUpdate(id, data).exec() return await events.findByIdAndUpdate(id, data).exec()
@ -23,9 +21,12 @@ const eventFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new events(data) const ev = new events(data)
await ev.save(); ev.save((err) => {
if (err)
throw err;
})
} catch (e) { } catch (e) {
console.warn(e) throw e;
} }
} }
} }
@ -46,19 +47,22 @@ const rosterFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new rosters(data) const ev = new rosters(data)
await ev.save(); ev.save((err) => {
if (err)
throw err;
})
} catch (e) { } catch (e) {
console.warn(e) throw e;
} }
} }
} }
const matchFns = { const matchFns = {
getAll: async () => { getAll: async () => {
return await matches.find().populate('orange').populate('blue').exec() return await matches.find().exec()
}, },
getById: async (id) => { getById: async (id) => {
return await matches.findById(id).populate('orange').populate('blue').exec() return await matches.findById(id).exec()
}, },
update: async(id, data) => { update: async(id, data) => {
return await matches.findByIdAndUpdate(id, data).exec() return await matches.findByIdAndUpdate(id, data).exec()
@ -69,32 +73,12 @@ const matchFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new matches(data) const ev = new matches(data)
await ev.save(); ev.save((err) => {
if (err)
throw err;
})
} catch (e) { } catch (e) {
console.warn(e) throw e;
}
}
}
const statsFns = {
getAll: async () => {
return await stats.find().exec()
},
getById: async (id) => {
return await stats.findById(id).exec()
},
update: async(id, data) => {
return await stats.findByIdAndUpdate(id, data).exec()
},
delete: async (id) => {
return await stats.findByIdAndDelete(id).exec()
},
create: async (data) => {
try {
const ev = new stats(data)
await ev.save();
} catch (e) {
console.warn(e)
} }
} }
} }
@ -103,17 +87,8 @@ const streamFns = {
getAll: async () => { getAll: async () => {
return await streams.find().exec() return await streams.find().exec()
}, },
getAllPop: async () => {
return await streams.find().populate('event').populate({
path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }],
}).populate('casters').populate('hosts').populate('stats').lean().exec()
},
getById: async (id) => { getById: async (id) => {
return await streams.findById(id).populate('event').populate({ return await streams.findById(id).exec()
path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }],
}).populate('casters').populate('hosts').populate('stats').lean().exec()
}, },
update: async(id, data) => { update: async(id, data) => {
return await streams.findByIdAndUpdate(id, data).exec() return await streams.findByIdAndUpdate(id, data).exec()
@ -124,9 +99,12 @@ const streamFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new streams(data) const ev = new streams(data)
await ev.save(); ev.save((err) => {
if (err)
throw err;
})
} catch (e) { } catch (e) {
console.warn(e) throw e;
} }
} }
} }
@ -147,9 +125,12 @@ const casterFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new casters(data) const ev = new casters(data)
await ev.save(); ev.save((err) => {
if (err)
throw err;
})
} catch (e) { } catch (e) {
console.warn(e) throw e;
} }
} }
} }
@ -170,41 +151,12 @@ const hostFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new hosts(data) const ev = new hosts(data)
await ev.save(); ev.save((err) => {
} catch (e) { if (err)
console.warn(e) throw err;
} })
}
}
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)
await ev.save();
} catch (e) { } catch (e) {
console.warn(e) throw e;
} }
} }
} }
@ -213,9 +165,7 @@ module.exports = {
'events': eventFns, 'events': eventFns,
'rosters': rosterFns, 'rosters': rosterFns,
'matches': matchFns, 'matches': matchFns,
'stats': statsFns,
'streams': streamFns, 'streams': streamFns,
'casters': casterFns, 'casters': casterFns,
'hosts': hostFns, 'hosts': hostFns,
'brackets': bracketFns,
} }

@ -1,7 +1,6 @@
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 = {};
@ -13,7 +12,7 @@ const handler = (ws, id) => {
ws.send(JSON.stringify({ ws.send(JSON.stringify({
event: 'info', event: 'info',
data: 'Welcome to APL Nuke v1.1.0!' data: 'Welcome to APL Nuke v1.0.0!'
})) }))
ws.on('message', (msg) => handleMsg(msg, id)) ws.on('message', (msg) => handleMsg(msg, id))
@ -27,42 +26,25 @@ const handleMsg = async (msg, id) => {
try { try {
const d = JSON.parse(msg) const d = JSON.parse(msg)
if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) { if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) {
console.log('received sub for ', d.subscribe)
const channel = d.subscribe.split(':')[0] const channel = d.subscribe.split(':')[0]
connections[id].events.push(d.subscribe) connections[id].events.push(d.subscribe)
const dm = { connections[id].connection.send(JSON.stringify(await crud[channel].getAll()))
event: `${channel}:read`,
data: await crud[channel].getAll(),
}
if (channel === 'streams' && d.subscribe === 'streams:full') {
dm.event = 'streams:full'
dm.data = await crud[channel].getAllPop()
}
try {
connections[id].connection.send(JSON.stringify(dm))
} catch (e) {
console.warn(e)
}
} 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':
dm = await crud[channel].create(d.data) await crud[channel].create(d.data)
recvUpdate(channel, dm)
fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll())
break; break;
case 'update': case 'update':
dm = await crud[channel].update(d.data.id, d.data.data) await crud[channel].update(d.data.id, d.data.data)
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;
} }
@ -73,21 +55,9 @@ const handleMsg = async (msg, id) => {
} }
const fanoutMsg = (channel, data) => { const fanoutMsg = (channel, data) => {
Object.keys(connections).forEach( async (k) => { Object.keys(connections).forEach((k) => {
if (connections[k].events.indexOf(`${channel}:read`) !== -1) { if (connections[k].events.indexOf(`${channel}:read`) !== -1) {
const d = { connections[k].connection.send(JSON.stringify(data))
event: `${channel}:read`,
data,
}
connections[k].connection.send(JSON.stringify(d))
if (connections[k].events.indexOf('streams:full') !== -1) {
const m = await crud['streams'].getAllPop()
const d = {
event: 'streams:full',
data: m,
}
connections[k].connection.send(JSON.stringify(d))
}
} }
}) })
} }

@ -1,56 +0,0 @@
const { v4 } = require('uuid')
const crud = require('./crud')
const connections = {}
const subscribe = (ws, tid, type) => {
const id = v4()
connections[id] = {
connection: ws,
tid,
type,
}
ws.send(JSON.stringify({
event: 'info',
data: 'Welcome to APL Nuke v1.1.0!',
}))
sendInitial(id)
ws.on('close', () => {
delete connections[id]
})
}
const recvUpdate = async (channel, data) => {
Object.keys(connections).forEach((k) => {
sendInitial(k)
})
}
const sendInitial = async (id) => {
try {
const c = connections[id]
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)
}
}
module.exports = {
subscribe,
recvUpdate,
}
Loading…
Cancel
Save