增加单人和调整超越人数的返回
This commit is contained in:
parent
8cff1cd12a
commit
502253c369
@ -114,6 +114,7 @@
|
|||||||
result: 1, //答题结果 1: 正确, 0 : 错误
|
result: 1, //答题结果 1: 正确, 0 : 错误
|
||||||
gameResult: 0, // 当前局游戏结果 0: 未完成, -1: 失败, 1: 胜利
|
gameResult: 0, // 当前局游戏结果 0: 未完成, -1: 失败, 1: 胜利
|
||||||
answer: '正确答案',
|
answer: '正确答案',
|
||||||
|
over: 0.1222, // 超越玩家值, 显示时x100
|
||||||
"stats": { // 当局的状态
|
"stats": { // 当局的状态
|
||||||
"1111": {
|
"1111": {
|
||||||
"answer": {// 每一题的结果, key为题目的id, 值为answer在答案中的index值, 0为正确值, -1为超时, 其他都为错误
|
"answer": {// 每一题的结果, key为题目的id, 值为answer在答案中的index值, 0为正确值, -1为超时, 其他都为错误
|
||||||
@ -303,6 +304,7 @@
|
|||||||
gameResult: 0, // 当前局游戏结果 0: 未完成, -1: 失败, 1: 胜利
|
gameResult: 0, // 当前局游戏结果 0: 未完成, -1: 失败, 1: 胜利
|
||||||
overtime: 0, // 当前回答是否超时 0: 未超时, 1: 超时
|
overtime: 0, // 当前回答是否超时 0: 未超时, 1: 超时
|
||||||
answer: '正确答案',
|
answer: '正确答案',
|
||||||
|
over: 0.1222, // 超越玩家值, 显示时x100
|
||||||
"stats": { // 当局的状态
|
"stats": { // 当局的状态
|
||||||
"1111": {
|
"1111": {
|
||||||
"answer": {// 每一题的结果, key为题目的id, 值为answer在答案中的index值, 0为正确值, -1为超时, 其他都为错误
|
"answer": {// 每一题的结果, key为题目的id, 值为answer在答案中的index值, 0为正确值, -1为超时, 其他都为错误
|
||||||
|
@ -174,7 +174,7 @@ class ExamController extends BaseController {
|
|||||||
session: history.id,
|
session: history.id,
|
||||||
}
|
}
|
||||||
await history.save()
|
await history.save()
|
||||||
const { total } = await updateExamRank(rankObj)
|
const { total, over } = await updateExamRank(rankObj)
|
||||||
let { rankList, userRank, rankTotal } = await getRank({
|
let { rankList, userRank, rankTotal } = await getRank({
|
||||||
shop: history.shop,
|
shop: history.shop,
|
||||||
level: history.activityId,
|
level: history.activityId,
|
||||||
@ -183,6 +183,7 @@ class ExamController extends BaseController {
|
|||||||
skip: 0,
|
skip: 0,
|
||||||
limit: 20,
|
limit: 20,
|
||||||
})
|
})
|
||||||
|
rspData.over = over
|
||||||
rspData.rankList = rankList
|
rspData.rankList = rankList
|
||||||
rspData.userRank = userRank
|
rspData.userRank = userRank
|
||||||
rspData.rankTotal = rankTotal
|
rspData.rankTotal = rankTotal
|
||||||
|
@ -162,7 +162,8 @@ class PuzzleController extends BaseController {
|
|||||||
mode: 0,
|
mode: 0,
|
||||||
session: history.id,
|
session: history.id,
|
||||||
}
|
}
|
||||||
await updateSingleRank(rankObj)
|
const { over } = await updateSingleRank(rankObj)
|
||||||
|
rspData.over = over
|
||||||
}
|
}
|
||||||
history.markModified('members')
|
history.markModified('members')
|
||||||
await history.save()
|
await history.save()
|
||||||
|
@ -193,6 +193,17 @@ export class RedisClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async zcount(key: string, min: number, max: number) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.pub.zcount(key, min, max, (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
return reject(err)
|
||||||
|
}
|
||||||
|
resolve(data)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
public async zrevrank(key: string, member: string) {
|
public async zrevrank(key: string, member: string) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.pub.zrevrank(key, member, (err, data) => {
|
this.pub.zrevrank(key, member, (err, data) => {
|
||||||
|
@ -169,7 +169,7 @@ export async function updateSingleRank({
|
|||||||
session: string
|
session: string
|
||||||
}) {
|
}) {
|
||||||
const key = rankKey(shop, level, mode)
|
const key = rankKey(shop, level, mode)
|
||||||
await updateRank(accountId, score, key)
|
const { over } = await updateRank(accountId, score, key)
|
||||||
|
|
||||||
let record = (await PuzzleRank.findOrCreate({ shop, level, accountId, mode })).doc
|
let record = (await PuzzleRank.findOrCreate({ shop, level, accountId, mode })).doc
|
||||||
if (record.score < score) {
|
if (record.score < score) {
|
||||||
@ -180,6 +180,7 @@ export async function updateSingleRank({
|
|||||||
let scoreTotal = await PuzzleRank.fetchTotalScore(shop, accountId, mode)
|
let scoreTotal = await PuzzleRank.fetchTotalScore(shop, accountId, mode)
|
||||||
const totalKey = rankKey(shop, 'total', mode)
|
const totalKey = rankKey(shop, 'total', mode)
|
||||||
await updateRank(accountId, scoreTotal, totalKey)
|
await updateRank(accountId, scoreTotal, totalKey)
|
||||||
|
return { over }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateExamRank({
|
export async function updateExamRank({
|
||||||
@ -198,10 +199,10 @@ export async function updateExamRank({
|
|||||||
session: string
|
session: string
|
||||||
}) {
|
}) {
|
||||||
const singleKey = rankKey(shop, level, mode)
|
const singleKey = rankKey(shop, level, mode)
|
||||||
let single = await updateRank(accountId, score, singleKey)
|
let { scoreReal, over } = await updateRank(accountId, score, singleKey)
|
||||||
const totalKey = rankKey(shop, level + '_total', mode)
|
const totalKey = rankKey(shop, level + '_total', mode)
|
||||||
let total = await updateTotalRank(accountId, score, totalKey)
|
let total = await updateTotalRank(accountId, score, totalKey)
|
||||||
return { single, total }
|
return { single: scoreReal, total, over }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getRank({
|
export async function getRank({
|
||||||
|
@ -19,11 +19,30 @@ function generateRankKey(subKey?: string) {
|
|||||||
*/
|
*/
|
||||||
export async function updateRank(accountId: string, score: number, subKey?: string) {
|
export async function updateRank(accountId: string, score: number, subKey?: string) {
|
||||||
let scoreOld = await getAccountScore(accountId, subKey)
|
let scoreOld = await getAccountScore(accountId, subKey)
|
||||||
|
let scoreL = parseFloat(`${score | 0}.${MAX_TIME - Date.now()}`)
|
||||||
|
const key = generateRankKey(subKey)
|
||||||
if (score > scoreOld) {
|
if (score > scoreOld) {
|
||||||
let scoreL = parseFloat(`${score | 0}.${MAX_TIME - Date.now()}`)
|
await new RedisClient().zadd(key, scoreL, accountId)
|
||||||
await new RedisClient().zadd(generateRankKey(subKey), scoreL, accountId)
|
|
||||||
}
|
}
|
||||||
return Math.max(score, scoreOld)
|
const countTotal = (await new RedisClient().zcard(key)) as number
|
||||||
|
const countOver = (await new RedisClient().zcount(key, 0, scoreL)) as number
|
||||||
|
const over = countTotal ? countOver / countTotal : 0.99
|
||||||
|
return { scoreReal: Math.max(score, scoreOld), over }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取超越只
|
||||||
|
* @param {string} accountId
|
||||||
|
* @param {number} score
|
||||||
|
* @param {string} subKey
|
||||||
|
* @return {Promise<number>}
|
||||||
|
*/
|
||||||
|
export async function currentIdx(accountId: string, score: number, subKey?: string) {
|
||||||
|
const key = generateRankKey(subKey)
|
||||||
|
let countTotal = (await new RedisClient().zcard(key)) as number
|
||||||
|
let scoreL = parseFloat(`${score | 0}.${MAX_TIME - Date.now()}`)
|
||||||
|
let countOver = (await new RedisClient().zcount(key, 0, scoreL)) as number
|
||||||
|
return countOver / countTotal
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user