gameLaunch.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. export type DemoGamePreset = 'classic' | 'score-o'
  2. export type BusinessLaunchSource = 'demo' | 'competition' | 'direct-event' | 'custom'
  3. export interface GameConfigLaunchRequest {
  4. configUrl: string
  5. configLabel: string
  6. configChecksumSha256?: string | null
  7. releaseId?: string | null
  8. routeCode?: string | null
  9. }
  10. export interface BusinessLaunchContext {
  11. source: BusinessLaunchSource
  12. competitionId?: string | null
  13. eventId?: string | null
  14. launchRequestId?: string | null
  15. participantId?: string | null
  16. sessionId?: string | null
  17. sessionToken?: string | null
  18. sessionTokenExpiresAt?: string | null
  19. realtimeEndpoint?: string | null
  20. realtimeToken?: string | null
  21. }
  22. export interface GameLaunchEnvelope {
  23. config: GameConfigLaunchRequest
  24. business: BusinessLaunchContext | null
  25. }
  26. export interface MapPageLaunchOptions {
  27. launchId?: string
  28. preset?: string
  29. configUrl?: string
  30. configLabel?: string
  31. configChecksumSha256?: string
  32. releaseId?: string
  33. routeCode?: string
  34. launchSource?: string
  35. competitionId?: string
  36. eventId?: string
  37. launchRequestId?: string
  38. participantId?: string
  39. sessionId?: string
  40. sessionToken?: string
  41. sessionTokenExpiresAt?: string
  42. realtimeEndpoint?: string
  43. realtimeToken?: string
  44. }
  45. type PendingGameLaunchStore = Record<string, GameLaunchEnvelope>
  46. const PENDING_GAME_LAUNCH_STORAGE_KEY = 'cmr.pendingGameLaunch.v1'
  47. const CLASSIC_REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/gotomars/event/classic-sequential.json'
  48. const SCORE_O_REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/gotomars/event/score-o.json'
  49. function normalizeOptionalString(value: unknown): string | null {
  50. if (typeof value !== 'string') {
  51. return null
  52. }
  53. const normalized = decodeURIComponent(value).trim()
  54. return normalized ? normalized : null
  55. }
  56. function resolveDemoPreset(value: string | null): DemoGamePreset {
  57. return value === 'score-o' ? 'score-o' : 'classic'
  58. }
  59. function resolveBusinessLaunchSource(value: string | null): BusinessLaunchSource {
  60. if (value === 'competition' || value === 'direct-event' || value === 'custom') {
  61. return value
  62. }
  63. return 'demo'
  64. }
  65. function buildDemoConfig(preset: DemoGamePreset): GameConfigLaunchRequest {
  66. if (preset === 'score-o') {
  67. return {
  68. configUrl: SCORE_O_REMOTE_GAME_CONFIG_URL,
  69. configLabel: '积分赛配置',
  70. }
  71. }
  72. return {
  73. configUrl: CLASSIC_REMOTE_GAME_CONFIG_URL,
  74. configLabel: '顺序赛配置',
  75. }
  76. }
  77. function hasBusinessFields(context: Omit<BusinessLaunchContext, 'source'>): boolean {
  78. return Object.values(context).some((value) => typeof value === 'string' && value.length > 0)
  79. }
  80. function buildBusinessLaunchContext(options?: MapPageLaunchOptions | null): BusinessLaunchContext | null {
  81. if (!options) {
  82. return null
  83. }
  84. const context = {
  85. competitionId: normalizeOptionalString(options.competitionId),
  86. eventId: normalizeOptionalString(options.eventId),
  87. launchRequestId: normalizeOptionalString(options.launchRequestId),
  88. participantId: normalizeOptionalString(options.participantId),
  89. sessionId: normalizeOptionalString(options.sessionId),
  90. sessionToken: normalizeOptionalString(options.sessionToken),
  91. sessionTokenExpiresAt: normalizeOptionalString(options.sessionTokenExpiresAt),
  92. realtimeEndpoint: normalizeOptionalString(options.realtimeEndpoint),
  93. realtimeToken: normalizeOptionalString(options.realtimeToken),
  94. }
  95. const launchSource = normalizeOptionalString(options.launchSource)
  96. if (!hasBusinessFields(context) && launchSource === null) {
  97. return null
  98. }
  99. return {
  100. source: resolveBusinessLaunchSource(launchSource),
  101. ...context,
  102. }
  103. }
  104. function loadPendingGameLaunchStore(): PendingGameLaunchStore {
  105. try {
  106. const stored = wx.getStorageSync(PENDING_GAME_LAUNCH_STORAGE_KEY)
  107. if (!stored || typeof stored !== 'object') {
  108. return {}
  109. }
  110. return stored as PendingGameLaunchStore
  111. } catch {
  112. return {}
  113. }
  114. }
  115. function savePendingGameLaunchStore(store: PendingGameLaunchStore): void {
  116. try {
  117. wx.setStorageSync(PENDING_GAME_LAUNCH_STORAGE_KEY, store)
  118. } catch {}
  119. }
  120. export function getDemoGameLaunchEnvelope(preset: DemoGamePreset = 'classic'): GameLaunchEnvelope {
  121. return {
  122. config: buildDemoConfig(preset),
  123. business: {
  124. source: 'demo',
  125. },
  126. }
  127. }
  128. export function stashPendingGameLaunchEnvelope(envelope: GameLaunchEnvelope): string {
  129. const launchId = `launch_${Date.now()}_${Math.random().toString(36).slice(2, 10)}`
  130. const store = loadPendingGameLaunchStore()
  131. store[launchId] = envelope
  132. savePendingGameLaunchStore(store)
  133. return launchId
  134. }
  135. export function consumePendingGameLaunchEnvelope(launchId: string): GameLaunchEnvelope | null {
  136. const normalizedLaunchId = normalizeOptionalString(launchId)
  137. if (!normalizedLaunchId) {
  138. return null
  139. }
  140. const store = loadPendingGameLaunchStore()
  141. const envelope = store[normalizedLaunchId] || null
  142. if (!envelope) {
  143. return null
  144. }
  145. delete store[normalizedLaunchId]
  146. savePendingGameLaunchStore(store)
  147. return envelope
  148. }
  149. export function buildMapPageUrlWithLaunchId(launchId: string): string {
  150. return `/pages/map/map?launchId=${encodeURIComponent(launchId)}`
  151. }
  152. export function prepareMapPageUrlForLaunch(envelope: GameLaunchEnvelope): string {
  153. return buildMapPageUrlWithLaunchId(stashPendingGameLaunchEnvelope(envelope))
  154. }
  155. export function resolveGameLaunchEnvelope(options?: MapPageLaunchOptions | null): GameLaunchEnvelope {
  156. const launchId = normalizeOptionalString(options ? options.launchId : undefined)
  157. if (launchId) {
  158. const pendingEnvelope = consumePendingGameLaunchEnvelope(launchId)
  159. if (pendingEnvelope) {
  160. return pendingEnvelope
  161. }
  162. }
  163. const configUrl = normalizeOptionalString(options ? options.configUrl : undefined)
  164. if (configUrl) {
  165. return {
  166. config: {
  167. configUrl,
  168. configLabel: normalizeOptionalString(options ? options.configLabel : undefined) || '线上配置',
  169. configChecksumSha256: normalizeOptionalString(options ? options.configChecksumSha256 : undefined),
  170. releaseId: normalizeOptionalString(options ? options.releaseId : undefined),
  171. routeCode: normalizeOptionalString(options ? options.routeCode : undefined),
  172. },
  173. business: buildBusinessLaunchContext(options),
  174. }
  175. }
  176. const preset = resolveDemoPreset(normalizeOptionalString(options ? options.preset : undefined))
  177. return getDemoGameLaunchEnvelope(preset)
  178. }