mirror of
https://github.com/flancian/garden.git
synced 2026-08-08 07:26:21 +00:00
autopushed
This commit is contained in:
+23
-12
@@ -1,20 +1,17 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#
|
|
||||||
# https://click.palletsprojects.com/en/8.1.x/arguments/
|
# https://click.palletsprojects.com/en/8.1.x/arguments/
|
||||||
# https://click.palletsprojects.com/en/8.1.x/options/
|
# https://click.palletsprojects.com/en/8.1.x/options/
|
||||||
#
|
|
||||||
# boilerplate shamelessly cloned from prime.py :).
|
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import math
|
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class AgoraCmd(click.Command):
|
class AgoraCmd(click.Command):
|
||||||
def format_help(self, ctx, formatter):
|
def format_help(self, ctx, formatter):
|
||||||
click.echo("""Usage:
|
click.echo("""Usage:
|
||||||
- Visit anagora.org/dice to execute this file in the Agora of Flancia.
|
- Visit anagora.org/dice to execute this file in the Agora of Flancia.
|
||||||
- Visit e.g. anagora.org/dice/17 to throw a 17-sided die, and others :)
|
- Visit e.g. anagora.org/dice/20 to roll a d20 (and see rolls for all smaller dice!).
|
||||||
- In general visit anagora.org/foo, anagora.org/foo/bar to execute e.g. <bin/foo.py bar> from your garden.
|
- In general visit anagora.org/foo, anagora.org/foo/bar to execute e.g. <bin/foo.py bar> from your garden.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@@ -31,23 +28,37 @@ class AgoraCmd(click.Command):
|
|||||||
except SystemExit:
|
except SystemExit:
|
||||||
sys.exit(exc.exit_code)
|
sys.exit(exc.exit_code)
|
||||||
|
|
||||||
def rand(n):
|
|
||||||
|
def roll_tower(n):
|
||||||
proof = []
|
proof = []
|
||||||
for i in range(2, n + 1):
|
for i in range(2, n + 1):
|
||||||
r = random.randint(1, i)
|
r = random.randint(1, i)
|
||||||
if i == 2:
|
if i == 2:
|
||||||
proof.append(f"You throw a fair coin and it lands [[{bool(r-1) and 'heads' or 'tails'}]].")
|
coin = "heads" if r == 2 else "tails"
|
||||||
|
proof.append(f"Coin flip (d2): [[{coin}]].")
|
||||||
else:
|
else:
|
||||||
proof.append(f"You throw a die of [[{i}]] sides and it comes up [[{r}]].")
|
proof.append(f"d[[{i}]] roll: [[{r}]].")
|
||||||
|
return list(reversed(proof))
|
||||||
|
|
||||||
return "\n".join(reversed(proof))
|
|
||||||
|
|
||||||
@click.command(cls=AgoraCmd)
|
@click.command(cls=AgoraCmd)
|
||||||
@click.argument('n', type=click.INT)
|
@click.argument('n', type=click.INT)
|
||||||
def dice(n):
|
def dice(n):
|
||||||
"""A simple randomness generator, imitating those we know and love from R^4 :)."""
|
"""Simulates rolling a d[[n]] die along with a polyhedral dice tower."""
|
||||||
proof = rand(n)
|
if n < 1:
|
||||||
click.echo(proof)
|
click.echo("Please provide a die size of at least 1.")
|
||||||
|
return
|
||||||
|
|
||||||
|
main_roll = random.randint(1, n)
|
||||||
|
click.echo(f"🎲 Rolling a d[[{n}]]: result is [[{main_roll}]]!\n")
|
||||||
|
|
||||||
|
if n > 1:
|
||||||
|
click.echo(f"Dice tower rolls from d[[2]] up to d[[{n}]]:")
|
||||||
|
tower = roll_tower(n)
|
||||||
|
for line in tower:
|
||||||
|
click.echo(f" • {line}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
dice()
|
dice()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user