From bdcf540d1e0d27ed7fb80376f66779e93da2227b Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Tue, 19 Jul 2022 22:20:15 +0100 Subject: [PATCH] Guess game - Easter egg (#2066) * start working on guess command * add guess game file * Allow to do more than 1 attempt in the game Co-authored-by: Theodore Aptekarev Co-authored-by: James Maslek --- guess_game.json | 8 +++ openbb_terminal/feature_flags.py | 8 +++ terminal.py | 115 +++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 guess_game.json diff --git a/guess_game.json b/guess_game.json new file mode 100644 index 00000000000..8e95c3c22b0 --- /dev/null +++ b/guess_game.json @@ -0,0 +1,8 @@ +{ + "load AAPL and display its exponential moving average": "stocks/load AAPL/ta/ema", + "get analyst prices from AAPL": "stocks/load AAPL/dd/analyst", + "get management team from AAPL": "stocks/load AAPL/fa/mgmt", + "check future IPOs": "stocks/disc/fipo", + "search tickers with word 'Technology' from India": "stocks/search Technology -c India", + "get consumer price index for Portugal": "economy/macro -p CPI -c Portugal" +} diff --git a/openbb_terminal/feature_flags.py b/openbb_terminal/feature_flags.py index e0b80530b63..a7589fd4ade 100644 --- a/openbb_terminal/feature_flags.py +++ b/openbb_terminal/feature_flags.py @@ -110,6 +110,14 @@ PREFERRED_DATA_SOURCE_FILE = str( ) ) +# Guess file +GUESS_EASTER_EGG_FILE = str( + os.getenv( + "OPENBB_GUESS_EASTER_EGG_FILE", + os.getcwd() + os.path.sep + "guess_game.json", + ) +) + try: version = pkg_resources.get_distribution("OpenBBTerminal").version except Exception: diff --git a/terminal.py b/terminal.py index ef50c1ff4c9..ff88021410d 100644 --- a/terminal.py +++ b/terminal.py @@ -22,6 +22,7 @@ from openbb_terminal.core.log.generation.path_tracking_file_handler import ( ) from openbb_terminal import feature_flags as obbff from openbb_terminal.helper_funcs import ( + check_positive, get_flair, parse_simple_args, ) @@ -56,6 +57,7 @@ class TerminalController(BaseController): "update", "featflags", "exe", + "guess", ] CHOICES_MENUS = [ "stocks", @@ -81,6 +83,11 @@ class TerminalController(BaseController): if file.endswith(".openbb") } + GUESS_TOTAL_TRIES = 0 + GUESS_NUMBER_TRIES_LEFT = 0 + GUESS_SUM_SCORE = 0.0 + GUESS_CORRECTLY = 0 + def __init__(self, jobs_cmds: List[str] = None): """Constructor""" super().__init__(jobs_cmds) @@ -131,6 +138,114 @@ class TerminalController(BaseController): mt.add_menu("reports") console.print(text=mt.menu_text, menu="Home") + def call_guess(self, other_args: List[str]) -> None: + """Process guess command""" + import time + import json + import random + + if self.GUESS_NUMBER_TRIES_LEFT == 0 and self.GUESS_SUM_SCORE < 0.01: + parser_exe = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog="guess", + description="Guess command to achieve task successfully.", + ) + parser_exe.add_argument( + "-l", + "--limit", + type=check_positive, + help="Number of tasks to attempt.", + dest="limit", + default=1, + ) + if other_args and "-" not in other_args[0][0]: + other_args.insert(0, "-l") + ns_parser_guess = parse_simple_args(parser_exe, other_args) + + if self.GUESS_TOTAL_TRIES == 0: + self.GUESS_NUMBER_TRIES_LEFT = ns_parser_guess.limit + self.GUESS_SUM_SCORE = 0 + self.GUESS_TOTAL_TRIES = ns_parser_guess.limit + + try: + with open(obbff.GUESS_EASTER_EGG_FILE) as f: + # Load the file as a JSON document + json_doc = json.load(f) + + task = random.choice(list(json_doc.keys())) + solution = json_doc[task] + + start = time.time() + console.print(f"\n[yellow]{task}[/yellow]\n") + an_input = session.prompt("GUESS / $ ") + time_dif = time.time() - start + + # When there are multiple paths to same solution + if isinstance(solution, List): + if an_input in solution: + self.queue = an_input.split("/") + ["home"] + console.print( + f"\n[green]You guessed correctly in {round(time_dif, 2)} seconds![green]\n" + ) + # If we are already counting successes + if self.GUESS_TOTAL_TRIES > 0: + self.GUESS_CORRECTLY += 1 + self.GUESS_SUM_SCORE += time_dif + else: + solutions_texts = "\n".join(solution) + console.print( + f"\n[red]You guessed wrong! The correct paths would have been:\n{solutions_texts}[/red]\n" + ) + + # When there is a single path to the solution + else: + if an_input == solution: + self.queue = an_input.split("/") + ["home"] + console.print( + f"\n[green]You guessed correctly in {round(time_dif, 2)} seconds![green]\n" + ) + # If we are already counting successes + if self.GUESS_TOTAL_TRIES > 0: + self.GUESS_CORRECTLY += 1 + self.GUESS_SUM_SCORE += time_dif + else: + console.print( + f"\n[red]You guessed wrong! The correct path would have been:\n{solution}[/red]\n" + ) + + # Compute average score and provide a result if it's the last try + if self.GUESS_TOTAL_TRIES > 0: + + self.GUESS_NUMBER_TRIES_LEFT -= 1 + if self.GUESS_NUMBER_TRIES_LEFT == 0 and self.GUESS_TOTAL_TRIES > 1: + color = ( + "green" + if self.GUESS_CORRECTLY == self.GUESS_TOTAL_TRIES + else "red" + ) + console.print( + f"[{color}]OUTCOME: You got {int(self.GUESS_CORRECTLY)} out of" + f" {int(self.GUESS_TOTAL_TRIES)}.[/{color}]\n" + ) + if self.GUESS_CORRECTLY == self.GUESS_TOTAL_TRIES: + avg = self.GUESS_SUM_SCORE / self.GUESS_TOTAL_TRIES + console.print( + f"[green]Average score: {round(avg, 2)} seconds![/green]\n" + ) + self.GUESS_TOTAL_TRIES = 0 + self.GUESS_CORRECTLY = 0 + self.GUESS_SUM_SCORE = 0 + else: + self.queue += ["guess"] + + except Exception as e: + console.print( + f"[red]Failed to load guess game from file: " + f"{obbff.GUESS_EASTER_EGG_FILE}[/red]" + ) + console.print(f"[red]{e}[/red]") + def call_update(self, _): """Process update command""" if not obbff.PACKAGED_APPLICATION: