Commit d2ff6879 authored by qinhaitao's avatar qinhaitao

feat: 🎸 游戏数据

parent 8ac9ef94
No preview for this file type
......@@ -155,7 +155,8 @@
{
"type": "common",
"behave": "check",
"name": "checkActivityTime"
"name": "checkActivityTime",
},
{
"type": "common",
......@@ -166,6 +167,12 @@
"type": "common",
"behave": "check",
"name": "checkExchangeCreditsTask"
},
{
"type": "common",
"behave": "check",
"name": "checkUserInfo",
"params": 3
}
]
}
......
......@@ -10,10 +10,7 @@ function getDecorators(controller, decoratorType) {
function getPreChecks(controller, checkType) {
checkType = checkType || 'common'
let decorators = []
decorators = [...decorators, ...Object.values(controller).map(v => v.decorators)].reduce(
(r, item) => r.concat(item),
[]
)
decorators = [...decorators, ...Object.values(controller).map(v => v.decorators)].flat()
const preChecks = [...decorators.filter(v => v.type === 'common' && v.name === 'preCheck').map(v => v.params)].flat()
return [...new Set(preChecks.filter(v => v.type === checkType && v.behave === 'check').map(v => v.name))]
......@@ -22,10 +19,7 @@ function getPreChecks(controller, checkType) {
function getPreUpdates(controller, updateType) {
updateType = updateType || 'common'
let decorators = []
decorators = [...decorators, ...Object.values(controller).map(v => v.decorators)].reduce(
(r, item) => r.concat(item),
[]
)
decorators = [...decorators, ...Object.values(controller).map(v => v.decorators)].flat()
const preUpdates = [
...decorators.filter(v => v.type === 'common' && v.name === 'preUpdate').map(v => v.params)
].flat()
......
......@@ -3,8 +3,8 @@
import { services, checkParams, preCheck, preUpdate } from '../decorator/common'
import { resultsModel } from '../sdk'
import { CommonUserService, CommonGameService } from '../service/common'
import { checkActivityTime, checkJoinId, checkUserInfo, checkVip } from '../utils/common/check'
import { updateUserInfo } from '../utils/common/update'
import { checkActivityTime, checkJoinId, checkUserInfo, checkVip, checkGameTimes } from '../utils/common/check'
import { updateUserInfo, reduceGameTimes } from '../utils/common/update'
export interface ISumitGameControllerInfos extends IControllerInfos {
joinInfo: IJoinRecord
......@@ -32,13 +32,8 @@ export default class Game {
@checkParams(['activityId'])
@services([CommonGameService])
@preCheck([checkActivityTime, checkVip, checkUserInfo({ gameTimes: { $gte: 3 } }, '游戏次数不足,做点任务吧')])
@preUpdate([
updateUserInfo({
$where: 'this.gameTimes>3',
$inc: { gameTimes: -3 }
})
])
@preCheck([checkActivityTime, checkVip, checkGameTimes(3)])
@preUpdate([reduceGameTimes(3)])
async startGame(
context: IContext<IParams>,
{ userInfo, activityInfo }: IControllerInfos,
......
......@@ -42,7 +42,7 @@ export default class Task {
* 商品相关任务 需要传itemId参数
* 积分兑换任务 需要传credits参数
*/
@checkParams(['activityId', 'taskType'])
@checkParams(['activityId', 'taskType', 'itemId?', 'credits?'])
@registeInfos(['session'])
@services([CommonTaskService])
@preCheck([checkActivityTime, checkTaskLimit, checkExchangeCreditsTask])
......
......@@ -38,10 +38,10 @@ export default class User {
/**
* 获取排行榜
*/
@checkParams(['activityId', 'key', 'timeKey'])
@checkParams(['activityId', 'key?', 'timeKey?', 'limit?'])
@services([CommonUserService])
async getRankList(context: IContext<IParams>, { userInfo }: IControllerInfos, [userService]: [CommonUserService]) {
const { key, timeKey, limit = 200 } = context.data
const { key = 'score', timeKey = 'updateScoreTime', limit = 200 } = context.data
const rankInfo = await userService.getMyRankInfo(key, timeKey, userInfo)
......
......@@ -2,14 +2,25 @@
import { resultsModel } from '../../sdk'
import { CODE_TYPES } from '../../constants'
import { logFn } from '../../utils'
// 检验参数是否存在
export default function checkParams(params: string[]) {
/**
* 检查接口参数
*
* @checkParams(['activityId', 'itemId?'])
*
* itemId? 问号结尾为可选参数 不做校验
*
* @export
* @param {string[]} params 参数列表
* @return {*}
*/
export default function checkParams(params: string[]): IFunction {
return function (target: Object, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
descriptor.value = function (...args: any[]) {
const [context, otherArgs = {}] = args
const dataKeys = Object.keys(context.data)
const [context, otherArgs = {}, services = [], preCheckData = {}] = args
const dataKeys = Object.keys(context.data).filter(v => !v.endsWith('?'))
for (let i = 0; i < params.length; i++) {
const param = params[i]
if (!dataKeys.includes(param) || context.data?.[param] === '') {
......@@ -17,7 +28,7 @@ export default function checkParams(params: string[]) {
return resultsModel.error(CODE_TYPES.PARAMS_ERROR, `缺少${param}参数`)
}
}
return method.apply(target, [context, { ...otherArgs }])
return method.apply(target, [context, { ...otherArgs }, services, preCheckData])
}
}
}
/** @format */
/**
*
* 默认接口最外层会tryCatch 某些特殊接口需要自定义tryCatch 可以使用该装饰器
*
* @export
* @param {*} target
* @param {string} name
* @param {PropertyDescriptor} descriptor
*/
export default function noCatch(target: any, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
descriptor.value = async function (...args: any[]) {
......
/** @format */
/**
*
* 默认所用接口都会校验活动信息和用户信息
* 不需要检查用户信息的使用noCheckUser装饰器 如:login getActivityBaseInfoById
*
* @export
* @param {*} target
* @param {string} name
* @param {PropertyDescriptor} descriptor
*/
export default function noCheckUser(target: any, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
descriptor.value = async function (...args: any[]) {
......
/** @format */
// 检验参数是否存在
/**
* 前置检查
*
* @export
* @param {IFunction[]} checks
* @return {*}
*/
export default function preCheck(checks: IFunction[]) {
return function (target: Object, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
......
......@@ -8,7 +8,7 @@ import { formatUpdatedDataByProjection, formatUpdateUserProjection } from '../..
import { dbUpdate } from '../../utils/common/mongodb'
import { USER_DB_NAME } from '../../db'
// 检验参数是否存在
// 前置更新
export default function preUpdate(checks: IFunction[]) {
return function (target: Object, name: string, descriptor: PropertyDescriptor) {
const method = descriptor.value
......
......@@ -31,6 +31,10 @@ export default function checkUserInfo(gameLimit: IGameLimit, errorMsg: string) {
}
}
export function checkGameTimes(count: number) {
return checkUserInfo({ gameTimes: { $gte: count } }, '游戏次数不足')
}
function validateData(data: any = {}, condition: IGameLimit) {
let allValid = true
Object.keys(condition).forEach(key => {
......
......@@ -9,7 +9,7 @@ import checkRemainTimes from './checkRemainTimes'
import checkTaskLimit from './checkTaskLimit'
import checkValidPrize, { checkValidEnamePrize, checkValidObjectPrize } from './checkValidPrize'
import checkVip from './checkVip'
import checkUserInfo from './checkUserInfo'
import checkUserInfo, { checkGameTimes } from './checkUserInfo'
import checkJoinId from './checkJoinId'
const check = {
......@@ -26,7 +26,8 @@ const check = {
checkValidObjectPrize,
checkVip,
checkUserInfo,
checkJoinId
checkJoinId,
checkGameTimes
}
export default check
......@@ -45,5 +46,6 @@ export {
checkValidObjectPrize,
checkVip,
checkUserInfo,
checkJoinId
checkJoinId,
checkGameTimes
}
......@@ -3,16 +3,17 @@ import updateVip from './updateVip'
import updateSignTask from './updateSignTask'
import updateOrderGoods from './updateOrderGoods'
import updateFirstLoginToday from './updateFirstLoginToday'
import updateUserInfo from './updateUserInfo'
import updateUserInfo, { reduceGameTimes } from './updateUserInfo'
const update = {
updateVip,
updateSignTask,
updateOrderGoods,
updateFirstLoginToday,
updateUserInfo
updateUserInfo,
reduceGameTimes
}
export default update
export { updateVip, updateSignTask, updateOrderGoods, updateFirstLoginToday, updateUserInfo }
export { updateVip, updateSignTask, updateOrderGoods, updateFirstLoginToday, updateUserInfo, reduceGameTimes }
......@@ -12,3 +12,10 @@ export default function updateUserInfo(update?: IPreUpdateQuery) {
return update
}
}
export function reduceGameTimes(gameTimes: number) {
return updateUserInfo({
$where: `this.gameTimes >= ${gameTimes}`,
$inc: { gameTimes: -gameTimes }
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment