Compare commits

..

19 Commits
v1.3.3 ... main

3473
package-lock.json generated

File diff suppressed because it is too large Load Diff

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

@ -6,11 +6,7 @@ const { get } = require('./uuids')
const handler = require('./ws/handler') const handler = require('./ws/handler')
const { subscribe } = require('./ws/streams') const { subscribe } = require('./ws/streams')
mongoose.connect(process.env.MONGO_URI, { mongoose.connect(process.env.MONGO_URI)
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
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!'))
@ -31,8 +27,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,8 +46,17 @@ 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,26 @@
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,6 +9,12 @@ 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,
@ -20,4 +26,4 @@ const casters = new Schema({
const Caster = model('Casters', casters) const Caster = model('Casters', casters)
module.exports = Caster module.exports = Caster

@ -5,9 +5,19 @@ const events = new mongoose.Schema({
type: String, type: String,
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('Event', events)
module.exports = Event module.exports = Event

@ -42,6 +42,12 @@ const matches = new Schema({
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,
@ -73,4 +79,4 @@ const matches = new Schema({
const Match = model('Matches', matches) const Match = model('Matches', matches)
module.exports = Match module.exports = Match

@ -28,6 +28,12 @@ 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,
@ -39,6 +45,12 @@ const rosters = new Schema({
type: String, type: String,
required: true, required: true,
}, },
name_arabic: {
type: String,
},
name_jpn: {
type: String,
},
logo: { logo: {
type: String, type: String,
required: true, required: true,
@ -53,4 +65,4 @@ const rosters = new Schema({
const Roster = model('Rosters', rosters) const Roster = model('Rosters', rosters)
module.exports = Roster module.exports = Roster

@ -0,0 +1,67 @@
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,6 +5,12 @@ 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', ref: 'Event',
@ -19,10 +25,14 @@ const streams = new Schema({
}], }],
hosts: [{ hosts: [{
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'Hosts', ref: 'Casters',
}],
stats: [{
type: Schema.Types.ObjectId,
ref: 'Stats'
}], }],
}) })
const Stream = model('Streams', streams) const Stream = model('Streams', streams)
module.exports = Stream module.exports = Stream

@ -2,9 +2,11 @@ const channels = [
'rosters', 'rosters',
'matches', 'matches',
'streams', 'streams',
'stats',
'events', 'events',
'casters', 'casters',
'hosts', 'hosts',
'brackets',
]; ];
const events = [ const events = [
@ -28,4 +30,4 @@ module.exports = {
channelEvents, channelEvents,
channels, channels,
events, events,
}; };

@ -1,9 +1,11 @@
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 () => {
@ -21,10 +23,7 @@ const eventFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new events(data) const ev = new events(data)
ev.save((err) => { await ev.save();
if (err)
console.warn(err)
})
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -47,10 +46,7 @@ const rosterFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new rosters(data) const ev = new rosters(data)
ev.save((err) => { await ev.save();
if (err)
console.warn(err)
})
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -73,10 +69,30 @@ const matchFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new matches(data) const ev = new matches(data)
ev.save((err) => { await ev.save();
if (err) } catch (e) {
console.warn(err) console.warn(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) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -91,13 +107,13 @@ const streamFns = {
return await streams.find().populate('event').populate({ return await streams.find().populate('event').populate({
path: 'matches', path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }], populate: [{ path: 'orange' }, { path: 'blue' }],
}).populate('casters').populate('hosts').exec() }).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).populate('event').populate({
path: 'matches', path: 'matches',
populate: [{ path: 'orange' }, { path: 'blue' }], populate: [{ path: 'orange' }, { path: 'blue' }],
}).populate('casters').populate('hosts').exec() }).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()
@ -108,10 +124,7 @@ const streamFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new streams(data) const ev = new streams(data)
ev.save((err) => { await ev.save();
if (err)
console.warn(err)
})
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -134,10 +147,7 @@ const casterFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new casters(data) const ev = new casters(data)
ev.save((err) => { await ev.save();
if (err)
console.warn(err)
})
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
@ -160,10 +170,39 @@ const hostFns = {
create: async (data) => { create: async (data) => {
try { try {
const ev = new hosts(data) const ev = new hosts(data)
ev.save((err) => { await ev.save();
if (err) } catch (e) {
console.warn(err) console.warn(e)
}) }
}
}
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) console.warn(e)
} }
@ -174,7 +213,9 @@ 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,
}

@ -38,7 +38,11 @@ const handleMsg = async (msg, id) => {
dm.event = 'streams:full' dm.event = 'streams:full'
dm.data = await crud[channel].getAllPop() dm.data = await crud[channel].getAllPop()
} }
connections[id].connection.send(JSON.stringify(dm)) 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]

@ -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') {
c.connection.send(JSON.stringify({ const stream = await crud['streams'].getById(c.tid)
event: 'streams:read', c.connection.send(JSON.stringify({
data: stream, 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) { } catch (e) {
console.warn(e) console.warn(e)
} }

Loading…
Cancel
Save