Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
feat: add a new cli interface for random artifacts
Browse files Browse the repository at this point in the history
This is a gimmick more than anything but it's fun to make random artifacts in the cli 🙂
  • Loading branch information
trumully committed Apr 19, 2024
1 parent 7a083b9 commit 134dc3e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ ruff = "^0.4.0"
hypothesis = "^6.100.1"
pytest-cov = "^5.0.0"

[tool.poetry.scripts]
artipy = "artipy.cli:main"

[tool.ruff]
line-length = 88
fix = true
Expand Down
10 changes: 8 additions & 2 deletions src/artipy/analysis/simulate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""This module contains functions to simulate artifacts."""

import random
from typing import Optional

from artipy.artifacts import Artifact, ArtifactBuilder, utils
from artipy.types import VALID_MAINSTATS, ArtifactSlot
from artipy.types import VALID_MAINSTATS, ArtifactSet, ArtifactSlot


def create_random_artifact(slot: ArtifactSlot, rarity: int = 5) -> Artifact:
def create_random_artifact(
slot: ArtifactSlot, rarity: int = 5, artifact_set: Optional[ArtifactSet] = None
) -> Artifact:
"""Create a random artifact.
Args:
Expand All @@ -16,6 +19,8 @@ def create_random_artifact(slot: ArtifactSlot, rarity: int = 5) -> Artifact:
Returns:
artipy.artifacts.Artifact: The random artifact.
"""
if artifact_set is None:
artifact_set = random.choice(list(ArtifactSet))

max_substats = rarity - 1
substat_count = max(0, max_substats if random.random() < 0.2 else max_substats - 1)
Expand All @@ -26,6 +31,7 @@ def create_random_artifact(slot: ArtifactSlot, rarity: int = 5) -> Artifact:
.with_rarity(rarity)
.with_substats(amount=substat_count)
.with_slot(slot)
.with_set(artifact_set)
.build()
)

Expand Down
75 changes: 75 additions & 0 deletions src/artipy/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import argparse
import random

from artipy.analysis.simulate import create_random_artifact
from artipy.artifacts import ArtifactBuilder
from artipy.types import STAT_NAMES, ArtifactSlot, StatType

INVERTED_STAT_NAMES: dict[str, StatType] = {
str(v).lower(): k for k, v in STAT_NAMES.items()
}


def create_artifact(args: argparse.Namespace) -> None:
"""Create an artifact based on the passed arguments.
Args:
args (argparse.Namespace): The arguments passed into the command line
"""
if args.random:
a_slot: ArtifactSlot = ArtifactSlot(random.choice(list(ArtifactSlot)))
artifact = create_random_artifact(a_slot, args.rarity)
print(artifact)
else:
artifact = (
ArtifactBuilder()
.with_level(args.level)
.with_rarity(args.rarity)
.with_mainstat(INVERTED_STAT_NAMES[args.mainstat.lower()])
.with_substats(amount=max(0, args.rarity - 1))
.with_slot(args.slot)
.build()
)
print(artifact)


def main() -> None:
"""The main function for the artipy CLI."""
parser = argparse.ArgumentParser(prog="artipy", description="Create an artifact.")
subparsers = parser.add_subparsers()

create_parser = subparsers.add_parser("create", help="Create an artifact.")
create_parser.add_argument(
"-l", "--level", type=int, default=0, help="The level of the artifact."
)
create_parser.add_argument(
"-r", "--rarity", type=int, default=5, help="The rarity of the artifact."
)
create_parser.add_argument(
"-m",
"--mainstat",
type=str,
default="hp",
help="The mainstat of the artifact.",
)
create_parser.add_argument(
"-s",
"--slot",
type=str,
default=ArtifactSlot.FLOWER,
help="The slot of the artifact.",
)
create_parser.add_argument(
"--random",
action="store_true",
help="Generate a random artifact.",
)

create_parser.set_defaults(func=create_artifact)

args = parser.parse_args()
args.func(args) if "func" in args else parser.print_help()


if __name__ == "__main__":
main()

0 comments on commit 134dc3e

Please sign in to comment.