save game outputs

This commit is contained in:
Waylon Walker 2023-05-15 20:11:35 -05:00
parent 05bf37796a
commit da42cbb4bb
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
2 changed files with 140 additions and 89 deletions

View file

@ -1,3 +1,3 @@
from rich.console import Console from rich.console import Console
console = Console() console = Console(record=True, width=80)

View file

@ -1,11 +1,12 @@
from marvin import ai_fn from typing import List, Tuple
from rich.table import Table import atexit
from marvin import ai_fn
import pydantic import pydantic
from pydantic import Field from pydantic import Field
from typing import List, Tuple
from rich.panel import Panel from rich.panel import Panel
from rich.prompt import Prompt from rich.prompt import Prompt
from rich.table import Table
from marvin_sw_text_adventure.console import console from marvin_sw_text_adventure.console import console
@ -20,29 +21,42 @@ def create_backstory(name: str) -> str:
"create a backstory for a character" "create a backstory for a character"
class Game(pydantic.BaseModel):
name: str = Field(..., description="The name of the game")
id: str = Field(..., description="The id of the game")
class Mission(pydantic.BaseModel): class Mission(pydantic.BaseModel):
name: str = Field(..., description='The name of a brand new never before heard of star wars mission') name: str = Field(
place: str = Field(..., description='The place of the mission') ...,
leader: str = Field(..., description='The name of the leader of the mission') description="The name of a brand new never before heard of star wars mission",
year: int = Field(..., description='The year of the mission') )
place: str = Field(..., description="The place of the mission")
leader: str = Field(..., description="The name of the leader of the mission")
year: int = Field(..., description="The year of the mission")
description: str = Field( description: str = Field(
..., description='The description of the brand new never before heard of star wars mission with important words surrounded by [b][/b]') ...,
risk: int = Field(..., description="The description of the brand new never before heard of star wars mission with important words surrounded by [b][/b]",
description='The risk of the mission in health the character will loose') )
reward: int = Field(..., risk: int = Field(
description='The reward of the mission in imperial credits') ..., description="The risk of the mission in health the character will loose"
image_prompt: str = Field(..., )
description='An ai image generator prompt of the mission') reward: int = Field(
..., description="The reward of the mission in imperial credits"
)
image_prompt: str = Field(
..., description="An ai image generator prompt of the mission"
)
@property @property
def stats(self) -> Table: def stats(self) -> Table:
table = Table(title=self.name, show_edge=False, show_header=False) table = Table(title=self.name, show_edge=False, show_header=False)
# table = Table(show_header=False) # table = Table(show_header=False)
table.add_row('place', self.place) table.add_row("place", self.place)
table.add_row('year', str(self.year)) table.add_row("year", str(self.year))
table.add_row('risk', f'{self.risk} hp') table.add_row("risk", f"{self.risk} hp")
table.add_row('reward', f'{self.reward} credits') table.add_row("reward", f"{self.reward} credits")
table.add_row('leader', self.leader) table.add_row("leader", self.leader)
return table return table
@ -54,37 +68,41 @@ class Mission(pydantic.BaseModel):
class MissionResult(pydantic.BaseModel): class MissionResult(pydantic.BaseModel):
success: bool = Field(..., description='The success of the mission') success: bool = Field(..., description="The success of the mission")
imperial_credits_spent: int = Field(..., imperial_credits_spent: int = Field(
description='The imperial credits spent on the mission') ..., description="The imperial credits spent on the mission"
imperial_credits_earned: int = Field(..., )
description='The imperial credits earned on the mission') imperial_credits_earned: int = Field(
health_lost: int = Field(..., description='The health lost on the mission') ..., description="The imperial credits earned on the mission"
health_gained: int = Field(..., description='The health gained on the mission') )
story: str = Field(..., description='The story of the mission') health_lost: int = Field(..., description="The health lost on the mission")
image_prompt: str = Field(..., health_gained: int = Field(..., description="The health gained on the mission")
description='An ai image generator prompt of the mission') story: str = Field(..., description="The story of the mission")
fuel_used: int = Field(..., description='The fuel used on the mission') image_prompt: str = Field(
..., description="An ai image generator prompt of the mission"
)
fuel_used: int = Field(..., description="The fuel used on the mission")
class Ship(pydantic.BaseModel): class Ship(pydantic.BaseModel):
name: str = Field(..., description='The name of the ship') name: str = Field(..., description="The name of the ship")
description: str = Field(..., description='The description of the ship') description: str = Field(..., description="The description of the ship")
capacity: int = Field(..., description='The capacity of the ship') capacity: int = Field(..., description="The capacity of the ship")
ship_type: str = Field(..., description='The type of the ship') ship_type: str = Field(..., description="The type of the ship")
year_built: int = Field(..., description='The year built of the ship') year_built: int = Field(..., description="The year built of the ship")
capacity: int = Field(..., description='The capacity of the ship') capacity: int = Field(..., description="The capacity of the ship")
fuel_type: str = Field(..., description='The fuel type of the ship') fuel_type: str = Field(..., description="The fuel type of the ship")
fuel_level: int = Field(..., description='The fuel level of the ship') fuel_level: int = Field(..., description="The fuel level of the ship")
image_prompt: str = Field(..., image_prompt: str = Field(
description='An ai image generator prompt of the mission') ..., description="An ai image generator prompt of the mission"
)
@property @property
def stats(self) -> Table: def stats(self) -> Table:
table = Table(title=self.name, show_edge=False, show_header=False) table = Table(title=self.name, show_edge=False, show_header=False)
table.add_row('year built', str(self.year_built)) table.add_row("year built", str(self.year_built))
table.add_row('capacity', str(self.capacity)) table.add_row("capacity", str(self.capacity))
table.add_row('fuel level', str(self.fuel_level)) table.add_row("fuel level", str(self.fuel_level))
return table return table
@property @property
@ -95,40 +113,50 @@ class Ship(pydantic.BaseModel):
class StarWarsCharacter(pydantic.BaseModel): class StarWarsCharacter(pydantic.BaseModel):
name: str = Field(..., name: str = Field(
description='The name of a brand new never before heard of star wars character') ...,
imperial_credits: int = Field(..., description="The name of a brand new never before heard of star wars character",
description='The imperial credits of the character') )
health: int = Field(..., description='The health of the character') imperial_credits: int = Field(
home_planet: str = Field(..., ..., description="The imperial credits of the character"
description='The name of the planet where the character was born') )
age: int = Field(..., description='The age of the character') health: int = Field(..., description="The health of the character")
birth_year_bby: int = Field(..., description='The birth year of the character') home_planet: str = Field(
backstory: str = Field(..., description='The backstory of the character') ..., description="The name of the planet where the character was born"
ship: Ship = Field(..., )
description='The name of the ship where the character operates') age: int = Field(..., description="The age of the character")
side: str = Field(..., description='The side of the character') birth_year_bby: int = Field(..., description="The birth year of the character")
city: str = Field(..., description='The name of the city where the character lives') backstory: str = Field(..., description="The backstory of the character")
friends: List[str] = Field(..., ship: Ship = Field(
description='The names of the friends of the character') ..., description="The name of the ship where the character operates"
team: str = Field(..., )
description='The name of the team that the character belongs to') side: str = Field(..., description="The side of the character")
enemies: List[str] = Field(..., city: str = Field(..., description="The name of the city where the character lives")
description='The names of the enemies of the character') friends: List[str] = Field(
mission: Mission = Field(..., description='The current mission of the character') ..., description="The names of the friends of the character"
role: str = Field(..., description='The role of the character') )
image_prompt: str = Field(..., team: str = Field(
description='An ai image generator prompt of the mission') ..., description="The name of the team that the character belongs to"
)
enemies: List[str] = Field(
..., description="The names of the enemies of the character"
)
mission: Mission = Field(..., description="The current mission of the character")
role: str = Field(..., description="The role of the character")
image_prompt: str = Field(
..., description="An ai image generator prompt of the mission"
)
previous_missions: List[Tuple[Mission, MissionResult]] = Field( previous_missions: List[Tuple[Mission, MissionResult]] = Field(
[], description='The previous missions of the character') [], description="The previous missions of the character"
)
@property @property
def stats(self) -> Table: def stats(self) -> Table:
table = Table(title=self.name, show_edge=False, show_header=False) table = Table(title=self.name, show_edge=False, show_header=False)
# table = Table(show_header=False) # table = Table(show_header=False)
table.add_row('health', str(self.health)) table.add_row("health", str(self.health))
table.add_row('imperial credits', str(self.imperial_credits)) table.add_row("imperial credits", str(self.imperial_credits))
table.add_row('fuel level', str(self.ship.fuel_level)) table.add_row("fuel level", str(self.ship.fuel_level))
return table return table
@property @property
@ -139,7 +167,11 @@ class StarWarsCharacter(pydantic.BaseModel):
table.add_row(self.ship.stats, self.ship.description) table.add_row(self.ship.stats, self.ship.description)
table.add_row() table.add_row()
table.add_row(self.mission.stats, self.mission.description) table.add_row(self.mission.stats, self.mission.description)
return Panel(table, title=f'{self.name}\'s Mission Card', title_align='left', ) return Panel(
table,
title=f"{self.name}'s Mission Card",
title_align="left",
)
@ai_fn @ai_fn
@ -148,25 +180,44 @@ def did_complete_mission(character: StarWarsCharacter, action: str) -> MissionRe
@ai_fn @ai_fn
def get_next_mission(character: StarWarsCharacter, action: str, last_mission_success: bool) -> Mission: def get_next_mission(
character: StarWarsCharacter, action: str, last_mission_success: bool
) -> Mission:
"""given a character, their action and the last mission success, return the next mission""" """given a character, their action and the last mission success, return the next mission"""
@ai_fn
def create_game(character: StarWarsCharacter) -> Game:
"create a new game"
@ai_fn @ai_fn
def create_character() -> StarWarsCharacter: def create_character() -> StarWarsCharacter:
"create a new character" "create a new character"
console.save_text(f"{game.name}-{character.name}-{game.id}.txt")
def game(): def game():
console.print('generating your character') console.print("generating your character")
prompt = Prompt() prompt = Prompt()
character = create_character() character = create_character()
game = create_game(character)
while character.health > 0 and character.imperial_credits > 0 and character.ship.fuel_level > 0: def save():
console.save_text(f"{game.name}-{character.name}-{game.id}.txt")
atexit.register(save)
while (
character.health > 0
and character.imperial_credits > 0
and character.ship.fuel_level > 0
):
console.print(character.describe) console.print(character.describe)
action = prompt.ask('What do you do ') action = prompt.ask("What do you do ")
result = did_complete_mission(character, action) result = did_complete_mission(character, action)
character.previous_missions.append((character.mission, result)) character.previous_missions.append((character.mission, result))
# keep only the last 5 missions # keep only the last 5 missions
@ -177,21 +228,21 @@ def game():
character.health += result.health_gained character.health += result.health_gained
character.ship.fuel_level -= result.fuel_used character.ship.fuel_level -= result.fuel_used
console.print() console.print()
console.print('Your mission has been completed') console.print("Your mission has been completed")
console.print(result.story) console.print(result.story)
console.print(f'You earned {result.imperial_credits_earned} imperial credits') console.print(f"You earned {result.imperial_credits_earned} imperial credits")
console.print(f'You spent {result.imperial_credits_spent} imperial credits') console.print(f"You spent {result.imperial_credits_spent} imperial credits")
console.print(f'You gained {result.health_gained} health') console.print(f"You gained {result.health_gained} health")
console.print(f'You lost {result.health_lost} health') console.print(f"You lost {result.health_lost} health")
character.mission = get_next_mission(character, action, result.success) character.mission = get_next_mission(character, action, result.success)
if character.health <= 0: if character.health <= 0:
console.print('You are dead') console.print("You are dead")
if character.imperial_credits <= 0: if character.imperial_credits <= 0:
console.print('You lost all your imperial credits') console.print("You lost all your imperial credits")
if character.ship.fuel_level <= 0: if character.ship.fuel_level <= 0:
console.print('You lost all your fuel') console.print("You lost all your fuel")
if __name__ == '__main__': if __name__ == "__main__":
game() game()