diff --git a/src/controllers/chest.controller.ts b/src/controllers/chest.controller.ts index e3a1239..61fb8c4 100644 --- a/src/controllers/chest.controller.ts +++ b/src/controllers/chest.controller.ts @@ -104,10 +104,13 @@ class BoxController extends BaseController { throw new ZError(15, 'can not enhance self') } const score = Math.floor(Math.random() * (chest.bounsCfg[1] - chest.bounsCfg[0] + 1) + chest.bounsCfg[0]) - chest.bonusUsers.push(uid) - chest.bonusScores.push(score) - chest.scoreBonus += score - await chest.save() + await ActivityChest.updateOne( + { _id: chest.id }, + { + $inc: { scoreBonus: score }, + $push: { bonusUsers: uid, bonusScores: score }, + }, + ) const chestsForUser = await ActivityChest.find({ user: uid, activity: user.activity }) // 如果用户没有宝箱, 则说明用户是新用户, 生成一个宝箱 if (chestsForUser.length === 0) { diff --git a/src/controllers/game.controller.ts b/src/controllers/game.controller.ts index 18f8806..e11a8a2 100644 --- a/src/controllers/game.controller.ts +++ b/src/controllers/game.controller.ts @@ -34,8 +34,6 @@ class GameController extends BaseController { throw new ZError(13, 'had not signed in') } const reward = seqSignScore(record.count) - gameRecord.lastSignDay = dateTag - gameRecord.tickets += 1 + reward const ticketRecord = new TicketRecord({ user: user.id, activity: user.activity, @@ -43,8 +41,11 @@ class GameController extends BaseController { data: {}, score: 1 + reward, }) + await ActivityGame.updateOne( + { user: user.id, activity: user.activity }, + { lastSignDay: dateTag, $inc: { tickets: 1 + reward } }, + ) await ticketRecord.save() - await gameRecord.save() return { ticket: reward + 1 } } @@ -97,7 +98,7 @@ class GameController extends BaseController { if (score === 0) { throw new ZError(15, 'invalid days') } - const record = new TicketRecord({ + const ticketRecord = new TicketRecord({ user: user.id, activity: user.activity, type: SIGN_TOTAL, @@ -108,9 +109,8 @@ class GameController extends BaseController { if (MANUAL_OPEN_GAME && gameRecord.status === 0) { throw new ZError(12, 'map not open') } - gameRecord.tickets += score - await gameRecord.save() - await record.save() + await ActivityGame.updateOne({ user: user.id, activity: user.activity }, { $inc: { tickets: score } }) + await ticketRecord.save() return { ticket: score } } /** @@ -209,7 +209,6 @@ class GameController extends BaseController { if (record.tickets < step) { throw new ZError(13, 'insufficient tickets') } - record.tickets -= step const ticketRecord = new TicketRecord({ user: user.id, activity: user.activity, @@ -217,17 +216,20 @@ class GameController extends BaseController { data: {}, score: -step, }) + let updateData: any = { + $inc: { tickets: -step }, + } let { score, chests } = generateStepReward(user.id, user.activity, step) if (chests.length > 0) { - record.maxNoChestCount = 0 + updateData.maxNoChestCount = 0 } else { - record.maxNoChestCount += step - if (record.maxNoChestCount >= RESET_STEP) { - record.maxNoChestCount = 0 + if (record.maxNoChestCount + step >= RESET_STEP) { + updateData.maxNoChestCount = 0 const level = generateChestLevel() chests.push(generateNewChest(user.id, user.activity, level, ChestStatusEnum.NORMAL)) } } + await ActivityGame.updateOne({ user: user.id, activity: user.activity }, updateData) await updateRankScore({ user: user.id, score: score, @@ -237,12 +239,12 @@ class GameController extends BaseController { step: step, }, }) + // batch save chests for (let chest of chests) { await chest.save() } await ticketRecord.save() // await ticketRecord.save({ session }) - await record.save() // await session.commitTransaction() // session.endSession() return { score, chests: chests.map(chest => chest.toJson()) }