import { LOBBY_GAME_URLS } from '#lib/constants' import { GameTypeEnum, IFrameGameURLEnum } from '#lib/enums' import { useGameToast, useNadal, useUser } from '#lib/composables' import { useRoute, useRuntimeConfig } from 'nuxt/app' import { useDevice } from '#imports' /** * Provides functions to generate URLs for various types of games and game-related resources. * This composable includes methods to construct game URLs based on game types, user tokens, * and other parameters, and supports both mobile and desktop environments. * * @returns {Object} An object containing functions for generating game URLs and iframe URLs. * @namespace */ export const useGameURL = () => { const { game } = useNadal() const environment = useRuntimeConfig().public const { gpToken, memberId } = useUser() const route = useRoute() const { onShowMaintainGame } = useGameToast() const { isMobile } = useDevice() /** * Constructs the URL for the Lode game based on the device type (mobile or desktop). * * @returns {string} The constructed Lode game URL. */ const getLodeUrl = (): string => { if (isMobile) { return `${environment.LODE_MOBILE_URL}${gpToken.value}` } return `${environment.LODE_URL}${gpToken.value}` } /** * Constructs the URL for the Lode DaiCat game. * * @returns {string} The constructed Lode DaiCat game URL. */ const getLodeDaiCatUrl = (): string => { return `${environment.LODE_DAICAT_URL}${gpToken.value}` } /** * Constructs the URL for the Lode SieuToc game based on the device type (mobile or desktop). * * @returns {string} The constructed Lode SieuToc game URL. */ const getLodeSieuTocUrl = (): string => { if (isMobile) { return `${environment.LODE_SIEUTOC_MOBILE_URL}${gpToken.value}` } return `${environment.LODE_SIEUTOC_URL}${gpToken.value}` } /** * Constructs the URL for the Keno game with necessary query parameters. * * @returns {string} The constructed Keno game URL with query parameters. */ const getKenoUrl = (): string => { const params = new URLSearchParams({ operator_id: '2000', lang: 'vi', currency: 'VND', token: gpToken.value, is_mobile: isMobile ? '1' : '0', }) return `${environment.TP_KENO_URL}?${params.toString()}` } /** * Constructs the URL for the TP Number game with necessary query parameters. * * @returns {string} The constructed TP Number game URL with query parameters. */ const getTpNumberGameUrl = (): string => { // TODO: get lang & currency from cookie const params = new URLSearchParams({ operator_id: '1000', lang: 'vi', currency: 'VND', is_mobile: isMobile ? '1' : '0', token: gpToken.value, }) return `${environment.TP_NUMBERGAME_URL}?${params.toString()}` } /** * Constructs the URL for the Lottery Las Vegas game. * * @returns {string} The constructed Lottery Las Vegas game URL. */ const getLotteryLasVegasUrl = (): string => { return `${environment.QUAYSO_LAS_VEGAS_URL}?token=${gpToken.value}` } /** * Constructs the URL for the Lottery CyberPunk game based on the validity of the plan. * * @param {boolean} isValidPlan - Whether the plan is valid for the CyberPunk lottery. * @returns {string} The constructed Lottery CyberPunk or Las Vegas game URL. */ const getLotteryCyberPunkUrl = (): string => { return `${environment.QUAYSO_CYPER_URL}?token=${gpToken.value}` } const getGameLiveStreamUrl = (apiUrl:string, gameId: string): string => { return `${apiUrl}?token=${gpToken.value}&gameid=${gameId}&brand=${environment.BRAND_NAME}&ru=${environment.SITE_DOMAIN}` } const getKenoVietlot = (gpToken: string) => { const kenoVietLotUrl = environment.KENO_VIETLOT if (isMobile) { return `${kenoVietLotUrl}${gpToken}&is_mobile=1` } return `${kenoVietLotUrl}${gpToken}` } const getQuaySoAtom = (gpToken: string) => { const quaySoAtom = environment.QUAYSO_ATOM if (isMobile) { return `${quaySoAtom}?token=${gpToken}&is_mobile=1` } return `${quaySoAtom}?token=${gpToken}` } const getNumberGame5 = (gpToken: string) => { const numberGame5Url = environment.NUMBER_GAME_5_URL if (isMobile) { return `${numberGame5Url}${gpToken}&is_mobile=1` } return `${numberGame5Url}${gpToken}` } const getGsKeno = (gpToken: string) => { const gsKenoUrl = isMobile ? environment.GS_KENO_MB_LINK : environment.GS_KENO_LINK return `${gsKenoUrl}&token=${gpToken}&lng=vi¤cy=VND` } /** * Constructs the URL for a game based on its type, game ID, and optional provider URL. * * @param {GameTypeEnum} gameType - The type of game. * @param {string} gameId - The ID of the game. * @param {string} [providerUrl] - Optional URL for the provider. * @returns {string} The constructed game URL based on the provided parameters. */ const getGameTypeUrl = (gameType: GameTypeEnum, gameId: string): string => { switch (gameType) { case GameTypeEnum.GP_URL: return getGpUrl(gameId) case GameTypeEnum.SPRIBE: case GameTypeEnum.INSTANT_GAMES_URL: return getInstantGameUrl(gameId) case GameTypeEnum.PG_URL: void getPGSoftUrl(gameId) return '' default: return '' } } /** * Constructs the URL for a GP game based on the game ID. * * @param {string} gameId - The ID of the game. * @returns {string} The constructed GP game URL. */ const getGpUrl = (gameId: string): string => { const gameUrl = `${environment.GP_URL}${gameId.replace('_', '')}/?` const params = new URLSearchParams({ token: gpToken.value, ru: getDomainUrl(route.fullPath), }) return `${gameUrl}&${params.toString()}` } /** * Constructs the domain URL based on the provided path. * * @param {string} path - The path to be included in the domain URL. * @returns {string} The constructed domain URL. */ const getDomainUrl = (path: string): string => { return `${window.location.origin}${path}` } /** * Constructs the URL for an instant game based on the game ID and other parameters. * * @param {string} gameId - The ID of the game. * @returns {string} The constructed instant game URL. */ const getInstantGameUrl = (gameId: string): string => { const currentTime = new Date().getTime() let gameUrl = '' if (gpToken.value) { const returnUrl = window.location.href || getDomainUrl(LOBBY_GAME_URLS.INSTANT_GAMES) const params = new URLSearchParams({ lang: 'VI', currency: 'VND', operator: 'proadvancepowerasia', token: `${gpToken.value}-${currentTime}`, user: memberId.value, return_url: returnUrl, }) gameUrl = `${environment.INSTANT_GAME_URL}${gameId}?${params.toString()}` } return gameUrl } /** * Constructs and opens a URL for PGSoft games in a new browser tab. * Displays a maintenance message if the URL could not be generated. * * @param {string} gameId - The ID of the PGSoft game. */ const getPGSoftUrl = async (gameId: string): Promise => { const params = { extra_args: { btt: 1, l: 'vi' }, path: `/${gameId}/index.html`, url_type: 'game-entry', } const data = await game.gameLauncher(params) if (data) { const newTab = window.open('', '_blank') if (newTab) { newTab.document.open() newTab.document.write(data as unknown as string) newTab.document.close() } } else { onShowMaintainGame() } } /** * Constructs the URL for a game to be displayed in an iframe based on the game URL or route slug. * * @param {string} gameUrl - The URL of the game or route slug. * @returns {string} The constructed iframe URL based on the provided game URL or route slug. */ const getGameUrlIframe = (gameUrl: string): string => { const url = gameUrl || route.params?.slug switch (url) { case IFrameGameURLEnum.LODE: return getLodeUrl() case IFrameGameURLEnum.LODE_DAICAT: return getLodeDaiCatUrl() case IFrameGameURLEnum.LODE_SIEUTOC: return getLodeSieuTocUrl() case IFrameGameURLEnum.TP_KENO: return getKenoUrl() case IFrameGameURLEnum.TP_NUMBER_GAME: return getTpNumberGameUrl() case IFrameGameURLEnum.LOTTERY_LAS_VEGAS: return getLotteryLasVegasUrl() case IFrameGameURLEnum.LOTTERY_CYBER_PUNK: return getLotteryCyberPunkUrl() case IFrameGameURLEnum.KENO_VIETLOT: return getKenoVietlot(gpToken.value) case IFrameGameURLEnum.QUAYSO_ATOM: return getQuaySoAtom(gpToken.value) case IFrameGameURLEnum.NUMBER_GAME_5_URL: return getNumberGame5(gpToken.value) case IFrameGameURLEnum.GS_KENO: return getGsKeno(gpToken.value) default: return '' } } return { getGameUrlIframe, getGameTypeUrl, getGameLiveStreamUrl } }