mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Ranklist since parameterá
This commit is contained in:
parent
bd6bf2de62
commit
1aa2dc656c
2 changed files with 91 additions and 20 deletions
|
@ -44,6 +44,7 @@ const quickVoteResultsDir = 'stats/qvote'
|
||||||
const quickVotes = 'stats/qvote/votes.json'
|
const quickVotes = 'stats/qvote/votes.json'
|
||||||
const testUsersFile = 'data/testUsers.json'
|
const testUsersFile = 'data/testUsers.json'
|
||||||
const idStatFile = 'stats/idstats'
|
const idStatFile = 'stats/idstats'
|
||||||
|
const idvStatFile = 'stats/idvstats'
|
||||||
|
|
||||||
// other constants
|
// other constants
|
||||||
const maxVeteranPwGetCount = 10
|
const maxVeteranPwGetCount = 10
|
||||||
|
@ -116,6 +117,19 @@ function GetApp() {
|
||||||
var userSpecificMotd = {}
|
var userSpecificMotd = {}
|
||||||
var testUsers = []
|
var testUsers = []
|
||||||
|
|
||||||
|
function mergeObjSum(a, b) {
|
||||||
|
const res = { ...b }
|
||||||
|
Object.keys(a).forEach((key) => {
|
||||||
|
if (res[key]) {
|
||||||
|
res[key] += a[key]
|
||||||
|
} else {
|
||||||
|
res[key] = a[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
function LoadVersion() {
|
function LoadVersion() {
|
||||||
version = utils.ReadFile(versionFile)
|
version = utils.ReadFile(versionFile)
|
||||||
}
|
}
|
||||||
|
@ -125,7 +139,12 @@ function GetApp() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function LoadUserSpecificMOTD() {
|
function LoadUserSpecificMOTD() {
|
||||||
|
try {
|
||||||
userSpecificMotd = utils.ReadJSON(userSpecificMotdFile)
|
userSpecificMotd = utils.ReadJSON(userSpecificMotdFile)
|
||||||
|
} catch (err) {
|
||||||
|
logger.Log('Couldnt parse user specific motd!', logger.GetColor('redbg'))
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function LoadTestUsers() {
|
function LoadTestUsers() {
|
||||||
|
@ -178,9 +197,11 @@ function GetApp() {
|
||||||
|
|
||||||
app.get('/ranklist', (req, res) => {
|
app.get('/ranklist', (req, res) => {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
|
let since = req.query.since
|
||||||
const user = req.session.user
|
const user = req.session.user
|
||||||
const data = utils.ReadJSON(idStatFile)
|
|
||||||
|
|
||||||
|
if (!since) {
|
||||||
|
const data = utils.ReadJSON(idStatFile)
|
||||||
const list = []
|
const list = []
|
||||||
|
|
||||||
Object.keys(data).forEach((key) => {
|
Object.keys(data).forEach((key) => {
|
||||||
|
@ -201,6 +222,56 @@ function GetApp() {
|
||||||
list: list,
|
list: list,
|
||||||
selfuserId: user.id,
|
selfuserId: user.id,
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
since = new Date(since)
|
||||||
|
if (!(since instanceof Date) || isNaN(since)) {
|
||||||
|
throw new Error('Not a date')
|
||||||
|
}
|
||||||
|
const data = utils.ReadJSON(idvStatFile)
|
||||||
|
const result = {}
|
||||||
|
|
||||||
|
Object.keys(data).forEach((key) => {
|
||||||
|
const dayliStat = data[key]
|
||||||
|
|
||||||
|
if (new Date(key) > since) {
|
||||||
|
Object.keys(dayliStat).forEach((userId) => {
|
||||||
|
const userStat = dayliStat[userId]
|
||||||
|
const uidRes = result[userId]
|
||||||
|
|
||||||
|
if (!uidRes) {
|
||||||
|
result[userId] = userStat
|
||||||
|
} else {
|
||||||
|
result[userId] = {
|
||||||
|
count: uidRes.count + userStat.count,
|
||||||
|
newQuestions: uidRes.newQuestions + userStat.newQuestions,
|
||||||
|
allQuestions: uidRes.allQuestions + userStat.allQuestions,
|
||||||
|
subjs: mergeObjSum(uidRes.subjs, userStat.subjs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const list = []
|
||||||
|
Object.keys(result).forEach((key) => {
|
||||||
|
list.push({
|
||||||
|
userId: parseInt(key),
|
||||||
|
...result[key],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
since: since,
|
||||||
|
list: list,
|
||||||
|
selfuserId: user.id,
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
res.json({
|
||||||
|
msg: 'invalid date format, or other error occured',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/quickvote', (req, res) => {
|
app.get('/quickvote', (req, res) => {
|
||||||
|
|
|
@ -258,9 +258,9 @@ function AddUserIdStat(userId) {
|
||||||
var m = new Date()
|
var m = new Date()
|
||||||
const now =
|
const now =
|
||||||
m.getFullYear() +
|
m.getFullYear() +
|
||||||
'/' +
|
'-' +
|
||||||
('0' + (m.getMonth() + 1)).slice(-2) +
|
('0' + (m.getMonth() + 1)).slice(-2) +
|
||||||
'/' +
|
'-' +
|
||||||
('0' + m.getDate()).slice(-2)
|
('0' + m.getDate()).slice(-2)
|
||||||
if (uvData[now] === undefined) {
|
if (uvData[now] === undefined) {
|
||||||
uvData[now] = {}
|
uvData[now] = {}
|
||||||
|
@ -289,9 +289,9 @@ function AddVisitStat(name) {
|
||||||
var m = new Date()
|
var m = new Date()
|
||||||
const now =
|
const now =
|
||||||
m.getFullYear() +
|
m.getFullYear() +
|
||||||
'/' +
|
'-' +
|
||||||
('0' + (m.getMonth() + 1)).slice(-2) +
|
('0' + (m.getMonth() + 1)).slice(-2) +
|
||||||
'/' +
|
'-' +
|
||||||
('0' + m.getDate()).slice(-2)
|
('0' + m.getDate()).slice(-2)
|
||||||
if (vData[now] === undefined) {
|
if (vData[now] === undefined) {
|
||||||
vData[now] = {}
|
vData[now] = {}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue