Skip to content

Commit

Permalink
Merge pull request #73 from jrsmth/feature/presentation/main
Browse files Browse the repository at this point in the history
Feature/presentation/main
  • Loading branch information
AdamJ335 authored Oct 22, 2024
2 parents 2fb865a + e5d0006 commit 01fc517
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 30 deletions.
8 changes: 8 additions & 0 deletions documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
# Releases
<!-- @LatestFirst -->

## [0.5.0] [![user](https://img.shields.io/badge/adamj335-181717.svg?style=flat&logo=github)](https://github.com/adamj335)
[Presentation](https://github.com/jrsmth/waffle-bot/milestone/3) (??/??/????)
- `#52` Update dethrone Message to announce new King
- `#69` Add rank-based titles to messaging
- `#67` Create 'motivational' message for streak of 1
- `#70` Beautify the interactions the Archbishop has in Slack

## [0.4.1] [![user](https://img.shields.io/badge/haydende-181717.svg?style=flat&logo=github)](https://github.com/haydende) [![user](https://img.shields.io/badge/jrsmth-181717.svg?style=flat&logo=github)](https://github.com/jrsmth) [![user](https://img.shields.io/badge/adamj335-181717.svg?style=flat&logo=github)](https://github.com/adamj335)
[Player Extension](https://github.com/jrsmth/waffle-bot/milestone/9) (22/05/2024)
- `#34` Add Sonar Qube Support
Expand Down Expand Up @@ -67,3 +74,4 @@
[0.3.1]: https://github.com/jrsmth/waffle-bot/compare/0.3.0...0.3.1
[0.4.0]: https://github.com/jrsmth/waffle-bot/compare/0.3.1...0.4.0
[0.4.1]: https://github.com/jrsmth/waffle-bot/compare/0.4.0...0.4.1
[0.5.0]: https://github.com/jrsmth/waffle-bot/compare/0.4.1...0.5.0
39 changes: 12 additions & 27 deletions src/app/archbishop/archbishop.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import requests
from flask import Blueprint, Response
from flask import request
from slack_bolt import BoltResponse
from slack_sdk.errors import SlackApiError
from src.app.model.event.event import Event
from src.app.model.group.group import Group
from src.app.model.group.player import Player
from src.app.deacon.deacon import handle_king, handle_commoner
from collections import namedtuple
from slack_bolt.adapter.flask import SlackRequestHandler

import shortuuid


# Archbishop Logic
def construct_blueprint(bolt, config, messages, redis):
archbishop = Blueprint('archbishop', __name__)
Expand Down Expand Up @@ -64,7 +65,7 @@ def handle_waffle(message, say):
event_score = event.get_score()
event_streak = event.get_streak()

if (event_score is None or event_streak is None):
if event.has_no_waffle_data():
log.debug("[handle_waffle] Disregarding event, could not extract waffle data")
return

Expand All @@ -73,6 +74,8 @@ def handle_waffle(message, say):

log.debug(f"[handle_waffle] Updating player information for [{player.name}]")
player.score += event_score
if event_streak == 0:
player.prev_streak = player.streak
player.streak = event_streak
player.games += 1

Expand Down Expand Up @@ -111,7 +114,7 @@ def get_player(event, group):
potential_player = [p for p in group.players if p.id == user_id]

if not potential_player:
player = Player(user_id, user_name, 0, shortuuid.uuid(), 0, 0)
player = Player(user_id, user_name, 'Newbie', 0, shortuuid.uuid(), 0, 0, 0)
group.players.append(player)
redis.set_complex(group.name, group)
log.debug(f"[get_player] [{user_name}] added to the system with id [{user_id}]")
Expand All @@ -129,30 +132,12 @@ def process_result(group, player):

# Player is the King...
if player.id == group.king:
# ...and loses
if player.streak == 0:
log.info(f"[process_result] The Reign of King {player.name} is over!")
log.info("[process_result] Searching for a new King...")
group.dethrone()
text = messages.load_with_params("result.king.lose", [player.name])
# ...and wins
else:
text = messages.load_with_params("result.king.win", [str(player.get_average())])

log.debug(f"[process_result] Consulting Deacon to handle King with Id [{group.king}")
text = handle_king(log, messages, group, player)
# Player is a commoner...
else:
# ...and loses
if player.streak == 0:
log.info(f"[process_result] The Streak of {player.name} has been broken!")
text = messages.load_with_params("result.common.lose", [player.name])
# ...and wins...
else:
# ...and deserves coronation
if group.get_streak_by_id(player.id) > group.get_streak_by_id(group.king):
group.crown(player)
text = messages.load_with_params("result.common.coronation", [player.name])
else:
text = messages.load_with_params("result.common.win", [player.name, str(player.get_average())])
log.debug(f"[process_result] Consulting Deacon to handle Player with Id [{player.id}")
text = handle_commoner(log, messages, group, player)

result = namedtuple("Result", "group text")
return result(group, text)
Expand All @@ -167,12 +152,12 @@ def present(result, to_channel):
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"{result} :blush:\n\n*I am under development*"
"text": f"{result}"
},
}
],
}
return message

# Blueprint return
return archbishop
return archbishop
Empty file added src/app/deacon/__init__.py
Empty file.
53 changes: 53 additions & 0 deletions src/app/deacon/deacon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import secrets


def handle_king(log, messages, group, player):
# King lost
if player.streak == 0:
log.info(f"[handle_king] The Reign of King {player.name} is over!")
log.info("[handle_king] Searching for a new King...")
group.dethrone()
new_king = group.get_player_by_id(group.king)[0]
if new_king is not None:
return messages.load_with_params("result.king.lose.new",
[player.name, str(player.prev_streak), new_king.name])
else:
return messages.load_with_params("result.king.lose", [player.name, str(player.prev_streak)])
# King Wins
else:
return messages.load_with_params("result.king.win", [str(player.get_average())])

def handle_commoner(log, messages, group, player):
# ...and loses
if player.streak == 0:
log.info(f"[handle_commoner] The Streak of {player.name} has been broken!")
return messages.load_with_params("result.common.lose", [player.name, str(player.prev_streak)])
# ...and begins
elif player.streak == 1:
log.info(f"[handle_commoner] Player {player.name} has begun their kingdom!")
random_message = secrets.SystemRandom().randrange(1,2) #NOSONAR
return messages.load_with_params("result.player.start."+str(random_message), [player.name])
# ...and wins...
else:
# ...and deserves coronation
if group.get_streak_by_id(player.id) > group.get_streak_by_id(group.king):
group.crown(player)
return messages.load_with_params("result.common.coronation", [player.name])
else:

player.title = update_title(player)
return messages.load_with_params("result.common.win", [player.name, str(player.get_average())])


def update_title(player):
score = player.get_average()
if score > 4.0:
return 'Knight'
if score > 3.0:
return 'Master'
if score > 2.0:
return 'Freemen'
if score > 1.0:
return 'Commoner'
else:
return 'Peasant'
3 changes: 3 additions & 0 deletions src/app/model/event/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def get_streak(self):
if streak_text is not None:
return int(streak_text.split(" ")[1].strip("\n"))

def has_no_waffle_data(self):
return self.get_score() is None or self.get_streak() is None

@staticmethod
def _search_elements_for_expr(elements, expr):
for elem in elements:
Expand Down
3 changes: 3 additions & 0 deletions src/app/model/group/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def dethrone(self):
def get_streak_by_id(self, id_value):
return [p.streak for p in self.players if p.id == id_value]

def get_player_by_id(self, id_value):
return [p for p in self.players if p.id == id_value]

def __is_unworthy(self, new_streak):
""" Determine if streak is unworthy of scroll update """
sorted_scroll = sorted(self.scroll, key=lambda x: x.streak, reverse=False)
Expand Down
4 changes: 4 additions & 0 deletions src/app/model/group/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Player(Base):
""" Tracked player information """
id: str
name: str
title: str
streak: int
streak_id: str
prev_streak: int
score: int
games: int

Expand All @@ -19,8 +21,10 @@ def from_dict(cls, dic):
return cls(
id=dic["id"],
name=dic["name"],
title=dic["title"],
streak=dic["streak"],
streak_id=dic["streak_id"],
prev_streak=dic["prev_streak"],
score=dic["score"],
games=dic["games"]
)
Expand Down
9 changes: 6 additions & 3 deletions src/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ event.request.handled=The Archbishop has handled your request
event.request.ignored=Awoken for nothing! Do send word when there is a new WaffleScore

## Result
result.player.start.1=Well done {0}! \n\n One battle conquered, you are well on the way to have a glorious kingdom!
result.player.start.2=Well done {0}! \n\n So it begins... :crown:
result.common.coronation=Vive Rex! The WaffleCrown now rests on your head {0}
result.common.lose=Unlucky {0}! Your kingdom must rebuild!
result.common.win=Another battlefield conquered, well done {0}!\n\nYour score is: {1}
result.king.lose=Unlucky {0}! The time has come to crown a new King
result.common.lose=Unlucky {0}! :headstone: \n\nYour crusade lasted {1} days\n\nYour kingdom must rebuild!
result.common.win=Another battlefield conquered, well done {0} {1}!\n\nYour score is: {2}
result.king.lose=Unlucky {0}! :headstone: \n\nYou are no longer worthy to be King\n\nYour reign lasted {1} days\n\nThe Waffle Kingdom must crown a new King...
result.king.lose.new=Unlucky {0}! :headstone: \n\nYou are no longer worthy to be King\n\nYour reign lasted {1} days\n\nThe Waffle Kingdom must crown a new King...\n\nI dub thee, King {2}\n\nLong live the King!!!
result.king.win=Congratulations, Your Highness! Another successful battle\n\nYour score is: {0}

command.scroll.entry={0}. King: {1}, Streak: {2} on the date of {3} \n

0 comments on commit 01fc517

Please sign in to comment.