252 lines
9.1 KiB
TypeScript
252 lines
9.1 KiB
TypeScript
import { loadBackendAuthTokens, loadBackendBaseUrl } from '../../utils/backendAuth'
|
|
import { getSessionResult } from '../../utils/backendApi'
|
|
import type { MapEngineResultSnapshot } from '../../engine/map/mapEngine'
|
|
import type { GameLaunchEnvelope } from '../../utils/gameLaunch'
|
|
|
|
type ResultPageData = {
|
|
sessionId: string
|
|
eventId: string
|
|
guestMode: boolean
|
|
statusText: string
|
|
sessionTitleText: string
|
|
sessionSubtitleText: string
|
|
activitySummaryText: string
|
|
listButtonText: string
|
|
rows: Array<{ label: string; value: string }>
|
|
}
|
|
|
|
function getAccessToken(): string | null {
|
|
const app = getApp<IAppOption>()
|
|
const tokens = app.globalData && app.globalData.backendAuthTokens
|
|
? app.globalData.backendAuthTokens
|
|
: loadBackendAuthTokens()
|
|
return tokens && tokens.accessToken ? tokens.accessToken : null
|
|
}
|
|
|
|
function formatValue(value: unknown): string {
|
|
if (value === null || value === undefined || value === '') {
|
|
return '--'
|
|
}
|
|
return String(value)
|
|
}
|
|
|
|
function formatRouteSummary(input: {
|
|
variantName?: string | null
|
|
routeCode?: string | null
|
|
}): string {
|
|
if (input.variantName && input.routeCode) {
|
|
return `${input.variantName} / ${input.routeCode}`
|
|
}
|
|
if (input.variantName) {
|
|
return input.variantName
|
|
}
|
|
if (input.routeCode) {
|
|
return input.routeCode
|
|
}
|
|
return '默认赛道'
|
|
}
|
|
|
|
function formatRuntimeValue(...candidates: Array<string | null | undefined>): string {
|
|
for (let index = 0; index < candidates.length; index += 1) {
|
|
const value = candidates[index]
|
|
if (typeof value === 'string' && value.trim().length > 0) {
|
|
return value.trim()
|
|
}
|
|
}
|
|
return '--'
|
|
}
|
|
|
|
function appendRuntimeRows(
|
|
rows: Array<{ label: string; value: string }>,
|
|
options: {
|
|
runtime?: {
|
|
runtimeBindingId?: string | null
|
|
placeId?: string | null
|
|
placeName?: string | null
|
|
mapId?: string | null
|
|
mapName?: string | null
|
|
tileReleaseId?: string | null
|
|
courseSetId?: string | null
|
|
courseVariantId?: string | null
|
|
routeCode?: string | null
|
|
} | null
|
|
variantName?: string | null
|
|
routeCode?: string | null
|
|
},
|
|
) {
|
|
if (!options.runtime) {
|
|
return rows
|
|
}
|
|
|
|
return rows.concat([
|
|
{ label: '运行绑定', value: formatRuntimeValue(options.runtime.runtimeBindingId) },
|
|
{ label: '地点', value: formatRuntimeValue(options.runtime.placeName, options.runtime.placeId) },
|
|
{ label: '地图', value: formatRuntimeValue(options.runtime.mapName, options.runtime.mapId) },
|
|
{ label: '赛道集', value: formatRuntimeValue(options.runtime.courseSetId) },
|
|
{ label: '赛道版本', value: formatRuntimeValue(options.runtime.courseVariantId, options.variantName) },
|
|
{ label: 'RouteCode', value: formatRuntimeValue(options.runtime.routeCode, options.routeCode) },
|
|
{ label: '瓦片版本', value: formatRuntimeValue(options.runtime.tileReleaseId) },
|
|
])
|
|
}
|
|
|
|
function loadPendingResultLaunchEnvelope(): GameLaunchEnvelope | null {
|
|
const app = getApp<IAppOption>()
|
|
return app.globalData && app.globalData.pendingResultLaunchEnvelope
|
|
? app.globalData.pendingResultLaunchEnvelope
|
|
: null
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
sessionId: '',
|
|
eventId: '',
|
|
guestMode: false,
|
|
statusText: '准备加载结果',
|
|
sessionTitleText: '结果页',
|
|
sessionSubtitleText: '未加载',
|
|
activitySummaryText: '你可以查看本局结果,也可以回到活动继续查看详情。',
|
|
listButtonText: '查看历史结果',
|
|
rows: [],
|
|
} as ResultPageData,
|
|
|
|
onLoad(query: { sessionId?: string }) {
|
|
const sessionId = query && query.sessionId ? decodeURIComponent(query.sessionId) : ''
|
|
this.setData({ sessionId })
|
|
this.applyPendingResultSnapshot()
|
|
if (sessionId) {
|
|
this.loadSingleResult(sessionId)
|
|
return
|
|
}
|
|
this.setData({
|
|
statusText: '未提供单局会话,已跳转历史结果',
|
|
})
|
|
wx.redirectTo({
|
|
url: '/pages/results/results',
|
|
})
|
|
},
|
|
|
|
applyPendingResultSnapshot() {
|
|
const app = getApp<IAppOption>()
|
|
const snapshot = app.globalData && app.globalData.pendingResultSnapshot
|
|
? app.globalData.pendingResultSnapshot as MapEngineResultSnapshot
|
|
: null
|
|
if (!snapshot) {
|
|
return
|
|
}
|
|
|
|
const pendingLaunchEnvelope = loadPendingResultLaunchEnvelope()
|
|
this.setData({
|
|
statusText: '正在加载结果',
|
|
sessionTitleText: snapshot.title,
|
|
sessionSubtitleText: snapshot.subtitle,
|
|
guestMode: !getAccessToken(),
|
|
eventId: pendingLaunchEnvelope && pendingLaunchEnvelope.business && pendingLaunchEnvelope.business.eventId
|
|
? pendingLaunchEnvelope.business.eventId
|
|
: '',
|
|
activitySummaryText: pendingLaunchEnvelope && pendingLaunchEnvelope.business && pendingLaunchEnvelope.business.eventId
|
|
? (!getAccessToken()
|
|
? '本局游客体验已结束,你可以回到活动继续查看,或返回地图体验。'
|
|
: '本局结果已生成,你可以继续查看详情,或回到活动页。')
|
|
: (!getAccessToken() ? '本局游客体验已结束,你可以返回地图体验。' : '本局结果已生成,你可以继续查看历史结果。'),
|
|
listButtonText: getAccessToken() ? '查看历史结果' : '返回地图体验',
|
|
rows: appendRuntimeRows([
|
|
{ label: snapshot.heroLabel, value: snapshot.heroValue },
|
|
...snapshot.rows.map((row) => ({
|
|
label: row.label,
|
|
value: row.value,
|
|
})),
|
|
], {
|
|
runtime: pendingLaunchEnvelope && pendingLaunchEnvelope.runtime ? pendingLaunchEnvelope.runtime : null,
|
|
variantName: pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.variantName : null,
|
|
routeCode: pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.routeCode : null,
|
|
}),
|
|
})
|
|
|
|
if (app.globalData) {
|
|
app.globalData.pendingResultSnapshot = null
|
|
}
|
|
},
|
|
|
|
async loadSingleResult(sessionId: string) {
|
|
const accessToken = getAccessToken()
|
|
if (!accessToken) {
|
|
this.setData({
|
|
guestMode: true,
|
|
statusText: '游客模式当前不加载后端单局结果,先展示本地结果摘要',
|
|
listButtonText: '返回地图体验',
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
statusText: '正在加载单局结果',
|
|
})
|
|
|
|
try {
|
|
const result = await getSessionResult({
|
|
baseUrl: loadBackendBaseUrl(),
|
|
accessToken,
|
|
sessionId,
|
|
})
|
|
const pendingLaunchEnvelope = loadPendingResultLaunchEnvelope()
|
|
this.setData({
|
|
statusText: '单局结果加载完成',
|
|
eventId: result.session.eventId || '',
|
|
sessionTitleText: result.session.eventName || result.session.eventDisplayName || result.session.eventId || result.session.id || result.session.sessionId,
|
|
sessionSubtitleText: `${result.session.status || result.session.sessionStatus} / ${result.result.status} / ${formatRouteSummary(result.session)}`,
|
|
activitySummaryText: result.session.eventId
|
|
? '你可以继续查看这场活动的详情,或回看历史结果。'
|
|
: '你可以继续回看历史结果。',
|
|
rows: appendRuntimeRows([
|
|
{ label: '赛道版本', value: formatRouteSummary(result.session) },
|
|
{ label: '最终得分', value: formatValue(result.result.finalScore) },
|
|
{ label: '最终用时(秒)', value: formatValue(result.result.finalDurationSec) },
|
|
{ label: '完成点数', value: formatValue(result.result.completedControls) },
|
|
{ label: '总点数', value: formatValue(result.result.totalControls) },
|
|
{ label: '累计里程(m)', value: formatValue(result.result.distanceMeters) },
|
|
{ label: '平均速度(km/h)', value: formatValue(result.result.averageSpeedKmh) },
|
|
{ label: '最大心率', value: formatValue(result.result.maxHeartRateBpm) },
|
|
], {
|
|
runtime: result.session.runtime || (pendingLaunchEnvelope && pendingLaunchEnvelope.runtime ? pendingLaunchEnvelope.runtime : null),
|
|
variantName: result.session.variantName || (pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.variantName : null),
|
|
routeCode: result.session.routeCode || (pendingLaunchEnvelope && pendingLaunchEnvelope.variant ? pendingLaunchEnvelope.variant.routeCode : null),
|
|
}),
|
|
})
|
|
const app = getApp<IAppOption>()
|
|
if (app.globalData) {
|
|
app.globalData.pendingResultLaunchEnvelope = null
|
|
}
|
|
} catch (error) {
|
|
const message = error && (error as { message?: string }).message ? (error as { message: string }).message : '未知错误'
|
|
this.setData({
|
|
statusText: `结果加载失败:${message}`,
|
|
})
|
|
}
|
|
},
|
|
|
|
handleBackToList() {
|
|
if (this.data.guestMode) {
|
|
wx.redirectTo({
|
|
url: '/pages/experience-maps/experience-maps',
|
|
})
|
|
return
|
|
}
|
|
wx.redirectTo({
|
|
url: '/pages/results/results',
|
|
})
|
|
},
|
|
|
|
handleBackToEvent() {
|
|
if (!this.data.eventId) {
|
|
wx.showToast({
|
|
title: '当前结果未关联活动',
|
|
icon: 'none',
|
|
})
|
|
return
|
|
}
|
|
wx.redirectTo({
|
|
url: `/pages/event/event?eventId=${encodeURIComponent(this.data.eventId)}`,
|
|
})
|
|
},
|
|
})
|