152 lines
3.9 KiB
JavaScript
152 lines
3.9 KiB
JavaScript
const app = getApp()
|
|
|
|
Page({
|
|
data: {
|
|
matchCode: '',
|
|
match: {
|
|
players: []
|
|
},
|
|
myPlayer: null,
|
|
currentGame: null,
|
|
myScoreInput: '',
|
|
opponentScoreInput: ''
|
|
},
|
|
|
|
onLoad(options) {
|
|
this.setData({ matchCode: options.code })
|
|
this.fetchMatchDetail()
|
|
},
|
|
|
|
onPullDownRefresh() {
|
|
this.fetchMatchDetail().then(() => {
|
|
wx.stopPullDownRefresh()
|
|
})
|
|
},
|
|
|
|
bindMyScoreInput(e) {
|
|
this.setData({ myScoreInput: e.detail.value })
|
|
},
|
|
|
|
bindOpponentScoreInput(e) {
|
|
this.setData({ opponentScoreInput: e.detail.value })
|
|
},
|
|
|
|
async handleSubmitScore() {
|
|
const { myScoreInput, opponentScoreInput, currentGame } = this.data
|
|
|
|
if (!myScoreInput || !opponentScoreInput) {
|
|
wx.showToast({ title: '请输入比分', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
if (!currentGame) return
|
|
|
|
wx.showLoading({ title: '提交中...' })
|
|
try {
|
|
if (!this.data.match || !this.data.match.id) {
|
|
throw new Error('比赛信息缺失')
|
|
}
|
|
|
|
await app.request('/api/match/ranking/submit-score', {
|
|
match_id: this.data.match.id,
|
|
my_score: parseInt(myScoreInput),
|
|
opponent_score: parseInt(opponentScoreInput)
|
|
}, 'POST')
|
|
|
|
wx.showToast({ title: '已提交,等待确认' })
|
|
this.fetchCurrentGame()
|
|
} catch (e) {
|
|
console.error('提交比分失败:', e)
|
|
wx.showToast({ title: '提交失败,请重试', icon: 'none' })
|
|
} finally {
|
|
wx.hideLoading()
|
|
}
|
|
},
|
|
|
|
async handleConfirmScore() {
|
|
if (!this.data.currentGame) return
|
|
|
|
wx.showModal({
|
|
title: '确认比分',
|
|
content: '确认比分无误并结束本局?',
|
|
success: async (res) => {
|
|
if (res.confirm) {
|
|
wx.showLoading({ title: '确认中...' })
|
|
try {
|
|
await app.request('/api/match/ranking/confirm-score', {
|
|
game_id: this.data.currentGame.id,
|
|
confirm: true
|
|
}, 'POST')
|
|
|
|
wx.showToast({ title: '已确认' })
|
|
// 刷新页面以获取最新状态(包括新对局)
|
|
this.fetchMatchDetail()
|
|
} catch (e) {
|
|
console.error('确认比分失败:', e)
|
|
} finally {
|
|
wx.hideLoading()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
async handleDisputeScore() {
|
|
if (!this.data.currentGame) return
|
|
|
|
wx.showModal({
|
|
title: '标记争议',
|
|
content: '标记为有争议将重置本局比分,是否继续?',
|
|
success: async (res) => {
|
|
if (res.confirm) {
|
|
wx.showLoading({ title: '提交中...' })
|
|
try {
|
|
await app.request('/api/match/ranking/confirm-score', {
|
|
game_id: this.data.currentGame.id,
|
|
confirm: false
|
|
}, 'POST')
|
|
|
|
wx.showToast({ title: '已标记争议' })
|
|
this.fetchCurrentGame()
|
|
} catch (e) {
|
|
console.error('提交失败:', e)
|
|
} finally {
|
|
wx.hideLoading()
|
|
}
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
async fetchMatchDetail() {
|
|
try {
|
|
const res = await app.request(`/api/match/ranking/${this.data.matchCode}`)
|
|
this.setData({ match: res.data })
|
|
|
|
// 找到我的参赛信息
|
|
const ladderUser = app.globalData.ladderUser
|
|
if (ladderUser) {
|
|
// 使用 == 比较,避免类型不一致问题
|
|
const myPlayer = res.data.players.find(p => p.ladderUserId == ladderUser.id)
|
|
this.setData({ myPlayer })
|
|
|
|
// 获取当前对局
|
|
if (myPlayer && myPlayer.status === 'playing') {
|
|
this.fetchCurrentGame()
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.error('获取排位赛详情失败:', e)
|
|
}
|
|
},
|
|
|
|
async fetchCurrentGame() {
|
|
try {
|
|
const res = await app.request(`/api/match/ranking/${this.data.matchCode}/my-game`)
|
|
this.setData({ currentGame: res.data.currentGame })
|
|
} catch (e) {
|
|
console.error('获取当前对局失败:', e)
|
|
}
|
|
}
|
|
})
|