Skip to content
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
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
.DS_Store

# C extensions
*.so
Expand Down Expand Up @@ -104,4 +105,4 @@ ENV/
.vscode

# custom
*.ignoreme
*.ignoreme
118 changes: 90 additions & 28 deletions hey_fireball.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Collaborator

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.

NEG_POINTS = os.environ.get('NEG_POINTS', "ALLOW")
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
'''
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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."""
Expand Down Expand Up @@ -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

Expand All @@ -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}!'
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
Loading