-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
174 lines (141 loc) · 4.57 KB
/
utils.py
File metadata and controls
174 lines (141 loc) · 4.57 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
An OpenLegend RPG dice bot for Discord servers.
Copyright (C) 2021 Utku Erol
Contact: utku.erol@icloud.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import xdice
def parse_msg(message_content):
attr_score = 0
adv = 0
vicious = False
repeat = False
destructive = False
repeat_factor = 1
args = message_content.split(" ")[1:]
for arg in args:
if arg == "-V" or arg == "-v":
vicious = True
elif arg == "-R" or arg == "-r":
repeat = True
elif arg == "-D" or arg == "-d":
destructive = True
args = [a for a in args if a.lstrip("+-").isdigit()]
if len(args) < 0 or len(args) > 3:
return -1
if len(args) == 0:
attr_score = 0
adv = 0
else:
attr_score = args[0]
if len(args) >= 2:
adv = args[1]
if int(adv) > 10:
adv = 10
if repeat and len(args) == 3:
repeat_factor = args[2]
return vicious, destructive, int(attr_score), int(adv), int(repeat_factor)
def roll20(adv, vicious, destructive):
d20_count = 1 + abs(adv)
if adv >= 0:
score = xdice.rolldice(20, d20_count, adv, 0)
elif adv < 0:
score = xdice.rolldice(20, d20_count, 0, abs(adv))
rolls = []
dropped = score.dropped
dropped_vicious = []
for res in list(score):
roll = [int(res)]
while res == 20 or (destructive and res == 19):
if vicious:
score = xdice.rolldice(20, 2, 1, 0)
else:
score = xdice.rolldice(20, 1, 0, 0)
res = int(score)
roll.append(res)
dropped_vicious = score.dropped
rolls.append(roll)
return rolls, dropped, dropped_vicious
def rollAttr(dcount, dsize, adv, destructive):
dcount += abs(adv)
if adv >= 0:
score = xdice.rolldice(dsize, dcount, adv, 0)
elif adv < 0:
score = xdice.rolldice(dsize, dcount, 0, abs(adv))
rolls = []
dropped = score.dropped
for res in list(score):
roll = [int(res)]
while res == dsize or (destructive and res == dsize - 1):
score = xdice.rolldice(dsize, 1, 0, 0)
res = int(score)
roll.append(res)
rolls.append(roll)
return rolls, dropped
def calculate_result(vicious, destructive, attr_score, adv, repeat_factor):
results = []
dcount, dsize = get_dice_from_attr(int(attr_score))
if dcount > 0:
info = "{}d{}".format(dcount, dsize)
for i in range(repeat_factor):
result = (roll20(0, vicious, destructive), rollAttr(
dcount, dsize, adv, destructive))
results.append(result)
else:
info = "0"
for i in range(repeat_factor):
result = (roll20(adv, vicious, destructive), ([[0]], [0]))
results.append(result)
return results, info
def get_dice_from_attr(attr_score):
count, size = 0, 0
if attr_score == 0:
return count, size
elif attr_score == 1:
count, size = 1, 4
return count, size
elif attr_score == 2:
count, size = 1, 6
return count, size
elif attr_score == 3:
count, size = 1, 8
return count, size
elif attr_score == 4:
count, size = 1, 10
return count, size
elif attr_score == 5:
count, size = 2, 6
return count, size
elif attr_score == 6:
count, size = 2, 8
return count, size
elif attr_score == 7:
count, size = 2, 10
return count, size
elif attr_score == 8:
count, size = 3, 8
return count, size
elif attr_score == 9:
count, size = 3, 10
return count, size
elif attr_score == 10:
count, size = 4, 8
return count, size
elif attr_score > 10:
count, size = 4, 8
return count, size
def roll_raw(pattern):
try:
xdice_pattern = xdice.Pattern(pattern)
except ValueError:
return -1
return xdice_pattern.roll()