import type { GameOptions } from '#lib/types' import { useGameToast, useUser, usePromotion } from '#lib/composables' import { ATHENA_GAMES } from '#lib/constants' import { useMaintenanceGamesStore } from '#lib/stores' import { GameCategoryEnum, GameProviderEnum } from '#lib/enums' import { useRuntimeConfig } from 'nuxt/app' /** * Provides various checks and conditions for handling game-related logic. * This composable is used to validate game states, manage user authentication, check promotions, * and handle maintenance and balance conditions. * * @returns {Object} An object containing functions to check game conditions and show maintenance alerts. * @namespace */ export const useCheckGame = () => { const mainternanceGamesStore = useMaintenanceGamesStore() const { openMaintainProvider } = useGameToast() const { token, user, balance, denyGameIds } = useUser() const { isUsingPromotion, isBonus, isCompletedPromotionBonus } = usePromotion() const environment = useRuntimeConfig().public const GAME_PROVIDERS_DISABLED = environment.GAME_PROVIDERS_DISABLED /** * Checks if the game is marked as coming soon. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if the game is coming soon, otherwise false. */ const checkCommingSoon = (game: GameOptions): boolean => { return Boolean(game.isCommingSoon) } /** * Checks if the game provider is currently under maintenance. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if the provider is under maintenance, otherwise false. */ const checkMaintainProvider = (game: GameOptions): boolean => { const { provider } = game return Boolean(provider && GAME_PROVIDERS_DISABLED?.includes(provider)) } /** * Checks if the game is part of the Athena games that are under maintenance. * @param {string} [gameId=''] - The ID of the game to check. * @returns {boolean} True if the Athena game is under maintenance, otherwise false. */ const isMaintainAthenaGames = (gameId: string = ''): boolean => { const athennaGameIds = ATHENA_GAMES.map((game) => game.id) return ( mainternanceGamesStore.isMaintain && athennaGameIds.includes(gameId.toString()) ) } /** * Checks if the game is under maintenance based on its ID or URL. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if the game is under maintenance, otherwise false. */ const checkMaintainAthenaGames = (game: GameOptions): boolean => { const { gameId, gameUrl } = game const isMaintainAthena = isMaintainAthenaGames(gameId) || isMaintainAthenaGames(gameUrl?.replaceAll('/', '')) return isMaintainAthena } /** * Checks if the game requires login and if the user is not authenticated. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if login is required and the user is not authenticated, otherwise false. */ const checkLogin = (game: GameOptions): boolean => { return Boolean(game.loginRequired && !token.value) } /** * Checks if the user has sufficient balance to play the game. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if the user does not have enough balance, otherwise false. */ const checkBalance = (game: GameOptions): boolean => { return Boolean(user.value && game.moneyRequired && balance.value <= 0) } /** * Checks if the game is prevented from promotion based on various conditions. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if the game promotion is prevented, otherwise false. */ const checkPromotion = (game: GameOptions): boolean => { if ( !isUsingPromotion.value && !(isBonus.value && !isCompletedPromotionBonus.value) ) { return false } const { provider, category = [], gameId = '', promotionPrevent } = game const regexPattern = new RegExp( denyGameIds.value.map((pattern) => pattern.replace('*', '.*')).join('|'), ) const isExcludedUser = environment.PROMOTION_EXCLUDED_PACKAGE_IDS.includes((user.value?.package_id?.toString() ?? '')) && environment.PROMOTION_EXCLUDED_PROVIDERS.includes((provider ?? '').toLowerCase()) if (isExcludedUser) { return false } return ( (denyGameIds.value.length > 0 && regexPattern.test(gameId)) || promotionPrevent || (provider === GameProviderEnum.PGsoft && category.includes(GameCategoryEnum.SLOTS)) || provider === GameProviderEnum.Spribe ) } /** * Checks all conditions to determine if a game meets any of the criteria for special handling. * @param {GameOptions} game - The game options to check. * @returns {boolean} True if any condition for special handling is met, otherwise false. */ const checkConditionsOfGame = (game: GameOptions): boolean => { return ( checkCommingSoon(game) || checkMaintainProvider(game) || checkMaintainAthenaGames(game) || checkLogin(game) || checkPromotion(game) || checkBalance(game) ) } /** * Checks if a game provider is under maintenance and triggers the maintenance modal if so. * @param {string} provider - The provider to check. * @returns {boolean} True if the provider is under maintenance, otherwise false. */ const showMaintenanceProvider = (provider: string): boolean => { if (provider && GAME_PROVIDERS_DISABLED?.includes(provider)) { openMaintainProvider() return true } return false } return { checkConditionsOfGame, showMaintenanceProvider, isMaintainAthenaGames, checkCommingSoon, checkMaintainProvider, checkMaintainAthenaGames, checkLogin, checkPromotion, checkBalance, } }