diff --git a/src/api.server.ts b/src/api.server.ts index 615b0df..834e1ad 100644 --- a/src/api.server.ts +++ b/src/api.server.ts @@ -42,6 +42,7 @@ export class ApiServer { if (process.env.NODE_ENV !== 'production') { this.server.register(require('@fastify/cors'), {}) + mongoose.set('debug', true) } } @@ -87,8 +88,6 @@ export class ApiServer { const options = { useNewUrlParser: true, maxPoolSize: 5, - keepAlive: true, - keepAliveInitialDelay: 300000, useUnifiedTopology: true, } const uri = process.env.DB_MAIN diff --git a/src/common/Constants.ts b/src/common/Constants.ts index ec0ae7c..7506a3d 100644 --- a/src/common/Constants.ts +++ b/src/common/Constants.ts @@ -26,4 +26,4 @@ export const STEP_CHEST_RATE = 0.1 // 宝箱各等级的概率 export const STEP_CHEST_LEVEL = [70, 85, 95, 100] // 低保步数 -export const RESET_STEP = 10 +export const RESET_STEP = 2 diff --git a/src/controllers/chest.controller.ts b/src/controllers/chest.controller.ts index 85c4898..06dd547 100644 --- a/src/controllers/chest.controller.ts +++ b/src/controllers/chest.controller.ts @@ -18,7 +18,9 @@ class BoxController extends BaseController { @router('get /api/chest/list') async chestList(req) { const user = req.user - const chests = await ActivityChest.find({ activity: user.activity, user, status: 0 }) + const chests = await ActivityChest.find({ activity: user.activity, user: user.id, status: { $in: [0, 1] } }).sort({ + level: -1, + }) return chests.map(chest => chest.toJson()) } /** @@ -71,46 +73,36 @@ class BoxController extends BaseController { const user = req.user const uid = user.uid - const session = await mongoose.startSession() - session.startTransaction() - try { - // TODO:: 待规则确定后, 检查用户是否符合助力条件 - const chest = await ActivityChest.findOne({ shareCode: code, activity: user.activity }).session(session) - if (chest.bonusUsers.includes(uid)) { - throw new ZError(10, 'user already enhanced') - } - if (chest.bonusUsers.length >= chest.maxBounsCount) { - throw new ZError(12, 'enhanced times exceed') - } - if (chest.status === ChestStatusEnum.OPENED) { - throw new ZError(13, 'chest already opened') - } - if (chest.status === ChestStatusEnum.LOCKED) { - throw new ZError(14, 'chest is locked') - } - if (chest.user === uid) { - 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({ session }) - const chestsForUser = await ActivityChest.find({ user: uid, activity: user.activity }).session(session) - // 如果用户没有宝箱, 则说明用户是新用户, 生成一个宝箱 - if (chestsForUser.length === 0) { - const newChest = generateNewChest(uid, user.activity, 1, ChestStatusEnum.LOCKED) - await newChest.save({ session }) - } - await session.commitTransaction() - session.endSession() - return { - score: 1, - } - } catch (error) { - session.abortTransaction() - session.endSession() - throw error + // TODO:: 待规则确定后, 检查用户是否符合助力条件 + const chest = await ActivityChest.findOne({ shareCode: code, activity: user.activity }) + if (chest.bonusUsers.includes(uid)) { + throw new ZError(10, 'user already enhanced') + } + if (chest.bonusUsers.length >= chest.maxBounsCount) { + throw new ZError(12, 'enhanced times exceed') + } + if (chest.status === ChestStatusEnum.OPENED) { + throw new ZError(13, 'chest already opened') + } + if (chest.status === ChestStatusEnum.LOCKED) { + throw new ZError(14, 'chest is locked') + } + if (chest.user === uid) { + 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() + const chestsForUser = await ActivityChest.find({ user: uid, activity: user.activity }) + // 如果用户没有宝箱, 则说明用户是新用户, 生成一个宝箱 + if (chestsForUser.length === 0) { + const newChest = generateNewChest(uid, user.activity, 1, ChestStatusEnum.LOCKED) + await newChest.save() + } + return { + score, } } diff --git a/src/controllers/game.controller.ts b/src/controllers/game.controller.ts index 3efabf7..489e1db 100644 --- a/src/controllers/game.controller.ts +++ b/src/controllers/game.controller.ts @@ -197,48 +197,48 @@ class GameController extends BaseController { throw new ZError(11, 'invalid step') } step = parseInt(step) - const session = await mongoose.startSession() - session.startTransaction() - try { - const record = await ActivityGame.insertOrUpdate({ user: user.id, activity: user.activity }, {}).session(session) - if (MANUAL_OPEN_GAME && record.status === 0) { - throw new ZError(12, 'map not open') - } - if (record.tickets < step) { - throw new ZError(13, 'insufficient tickets') - } - record.tickets -= step - const ticketRecord = new TicketRecord({ - user: user.id, - activity: user.activity, - type: USE_TICKET, - data: {}, - score: -step, - }) - let { score, chests } = generateStepReward(user.id, user.activity, step) - if (chests.length > 0) { - record.maxNoChestCount = 0 - } else { - record.maxNoChestCount += step - if (record.maxNoChestCount >= RESET_STEP) { - record.maxNoChestCount = 0 - const level = generateChestLevel() - chests.push(generateNewChest(user.id, user.activity, level, ChestStatusEnum.NORMAL)) - } - } - for (let chest of chests) { - await chest.save({ session }) - } - await ticketRecord.save({ session }) - await record.save() - await session.commitTransaction() - - return { score, chests: chests.map(chest => chest.toJson()) } - } catch (e) { - session.abortTransaction() - throw e - } finally { - session.endSession() + // const session = await mongoose.startSession() + // session.startTransaction() + // try { + const record = await ActivityGame.insertOrUpdate({ user: user.id, activity: user.activity }, {}) + if (MANUAL_OPEN_GAME && record.status === 0) { + throw new ZError(12, 'map not open') } + if (record.tickets < step) { + throw new ZError(13, 'insufficient tickets') + } + record.tickets -= step + const ticketRecord = new TicketRecord({ + user: user.id, + activity: user.activity, + type: USE_TICKET, + data: {}, + score: -step, + }) + let { score, chests } = generateStepReward(user.id, user.activity, step) + if (chests.length > 0) { + record.maxNoChestCount = 0 + } else { + record.maxNoChestCount += step + if (record.maxNoChestCount >= RESET_STEP) { + record.maxNoChestCount = 0 + const level = generateChestLevel() + chests.push(generateNewChest(user.id, user.activity, level, ChestStatusEnum.NORMAL)) + } + } + 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()) } + // } catch (e) { + // session.abortTransaction() + // session.endSession() + // throw e + // } } } diff --git a/src/models/ActivityGame.ts b/src/models/ActivityGame.ts index f76fccb..eeb50a5 100644 --- a/src/models/ActivityGame.ts +++ b/src/models/ActivityGame.ts @@ -21,14 +21,14 @@ class ActivityGameClass extends BaseModule { @prop({ default: 0 }) public status: number // 拥有的ticket数量 - @prop() + @prop({ default: 0 }) public tickets: number // 最后一次签到日期 @prop() public lastSignDay: string // 最大宝箱无宝箱步数 - @prop() + @prop({ default: 0 }) public maxNoChestCount: number public toJson() { diff --git a/src/services/game.svr.ts b/src/services/game.svr.ts index 669d6dc..582d6e7 100644 --- a/src/services/game.svr.ts +++ b/src/services/game.svr.ts @@ -14,7 +14,7 @@ export const generateNewChest = (uid: string, activity: string, level = 1, statu if (!cfg) { throw new ZError(11, 'chest cfg not found') } - let scoreInit = Math.floor(Math.random() * (cfg.scoreMax - cfg.scoreMin + 1) + cfg.scoreMin) + let scoreInit = Math.floor(Math.random() * (cfg.initScoreMax - cfg.initScoreMin + 1) + cfg.initScoreMin) let chest = new ActivityChest({ user: uid, activity: activity,