Skip to content

Commit

Permalink
Version 0.2.4.0 Release - reward codes are now automatically sent (#10)
Browse files Browse the repository at this point in the history
# Version 0.2.4.0 Release
- Reward codes are now automatically sent
- Improved mock test cases with test_core.py
- Test coverage improved from 87% to 92%.
- Moved `core` object instantiation outside of the while loop in `main.py`
- Fixed a calculation error with sleep time when encountering a Reddit API Exception in `send_message_to_winners`
  • Loading branch information
William Lam authored May 12, 2021
1 parent 3b12aea commit 263cf5e
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*.json
winners_list.txt
patch_notes.txt
reward_codes.txt
cache/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ Navigate to the project root directory in your terminal.
- To run the script, use `./scripts.sh start`
- To run unit tests, use `./scripts.sh test`
- To reset the cache & database, use `./scripts.sh reset` before running `./scripts.sh start`
- To pick a list of winners from the existing local database, use `./scripts.sh winners 20`, where the "20" can be replaced with an integer to specify the number of winners picked

## More Usage Notes

For unreleased patch notes, **do not commit and push the branch to source control before their official release**.

For `hon_patch_notes_game_bot/cache/reward_codes.txt`, populate the file manually (this is done via private means).

To change up some commonly changed configuration before starting a new run, go to `hon_patch_notes_game_bot/config/config.py` and edit them.

## Extra Documentation
Expand Down
2 changes: 1 addition & 1 deletion hon_patch_notes_game_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.3.1"
__version__ = "0.2.4.0"
5 changes: 5 additions & 0 deletions hon_patch_notes_game_bot/cache/reward_codes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REWARD_CODE_1
REWARD_CODE_2
REWARD_CODE_3
REWARD_CODE_4
REWARD_CODE_5
35 changes: 27 additions & 8 deletions hon_patch_notes_game_bot/communications.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ def send_message_to_staff(
continue


def send_message_to_winners(
reddit: Reddit, winners_list: List[str], version_string: str, gold_coin_reward: int
def send_message_to_winners( # noqa: C901
reddit: Reddit,
winners_list: List[str],
reward_codes_list: List[str],
version_string: str,
gold_coin_reward: int,
):
"""
Sends the winners list results to a list of recipients via Private Message (PM).
Expand All @@ -150,6 +154,7 @@ def send_message_to_winners(
Attributes:
reddit: the PRAW Reddit instance
winners_list: a list of winning recipients for the PM
reward_codes_list: a list of reward codes for each winner
version_string: the version of the patch notes
gold_coin_reward: the number of Gold Coins intended for the reward
"""
Expand All @@ -159,17 +164,27 @@ def send_message_to_winners(
failed_recipients_list = []

for recipient in winners_list:
reward_code = (
"N/A - all possible reward codes have been used up.\n\n"
f"Please contact {STAFF_MEMBER_THAT_HANDS_OUT_REWARDS} for a code to be issued manually."
)
if len(reward_codes_list) > 0:
reward_code = reward_codes_list[0]

message = (
f"Congratulations {recipient}!\n\n"
f"You have been chosen by the bot as a winner for the {version_string} Patch Notes Guessing Game!\n\n"
f"Please send /u/{STAFF_MEMBER_THAT_HANDS_OUT_REWARDS} a Private Message (PM) to request a code"
f" for {str(gold_coin_reward)} Gold Coins.\n\n"
"Be sure to check your Reddit Chat inbox as well (not just your Reddit mail inbox)!\n\n"
f"Your reward code for {str(gold_coin_reward)} Gold Coins is: **{reward_code}**.\n\n"
"You can redeem your reward code here: https://www.heroesofnewerth.com/redeem/\n\n"
"Thank you for participating in the game! =)"
)
try:
reddit.redditor(recipient).message(subject=subject_line, message=message)
print(f"Winner message sent to {recipient}")
print(f"Winner message sent to {recipient}, with code: {reward_code}")

# Pop reward code from list only if the message was sent successfully
if len(reward_codes_list) > 0:
reward_codes_list.pop(0)

# Reddit API Exception
except RedditAPIException as redditException:
Expand Down Expand Up @@ -198,7 +213,7 @@ def send_message_to_winners(
else:
# Use named groups from regex capture and assign them to a dictionary
sleep_time_regex_groups = regex_capture.groupdict(default=0)
secondsToSleep = int(
secondsToSleep = 60 * int(
sleep_time_regex_groups.get("minutes") # type: ignore
) + int(
sleep_time_regex_groups.get("seconds") # type: ignore
Expand All @@ -223,5 +238,9 @@ def send_message_to_winners(
failed_recipients = len(failed_recipients_list)
if failed_recipients > 0 and failed_recipients < len(winners_list):
send_message_to_winners(
reddit, failed_recipients_list, version_string, gold_coin_reward
reddit,
failed_recipients_list,
reward_codes_list,
version_string,
gold_coin_reward,
)
1 change: 1 addition & 0 deletions hon_patch_notes_game_bot/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
SUBMISSION_CONTENT_PATH: str = "config/submission_content.md"
COMMUNITY_SUBMISSION_CONTENT_PATH: str = "config/community_patch_notes_compilation.md"
WINNERS_LIST_FILE_PATH: str = "cache/winners_list.txt"
REWARD_CODES_FILE_PATH: str = "cache/reward_codes.txt"
BLANK_LINE_REPLACEMENT: str = "..."

# ================
Expand Down
9 changes: 7 additions & 2 deletions hon_patch_notes_game_bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from hon_patch_notes_game_bot.utils import (
get_patch_notes_line_number,
generate_submission_compiled_patch_notes_template_line,
get_reward_codes_list,
is_game_expired,
output_winners_list_to_file,
)
Expand All @@ -39,6 +40,7 @@
NUM_WINNERS,
STAFF_RECIPIENTS_LIST,
WINNERS_LIST_FILE_PATH,
REWARD_CODES_FILE_PATH,
)


Expand All @@ -60,6 +62,8 @@ def __init__(
self.submission = submission
self.community_submission = community_submission
self.patch_notes_file = patch_notes_file
self.reward_codes_filepath = REWARD_CODES_FILE_PATH
self.game_end_time = GAME_END_TIME

def has_exceeded_revealed_line_count(self) -> bool:
"""
Expand Down Expand Up @@ -309,6 +313,7 @@ def perform_post_game_actions(self):
send_message_to_winners(
reddit=self.reddit,
winners_list=winners_list,
reward_codes_list=get_reward_codes_list(self.reward_codes_filepath),
version_string=version_string,
gold_coin_reward=GOLD_COIN_REWARD,
)
Expand Down Expand Up @@ -488,7 +493,7 @@ def loop(self): # noqa: C901
# Check unread replies
try:
# Stop indefinite loop if current time is greater than the closing time.
if is_game_expired(GAME_END_TIME):
if is_game_expired(self.game_end_time):
print("Reddit Bot script ended via time deadline")
return False

Expand Down Expand Up @@ -523,7 +528,7 @@ def loop(self): # noqa: C901
return False

# Stop indefinite loop if current time is greater than the closing time.
if is_game_expired(GAME_END_TIME):
if is_game_expired(self.game_end_time):
print("Reddit Bot script ended via time deadline")
return False

Expand Down
16 changes: 9 additions & 7 deletions hon_patch_notes_game_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ def main():
COMMUNITY_SUBMISSION_CONTENT_PATH,
)

# Create core object
core = Core(
reddit=reddit,
db=database,
submission=submission,
community_submission=community_submission,
patch_notes_file=patch_notes_file,
)

# ===============================================================
# Core loop to listen to unread comment messages on Reddit
# ===============================================================
print("Reddit Bot's core loop started")
while 1:
core = Core(
reddit=reddit,
db=database,
submission=submission,
community_submission=community_submission,
patch_notes_file=patch_notes_file,
)
if not core.loop():
print("Reddit Bot script ended via core loop end conditions")
break
Expand Down
16 changes: 16 additions & 0 deletions hon_patch_notes_game_bot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
MAX_NUM_GUESSES,
MAX_PERCENT_OF_LINES_REVEALED,
NUM_WINNERS,
REWARD_CODES_FILE_PATH,
)


Expand Down Expand Up @@ -164,3 +165,18 @@ def processed_community_notes_thread_submission_content(

submission_content += f"\n\n**Guesses in this thread will not be responded to by the bot. [Visit the main thread instead!]({main_submission_url})**\n\nFeel free to discuss patch changes here liberally (based on the currently revealed notes)! :)" # noqa: E501
return submission_content


def get_reward_codes_list(
reward_codes_filepath: str = REWARD_CODES_FILE_PATH,
) -> List[str]:
"""
Retrieves the reward codes from `REWARD_CODES_FILE_PATH` as a list of strings.
Returns:
- A list of reward code strings
"""
with open(reward_codes_filepath, "r") as file:
reward_codes_file_content = file.readlines()
reward_codes_list = [line.rstrip() for line in reward_codes_file_content]
return reward_codes_list
51 changes: 27 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["William Lam <[email protected]>"]
description = ""
name = "hon_patch_notes_game_bot"
version = "0.2.1"
version = "0.2.4.0"

[tool.poetry.dependencies]
praw = "^7.2.0"
Expand Down
5 changes: 5 additions & 0 deletions tests/cache/reward_codes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REWARD_CODE_1
REWARD_CODE_2
REWARD_CODE_3
REWARD_CODE_4
REWARD_CODE_5
Loading

0 comments on commit 263cf5e

Please sign in to comment.