bug fix
This commit is contained in:
parent
308683cf90
commit
689ad09734
@ -42,6 +42,7 @@ export class ApiServer {
|
|||||||
|
|
||||||
if (process.env.NODE_ENV !== 'production') {
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
this.server.register(require('@fastify/cors'), {})
|
this.server.register(require('@fastify/cors'), {})
|
||||||
|
mongoose.set('debug', true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,8 +88,6 @@ export class ApiServer {
|
|||||||
const options = {
|
const options = {
|
||||||
useNewUrlParser: true,
|
useNewUrlParser: true,
|
||||||
maxPoolSize: 5,
|
maxPoolSize: 5,
|
||||||
keepAlive: true,
|
|
||||||
keepAliveInitialDelay: 300000,
|
|
||||||
useUnifiedTopology: true,
|
useUnifiedTopology: true,
|
||||||
}
|
}
|
||||||
const uri = process.env.DB_MAIN
|
const uri = process.env.DB_MAIN
|
||||||
|
@ -26,4 +26,4 @@ export const STEP_CHEST_RATE = 0.1
|
|||||||
// 宝箱各等级的概率
|
// 宝箱各等级的概率
|
||||||
export const STEP_CHEST_LEVEL = [70, 85, 95, 100]
|
export const STEP_CHEST_LEVEL = [70, 85, 95, 100]
|
||||||
// 低保步数
|
// 低保步数
|
||||||
export const RESET_STEP = 10
|
export const RESET_STEP = 2
|
||||||
|
@ -18,7 +18,9 @@ class BoxController extends BaseController {
|
|||||||
@router('get /api/chest/list')
|
@router('get /api/chest/list')
|
||||||
async chestList(req) {
|
async chestList(req) {
|
||||||
const user = req.user
|
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())
|
return chests.map(chest => chest.toJson())
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -71,46 +73,36 @@ class BoxController extends BaseController {
|
|||||||
const user = req.user
|
const user = req.user
|
||||||
const uid = user.uid
|
const uid = user.uid
|
||||||
|
|
||||||
const session = await mongoose.startSession()
|
// TODO:: 待规则确定后, 检查用户是否符合助力条件
|
||||||
session.startTransaction()
|
const chest = await ActivityChest.findOne({ shareCode: code, activity: user.activity })
|
||||||
try {
|
if (chest.bonusUsers.includes(uid)) {
|
||||||
// TODO:: 待规则确定后, 检查用户是否符合助力条件
|
throw new ZError(10, 'user already enhanced')
|
||||||
const chest = await ActivityChest.findOne({ shareCode: code, activity: user.activity }).session(session)
|
}
|
||||||
if (chest.bonusUsers.includes(uid)) {
|
if (chest.bonusUsers.length >= chest.maxBounsCount) {
|
||||||
throw new ZError(10, 'user already enhanced')
|
throw new ZError(12, 'enhanced times exceed')
|
||||||
}
|
}
|
||||||
if (chest.bonusUsers.length >= chest.maxBounsCount) {
|
if (chest.status === ChestStatusEnum.OPENED) {
|
||||||
throw new ZError(12, 'enhanced times exceed')
|
throw new ZError(13, 'chest already opened')
|
||||||
}
|
}
|
||||||
if (chest.status === ChestStatusEnum.OPENED) {
|
if (chest.status === ChestStatusEnum.LOCKED) {
|
||||||
throw new ZError(13, 'chest already opened')
|
throw new ZError(14, 'chest is locked')
|
||||||
}
|
}
|
||||||
if (chest.status === ChestStatusEnum.LOCKED) {
|
if (chest.user === uid) {
|
||||||
throw new ZError(14, 'chest is locked')
|
throw new ZError(15, 'can not enhance self')
|
||||||
}
|
}
|
||||||
if (chest.user === uid) {
|
const score = Math.floor(Math.random() * (chest.bounsCfg[1] - chest.bounsCfg[0] + 1) + chest.bounsCfg[0])
|
||||||
throw new ZError(15, 'can not enhance self')
|
chest.bonusUsers.push(uid)
|
||||||
}
|
chest.bonusScores.push(score)
|
||||||
const score = Math.floor(Math.random() * (chest.bounsCfg[1] - chest.bounsCfg[0] + 1) + chest.bounsCfg[0])
|
chest.scoreBonus += score
|
||||||
chest.bonusUsers.push(uid)
|
await chest.save()
|
||||||
chest.bonusScores.push(score)
|
const chestsForUser = await ActivityChest.find({ user: uid, activity: user.activity })
|
||||||
chest.scoreBonus += score
|
// 如果用户没有宝箱, 则说明用户是新用户, 生成一个宝箱
|
||||||
await chest.save({ session })
|
if (chestsForUser.length === 0) {
|
||||||
const chestsForUser = await ActivityChest.find({ user: uid, activity: user.activity }).session(session)
|
const newChest = generateNewChest(uid, user.activity, 1, ChestStatusEnum.LOCKED)
|
||||||
// 如果用户没有宝箱, 则说明用户是新用户, 生成一个宝箱
|
await newChest.save()
|
||||||
if (chestsForUser.length === 0) {
|
}
|
||||||
const newChest = generateNewChest(uid, user.activity, 1, ChestStatusEnum.LOCKED)
|
return {
|
||||||
await newChest.save({ session })
|
score,
|
||||||
}
|
|
||||||
await session.commitTransaction()
|
|
||||||
session.endSession()
|
|
||||||
return {
|
|
||||||
score: 1,
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
session.abortTransaction()
|
|
||||||
session.endSession()
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,48 +197,48 @@ class GameController extends BaseController {
|
|||||||
throw new ZError(11, 'invalid step')
|
throw new ZError(11, 'invalid step')
|
||||||
}
|
}
|
||||||
step = parseInt(step)
|
step = parseInt(step)
|
||||||
const session = await mongoose.startSession()
|
// const session = await mongoose.startSession()
|
||||||
session.startTransaction()
|
// session.startTransaction()
|
||||||
try {
|
// try {
|
||||||
const record = await ActivityGame.insertOrUpdate({ user: user.id, activity: user.activity }, {}).session(session)
|
const record = await ActivityGame.insertOrUpdate({ user: user.id, activity: user.activity }, {})
|
||||||
if (MANUAL_OPEN_GAME && record.status === 0) {
|
if (MANUAL_OPEN_GAME && record.status === 0) {
|
||||||
throw new ZError(12, 'map not open')
|
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()
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,14 +21,14 @@ class ActivityGameClass extends BaseModule {
|
|||||||
@prop({ default: 0 })
|
@prop({ default: 0 })
|
||||||
public status: number
|
public status: number
|
||||||
// 拥有的ticket数量
|
// 拥有的ticket数量
|
||||||
@prop()
|
@prop({ default: 0 })
|
||||||
public tickets: number
|
public tickets: number
|
||||||
// 最后一次签到日期
|
// 最后一次签到日期
|
||||||
@prop()
|
@prop()
|
||||||
public lastSignDay: string
|
public lastSignDay: string
|
||||||
|
|
||||||
// 最大宝箱无宝箱步数
|
// 最大宝箱无宝箱步数
|
||||||
@prop()
|
@prop({ default: 0 })
|
||||||
public maxNoChestCount: number
|
public maxNoChestCount: number
|
||||||
|
|
||||||
public toJson() {
|
public toJson() {
|
||||||
|
@ -14,7 +14,7 @@ export const generateNewChest = (uid: string, activity: string, level = 1, statu
|
|||||||
if (!cfg) {
|
if (!cfg) {
|
||||||
throw new ZError(11, 'chest cfg not found')
|
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({
|
let chest = new ActivityChest({
|
||||||
user: uid,
|
user: uid,
|
||||||
activity: activity,
|
activity: activity,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user