Skip to content

Commit

Permalink
feat(fight/plunder): Added attack notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickelza committed May 8, 2024
1 parent 046f92f commit a8aba31
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 53 deletions.
32 changes: 26 additions & 6 deletions resources/phrases.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def surround_with_arrows(text: str) -> str:
KEY_BUY = "Buy"
KEY_MANAGE_DEVIL_FRUIT = "Manage Devil Fruit"
KEY_JOIN_A_CREW = "Join a Crew"
KEY_VIEW_LOG = "View log"

TXT_SETTINGS = "Which setting would you like to change?"

Expand Down Expand Up @@ -1422,6 +1423,8 @@ def surround_with_arrows(text: str) -> str:
NOTIFICATION_CATEGORY_DEVIL_FRUIT = "Devil Fruit"
NOTIFICATION_CATEGORY_WARLORD = "Warlord"
NOTIFICATION_CATEGORY_DAVY_BACK_FIGHT = "Davy Back Fight"
NOTIFICATION_CATEGORY_FIGHT = "Fight"
NOTIFICATION_CATEGORY_PLUNDER = "Plunder"

# Notification - Crew Leave
CREW_LEAVE_NOTIFICATION = "{} has left the Crew"
Expand Down Expand Up @@ -1767,6 +1770,27 @@ def surround_with_arrows(text: str) -> str:
)
WARLORD_REVOCATION_NOTIFICATION_KEY = "Warlord revocation"

# Notification - Fight attack
FIGHT_ATTACK_NOTIFICATION = "You {} in a fight against {}{}\n\nAmount {}: ฿*{}*"
FIGHT_ATTACK_NOTIFICATION_DESCRIPTION = "If to be notified when you are fought by another player"
FIGHT_ATTACK_NOTIFICATION_KEY = "Fight"

# Notification - Plunder attack
PLUNDER_ATTACK_NOTIFICATION = ""
PLUNDER_ATTACK_NOTIFICATION_WON = (
"You defeated {} while they were trying to plunder you"
+ Emoji.CONFETTI
+ "\n\nAmount won \\(loan\\): ฿*{}*"
)
PLUNDER_ATTACK_NOTIFICATION_LOST = (
"You were plundered by {}" + Emoji.LOSER + "\n\nAmount lost: ฿*{}*"
)
PLUNDER_ATTACK_NOTIFICATION_DESCRIPTION = (
"If to be notified when you are plundered by another player"
)
PLUNDER_ATTACK_NOTIFICATION_KEY = "Plunder"


# List
LIST_OVERVIEW = (
"Select" + " {} *{}* from the list below\n{}"
Expand Down Expand Up @@ -1804,9 +1828,7 @@ def surround_with_arrows(text: str) -> str:
FIGHT_LOG_KEY = "Fights"
FIGHT_LOG_ITEM_DETAIL_TEXT_FILL_IN = "Fight"
FIGHT_LOG_ITEM_TEXT = "{} vs {} \\(฿{}\\)"
FIGHT_LOG_ITEM_DETAIL_TEXT = (
f"*{{}}*: {{}}\n*Date*: {{}}\n*Win probability*: {{}}%\n\n{{}}{LOG_ITEM_DETAIL_GO_TO_MESSAGE}"
)
FIGHT_LOG_ITEM_DETAIL_TEXT = "*{}*: {}\n*Date*: {}\n*Win probability*: {}%\n\n{}{}"
FIGHT_LOG_STATS_TEXT = (
"*Total fights*: {}\n*Wins*: {} \\({}%\\)\n*Losses*: {} \\({}%\\)\n*Belly won*: ฿{}\n*Belly"
" lost*: ฿{}\n*Max belly won*: [฿{} \\({}\\)]({})\n*Max belly lost*: [฿{} \\({}\\)]({})\n*Most"
Expand All @@ -1817,9 +1839,7 @@ def surround_with_arrows(text: str) -> str:
PLUNDER_LOG_KEY = "Plunders"
PLUNDER_LOG_ITEM_DETAIL_TEXT_FILL_IN = "Plunder"
PLUNDER_LOG_ITEM_TEXT = "{} vs {} \\(฿{}\\)"
PLUNDER_LOG_ITEM_DETAIL_TEXT = (
f"*{{}}*: {{}}\n*Date*: {{}}\n*Win probability*: {{}}%\n\n{{}}{LOG_ITEM_DETAIL_GO_TO_MESSAGE}"
)
PLUNDER_LOG_ITEM_DETAIL_TEXT = "*{}*: {}\n*Date*: {}\n*Win probability*: {}%\n\n{}{}"
PLUNDER_LOG_ITEM_DETAIL_SENTENCE_DURATION = "\n\n*Impel Down sentence*: {}"
PLUNDER_LOG_STATS_TEXT = (
"*Total plunders*: {}"
Expand Down
13 changes: 1 addition & 12 deletions src/chat/private/screens/screen_logs_type.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
from enum import StrEnum

from telegram import Update
from telegram.ext import ContextTypes

import resources.phrases as phrases
from src.model.User import User
from src.model.enums.Log import Log, get_log_by_type, LOG_TYPE_DETAIL_TEXT_FILL_IN
from src.model.enums.LogType import LogType
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys, LogTypeReservedKeys
from src.model.enums.Screen import Screen
from src.model.pojo.Keyboard import Keyboard
from src.service.list_service import get_items_text_keyboard
from src.service.message_service import full_message_send
from src.service.user_service import user_is_boss


class LogTypeReservedKeys(StrEnum):
"""
The reserved keys for this screen
"""

TYPE = "a"
ITEM_ID = "b"


async def manage(
update: Update, context: ContextTypes.DEFAULT_TYPE, inbound_keyboard: Keyboard, user: User
) -> None:
Expand Down
12 changes: 1 addition & 11 deletions src/chat/private/screens/screen_logs_type_detail.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
from enum import StrEnum

from telegram import Update
from telegram.ext import ContextTypes

from src.chat.private.screens.screen_logs_type import validate
from src.model.User import User
from src.model.enums.Log import Log, get_log_by_type
from src.model.enums.LogType import LogType
from src.model.enums.ReservedKeyboardKeys import LogTypeReservedKeys
from src.model.enums.Screen import Screen
from src.model.pojo.Keyboard import Keyboard
from src.service.message_service import full_message_send


class LogTypeReservedKeys(StrEnum):
"""
The reserved keys for this screen
"""

TYPE = "a"
ITEM_ID = "b"


async def manage(
update: Update, context: ContextTypes.DEFAULT_TYPE, inbound_keyboard: Keyboard, user: User
) -> None:
Expand Down
18 changes: 9 additions & 9 deletions src/model/Fight.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ class Fight(BaseModel):
Fight class
"""

id = PrimaryKeyField()
challenger = ForeignKeyField(
id: int | PrimaryKeyField = PrimaryKeyField()
challenger: User | ForeignKeyField = ForeignKeyField(
User, backref="fight_challengers", on_delete="CASCADE", on_update="CASCADE"
)
opponent = ForeignKeyField(
opponent: User | ForeignKeyField = ForeignKeyField(
User, backref="fight_opponents", on_delete="CASCADE", on_update="CASCADE"
)
win_probability = FloatField()
date = DateTimeField(default=datetime.datetime.now)
status = SmallIntegerField(default=GameStatus.IN_PROGRESS)
group_chat = ForeignKeyField(
win_probability: float | FloatField = FloatField()
date: datetime.datetime | DateTimeField = DateTimeField(default=datetime.datetime.now)
status: bool | SmallIntegerField = SmallIntegerField(default=GameStatus.IN_PROGRESS)
group_chat: GroupChat | ForeignKeyField = ForeignKeyField(
GroupChat,
null=True,
backref="fight_groups_chats",
on_delete="RESTRICT",
on_update="CASCADE",
)
message_id = IntegerField(null=True)
belly = BigIntegerField(null=True)
message_id: int | IntegerField = IntegerField(null=True)
belly: int | BigIntegerField = BigIntegerField(null=True)

class Meta:
db_table = "fight"
Expand Down
18 changes: 9 additions & 9 deletions src/model/Plunder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ class Plunder(BaseModel):
Plunder class
"""

id = PrimaryKeyField()
challenger = ForeignKeyField(
id: int | PrimaryKeyField = PrimaryKeyField()
challenger: User | ForeignKeyField = ForeignKeyField(
User, backref="plunder_challengers", on_delete="CASCADE", on_update="CASCADE"
)
opponent = ForeignKeyField(
opponent: User | ForeignKeyField = ForeignKeyField(
User, backref="plunder_opponents", on_delete="CASCADE", on_update="CASCADE"
)
win_probability = FloatField()
date = DateTimeField(default=datetime.datetime.now)
status = SmallIntegerField(default=GameStatus.IN_PROGRESS)
group_chat = ForeignKeyField(
win_probability: float | FloatField = FloatField()
date: datetime.datetime | DateTimeField = DateTimeField(default=datetime.datetime.now)
status: bool | SmallIntegerField = SmallIntegerField(default=GameStatus.IN_PROGRESS)
group_chat: GroupChat | ForeignKeyField = ForeignKeyField(
GroupChat,
null=True,
backref="plunder_groups_chats",
on_delete="RESTRICT",
on_update="CASCADE",
)
message_id = IntegerField(null=True)
belly = BigIntegerField(null=True)
message_id: int | IntegerField = IntegerField(null=True)
belly: int | BigIntegerField = BigIntegerField(null=True)
sentence_duration = IntegerField(null=True)

class Meta:
Expand Down
20 changes: 15 additions & 5 deletions src/model/enums/Log.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from src.model.enums.ListPage import ListPage, EmojiLegend
from src.model.enums.Location import get_first_new_world
from src.model.enums.LogType import LogType
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys, LogTypeReservedKeys
from src.model.enums.Screen import Screen
from src.model.enums.income_tax.IncomeTaxBreakdown import IncomeTaxBreakdown
from src.model.enums.income_tax.IncomeTaxContribution import IncomeTaxContribution
Expand Down Expand Up @@ -151,8 +151,6 @@ def get_deeplink_by_type(log_type: LogType, item_id: int) -> str:
:return: The deeplink
"""

from src.chat.private.screens.screen_logs_type_detail import LogTypeReservedKeys

info: dict = {LogTypeReservedKeys.TYPE: log_type, LogTypeReservedKeys.ITEM_ID: item_id}
return get_deeplink(info, screen=Screen.PVT_LOGS_TYPE_DETAIL)

Expand Down Expand Up @@ -250,13 +248,19 @@ def get_item_detail_text(self) -> str:
GAME_STATUS_DESCRIPTIONS[self.effective_status]
)

go_to_message_text = ""
if self.object.group_chat is not None:
go_to_message_text = phrases.LOG_ITEM_DETAIL_GO_TO_MESSAGE.format(
get_message_url(self.object.message_id, self.object.group_chat)
)

return phrases.FIGHT_LOG_ITEM_DETAIL_TEXT.format(
challenger_text,
self.opponent.get_markdown_mention(),
date,
self.object.get_win_probability(self.user),
outcome_text,
get_message_url(self.object.message_id, self.object.group_chat),
go_to_message_text,
)

def get_stats_text(self) -> str:
Expand Down Expand Up @@ -1156,13 +1160,19 @@ def get_item_detail_text(self) -> str:
convert_hours_to_duration(self.object.sentence_duration, show_full=True)
)

go_to_message_text = ""
if self.object.group_chat is not None:
go_to_message_text = phrases.LOG_ITEM_DETAIL_GO_TO_MESSAGE.format(
get_message_url(self.object.message_id, self.object.group_chat)
)

return phrases.PLUNDER_LOG_ITEM_DETAIL_TEXT.format(
challenger_text,
self.opponent.get_markdown_mention(),
date,
self.object.get_win_probability(self.user),
outcome_text,
get_message_url(self.object.message_id, self.object.group_chat),
go_to_message_text,
)

def get_stats_text(self) -> str:
Expand Down
94 changes: 93 additions & 1 deletion src/model/enums/Notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@
from src.model.DavyBackFightParticipant import DavyBackFightParticipant
from src.model.DevilFruit import DevilFruit
from src.model.DevilFruitTrade import DevilFruitTrade
from src.model.Fight import Fight
from src.model.Game import Game
from src.model.ImpelDownLog import ImpelDownLog
from src.model.Plunder import Plunder
from src.model.Prediction import Prediction
from src.model.PredictionOption import PredictionOption
from src.model.PredictionOptionUser import PredictionOptionUser
from src.model.User import User
from src.model.Warlord import Warlord
from src.model.enums.Emoji import Emoji
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys
from src.model.enums.GameStatus import GameStatus
from src.model.enums.LogType import LogType
from src.model.enums.ReservedKeyboardKeys import ReservedKeyboardKeys, LogTypeReservedKeys
from src.model.enums.Screen import Screen
from src.model.enums.impel_down.ImpelDownBountyAction import ImpelDownBountyAction
from src.model.enums.impel_down.ImpelDownSentenceType import ImpelDownSentenceType
Expand Down Expand Up @@ -51,6 +55,8 @@ class NotificationCategory(IntEnum):
BOUNTY_LOAN = 9
WARLORD = 10
DAVY_BACK_FIGHT = 11
FIGHT = 12
PLUNDER = 13


NOTIFICATION_CATEGORY_DESCRIPTIONS = {
Expand All @@ -65,6 +71,8 @@ class NotificationCategory(IntEnum):
NotificationCategory.BOUNTY_LOAN: phrases.NOTIFICATION_CATEGORY_BOUNTY_LOAN,
NotificationCategory.WARLORD: phrases.NOTIFICATION_CATEGORY_WARLORD,
NotificationCategory.DAVY_BACK_FIGHT: phrases.NOTIFICATION_CATEGORY_DAVY_BACK_FIGHT,
NotificationCategory.FIGHT: phrases.NOTIFICATION_CATEGORY_FIGHT,
NotificationCategory.PLUNDER: phrases.NOTIFICATION_CATEGORY_PLUNDER,
}


Expand Down Expand Up @@ -109,6 +117,8 @@ class NotificationType(IntEnum):
CREW_CONSCRIPTION_END = 36
DEVIL_FRUIT_SOLD = 37
CREW_CAPTAIN_PROMOTION = 38
FIGHT_ATTACK = 39
PLUNDER_ATTACK = 40


class Notification:
Expand Down Expand Up @@ -1350,6 +1360,86 @@ def __init__(self):
)


class FightAttackNotification(Notification):
"""Class for fight attack notifications."""

def __init__(self, fight: Fight = None):
"""
Constructor
:param fight: The fight
"""

self.fight = fight

super().__init__(
NotificationCategory.FIGHT,
NotificationType.FIGHT_ATTACK,
phrases.FIGHT_ATTACK_NOTIFICATION,
phrases.FIGHT_ATTACK_NOTIFICATION_DESCRIPTION,
phrases.FIGHT_ATTACK_NOTIFICATION_KEY,
item_screen=Screen.PVT_LOGS_TYPE_DETAIL,
item_info={
LogTypeReservedKeys.TYPE: LogType.FIGHT,
LogTypeReservedKeys.ITEM_ID: fight.id if fight is not None else None,
},
go_to_item_button_text=phrases.KEY_VIEW_LOG,
)

def build(self) -> str:
if self.fight.get_status() is GameStatus.LOST:
won_lost_text = phrases.TEXT_WON
won_lost_emoji = Emoji.CONFETTI
else:
won_lost_text = phrases.TEXT_LOST
won_lost_emoji = Emoji.LOSER

return self.text.format(
won_lost_text,
self.fight.opponent.get_markdown_mention(),
won_lost_emoji,
won_lost_text,
get_belly_formatted(self.fight.belly),
)


class PlunderAttackNotification(Notification):
"""Class for plunder attack notifications."""

def __init__(self, plunder: Plunder = None):
"""
Constructor
:param plunder: The plunder
"""

self.plunder = plunder

super().__init__(
NotificationCategory.PLUNDER,
NotificationType.PLUNDER_ATTACK,
phrases.PLUNDER_ATTACK_NOTIFICATION,
phrases.PLUNDER_ATTACK_NOTIFICATION_DESCRIPTION,
phrases.PLUNDER_ATTACK_NOTIFICATION_KEY,
item_screen=Screen.PVT_LOGS_TYPE_DETAIL,
item_info={
LogTypeReservedKeys.TYPE: LogType.PLUNDER,
LogTypeReservedKeys.ITEM_ID: plunder.id if plunder is not None else None,
},
go_to_item_button_text=phrases.KEY_VIEW_LOG,
)

def build(self) -> str:
if self.plunder.get_status() is GameStatus.LOST:
text = phrases.PLUNDER_ATTACK_NOTIFICATION_WON
else:
text = phrases.PLUNDER_ATTACK_NOTIFICATION_LOST

return text.format(
self.plunder.opponent.get_markdown_mention(), get_belly_formatted(self.plunder.belly)
)


NOTIFICATIONS = [
CrewLeaveNotification(),
LocationUpdateNotification(),
Expand Down Expand Up @@ -1389,6 +1479,8 @@ def __init__(self):
DavyBackFightEndNotification(),
DevilFruitSoldNotification(),
CrewCaptainPromotionNotification(),
FightAttackNotification(),
PlunderAttackNotification(),
]


Expand Down
Loading

0 comments on commit a8aba31

Please sign in to comment.