import { useDebounceFn } from '@vueuse/core' import { useTokenValidation } from '#layers/composables/useTokenValidation' import { csrGoStoveLogin } from '#layers/utils/stoveUtil' export const useCheckGameStart = () => { // const { tm } = useI18n() const modalStore = useModalStore() const runtimeConfig = useRuntimeConfig() const isProcessing = ref(false) // 연속 클릭 방지 const isShowCheckLauncher = ref(false) // 런처 실행 로딩 표시 const isShowDownloadLauncher = ref(false) // 런처 다운로드 표시 const customerService = { title: '확인', link: 'https://www.google.com' } //[TODO] 고객센터 링크 // 로그인 모달 표시 const showLoginModal = () => { modalStore.handleOpenConfirm({ contentText: '로그인이 필요합니다.', confirmButtonText: '스토브 로그인', modalName: 'modal-login', confirmButtonEvent: () => { csrGoStoveLogin() }, }) } // 에러 처리 const errorHandler = (errorCode: number) => { switch (errorCode) { case 601: // PC 클라이언트 미설치 break case 40101: // 로그인 정보 확인 중 오류가 발생했습니다. 재로그인 후 다시 이용해 주세요. modalStore.handleOpenConfirm({ contentText: '로그인 정보 확인 중 오류가 발생했습니다. 재로그인 후 다시 이용해 주세요.', confirmButtonText: '스토브 로그인', modalName: 'modal-login', confirmButtonEvent: () => { csrGoStoveLogin() }, }) break case 40103: // 로그인 정보가 만료되었습니다. 재로그인 후 다시 이용해 주세요. modalStore.handleOpenConfirm({ contentText: '로그인 정보가 만료되었습니다. 재로그인 후 다시 이용해 주세요.', confirmButtonText: '스토브 로그인', modalName: 'modal-login', confirmButtonEvent: () => { csrGoStoveLogin() }, }) break case 602: case 504: case 70051: case 500000: case 701: case 70052: default: // 일시적으로 오류가 발생했습니다. 잠시 후 다시 이용해 주세요. 동일한 현상이 계속 발생할 경우 고객센터로 문의해 주세요. modalStore.handleOpenConfirm({ contentText: '일시적으로 오류가 발생했습니다. 잠시 후 다시 이용해 주세요. 동일한 현상이 계속 발생할 경우 고객센터로 문의해 주세요.', confirmButtonText: customerService.title, cancelButtonText: '취소', confirmButtonEvent: () => { window.open(customerService.link, '_blank') }, }) break } } // 런처 실행 로딩 시작 UI 처리 const startLoadingForLauncher = () => { if (import.meta.client) { isShowCheckLauncher.value = true isShowDownloadLauncher.value = false setTimeout(() => { if (isShowCheckLauncher.value) { isShowDownloadLauncher.value = true } }, 5000) } } // 런처 실행 로딩 종료 UI 처리 const stopLoadingForLauncher = () => { if (import.meta.client) { isShowCheckLauncher.value = false isShowDownloadLauncher.value = false } } // 런처 호출 const runLauncher = async () => { // 클라이언트에서만 실행 if (!import.meta.client) return const gameDataStore = useGameDataStore() const { gameData } = storeToRefs(gameDataStore) const accessTokenSub = useCookie('SUAT') const stoveGameId = gameData.value?.game_id || '' const nationCookie = useCookie('NNTO').value const localeCookie = useCookie('LOCALE').value window.stoveJsService = window.stoveJsService || {} // 토큰 유효성 체크 const { validateToken } = useTokenValidation() const validateTokenResult = await validateToken(accessTokenSub.value || '') // 토큰 유효성 체크 실패 시 로그인 모달 표시 if (!validateTokenResult) { showLoginModal() isProcessing.value = false return } // 런처 실행 로딩 시작 startLoadingForLauncher() window.stoveJsService.launcher .run({ gameId: stoveGameId, nation: nationCookie, lang: localeCookie, isSkipMaintenance: true, }) .then(() => { // 런처 실행 성공 시 처리 stopLoadingForLauncher() }) .catch((error: any) => { // 런처 실행 실패시 처리 if (error.code !== 601) { stopLoadingForLauncher() } errorHandler(error.code) }) .finally(() => { isProcessing.value = false }) } // 디바운스 설정 const debounceHandler = useDebounceFn(runLauncher, 500) // 런처 상태 검사 const validateLauncher = () => { if (isProcessing.value) return isProcessing.value = true debounceHandler() } // 런처 다운로드 함수 const downloadLauncher = () => { const stoveClientDownloadUrl = runtimeConfig.public.stoveClientDownloadUrl location.href = stoveClientDownloadUrl isProcessing.value = false } return { isProcessing, // 연속 클릭 방지 isShowCheckLauncher, // 런처 실행 로딩 표시 isShowDownloadLauncher, // 런처 다운로드 표시 validateLauncher, // 런처 검사 함수 downloadLauncher, // 런처 실행 함수 } }