Skip to content

Commit

Permalink
fix: 对战过程偶现卡死, 随机匹配再来一局错误, 人机对战偶现重复选择异常
Browse files Browse the repository at this point in the history
  • Loading branch information
arleyGuoLei committed Sep 25, 2022
1 parent eb00535 commit 8817521
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
15 changes: 10 additions & 5 deletions miniprogram/pages/combat/components/npcScene/npcScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import config from './../../../../utils/config'
import KvModel, { INPC } from './../../../../models/kv'
import combatModel from './../../../../models/combat'

const TIMER_NULL = -1

// NOTE: FIX 人机多次选择 2022-09-25
/** npc 自动选择器 */
let npcSelectTimer = TIMER_NULL

App.Component({
data: {
/** 人机是否已经选择,false 为未选 */
npcSelected: false,

/** npc 自动选择器 */
$npcSelectTimer: 0,

/** 当前题目开始时间,用于选择时做分数计算,该值和 pkScene 中的 countDownStartTime 一致 */
startTime: 0,

Expand All @@ -36,13 +39,14 @@ App.Component({
this.data.npcSelected = false
this.data.startTime = Date.now()

this.data.$npcSelectTimer = setTimeout(() => {
npcSelectTimer = setTimeout(() => {
void this.npcSelect()
}, config.minNPCSelectTime + config.NPCSelectMaxGap * Math.random())
},
async npcSelect () {
if (!this.data.npcSelected) {
clearTimeout(this.data.$npcSelectTimer)
clearTimeout(npcSelectTimer)
this.data.npcSelected = true

const id = store.$state.combat?._id as DB.DocumentId
const wordsIndex = store.$state.combat?.wordsIndex!
Expand All @@ -57,6 +61,7 @@ App.Component({

// NOTE: 选择失败,进行重试
if (!isSelect) {
this.data.npcSelected = false
void this.npcSelect()
return
}
Expand Down
2 changes: 1 addition & 1 deletion miniprogram/pages/combat/components/pkScene/pkScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let detectCombatTimer = TIMER_NULL
/** 每题的倒计时开始时间,用于选择时做分数计算 */
let countDownStartTime = 0

let bgm = wx.createInnerAudioContext()
let bgm = wx.createInnerAudioContext({ useWebAudioImplement: true })

App.Component({
data: {
Expand Down
10 changes: 7 additions & 3 deletions miniprogram/pages/combat/components/settleScene/settleScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ App.Component({
const incExperience = this.getIncExperience()
isOwner && combatModel.end(_id)

const isWin = isOwner ? users[0].gradeTotal >= users[1].gradeTotal : users[1].gradeTotal >= users[0].gradeTotal
// NOTE: 对战结算出现 users 长度 < 2 的情况,可能是用户逃离、异常,做兜底处理
const otherUserGradeTotal = (users.length > 1 && users[1].gradeTotal) ? users[1].gradeTotal : 0
const isWin = isOwner ? users[0]?.gradeTotal >= otherUserGradeTotal : otherUserGradeTotal >= users[0]?.gradeTotal

// NOTE: 云端增加词力值
await userModel.incExperience(incExperience, isWin)
Expand Down Expand Up @@ -81,8 +83,10 @@ App.Component({
const { isOwner, _id, type } = store.$state.combat!
const isShareResult = this.data.isShareResult

// 分享的房间点击再来一局 或 房主点击再来一局,将创建一个好友对战的房间
if (isShareResult || isOwner) {
// fix: 修复对战结束后的随机匹配再来一局
// 分享的房间点击再来一局 或 房主点击再来一局,好友对战将创建一个好友对战的房间,随机匹配和人机统一创建一局随机对战
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (isShareResult || isOwner || type === 'random') {
// 结束本局的数据变更监听
this.triggerEvent('onCloseWatcher')
loading.show('创建中')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { store, events } from './../../../../app'
import userModel from './../../../../models/user'
import { playPronunciation } from './../../../../utils/util'

let bgm = wx.createInnerAudioContext()
let bgm = wx.createInnerAudioContext({ useWebAudioImplement: true })

App.Component({
options: {
Expand Down
2 changes: 1 addition & 1 deletion miniprogram/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function chunk<T> (arr: T[], size: number): T[][] {
}

export function playAudio (src: string): void {
const innerAudioContext = wx.createInnerAudioContext()
const innerAudioContext = wx.createInnerAudioContext({ useWebAudioImplement: true })
innerAudioContext.autoplay = true
innerAudioContext.src = src
innerAudioContext.onError((res) => console.log(res))
Expand Down
2 changes: 1 addition & 1 deletion typings/types/wx/lib.wx.api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11792,7 +11792,7 @@ wx.downloadFile({
* 创建内部 [audio](https://developers.weixin.qq.com/miniprogram/dev/component/audio.html) 上下文 [InnerAudioContext](https://developers.weixin.qq.com/miniprogram/dev/api/media/audio/InnerAudioContext.html) 对象。
*
* 最低基础库: `1.6.0` */
createInnerAudioContext(): InnerAudioContext
createInnerAudioContext({useWebAudioImplement}: {useWebAudioImplement?: boolean}): InnerAudioContext
/** [[IntersectionObserver](https://developers.weixin.qq.com/miniprogram/dev/api/wxml/IntersectionObserver.html) wx.createIntersectionObserver(Object component, Object options)](https://developers.weixin.qq.com/miniprogram/dev/api/wxml/wx.createIntersectionObserver.html)
*
* 创建并返回一个 IntersectionObserver 对象实例。在自定义组件或包含自定义组件的页面中,应使用 `this.createIntersectionObserver([options])` 来代替。
Expand Down

0 comments on commit 8817521

Please sign in to comment.