Commit d2ff6879 authored by qinhaitao's avatar qinhaitao

feat: 🎸 游戏数据

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