-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__init__.py
55 lines (45 loc) · 2.16 KB
/
__init__.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
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill, intent_handler, intent_file_handler
from mycroft.util.log import LOG
from mycroft.util.parse import extract_number, normalize
import random
import re
class DiceSkill(MycroftSkill):
def __init__(self):
super(DiceSkill, self).__init__(name="DiceSkill")
@intent_file_handler("Dice.intent")
def handle_roll_dice_intent(self, message):
dice_count = message.data.get('count') or "1"
dice_count = int(extract_number(dice_count))
dice_type = message.data.get('type') or "6"
modificator = message.data.get('modifier')
if modificator:
modificator = int(extract_number(modificator))
self.roll(dice_count, int(dice_type), modificator)
@intent_file_handler("NormalDice.intent")
def handle_roll_normal_intent(self, message):
dice_count = message.data.get('count') or "1"
dice_count = int(extract_number(dice_count))
self.roll(dice_count, 6, None)
def roll(self, dice_count, dice_range, modificator):
numbers = [random.randint(1, dice_range) for i in range(dice_count)]
if modificator:
total = sum(numbers) + modificator
if len(numbers) == 1:
self.speak_dialog("single.modificator.result" , data={"result" : numbers[0], "total" : total})
else:
self.speak_dialog("multiple.modificator.result", data={"result" : ', '.join([str(x) for x in numbers]), "total": total})
else:
if len(numbers) == 1:
self.speak_dialog("single.result", data={"result" : numbers[0]})
else:
self.speak_dialog("multiple.result", data={"result" : ', '.join([str(x) for x in numbers])})
@intent_file_handler("Range.intent")
def handle_generate_number_intent(self, message):
low = extract_number(message.data.get('low'))
high = extract_number(message.data.get('high'))
if low > high:
low, high = high, low
self.speak_dialog("single.result", data={"result" : random.randint(low, high)})
def create_skill():
return DiceSkill()