Make react provide useful error messages

pull/10/head
Stijn De Clercq 2020-12-17 01:08:01 +01:00
parent 13ce50f4fa
commit c882179b0d
1 changed files with 173 additions and 77 deletions

View File

@ -1,13 +1,167 @@
def check(content, earlier=[]):
# No arguments passed
if len(content) < 1:
return False, ["Controleer je argumenten."]
if any(not (x.isalpha() or x.isdigit() or x in ["#", "*", "!", "?"]) for x in content):
return False, ["Dit is geen geldig woord."]
if content[-1].isdigit() and len(content[-1]) > 10:
content[0] = "".join(x for x in content[:-1])
else:
content[0] = "".join(x for x in content)
# This command should work case insensitive
for i, word in enumerate(content):
content[i] = word.lower()
invalid_characters = []
# Check valid characters
for word in content:
for letter in word:
if letter not in allowedCharacters():
invalid_characters.append(letter)
# Get uniques
invalid_characters = list(set(invalid_characters))
# Invalid char was passed
if len(invalid_characters) == 1:
return False, ["**{}** is geen geldige letter.".format(invalid_characters[0])]
# Multiple invalid chars were passed
if len(invalid_characters) > 1:
return False, ["**{}** en **{}** zijn geen geldige letters.".format(
", ".join(invalid_characters[:-1]), invalid_characters[-1]
)]
# Check if ID was passed or not, remove from list of words if so
# Also join everything into one word
if content[-1].isdigit() and len(content[-1]) > 10:
content[0] = "".join(content[:-1])
else:
content[0] = "".join(content)
# In case a space was passed using "a b", strip it out as well
content[0] = content[0].replace(" ", "")
doubles = getDoubles()
nums = getNumbers()
specials = getSpecialCharacters()
unidic = getUnicodeDict()
# Check for reactions that were already added earlier
for x in earlier:
# Check if reaction is a random reaction or a letter/number
if x in unidic.values():
word = ""
# Find the key used, remove it from the list of remaining available letters
for key in unidic:
if unidic[key] == x:
word = key
break
del unidic[word]
# Same thing for doubles
for k in list(doubles.keys()):
if word in doubles[k]:
doubles[k].remove(word)
if len(doubles[k]) == 0:
del doubles[k]
break
# Same thing for numbers
for k in list(nums.keys()):
if nums[k] == word:
del nums[k]
# Same thing for special characters
for k in list(specials.keys()):
if word in specials[k]:
specials[k].remove(word)
if len(specials[k]) == 0:
del specials[k]
# Check if earlier letters made this reaction impossible
for letter in content[0]:
c = content[0].count(letter)
# If a letter was added twice, but there is only one, that's an issue
if c != 1 and letter not in doubles:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
elif c > 1 and letter in doubles and len(doubles[letter]) < c:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
# Array of emoji's
arr = []
# Start checking every character
for letter in content[0]:
# Letters
if letter.isalpha():
# Check if this letter has not been added yet
if "regional_indicator_{}".format(letter) in unidic and unidic["regional_indicator_{}".format(letter)] not in arr:
arr.append(unidic["regional_indicator_{}".format(letter)])
# Remove this letter as an option from the list of doubles
if letter in doubles:
doubles[letter] = doubles[letter][1:]
# Letter has been added before, but there's a double for it
elif letter in doubles:
if len(doubles[letter]) == 0:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
# Remove the number-equivalent from nums if it is used as a substitute here
arr.append(unidic[doubles[letter][0]])
if doubles[letter][0] in nums.values():
for k in nums:
if nums[k] == doubles[letter][0]:
del nums[k]
break
# Remove this character from the list of available doubles
doubles[letter] = doubles[letter][1:]
# Remove from the dictionary of doubles if the last double was used
if len(doubles[letter]) == 0:
del doubles[letter]
else:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
# Special characteres
elif letter in specials:
# No more options
if len(specials[letter]) == 0:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
# Add it to the array & remove from the list of options
arr.append(unidic[specials[letter][0]])
specials[letter].pop(0)
if len(specials[letter]) == 0:
del specials[letter]
# Number
else:
# Number was used before as a double for a letter
if letter not in nums:
return False, ["Er zijn niet genoeg **{}**'s om dit woord te reacten.".format(letter)]
arr.append(unidic[nums[letter]])
# Add this emoji to the array, remove it as a double everywhere
for x in doubles:
# Remove this number as a substitute if it is used anywhere
if nums[letter] == doubles[x][-1]:
doubles[x] = doubles[x][:-1]
if len(doubles[x]) == 0:
del doubles[x]
break
return True, arr
def allowedCharacters():
return ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z', "!", "?", "#", "*", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
def getDoubles():
doubles = {
"a": ["regional_indicator_a", "a", "four"],
"b": ["regional_indicator_b", "b"],
@ -22,6 +176,10 @@ def check(content, earlier=[]):
"?": ["question", "grey_question"]
}
return doubles
def getNumbers():
nums = {
"0": "zero",
"1": "one",
@ -35,6 +193,10 @@ def check(content, earlier=[]):
"9": "nine"
}
return nums
def getSpecialCharacters():
specials = {
"?": ["question", "grey_question"],
"!": ["exclamation", "grey_exclamation"],
@ -42,6 +204,10 @@ def check(content, earlier=[]):
"#": ["hash"]
}
return specials
def getUnicodeDict():
unidic = {
"regional_indicator_a": "🇦",
"regional_indicator_b": "🇧",
@ -93,74 +259,4 @@ def check(content, earlier=[]):
"asterisk": "*️⃣"
}
for x in earlier:
if x in unidic.values():
word = ""
for key in unidic:
if unidic[key] == x:
word = key
break
del unidic[word]
for k in list(doubles.keys()):
if word in doubles[k]:
doubles[k].remove(word)
if len(doubles[k]) == 0:
del doubles[k]
break
for k in list(nums.keys()):
if nums[k] == word:
del nums[k]
for k in list(specials.keys()):
if word in specials[k]:
specials[k].remove(word)
if len(specials[k]) == 0:
del specials[k]
for letter in content[0]:
c = content[0].count(letter)
if c != 1 and letter not in doubles:
return False, ["Dit is geen geldig woord."]
elif c > 1 and letter in doubles and len(doubles[letter]) < c:
return False, ["Dit is geen geldig woord."]
arr = []
for letter in content[0]:
if letter.isalpha():
if "regional_indicator_{}".format(letter) in unidic and unidic["regional_indicator_{}".format(letter)] not in arr:
arr.append(unidic["regional_indicator_{}".format(letter)])
if letter in doubles:
doubles[letter] = doubles[letter][1:]
elif letter in doubles:
if len(doubles[letter]) == 0:
return False, ["Dit is geen geldig woord."]
# Remove the number-equivalent from nums if it is used as a substitute here
arr.append(unidic[doubles[letter][0]])
if doubles[letter][0] in nums.values():
for k in nums:
if nums[k] == doubles[letter][0]:
del nums[k]
break
doubles[letter] = doubles[letter][1:]
if len(doubles[letter]) == 0:
del doubles[letter]
else:
return False, ["Dit is geen geldig woord."]
elif letter in specials:
if len(specials[letter]) == 0:
return False, ["Dit is geen geldig woord."]
arr.append(unidic[specials[letter][0]])
specials[letter].pop(0)
if len(specials[letter]) == 0:
del specials[letter]
else:
if letter not in nums:
return False, ["Dit is geen geldig woord."]
arr.append(unidic[nums[letter]])
for x in doubles:
# Remove this number as a substitute if it is used anywhere
if nums[letter] == doubles[x][-1]:
doubles[x] = doubles[x][:-1]
if len(doubles[x]) == 0:
del doubles[x]
break
return True, arr
return unidic