add ctech dice rolling method, for cthulhutech.
does greatest/best set/best straight methods. use normal roll for stuff that should be summed, like damages
This commit is contained in:
parent
27ff7e257d
commit
47022f9428
104
modules/Dice.py
104
modules/Dice.py
|
@ -14,6 +14,7 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import math
|
||||||
import re
|
import re
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -94,6 +95,109 @@ class Dice(Module):
|
||||||
reply += "; "
|
reply += "; "
|
||||||
if reply is not "":
|
if reply is not "":
|
||||||
return self.reply(connection, replypath, nick + ': ' + reply)
|
return self.reply(connection, replypath, nick + ': ' + reply)
|
||||||
|
if whats[0] == 'ctech':
|
||||||
|
rollitrs = re.split(';\s*', ' '.join(whats[1:]))
|
||||||
|
reply = ""
|
||||||
|
for count, roll in enumerate(rollitrs):
|
||||||
|
pattern = '^(\d+)d(?:(\+|\-)(\d+))?(?:\s+(.*))?'
|
||||||
|
regex = re.compile(pattern)
|
||||||
|
matches = regex.search(roll)
|
||||||
|
if matches is not None:
|
||||||
|
dice = int(matches.group(1))
|
||||||
|
comment = ''
|
||||||
|
modifier = 0
|
||||||
|
|
||||||
|
if matches.group(2) is not None and matches.group(3) is not None:
|
||||||
|
if str(matches.group(2)) == '-':
|
||||||
|
modifier = -1 * int(matches.group(3))
|
||||||
|
else:
|
||||||
|
modifier = int(matches.group(3))
|
||||||
|
|
||||||
|
if matches.group(4) is not None:
|
||||||
|
comment = matches.group(4)
|
||||||
|
|
||||||
|
result = roll + ': '
|
||||||
|
|
||||||
|
rolls = []
|
||||||
|
for d in range(dice):
|
||||||
|
rolls.append(random.randint(1, 10))
|
||||||
|
rolls.sort()
|
||||||
|
rolls.reverse()
|
||||||
|
|
||||||
|
# highest single die method
|
||||||
|
method1 = rolls[0]
|
||||||
|
|
||||||
|
# highest set method
|
||||||
|
method2 = 0
|
||||||
|
rolling_sum = 0
|
||||||
|
for i, r in enumerate(rolls):
|
||||||
|
# if next roll is same as current, sum and continue, else see if sum is best so far
|
||||||
|
if i+1 < len(rolls) and rolls[i+1] == r:
|
||||||
|
if rolling_sum == 0:
|
||||||
|
rolling_sum = r
|
||||||
|
rolling_sum += r
|
||||||
|
else:
|
||||||
|
if rolling_sum > method2:
|
||||||
|
method2 = rolling_sum
|
||||||
|
rolling_sum = 0
|
||||||
|
# check for set in progress (e.g. lots of 1s)
|
||||||
|
if rolling_sum > method2:
|
||||||
|
method2 = rolling_sum
|
||||||
|
|
||||||
|
# straight method
|
||||||
|
method3 = 0
|
||||||
|
rolling_sum = 0
|
||||||
|
count = 0
|
||||||
|
for i, r in enumerate(rolls):
|
||||||
|
# if next roll is one less as current, sum and continue, else check len and see if sum is best so far
|
||||||
|
if i+1 < len(rolls) and rolls[i+1] == r-1:
|
||||||
|
if rolling_sum == 0:
|
||||||
|
rolling_sum = r
|
||||||
|
count += 1
|
||||||
|
rolling_sum += r-1
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
if count >= 3 and rolling_sum > method3:
|
||||||
|
method3 = rolling_sum
|
||||||
|
rolling_sum = 0
|
||||||
|
# check for straight in progress (e.g. straight ending in 1)
|
||||||
|
if count >= 3 and rolling_sum > method3:
|
||||||
|
method3 = rolling_sum
|
||||||
|
|
||||||
|
# get best roll
|
||||||
|
best = max([method1, method2, method3])
|
||||||
|
|
||||||
|
# check for critical failure
|
||||||
|
botch = False
|
||||||
|
ones = 0
|
||||||
|
for r in rolls:
|
||||||
|
if r == 1:
|
||||||
|
ones += 1
|
||||||
|
if ones >= math.ceil(float(len(rolls))/2):
|
||||||
|
botch = True
|
||||||
|
|
||||||
|
if botch:
|
||||||
|
result += 'BOTCH'
|
||||||
|
else:
|
||||||
|
result += str(best + modifier)
|
||||||
|
rollres = ''
|
||||||
|
for i,r in enumerate(rolls):
|
||||||
|
rollres += str(r)
|
||||||
|
if i is not len(rolls)-1:
|
||||||
|
rollres += ','
|
||||||
|
result += ' [' + rollres
|
||||||
|
if modifier != 0:
|
||||||
|
if modifier > 0:
|
||||||
|
result += ' +' + str(modifier)
|
||||||
|
else:
|
||||||
|
result += ' -' + str(modifier * -1)
|
||||||
|
result += ']'
|
||||||
|
|
||||||
|
reply += result
|
||||||
|
if count is not len(rollitrs)-1:
|
||||||
|
reply += "; "
|
||||||
|
if reply is not "":
|
||||||
|
return self.reply(connection, replypath, nick + ': ' + reply)
|
||||||
|
|
||||||
# vi:tabstop=4:expandtab:autoindent
|
# vi:tabstop=4:expandtab:autoindent
|
||||||
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|
||||||
|
|
Loading…
Reference in New Issue