protected async getSign()

in src/app/pages/game/services/ai-strategies/ai-strategy-base.ts [50:76]


  protected async getSign(additionalData: string = ''): Promise<Signs> {
    /**
     * It will be used when:
     * - Gemini API will fail
     * - Gemini API execution will be greater than timeout
     */
    const fallbackValue = this.fallbackFn();

    try {
      const geminiRequest = this.geminiService.askGemini<Signs>(this.getPrompt(additionalData));

      const fallback = new Promise<Signs>((resolve) => {
        setTimeout(() => {
          resolve(fallbackValue);
        }, TIMEOUT);
      });

      /**
       * In case when Gemini API won't return one of the expected value `Rock`, `Paper` or `Scissors` then use fallback value.
       */
      const result = await Promise.race([geminiRequest, fallback]);

      return result ?? fallbackValue;
    } catch {
      return fallbackValue;
    }
  }