Files
web-temp/layers/composables/useGameStart.ts
2025-10-02 17:34:41 +09:00

192 lines
6.0 KiB
TypeScript

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 disabledDoubleClick = ref(false) // 연속 호출 클릭 방지
const isCheckLauncher = ref(false) // 런처 실행 로딩 상태
const isShowDownloadLauncher = ref(false) // 런처 다운로드 표시
const customerService = { title: '확인', link: 'https://www.google.com' } //[TODO]
// 로그인 모달 표시
const showLoginModal = () => {
modalStore.handleOpenAlert({
contentText: '로그인',
confirmButtonText: '스토브 로그인',
className: 'modal-login',
confirmButtonEvent: () => {
modalStore.handleResetAlert()
csrGoStoveLogin()
},
closeButtonEvent: () => {
modalStore.handleResetAlert()
disabledDoubleClick.value = false
},
})
}
// 모든 런처 버튼 비활성화
const setLauncherButtonDisabled = (disabled: boolean) => {
const launcherButton = document.querySelectorAll(
'#btn-launcher'
) as NodeListOf<HTMLButtonElement>
launcherButton.forEach(button => {
button.disabled = disabled
})
}
// 런처 실행 로딩 시작 UI 처리
const startLoadingForLauncher = () => {
if (import.meta.client) {
setLauncherButtonDisabled(true)
isCheckLauncher.value = true
setTimeout(() => {
if (isCheckLauncher.value) {
isShowDownloadLauncher.value = true
}
}, 5000)
}
}
// 런처 실행 로딩 종료 UI 처리
const stopLoadingForLauncher = () => {
if (import.meta.client) {
setLauncherButtonDisabled(false)
isCheckLauncher.value = false
isShowDownloadLauncher.value = false
}
}
// 런처 호출
const runLauncher = async () => {
// 클라이언트에서만 실행
if (!import.meta.client) return
const gameDataStore = useGameDataStore()
const stoveGameId = gameDataStore.gameData?.game_id || ''
const accessTokenSub = useCookie('SUAT')
const nationCookie = useCookie('NNTO').value
const localeCookie = useCookie('LOCALE').value
const isAgent = true
disabledDoubleClick.value = true
window.stoveJsService = window.stoveJsService || {}
// 토큰 유효성 체크
const { validateToken } = useTokenValidation()
const validateTokenResult = await validateToken(accessTokenSub.value || '')
if (validateTokenResult) {
startLoadingForLauncher()
window.stoveJsService.launcher
.run({
gameId: stoveGameId,
nation: nationCookie,
lang: localeCookie,
isSkipMaintenance: isAgent,
})
.then(() => {
// 런처 실행 성공 시 처리
stopLoadingForLauncher()
})
.catch((error: any) => {
// 런처 실행 실패시 처리
if (error.code !== 601) {
stopLoadingForLauncher()
}
errorHandler(error.code)
})
.finally(() => {
disabledDoubleClick.value = false
})
} else {
showLoginModal()
}
}
// PC 클라이언트 설치 전 (에러 처리)
const errorHandler = (errorCode: number) => {
switch (errorCode) {
case 601: // PC 클라이언트 미설치
break
case 40101: // 로그인 정보 확인 중 오류가 발생했습니다. 재로그인 후 다시 이용해 주세요.
modalStore.handleOpenAlert({
contentText:
'로그인 정보 확인 중 오류가 발생했습니다. 재로그인 후 다시 이용해 주세요.',
confirmButtonText: '스토브 로그인',
className: 'modal-login',
confirmButtonEvent: () => {
modalStore.handleResetAlert()
csrGoStoveLogin()
},
})
break
case 40103: // 로그인 정보가 만료되었습니다. 재로그인 후 다시 이용해 주세요.
modalStore.handleOpenAlert({
contentText:
'로그인 정보가 만료되었습니다. 재로그인 후 다시 이용해 주세요.',
confirmButtonText: '스토브 로그인',
className: 'modal-login',
confirmButtonEvent: () => {
modalStore.handleResetAlert()
csrGoStoveLogin()
},
})
break
case 602:
case 504:
case 70051:
case 500000:
case 701:
case 70052:
default:
// 일시적으로 오류가 발생했습니다. 잠시 후 다시 이용해 주세요. 동일한 현상이 계속 발생할 경우 고객센터로 문의해 주세요.
modalStore.handleOpenConfirm({
contentText:
'일시적으로 오류가 발생했습니다. 잠시 후 다시 이용해 주세요. 동일한 현상이 계속 발생할 경우 고객센터로 문의해 주세요.',
confirmButtonText: customerService.title,
cancelButtonText: '취소',
confirmButtonEvent: () => {
modalStore.handleResetConfirm()
window.open(customerService.link, '_blank')
},
})
break
}
}
// 디바운스 설정
const debounceHandler = useDebounceFn(runLauncher, 500)
// 런처 상태 검사
const validateLauncher = () => {
if (!disabledDoubleClick.value) {
debounceHandler()
}
}
// 런처 다운로드 함수
const downloadLauncher = () => {
const stoveClientDownloadUrl = runtimeConfig.public.stoveClientDownloadUrl
location.href = stoveClientDownloadUrl
disabledDoubleClick.value = false
}
return {
disabledDoubleClick, // 연속 클릭 방지
isCheckLauncher, // 런처 실행 로딩 상태
isShowDownloadLauncher, // 런처 다운로드 표시
validateLauncher, // 런처 검사 함수
downloadLauncher, // 런처 실행 함수
stopLoadingForLauncher, // 런처 실행 로딩 종료 함수
}
}