Made all commands case insensitive

Added truescrollutil, for converting actual brother numbers to scrolls, and vice versa
This commit is contained in:
Jacob Henry 2016-11-24 19:49:30 +00:00
parent d76e69c197
commit f4dc6e06a5
2 changed files with 79 additions and 0 deletions

72
TrueScrollUtil.py Normal file
View File

@ -0,0 +1,72 @@
"""
This file handles conversion of users scrolls into numbers
"""
import re
from SlackUtil import reply
p = re.compile("truescroll\\s*(\d*)$")
def handleTrueScrollMsg(slack, msg):
text = msg['text']
response = None
m = p.match(text)
if not m:
response = "Could not parse your query. Please invoke as \"truescroll <scroll#>\""
else:
num = m.group(1)
brotherScroll = getBrotherScroll(num)
trueScroll = getTrueScroll(num)
response = "The brother with scroll {0} is in fact the {1} brother to sign\n"
response += "The {2} brother to sign will have scroll {3}\n"
response = response.format(num, trueScroll, num, brotherScroll)
reply(slack, msg, response, username = "scrollbot")
brotherNums = [str(x) for x in range(10) if (not x == 3)]
trueNums = [str(x) for x in range(10)]
def getTrueScroll(brotherScroll):
return convertBase(brotherScroll, brotherNums, trueNums)
def getBrotherScroll(trueScroll):
return convertBase(trueScroll, trueNums, brotherNums)
def convertBase(numStr, srcBaseNums, targBaseNums):
#Returns int value
def numberFromBase(ns, numerals):
base = len(numerals)
basePower = 1
total = 0
#Go by character.
for c in ns[::-1]:
try:
digitVal = numerals.index(c)
total += digitVal * basePower
except ValueError:
total += 0
basePower = basePower * base
return total
#Returns string array, each elt is the corresponding numeral
def numberToBase(n, numerals):
if n==0:
return [0]
digits = []
while n:
digVal = int(n % len(numerals))
digits.append(numerals[digVal])
n /= len(numerals)
n = int(n)
return "".join(digits[::-1])
return numberToBase(numberFromBase(numStr, srcBaseNums), targBaseNums)

View File

@ -4,6 +4,7 @@ from SlackUtil import *
from WaitonUtil import handleWaitonMsg #For waitons
from ScrollUtil import handleScrollMsg #For scrolls
from TrueScrollUtil import handleTrueScrollMsg #For true scrolls
import re
@ -44,6 +45,7 @@ def handleProfilelessScum(slack, msg, user, for_user=None):
waiton_pattern = re.compile("^waiton")
scroll_pattern = re.compile("^scroll")
true_scroll_pattern = re.compile("^truescroll")
housejob_pattern = re.compile("^(house)?job")
def main():
@ -69,6 +71,7 @@ def main():
#Handle Message
text = msg['text'].lower()
msg['text'] = text
if not isValidProfile(user):#invalid profile
print("Received profileless")
handleProfilelessScum(slack, msg, user)
@ -85,6 +88,10 @@ def main():
print("Received scroll from " + user['user']['name'])
handleScrollMsg(slack, msg)
elif true_scroll_pattern.match(text):
print("Received true scroll from " + user['user']['name'])
handleTrueScrollMsg(slack, msg)
elif housejob_pattern.match(text):
print("Received housejob from " + user['user']['name'])
reply(slack, msg, "I cannot do that (yet)", username="sadbot")