-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add in ability to give negative points. #13 #22
Open
JacobJerrell
wants to merge
18
commits into
K-2L:develop
Choose a base branch
from
JacobJerrell:neg_fireball
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
20f6c8e
Merge pull request #5 from kylesykes/develop
kylesykes a1ba5da
Merge pull request #6 from kylesykes/develop
kylesykes 4b6a6d6
Merge pull request #8 from kylesykes/develop
kylesykes 2cfdefb
Merge pull request #15 from kylesykes/develop
kylesykes 15f0b74
Abiltiy to silence DM messages from HeyFireball #14
JacobJerrell fdbe303
PM Preferences -> AzureStorage
JacobJerrell 5311be1
PM Preference -> Redis Storage
JacobJerrell f9d301d
Resolved merge conflict by removing unnecessary line.
JacobJerrell ae84561
revert branch to K-2L develop
JacobJerrell add1114
Negative `Emoji` environment variables
JacobJerrell 794c991
max_neg_points_per_day
JacobJerrell 69e476b
cmds, _check_count, grammar
JacobJerrell d10b911
neg_points methods/fuctions
JacobJerrell 9680679
_extract_type(), neg_points_remaining, 'allneg' command
JacobJerrell a353677
Removing .DS_Store
JacobJerrell e5ac98a
.DS_Store banished!
JacobJerrell 26b80e0
Added `kind` functionality, and command handling for -Points
JacobJerrell f7c4ebd
Removing useless check
JacobJerrell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,25 +19,33 @@ | |
BOT_ID = os.environ.get("BOT_ID") | ||
EMOJI = os.environ.get('EMOJI') | ||
POINTS = os.environ.get('POINTS') | ||
SELF_POINTS = os.environ.get('SELF_POINTS', "DISALLOW") | ||
SELF_POINTS = os.environ.get('SELF_POINTS', "ALLOW") | ||
NEG_POINTS = os.environ.get('NEG_POINTS', "ALLOW") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the default should be DISALLOW. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. I must have forgotten to switch those off when I was done testing |
||
|
||
if NEG_POINTS == "ALLOW": | ||
NEGATIVE_EMOJI = os.environ.get('NEGATIVE_EMOJI') | ||
NEGATIVE_POINTS = os.environ.get('NEGATIVE_POINTS') | ||
# constants | ||
AT_BOT = "<@" + BOT_ID + ">" | ||
|
||
MAX_POINTS_PER_DAY = 5 | ||
MAX_NEG_POINTS_PER_DAY = 3 | ||
#EMOJI = ':fireball:' | ||
#POINTS = 'shots' | ||
|
||
# instantiate Slack & Twilio clients | ||
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN')) | ||
|
||
commands = ['leaderboard', 'fullboard', POINTS, '{}left'.format(POINTS)] | ||
commands_with_target = [POINTS, 'all'] | ||
commands = ['leaderboard', 'fullboard', POINTS, '{}left'.format(POINTS), | ||
NEGATIVE_POINTS, '-{}left'.format(NEGATIVE_POINTS)] | ||
commands_with_target = [POINTS, 'all', NEGATIVE_POINTS, 'allneg'] | ||
|
||
user_list = slack_client.api_call("users.list")['members'] | ||
user_name_lookup = {x['id'] : x['name'] for x in user_list} # U1A1A1A1A : kyle.sykes | ||
|
||
def get_username(user_id): | ||
"""Return the user's username based on the user_id | ||
else, return the user_id""" | ||
try: | ||
return user_name_lookup[user_id] | ||
except KeyError: | ||
|
@@ -79,6 +87,7 @@ def __init__(self, msg): | |
except: | ||
self.target_name = self.target_id | ||
self.command = self._extract_command() | ||
self.type = self._extract_type() | ||
self.count = self._extract_count() | ||
|
||
def __str__(self): | ||
|
@@ -123,6 +132,22 @@ def _extract_command(self): | |
return self.parts[idx].lower() | ||
return None | ||
|
||
def _extract_type(self): | ||
if self.bot_is_first: | ||
if self.target_id: | ||
idx = 2 | ||
else: | ||
idx = 1 | ||
if self.parts[idx] == EMOJI: | ||
return EMOJI | ||
elif self.parts[idx] == NEGATIVE_EMOJI: | ||
return NEGATIVE_EMOJI | ||
else: | ||
try: | ||
return str(self.parts[idx]) | ||
except ValueError: | ||
pass | ||
|
||
def _extract_count(self): | ||
#TODO: Clean up this gnarly logic. Stop hardcoding indices | ||
if self.bot_is_first: | ||
|
@@ -131,10 +156,12 @@ def _extract_count(self): | |
else: | ||
idx = 1 | ||
if self.parts[idx] == EMOJI: | ||
return sum(part==EMOJI for part in self.parts[idx:]) | ||
return sum(part == EMOJI for part in self.parts[idx:]) | ||
elif self.parts[idx] == NEGATIVE_EMOJI: | ||
return sum(part == NEGATIVE_EMOJI for part in self.parts[idx:]) | ||
else: | ||
try: | ||
return int(self.parts[idx]) | ||
return int(self.parts[idx]) # May need changes for NEGATIVE_EMOJI? | ||
except ValueError: | ||
pass | ||
else: | ||
|
@@ -144,9 +171,11 @@ def _extract_count(self): | |
idx = 0 | ||
if self.parts[idx] == EMOJI: | ||
return sum(part == EMOJI for part in self.parts[idx:]) | ||
elif self.parts[idx] == NEGATIVE_EMOJI: | ||
return sum(part == NEGATIVE_EMOJI for part in self.parts[idx:]) | ||
else: | ||
try: | ||
return int(self.parts[idx]) | ||
return int(self.parts[idx]) # May need changes for NEGATIVE_EMOJI? | ||
except ValueError: | ||
pass | ||
''' | ||
|
@@ -165,7 +194,7 @@ def wrapper(*args, **kwargs): | |
|
||
def set_storage(storage_type: str): | ||
"""Set the storage mechanism. | ||
|
||
Must be set before calling storge functions. | ||
""" | ||
global _storage | ||
|
@@ -179,18 +208,30 @@ def set_storage(storage_type: str): | |
else: | ||
raise ValueError('Unknown storage type.') | ||
|
||
|
||
def get_user_points_remaining(user_id: str) -> int: | ||
# Points used | ||
def get_user_points_remaining(user_id: str, kind: str) -> int: | ||
"""Return the number of points remaining for user today.""" | ||
used_pts = _storage.get_user_points_used(user_id) | ||
return MAX_POINTS_PER_DAY - used_pts | ||
|
||
if kind == EMOJI: | ||
used_pts = _storage.get_user_points_used(user_id) | ||
return MAX_POINTS_PER_DAY - used_pts | ||
if kind == NEGATIVE_EMOJI: | ||
used_pts = _storage.get_user_neg_points_used(user_id) | ||
return MAX_NEG_POINTS_PER_DAY - used_pts | ||
|
||
def get_user_neg_points_remaining(user_id: str) -> int: | ||
"""Return the number of negative points remaining for user today""" | ||
used_pts = _storage.get_user_neg_points_used(user_id) | ||
return MAX_NEG_POINTS_PER_DAY - used_pts | ||
|
||
def add_user_neg_points_used(user_id: str, num: int): | ||
"""Add `num` to user's total used negative points.""" | ||
_storage.add_user_neg_points_used(user_id, num) | ||
|
||
def add_user_points_used(user_id: str, num: int): | ||
"""Add `num` to user's total used points.""" | ||
_storage.add_user_points_used(user_id, num) | ||
|
||
|
||
# Points received | ||
def get_user_points_received_total(user_id: str) -> int: | ||
"""Return the number of points received by this user total.""" | ||
return _storage.get_user_points_received_total(user_id) | ||
|
@@ -200,6 +241,13 @@ def add_user_points_received(user_id: str, num: int): | |
"""Add `num` to user's total and today's received points.""" | ||
_storage.add_user_points_received(user_id, num) | ||
|
||
def get_user_neg_points_received(user_id: str): | ||
"""Return the number of negative points received by this user.""" | ||
return _storage.get_user_neg_points_received(user_id) | ||
|
||
def add_user_neg_points_received(user_id: str, num: int): | ||
"""Add `num` to user's negative points received""" | ||
_storage.add_user_neg_points_received(user_id, num) | ||
|
||
def get_users_and_scores() -> list: | ||
"""Return list of (user, total points received) tuples.""" | ||
|
@@ -249,14 +297,21 @@ def extract_fireball_info(slack_msg): | |
# Handle `all` command. | ||
if fireball.command == 'all': | ||
fireball.command = 'give' | ||
fireball.count = get_user_points_remaining(fireball.requestor_id) | ||
fireball.count = get_user_points_remaining(fireball.requestor_id, EMOJI) | ||
|
||
if fireball.command == 'allneg': | ||
fireball.command = 'giveneg' | ||
fireball.count = get_user_points_remaining(fireball.requestor_id, NEGATIVE_EMOJI) | ||
|
||
# Determine if the `give` command was implied. | ||
if (fireball.command is None | ||
and fireball.target_id | ||
and fireball.count): | ||
fireball.command = 'give' | ||
|
||
if fireball.type == EMOJI: | ||
fireball.command = 'give' | ||
elif fireball.type == NEGATIVE_EMOJI: | ||
fireball.command = 'giveneg' | ||
|
||
fireball.valid = is_valid_message(fireball) | ||
return fireball | ||
|
||
|
@@ -272,21 +327,28 @@ def handle_command(fireball_message): | |
""" | ||
msg = '' | ||
attach = None | ||
if fireball_message.command == 'give': | ||
if fireball_message.command == 'give' or fireball_message.command == 'giveneg': | ||
# Check if self points are allowed. | ||
if SELF_POINTS == 'DISALLOW' and (fireball_message.requestor_id == fireball_message.target_id): | ||
msg = 'You cannot give points to yourself!' | ||
send_message_to = fireball_message.requestor_id_only | ||
|
||
# Determine if requestor has enough points to give. | ||
elif check_points(fireball_message.requestor_id, fireball_message.count): | ||
# Add points to target score. | ||
add_user_points_received(fireball_message.target_id, fireball_message.count) | ||
# Add points to requestor points used. | ||
add_user_points_used(fireball_message.requestor_id, fireball_message.count) | ||
msg = f'You received {fireball_message.count} {POINTS} from {fireball_message.requestor_name}' | ||
send_message_to = fireball_message.target_id_only | ||
|
||
elif check_points(fireball_message.requestor_id, fireball_message.count, fireball_message.type): | ||
if fireball_message.type == EMOJI: | ||
# Add points to target score. | ||
add_user_points_received(fireball_message.target_id, fireball_message.count) | ||
# Add points to requestor points used. | ||
add_user_points_used(fireball_message.requestor_id, fireball_message.count) | ||
msg = f'You received {fireball_message.count} {POINTS} from {fireball_message.requestor_name}!' | ||
send_message_to = fireball_message.target_id_only | ||
elif fireball_message.type == NEGATIVE_EMOJI: | ||
# Add points to targets negative score. | ||
add_user_neg_points_received(fireball_message.target_id, fireball_message.count) | ||
# Add points to requestor negative points used. | ||
add_user_neg_points_used(fireball_message.requestor_id, fireball_message.count) | ||
msg = f'You received {fireball_message.count} negative {POINTS} from {fireball_message.requestor_name}!' | ||
send_message_to = fireball_message.target_id_only | ||
else: | ||
# Requestor lacks enough points to give. | ||
msg = f'You do not have enough {POINTS}!' | ||
|
@@ -319,7 +381,7 @@ def handle_command(fireball_message): | |
|
||
elif fireball_message.command == f'{POINTS}left': | ||
# Return requestor's points remaining. | ||
points_rmn = get_user_points_remaining(fireball_message.requestor_id) | ||
points_rmn = get_user_points_remaining(fireball_message.requestor_id, EMOJI) | ||
msg = f"You have {points_rmn} {POINTS} remaining" | ||
send_message_to = fireball_message.requestor_id_only | ||
|
||
|
@@ -345,10 +407,10 @@ def remove_points(user_id, number_of_points): | |
pass | ||
|
||
|
||
def check_points(user_id, number_of_points): | ||
def check_points(user_id, number_of_points, kind): | ||
"""Check to see if user_id has enough points remaining today. | ||
""" | ||
return get_user_points_remaining(user_id) >= number_of_points | ||
return get_user_points_remaining(user_id, kind) >= number_of_points | ||
|
||
''' | ||
fireball color palette | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the default should be DISALLOW.