File size: 11,743 Bytes
71e71e8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | import datetime
from peewee import *
import resources.Environment as Env
from src.model.BaseModel import BaseModel
from src.model.Crew import Crew
from src.model.User import User
from src.model.enums.GameStatus import GameStatus
from src.model.enums.income_tax.IncomeTaxEventType import IncomeTaxEventType
from src.model.game.GameOutcome import GameOutcome
from src.service.date_service import datetime_is_after, get_remaining_duration
from src.utils.string_utils import get_belly_formatted
class DavyBackFight(BaseModel):
"""
DavyBackFight class
"""
challenger_crew: Crew | ForeignKeyField = ForeignKeyField(
Crew, backref="davy_back_fights_challengers"
)
opponent_crew: Crew | ForeignKeyField = ForeignKeyField(
Crew, backref="davy_back_fights_opponents"
)
challenger_chest: int | BigIntegerField = BigIntegerField(default=0)
opponent_chest: int | BigIntegerField = BigIntegerField(default=0)
participants_count: int | IntegerField = IntegerField()
duration_hours: int | IntegerField = IntegerField()
penalty_days = IntegerField()
status: int | SmallIntegerField = SmallIntegerField(
default=GameStatus.AWAITING_OPPONENT_CONFIRMATION
)
start_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
end_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
conscript: User | ForeignKeyField = ForeignKeyField(
User, backref="davy_back_fights_conscripts", null=True
)
conscript_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
penalty_end_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
penalty_payout: int | BigIntegerField = BigIntegerField(default=0)
is_auto_accepted: bool | BooleanField = BooleanField(default=False)
# Backlinks
davy_back_fight_participants = None
class Meta:
db_table = "davy_back_fight"
@staticmethod
async def delete_expired_requests():
"""
Delete expire requests, so requests that are in AWAITING_OPPONENT_CONFIRMATION status and
were created more than x minutes ago
"""
DavyBackFight.delete().where(
DavyBackFight.status == GameStatus.AWAITING_OPPONENT_CONFIRMATION,
DavyBackFight.date
< (
datetime.datetime.now()
- datetime.timedelta(minutes=Env.DAVY_BACK_FIGHT_REQUEST_EXPIRATION_TIME.get_int())
),
).execute()
def get_participants(self, crew: Crew = None) -> list[any]:
"""
Get all the participants of a Davy Back Fight
:param crew: The crew object
:return: The list of participants
"""
if crew is None:
return self.davy_back_fight_participants
return [p for p in self.davy_back_fight_participants if p.crew == crew]
def get_participants_users(self, crew: Crew = None) -> list[User]:
"""
Get all the participants of a Davy Back Fight
:param crew: The crew object
:return: The list of participants
"""
if crew is None:
return [p.user for p in self.davy_back_fight_participants]
return [p.user for p in self.davy_back_fight_participants if p.crew == crew]
def get_non_participants_users(self, crew: Crew) -> list[any]:
"""
Get all the non-participants of a Davy Back Fight
:param crew: The crew object
:return: The list of non-participants
"""
return [m for m in crew.get_members() if m not in self.get_participants_users(crew=crew)]
def get_status(self) -> GameStatus:
"""
Get the status of the Davy Back Fight
:return: The status
"""
return GameStatus(self.status)
def in_progress(self) -> bool:
"""
Check if the game is in progress
:return: True if the game is in progress, False otherwise
"""
return self.get_status() is GameStatus.IN_PROGRESS
def has_ended(self) -> bool:
"""
Check if the game has ended
:return: True if the game has ended, False otherwise
"""
return self.get_status().is_finished()
def get_crew_gain(self, crew: Crew) -> int:
"""
Get the crew gain
:param crew: The crew object
:return: The crew gain
"""
return sum([p.contribution for p in self.davy_back_fight_participants if p.crew == crew])
def get_opponent_crew(self, crew: Crew):
"""
Get the opponent crew
:param crew: The crew object
:return: The opponent crew
"""
if crew == self.challenger_crew:
return self.opponent_crew
return self.challenger_crew
def get_in_progress_display_status(self, crew: Crew) -> GameStatus:
"""
Get the in progress display status [WINNING, LOSING, DRAW]
:param crew: The crew object
:return: The in progress display status
"""
crew_total = self.get_crew_gain(crew)
opponent_total = self.get_crew_gain(self.get_opponent_crew(crew))
if crew_total > opponent_total:
return GameStatus.WINNING
if crew_total < opponent_total:
return GameStatus.LOSING
return GameStatus.DRAW
def get_participant(self, user: User) -> any:
"""
Get the participant
:param user: The user object
:return: The participant
"""
from src.model.DavyBackFightParticipant import DavyBackFightParticipant
return self.davy_back_fight_participants.where(
DavyBackFightParticipant.user == user
).get_or_none()
def is_participant(self, user: User) -> bool:
"""
Check if the user is a participant
:param user: The user object
:return: True if the user is a participant, False otherwise
"""
return self.get_participant(user) is not None
@staticmethod
def get_contribution_events() -> list[IncomeTaxEventType]:
"""
Get the tax events that generate a DBF contribution
:return: The contribution events
"""
return [IncomeTaxEventType.GAME, IncomeTaxEventType.FIGHT, IncomeTaxEventType.PLUNDER]
def get_chest_amount(self, crew: Crew) -> int:
"""
Get the chest amount
:param crew: The crew object
:return: The chest amount
"""
if crew == self.challenger_crew:
return self.challenger_chest
return self.opponent_chest
def get_opponent_chest_amount(self, crew: Crew) -> int:
"""
Get the chest amount
:param crew: The crew object
:return: The chest amount
"""
return self.get_chest_amount(self.get_opponent_crew(crew))
def get_winner_crew(self) -> Crew | None:
"""
Get the winner
:return: The winner
"""
if self.get_status() not in [GameStatus.WON, GameStatus.LOST]:
return None
if self.get_status() is GameStatus.WON:
return self.challenger_crew
return self.opponent_crew
def is_winner_crew(self, crew: Crew) -> bool:
"""
Check if the crew is the winner
:param crew: The crew object
:return: True if the crew is the winner, False otherwise
"""
return self.get_winner_crew() == crew
def get_remaining_time(self) -> str:
from src.service.date_service import get_remaining_duration
return get_remaining_duration(self.end_date)
def get_outcome(self) -> GameOutcome:
"""
Get the outcome of the game
:return: The outcome
"""
from src.model.DavyBackFightParticipant import DavyBackFightParticipant
if DavyBackFightParticipant.get_total_contributions(
self, self.challenger_crew
) >= DavyBackFightParticipant.get_total_contributions(self, self.opponent_crew):
return GameOutcome.CHALLENGER_WON
return GameOutcome.OPPONENT_WON
def get_penalty_payout(self):
"""
Get the penalty payout
:return: The penalty payout
"""
if self.get_status() is GameStatus.WON:
return self.challenger_chest + self.penalty_payout
return self.opponent_chest + self.penalty_payout
def get_penalty_payout_formatted(self):
"""
Get the penalty payout formatted
:return: The penalty payout formatted
"""
return get_belly_formatted(self.get_penalty_payout())
def in_penalty_period(self) -> bool:
"""
Check if the game is in penalty period
:return: True if the game is in penalty period, False otherwise
"""
return datetime_is_after(self.penalty_end_date)
def get_penalty_remaining_time(self) -> str:
"""
Get the penalty remaining time
:return: The penalty remaining time
"""
return get_remaining_duration(self.penalty_end_date)
@staticmethod
def get_max_participants(challenger_crew: Crew, opponent_crew: Crew) -> int:
"""
Get the max participants
:param challenger_crew: The challenger crew
:param opponent_crew: The opponent crew
:return: The max participants
"""
return min(challenger_crew.get_member_count(), opponent_crew.get_member_count())
@staticmethod
def get_active_status() -> list[GameStatus]:
"""
Get the active status
:return: The active status
"""
return [GameStatus.COUNTDOWN_TO_START, GameStatus.IN_PROGRESS]
@staticmethod
def get_ended_status() -> list[GameStatus]:
"""
Get the ended status
:return: The ended status
"""
return [GameStatus.WON, GameStatus.LOST]
@staticmethod
def get_active() -> list["DavyBackFight"]:
"""
Get the active Davy Back Fights
:return: The active Davy Back Fights
"""
return DavyBackFight.select().where(
DavyBackFight.status << DavyBackFight.get_active_status()
)
@staticmethod
def get_crew_with_enough_members() -> list[Crew]:
"""
Get the crews with enough members for Davy Back Fight
:return: The crews with enough members for Davy Back Fight
"""
return [
c
for c in Crew.select()
if c.get_member_count() >= Env.DAVY_BACK_FIGHT_MIN_PARTICIPANTS.get_int()
]
@staticmethod
def get_crews_in_active() -> list[Crew]:
"""
Returns the crew in active Davy Back Fight
:return: The crew in active Davy Back Fight
"""
active = DavyBackFight.get_active()
return [d.challenger_crew for d in active] + [d.opponent_crew for d in active]
@staticmethod
def get_ended_with_penalty() -> list["DavyBackFight"]:
"""
Get the ended Davy Back Fights with penalty still active
:return: The ended Davy Back Fights with penalty
"""
return DavyBackFight.select().where(
DavyBackFight.status.in_(DavyBackFight.get_ended_status()),
DavyBackFight.penalty_end_date > datetime.datetime.now(),
)
@staticmethod
def get_crews_in_penalty() -> list[Crew]:
"""
Returns the crew in Davy Back Fight penalty
:return: The crew in Davy Back Fight penalty
"""
ended_with_penalty = DavyBackFight.get_ended_with_penalty()
return [d.challenger_crew for d in ended_with_penalty] + [
d.opponent_crew for d in ended_with_penalty
]
DavyBackFight.create_table()
|