mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
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 <aptekarev@gmail.com> Co-authored-by: James Maslek <jmaslek11@gmail.com>
This commit is contained in:
8
guess_game.json
vendored
Normal file
8
guess_game.json
vendored
Normal file
@@ -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"
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
115
terminal.py
115
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:
|
||||
|
||||
Reference in New Issue
Block a user