tsghost/src/controllers/rank.controller.ts
2021-08-11 15:42:25 +08:00

101 lines
3.7 KiB
TypeScript

import BaseController from '../common/base.controller'
import { role, router } from '../decorators/router'
import { getAccountRank, getAccountScore, getRankCount, getRankList, rankKey, updateRank } from '../services/rank.svr'
import { RankUser } from '../models/RankUser'
import { fetchRankCfg, randomUsers } from '../services/jcfw.svr'
import RankSchedule from '../schedule/rank.schedule'
class RankController extends BaseController {
@role('anon')
@router('post /api/svr/games/rank')
async reqRankList(req: any) {
let { gameId, channelId, limit, skip, accountId, all, rankType } = req.params
const { min, max, type, valType } = await fetchRankCfg(gameId, channelId)
skip = +skip || 0
limit = +limit || 20
all = all || type > 0
rankType = rankType || rankKey(gameId, channelId, all)
let datas: any = await getRankList(skip, limit, rankType)
const rankList = await RankUser.parseRankList(datas)
const userInfo = await RankUser.getByAccountID(accountId)
let userRank = (await getAccountRank(accountId, rankType)) ?? 999
let userScore = (await getAccountScore(accountId, rankType)) || 0
let rankTotal = await getRankCount(rankType)
return {
nt: 1,
errcode: 0,
errmsg: '',
records: rankList,
total: rankTotal,
userRank,
userScore,
userTitle: userInfo?.rankTitle,
}
}
@role('anon')
@router('post /api/svr/games/rank/update')
async reqUpdateRank(req: any) {
let { gameId, channelId, accountId, score, needType, all, rankTitle, nickname, avatar, extInfo } = req.params
const { min, max, type, valType } = await fetchRankCfg(gameId, channelId)
all = all || type > 0
needType = needType || 0
await fetchRankCfg(gameId, channelId)
const weekKey = rankKey(gameId, channelId, false)
let week = await updateRank(accountId, score, weekKey, valType)
const totalKey = rankKey(gameId, channelId, true)
let total = await updateRank(accountId, score, totalKey, valType)
const key = all ? totalKey : weekKey
const totalCount = all ? total.total : week.total
let userRank = (await getAccountRank(accountId, key)) as number
let userScore = (await getAccountScore(accountId, key)) as number
await RankUser.insertOrUpdate({ gameId, channelId, accountId }, { rankTitle, nickname, avatar, extInfo })
if (!needType) {
return { nt: 1, errcode: 0, errmsg: '', userRank, userScore }
} else {
let preRecord = null
let nextRecord = null
if (userRank > 0) {
let preDatas: any = await getRankList(userRank - 1, userRank, key)
let preList = await RankUser.parseRankList(preDatas)
preRecord = preList[0]
}
if (userRank < totalCount - 1) {
let nextDatas: any = await getRankList(userRank, userRank + 1, key)
let nextList = await RankUser.parseRankList(nextDatas)
nextRecord = nextList[0]
}
return {
nt: 1,
errcode: 0,
errmsg: '',
preRecord: preRecord,
nextRecord: nextRecord,
userRank,
userScore,
userTitle: rankTitle,
over: all ? total.over : week.over,
}
}
}
@role('anon')
@router('get /api/svr/games/rank/challenge/:score/:accountId/:count')
@router('get /api/svr/games/rank/challenge/:score/:accountId')
async challenge(req: any) {
let { score, accountId, count } = req.params
count = count || 10
count = count > 100 ? 100 : count
const users = await randomUsers(accountId, count)
return { nt: 1, errcode: 0, errmsg: '', users: users }
}
@role('anon')
@router('post /api/svr/games/rank/init')
async update(req: any) {
let { gameId, channelId } = req.params
await new RankSchedule().initRankData({ gameId, channelId })
return {}
}
}