]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandDSACheck.py
README.md
[pub/jan/mattermost-bot.git] / modules / CommandDSACheck.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2021 by @top_left <gisilfrid@protonmail.com>
3 #  published under MIT-License
4
5 from random import randint
6 import re
7 import math
8
9 from AbstractCommand import *
10
11 class CommandDSACheck(AbstractCommand):
12     TRIGGER = "dsacheck"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "n [n n] [(+|-)b] [m]",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Directly do an attribute or skill check, optionally with modifications and skill points"
17
18
19     def on_POST(self, request, data):
20         if re.search(r"^\d{1,2}( \+\d{1,3}| -\d{1,3})?$", data["text"]):
21             tokens = data["text"].split(" ")
22             attribute = int(tokens[0])
23             msg = "Checking for {}".format(attribute)
24             modification = 0
25             if len(tokens) == 1:
26                 msg += ":\n"
27             else:
28                 modification = int(tokens[1])
29                 msg += " modified by {}:\n".format(modification)
30             throw = randint(1, 20)
31             modified_attribute = attribute + modification
32             if modified_attribute <= 0:
33                 msg += "Failed, attribute below 1"
34             elif throw == 1:
35                 msg += "Critical Success"
36             elif throw == 20:
37                 msg += "Botched"
38             elif throw <= modified_attribute:
39                 msg += "Success ({})".format(throw)
40             else:
41                 msg += "Failed ({})".format(throw)
42
43         elif re.search(r"^\d{1,2} \d{1,2} \d{1,2}( \+\d{1,3}| -\d{1,3})?( \d{1,3})?$", data["text"]):
44             tokens = data["text"].split(" ")
45             attributes = tokens[:3]
46             tokens = tokens[3:]
47             msg = "Checking for {}".format(attributes)
48
49             modification = 0
50             if len(tokens) > 0 and (tokens[0][0] == "+" or tokens[0][0] == "-"):
51                 modification = int(tokens[0])
52                 tokens = tokens[1:]
53                 msg += " modified by {}".format(modification)
54
55             skill_points = 0
56             if len(tokens) > 0:
57                 skill_points = int(tokens[0])
58                 msg += " with {} skill points".format(skill_points)
59
60             msg += ":\n"
61
62             attributes = [int(attribute) + modification for attribute in attributes]
63             if any(attribute < 1 for attribute in attributes):
64                 msg += "Failed, attribute below 1"
65             else :
66                 success = True
67                 throws = []
68                 for attribute in attributes:
69                     throw = randint(1, 20)
70                     throws.append(throw)
71                     if attribute + skill_points < throw:
72                         success = False
73                     elif attribute < throw:
74                         skill_points -= throw - attribute
75                 ones = len([throw for throw in throws if throw == 1])
76                 twenties = len([throw for throw in throws if throw == 20])
77                 if ones == 2:
78                     msg += "Critical Success"
79                 elif ones == 3:
80                     msg += "Spectacular Success"
81                 elif twenties == 2:
82                     msg += "Botched"
83                 elif twenties == 3:
84                     msg += "Botched spectacularly"
85                 elif not success:
86                     msg += "Failure ({})".format(throws)
87                 else:
88                     quality_level = math.ceil(skill_points / 3)
89                     if quality_level == 0:
90                         quality_level = 1
91                     msg += "Success ({}), QS: {}".format(throws, quality_level)
92
93         else:
94             request.cmd_respond_text_temp(
95                 "Possible invocations are either m [(+|-)b] or m n o [(+|-)b] [x] where n, m, o is between 1 and 99 and b and x can be any integers between -999 and 999 "
96             )
97             return
98
99         request.cmd_respond_text_chan(msg)