feat: static top list for player
This commit is contained in:
1 parent
260007ca5b
commit
6b64955eac
2 files changed
+47
-19
No files matched your search
@@ -85,23 +85,35 @@ export { getTrack, putTrack, updateTrack }
|
||||
* Oldest a vote can be
|
||||
* @type Date
|
||||
*/
|
||||
const timeValid = (() => {
|
||||
const timeValid = (set) => {
|
||||
switch (process.env.TOP_TIME_VALID) {
|
||||
default:
|
||||
case 'period':
|
||||
let now = new Date()
|
||||
return new Date(now.getTime() - process.env.TOP_TIME_VALUE * 1e3)
|
||||
case 'monday':
|
||||
case 'period': // constant period
|
||||
let now = set ?? new Date(),
|
||||
period = process.env.TOP_TIME_VALUE
|
||||
|
||||
if (now.getDay() == 6) {
|
||||
let saturday = new Date(new Date().setDate(now.getDate() - 2))
|
||||
|
||||
saturday.setHours(0, 0, 0, 0)
|
||||
|
||||
return saturday
|
||||
}
|
||||
|
||||
return new Date(now.getTime() - period * 1e3)
|
||||
|
||||
case 'monday': // beginning of the week
|
||||
var monday = new Date()
|
||||
monday.setHours(0, 0, 0)
|
||||
monday.setDate(monday.getDate() - ((monday.getDay() + 6) % 7))
|
||||
|
||||
return monday
|
||||
case 'date':
|
||||
|
||||
case 'date': // fixed date
|
||||
let date = new Date(...JSON.parse(process.env.TOP_TIME_VALUE))
|
||||
return date
|
||||
}
|
||||
})()
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all valid votes for a track
|
||||
@@ -110,7 +122,7 @@ const timeValid = (() => {
|
||||
*/
|
||||
function getVotes(tid) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Vote.find({ tid, createdAt: { $gt: timeValid } })
|
||||
Vote.find({ tid, createdAt: { $gt: timeValid() } })
|
||||
.then((votes) => {
|
||||
resolve(votes)
|
||||
})
|
||||
@@ -138,6 +150,7 @@ function countTrackVotes(tid) {
|
||||
resolve({
|
||||
up,
|
||||
down,
|
||||
total: up - down,
|
||||
})
|
||||
})
|
||||
.catch((err) => reject(err))
|
||||
@@ -156,7 +169,7 @@ function getUserVote(tid, uid) {
|
||||
tid,
|
||||
uid,
|
||||
$or: [{ value: 'up' }, { value: 'down' }],
|
||||
createdAt: { $gt: timeValid },
|
||||
createdAt: { $gt: timeValid() },
|
||||
})
|
||||
.then((vote) => resolve(vote))
|
||||
.catch((err) => reject(err))
|
||||
@@ -207,10 +220,10 @@ function updateUserVote(tid, uid, value) {
|
||||
* Count votes per track and sort
|
||||
* @returns {Promise<[]>} sorted array of Track ids
|
||||
*/
|
||||
function countAllVotes() {
|
||||
function countAllVote2s(from) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Vote.find({
|
||||
createdAt: { $gt: timeValid },
|
||||
createdAt: { $gt: timeValid(from), $lt: from ?? new Date() },
|
||||
$or: [{ value: 'up' }, { value: 'down' }],
|
||||
})
|
||||
.then((votes) => {
|
||||
|
||||
+23
-8
@@ -1,6 +1,7 @@
|
||||
import express from 'express'
|
||||
|
||||
import db from '../db.js'
|
||||
import { countAllVotes } from '../mongo.js'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
@@ -79,15 +80,29 @@ router.put(
|
||||
}
|
||||
)
|
||||
|
||||
router.get('/get/top/', ({ headers: { authorization } }, res) => {
|
||||
if (authorization === undefined)
|
||||
return res.status(401).end('Authorization required')
|
||||
router.get(
|
||||
'/get/top/',
|
||||
({ query: { mode = 'once' }, headers: { authorization } }, res) => {
|
||||
if (authorization === undefined)
|
||||
return res.status(401).end('Authorization required')
|
||||
|
||||
if (authorization != playerSecret) return res.status(403).end('Token invalid')
|
||||
if (authorization != playerSecret)
|
||||
return res.status(403).end('Token invalid')
|
||||
|
||||
db.top.get().then(({ top }) => {
|
||||
res.json(top)
|
||||
})
|
||||
})
|
||||
if (mode === 'once') {
|
||||
let morning = new Date()
|
||||
|
||||
morning.setHours(8, 0, 0, 0)
|
||||
|
||||
countAllVotes().then((tids) => {
|
||||
res.json(tids)
|
||||
})
|
||||
} else if ((mode = 'rolling')) {
|
||||
db.top.get().then(({ top }) => {
|
||||
res.json(top)
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export default router
|
||||
Reference in new issue
Block a user