Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

还global吗 #6

Open
simon300000 opened this issue Oct 31, 2019 · 2 comments
Open

还global吗 #6

simon300000 opened this issue Oct 31, 2019 · 2 comments

Comments

@simon300000
Copy link
Member

global.amdb = false
global.utrdb = false

global.amdb = client.db('amdb').collection('maindb')
global.utrdb = client.db('amdb').collection('utrdb')

if (!global.amdb) return
const amdb = global.amdb

if (!global.amdb) return
const amdb = global.amdb
if (!global.utrdb) return
const utrdb = global.utrdb

global.adbRunning = false

if (global.adbRunning) return
global.adbRunning = true

if (!global.amdb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: AMDB ERR'
await next()
return
}
if (!global.predb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: PreDB ERR'
await next()
return
}
const amdb = global.amdb
const predb = global.predb
if (!ctx.request.body.roomid || isNaN(Number(ctx.request.body.roomid))) {
ctx.response.status = 404
ctx.response.body = 'Bad Request Format'
await next()
} else {
try {
ctx.response.status = 200
const roomid = Number(ctx.request.body.roomid)
log(`LOG req in rid ${roomid}`)
const finded = await amdb
.find({ roomid })
.sort('ts', -1)
.limit(50)
.toArray()
log(`LOG amdb complete ${roomid}`)
const preFinded = await predb
.find({ roomid })
.sort('ts', -1)
.limit(30)
.toArray()
log(`LOG predb complete ${roomid}`)
const tsList = new Array()
const rList = []
for (const item of finded) {
if (!item.livets) continue
if (Number(item.hide) > 0) continue
let lts = Number(item.livets)
for (i = lts; i > lts - 300; i--) {
if (tsList.includes(Number(i))) {
lts = Number(i)
break
}
}
if (!tsList.includes(lts)) tsList.push(lts)
if (!rList[lts]) rList[lts] = new Array()
rList[lts].push({ ...item, ts: Number(item.ts * 1000), sc: 1 })
}
for (const item of preFinded) {
if (
ctx.request.body.filter &&
(isNaN(Number(item.price)) || Number(item.price) < 30)
)
continue
if (!item.livets) continue
if (Number(item.hide) > 0) continue
let lts = Number(item.livets)
/// lts是本次直播开始的时间
/// 现在需要将时间间隔在300000000ms内的两场直播合并
/// 并找出那次直播的时间(livets),赋值给lts
// for (i = lts; i > lts - 300000000; i--) {
// if (tsList.includes(Number(i))) {
// lts = Number(i)
// break
// }
// }
/// 以上为Il Harper@绝赞自裁中的……算了丢人
/// 以下为群中的大佬写的示例
let newlts = lts - 300000001
for (let num of tsList) {
newlts = newlts < num && num <= lts ? num : newlts
}
if (newlts !== lts - 300000001) lts = newlts
/// END
if (!tsList.includes(lts)) tsList.push(lts)
if (!rList[lts]) rList[lts] = new Array()
rList[lts].push({ ...item, ...giftConv(item), sc: 0 })
}
// for (const tl of rList) {
// tl.sort((a, b) => Number(b.ts) - Number(a.ts))
// }
log(`LOG local sort ${roomid}`)
const output = []
for (const ts of tsList) {
// rList[ts].sort((a, b) => Number(b.ts) - Number(a.ts))
output.push({
ts,
data: rList[ts]
})
}
// output.sort((a, b) => Number(b.ts) - Number(a.ts))
log(`LOG output ${roomid}`)
ctx.response.body = output
log(`LOG next start ${roomid}`)
await next()
log(`LOG next end ${roomid}`)
} catch (e) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error'
await next()
console.log(e)
}
}
})
// /sc/submit
sc.post('/submit', async (ctx, next) => {
if (!global.amdb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: AMDB ERR'
await next()
return
}
if (!global.udb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: UDB ERR'
await next()
return
}
const amdb = global.amdb
const udb = global.udb
if (
!ctx.request.body.username ||
!ctx.request.body.password ||
!ctx.request.body.id ||
!ctx.request.body.tr
) {
ctx.response.status = 404
ctx.response.body = 'Bad Request Format'
await next()
return
}
try {
const u = await udb
.find({
username: ctx.request.body.username,
password: ctx.request.body.password
})
.limit(1)
.toArray()
if (u.length < 1) {
ctx.response.status = 403
ctx.response.body = 'Forbidden'
await next()
return
}
const ust = u[0].trstatus
const item = await amdb
.find({ _id: Number(ctx.request.body.id) })
.limit(1)
.toArray()
if (Number(item.trstatus) > Number(ust)) {
ctx.response.status = 200
ctx.response.body = 1
await next()
return
}
await amdb.updateOne(
{ _id: Number(ctx.request.body.id) },
{ $set: { msgtr: ctx.request.body.tr } }
)
ctx.response.status = 200
ctx.response.body = 0
await next()
} catch (e) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error'
await next()
console.log(e)
}
})
// /sc/hide
sc.post('/hide', async (ctx, next) => {
if (!global.amdb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: AMDB ERR'
await next()
return
}
if (!global.udb) {
ctx.response.status = 500
ctx.response.body = 'Internal Server Error: UDB ERR'
await next()
return
}
const amdb = global.amdb
const udb = global.udb

global.amdb = false
global.udb = false

try {
await client.connect()
global.amdb = client.db('amdb').collection('maindb')
global.utrdb = client.db('amdb').collection('utrdb')
} catch (err) {
console.log('ERR when connect to AMDB')
console.log(err)
process.exit(1)
}
global.amdb.createIndex(
{
// _id: -1,
roomid: -1,
livets: -1
// ts: -1
},
(e, s) => {}
)
global.amdb.createIndex(
{
_id: -1
},
(e, s) => {}
)
global.amdb.createIndex(
{
roomid: -1
},
(e, s) => {}
)
global.amdb.createIndex(
{
ts: -1
},
(e, s) => {}
)
global.utrdb.createIndex(
{
uname: -1
},
(e, s) => {}
)
// global.amdb.createIndex(
// {
// _id: -1,
// status: -1
// },
// (e, s) => {}
// )

if (!global.amdb) return
const amdb = global.amdb
if (!global.utrdb) return
const utrdb = global.utrdb

// global.adbRunning = false
global.predb = false
global.utrdb = false
// global.rmdb = false
// global.umdb = false
global.usingAtHome = process.env.USINGATHOME

if (!global.predb) return
const predb = global.predb
if (!global.utrdb) return
const utrdb = global.utrdb

@simon300000
Copy link
Member Author

simon300000 commented Oct 31, 2019

建议把这些db独立出一个shared然后在api里面的文件访问ya

@ilharp
Copy link
Collaborator

ilharp commented Nov 1, 2019

好的,之后有时间的话会进行修改。

simon300000 added a commit that referenced this issue Nov 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants