-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced.py
80 lines (60 loc) · 1.57 KB
/
advanced.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
This is an advanced version of the roller script,
it functions as an arbitrary dice calculator
but it can also store and use character information in
its calculations
"""
from random import randint
import json
import click
import re
diceMatcher = re.compile('\d\d*d\d\d*')
def toString(tup):
return ' '.join(list(tup))
def save(char):
with open(char['name']+'.json', 'w') as f:
json.dump(char, f)
def load(charName):
with open(charName+'.json', 'r') as f:
return json.load(f)
def isDice(dice):
return diceMatcher.match(dice) is not None
def rollDie(die):
return randint(1, die)
def rollDice(dice):
n, d = dice.split('d')
return sum( [ rollDie(int(d)) for _ in range(int(n)) ] )
def filterDice(expression):
return " ".join([ str(rollDice(e)) if isDice(e) else e for e in expression.split() ])
def filterAttributes(expression):
def f(e):
name, att = e.split('.')
try:
char = load(name)
return char[att]
except:
click.echo('invalid character name')
return " ".join([f(e) if '.' in e else e for e in expression.split()])
@click.group()
def cli():
pass
@cli.command()
@click.argument('name')
def create(name):
save({'name': name})
@cli.command()
@click.argument("att")
@click.argument("value", nargs=-1)
def modify(att, value):
try:
name, attribute = att.split('.')
char = load(name)
char[attribute] = eval(filterDice(filterAttributes(toString(value))))
save(char)
except:
click.echo('error')
@cli.command()
@click.argument("expression", nargs=-1)
def roll(expression):
click.echo(eval(filterDice(filterAttributes(toString(expression)))))
cli()