Commit 3858a3a8 authored by qinhaitao's avatar qinhaitao

feat: 🎸 提交接口\

parent 76c36558
......@@ -31,8 +31,16 @@ export default class StatController {
{ activityInfo, userInfo }: IControllerInfos,
[statService]: [StatService]
) {
const commonStatGetstatsResult1 = await statService.getStats(context.data.activityId, context.data.startDay, context.data.endDay)
const commonStatBuildexcelResult2 = await statService.buildExcel(context.data.startDay, context.data.endDay, commonStatGetstatsResult1)
const commonStatGetstatsResult1 = await statService.getStats(
context.data.activityId,
context.data.startDay,
context.data.endDay
)
const commonStatBuildexcelResult2 = await statService.buildExcel(
context.data.startDay,
context.data.endDay,
commonStatGetstatsResult1
)
return resultsModel.success({
...commonStatBuildexcelResult2
})
......
......@@ -35,7 +35,7 @@ export default class Game {
@preCheck([checkActivityTime, checkVip, checkUserInfo({ gameTimes: { $gte: 3 } }, '游戏次数不足,做点任务吧')])
@preUpdate([
updateUserInfo({
$where: 'this.gameTimes >3',
$where: 'this.gameTimes>3',
$inc: { gameTimes: -3 }
})
])
......@@ -60,7 +60,7 @@ export default class Game {
[gameService]: [GameService]
) {
const { id, score } = context.data
const result = await gameService.submitGame(id, score, joinInfo)
const result = await gameService.submitGame(id, score, userInfo, joinInfo)
return resultsModel.success(result)
}
......
......@@ -12,7 +12,7 @@ import GameController from './controller/game.controller'
const { getVipInfo, getRankList } = new UserController()
const { login } = new LoginController()
const { getGameInfo, startGame } = new GameController()
const { getGameInfo, startGame, submitGame } = new GameController()
const {
getTaskList,
receiveTaskRewards,
......@@ -42,6 +42,8 @@ export default {
getStats,
addStat,
getRankList,
// 游戏类
getGameInfo,
startGame
startGame,
submitGame
}
......@@ -9,11 +9,11 @@ import { JOIN_DB_NAME } from '../../db'
import { ACTIVITY_STATUS } from '../../constants'
import { getToday } from '../../utils'
export default class GameService {
export default class GameService extends UserService {
context: IContext<IParams>
joindao: IBaseDao
constructor(context: IContext<IParams>) {
this.context = context
super(context)
this.joindao = new BaseDao(context, JOIN_DB_NAME)
}
......@@ -41,7 +41,28 @@ export default class GameService {
return { id }
}
async submitGame(id: string, score: number, joinInfo: IJoinRecord) {
async submitGame(id: string, score: number, userInfo: IUserInfo, joinInfo: IJoinRecord) {
const { duration } = await this.updateJoinRecord(id, score, userInfo, joinInfo)
const { maxScore, totalScore } = await this.updateUserScore(score, userInfo)
const myRankInfo = await this.getMyRankInfo('maxScore', 'updateScoreTime', userInfo)
const { gameTimes, avatar, userNick } = userInfo
return {
score,
maxScore,
totalScore,
gameTimes,
duration,
avatar,
userNick,
rank: myRankInfo.rank
}
}
async updateJoinRecord(id: string, score: number, userInfo: IUserInfo, joinInfo: IJoinRecord) {
const now = Date.now()
const today = getToday()
const duration = now - joinInfo.createTime
......@@ -53,18 +74,28 @@ export default class GameService {
submitDay: today
}
await this.joindao.update(
{
_id: id
},
{
$set: playInfo
}
)
await this.joindao.update({ _id: id }, { $set: playInfo })
return playInfo
}
async updateUserScore(score: number, userInfo: IUserInfo) {
const now = Date.now()
const newMaxScore = score >= userInfo?.maxScore
const maxScore = newMaxScore ? score : userInfo?.maxScore
const updateScoreTime = newMaxScore ? now : userInfo?.updateScoreTime
const { _id, totalScore } = userInfo
await this.updateUser(_id, {
$set: { maxScore, updateScoreTime },
$inc: { totalScore: score }
})
return {
score,
duration
maxScore,
updateScoreTime,
totalScore: totalScore + score
}
}
}
......@@ -177,7 +177,7 @@ class UserService extends BaseService {
return {
list: list.map((v, i) => {
return {
score: v[sortValueKey],
[sortValueKey]: v[sortValueKey],
avatar: v.avatar,
userNick: formatUserNick(v.userNick),
rank: i + 1
......@@ -222,7 +222,7 @@ class UserService extends BaseService {
rank,
userNick,
avatar,
score: userValue
[sortValueKey]: userValue
}
}
......
......@@ -16,6 +16,11 @@ interface IUserInfo {
remainTimes?: IRemainTimesInfo
taskInfo?: object
login?: object
// 游戏类
gameTimes?: number
maxScore?: number
totalScore?: number
updateScoreTime?: number
}
interface IRemainTimesInfo {
......
......@@ -23,15 +23,15 @@ export function formatUpdateUserProjection(projection: IUpdateQuery): IUpdateQue
}
// 用户昵称脱敏
export function formatUserNick(name: string) {
export function formatUserNick(name: string, replaceStr = '***') {
let newStr: string
if (name.length === 2) {
newStr = name.substr(0, 1) + '***' + name.substr(-1, 1)
newStr = name.substr(0, 1) + replaceStr + name.substr(-1, 1)
} else if (name.length > 2) {
let char = '***'
let char = replaceStr
newStr = name.substr(0, 1) + char + name.substr(-1, 1)
} else {
newStr = name + '***'
newStr = name + replaceStr
}
return newStr
}
......
......@@ -23,6 +23,7 @@
],
"exclude": [
"./src/controller/common/**",
"./src/controller/custom/**",
"./dist/**/*"
]
}
\ No newline at end of file
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