File size: 17,338 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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | import datetime
import json
from peewee import *
import resources.Environment as Env
from resources import phrases
from src.model.BaseModel import BaseModel
from src.model.GroupChat import GroupChat
from src.model.User import User
from src.model.enums.GameStatus import GameStatus
from src.model.enums.SavedMediaName import SavedMediaName
from src.model.game.GameBoard import GameBoard
from src.model.game.GameDifficulty import GameDifficulty
from src.model.game.GameOutcome import GameOutcome
from src.model.game.GameType import GameType
from src.model.wiki.Character import Character
from src.model.wiki.Terminology import Terminology
from src.utils.string_utils import get_belly_formatted
class Game(BaseModel):
"""
Game class
"""
challenger: User | ForeignKeyField = ForeignKeyField(
User, backref="game_challengers", on_delete="CASCADE", on_update="CASCADE"
)
opponent: User | ForeignKeyField = ForeignKeyField(
User, backref="game_opponents", on_delete="CASCADE", on_update="CASCADE", null=True
)
type: GameType | SmallIntegerField = SmallIntegerField(null=True)
# Changed from CharField(max_length=9999) to TextField to fix the MySQL 65535 byte row limit error
board: str | TextField = TextField(null=True)
opponent_board: str | TextField = TextField(null=True)
status: GameStatus | SmallIntegerField = SmallIntegerField(
default=GameStatus.AWAITING_SELECTION
)
group_chat: GroupChat | ForeignKeyField = ForeignKeyField(
GroupChat,
null=True,
backref="game_groups_chats",
on_delete="RESTRICT",
on_update="CASCADE",
)
message_id: int | IntegerField = IntegerField(null=True)
challenger_message_id: int | IntegerField = IntegerField(null=True)
opponent_message_id: int | IntegerField = IntegerField(null=True)
wager: int | BigIntegerField = BigIntegerField(null=True)
last_interaction_date: datetime.datetime | DateTimeField = DateTimeField(
default=datetime.datetime.now
)
global_challenger_start_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
global_challenger_end_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
global_opponent_start_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
global_opponent_end_date: datetime.datetime | DateTimeField = DateTimeField(null=True)
global_challenger_has_finished: bool | BooleanField = BooleanField(default=False)
last_hint_date = DateTimeField(null=True)
last_hint_opponent_date = DateTimeField(null=True)
thread_restart_count = IntegerField(default=0)
class Meta:
db_table = "game"
def is_player(self, user: User) -> bool:
"""
Check if the user is a player of the game
:param user: The user
:return: True if the user is a player, False otherwise
"""
return self.challenger == user or self.opponent == user
def get_saved_media_name(self) -> SavedMediaName:
"""
Get the SavedMediaName for this GameType
:return: The SavedMediaName
"""
return GameType(self.type).get_saved_media_name()
def get_difficulty(self) -> GameDifficulty:
"""
Get the difficulty level of the game
:return: The difficulty level
"""
return GameDifficulty.get_from_total_wager(int(str(self.wager)))
def get_name(self) -> str:
"""
Get the name of the game
:return: The name
"""
return GameType(self.type).get_name()
@staticmethod
def get_total_win_or_loss_or_draw(user: User, status: GameStatus) -> int:
"""
Get the total amount of wins or losses or draws a user has
param user: The user
param status: The status (won or lost or draw)
return: The total amount of wins or losses or draws
"""
if status in (GameStatus.WON, GameStatus.LOST):
return (
Game().select().where((Game.challenger == user) & (Game.status == status)).count()
+ Game()
.select()
.where((Game.opponent == user) & (Game.status == status.get_opposite_status()))
.count()
)
return Game().select().where((Game.challenger == user) & (Game.status == status)).count()
@staticmethod
def get_total_belly_won_or_lost(user: User, status: GameStatus) -> int:
"""
Get the total amount of belly a user has won or lost
param user: The user
param status: The status (won or lost)
return: The total amount of belly
"""
return (
Game.select(fn.SUM(Game.wager)).where(
(Game.challenger == user) & (Game.status == status)
)
).scalar() + (
Game.select(fn.SUM(Game.wager)).where(
(Game.opponent == user) & (Game.status == status.get_opposite_status())
)
).scalar()
@staticmethod
def get_max_won_or_lost(user: User, status: GameStatus) -> "Game":
"""
Get the game with the max belly won or lost
param user: The user
param status: The status (won or lost)
return: The game
"""
# Max game as challenger
max_game_as_challenger: Game = (
Game()
.select()
.where((Game.challenger == user) & (Game.status == status))
.order_by(Game.wager.desc())
.first()
)
# Max game as opponent
max_game_as_opponent: Game = (
Game()
.select()
.where((Game.opponent == user) & (Game.status == status.get_opposite_status()))
.order_by(Game.wager.desc())
.first()
)
if max_game_as_challenger is None:
return max_game_as_opponent
if max_game_as_opponent is None:
return max_game_as_challenger
if max_game_as_challenger.wager > max_game_as_opponent.wager:
return max_game_as_challenger
return max_game_as_opponent
@staticmethod
def get_most_challenged_user(user: User) -> (User, int):
"""
Get the most challenged user and the amount of games
param user: The user
return: The most challenged user and the amount of games
"""
# noinspection DuplicatedCode
most_challenged_user_as_challenger = (
Game.select(Game.opponent, fn.COUNT(Game.opponent).alias("count"))
.where(Game.challenger == user)
.group_by(Game.opponent)
.order_by(SQL("count").desc())
.first()
)
amount_as_challenger = most_challenged_user_as_challenger.count
most_challenged_user_as_opponent = (
Game.select(Game.challenger, fn.COUNT(Game.challenger).alias("count"))
.where(Game.opponent == user)
.group_by(Game.challenger)
.order_by(SQL("count").desc())
.first()
)
amount_as_opponent = most_challenged_user_as_opponent.count
if amount_as_challenger > amount_as_opponent:
return most_challenged_user_as_challenger.opponent, amount_as_challenger
return most_challenged_user_as_opponent.challenger, amount_as_opponent
@staticmethod
def get_most_played_game(user: User) -> (GameType, int):
"""
Get the most played game and the amount of games
param user: The user
return: The most played game and the amount of games
"""
# noinspection DuplicatedCode
most_played_as_challenger = (
Game.select(Game.type, fn.COUNT(Game.type).alias("count"))
.where(Game.challenger == user)
.group_by(Game.type)
.order_by(SQL("count").desc())
.first()
)
amount_as_challenger = most_played_as_challenger.count
most_played_as_opponent = (
Game.select(Game.type, fn.COUNT(Game.type).alias("count"))
.where(Game.opponent == user)
.group_by(Game.type)
.order_by(SQL("count").desc())
.first()
)
amount_as_opponent = most_played_as_opponent.count
if amount_as_challenger > amount_as_opponent:
return GameType(most_played_as_challenger.type), amount_as_challenger
return GameType(most_played_as_opponent.type), amount_as_opponent
def get_type(self) -> GameType:
"""
Get the GameType
:return: The GameType
"""
return GameType(self.type)
def get_status(self) -> GameStatus:
"""
Get the status of the game
:return: The status of the game
"""
return GameStatus(self.status)
def get_players(self) -> list[User]:
"""
Get the players
:return: The players
"""
return [self.challenger, self.opponent]
def is_global(self) -> bool:
"""
Check if the game is global
:return: True if the game is global, False otherwise
"""
return self.global_challenger_start_date is not None
@staticmethod
def get_global_games() -> list["Game"]:
"""
Get all global games that are in progress and an opponent has not being found yet,
first those of which the challenger has finished, then oldest
:return: The global games
"""
return (
Game.select()
.where(
(
Game.global_challenger_start_date.is_null(False)
& (Game.global_opponent_start_date.is_null())
& (Game.status == GameStatus.IN_PROGRESS)
)
)
.order_by(
Game.global_challenger_has_finished.desc(), Game.global_challenger_start_date
)
)
def get_wager_formatted(self) -> str:
"""
Get the formatted wager
:return: The formatted wager
"""
return get_belly_formatted(self.wager)
def get_half_wager_formatted(self) -> str:
"""
Get the formatted half wager
:return: The formatted half wager
"""
return get_belly_formatted(self.wager // 2)
def is_challenger(self, user: User) -> bool:
"""
Check if the user is the challenger
:param user: The user
:return: True if the user is the challenger, False otherwise
"""
return user == self.challenger
def is_opponent(self, user: User) -> bool:
"""
Check if the user is the opponent
:param user: The user
:return: True if the user is the opponent, False otherwise
"""
return not self.is_challenger(user)
def get_other_player(self, user: User) -> User:
"""
Get the other player
:param user: The user
:return: The other player
"""
if self.is_challenger(user):
return self.opponent
return self.challenger
def get_winner(self) -> User | None:
"""
Get the winner of the game
:return: The winner
"""
if self.get_status() is GameStatus.WON:
return self.challenger
if self.get_status() is GameStatus.LOST:
return self.opponent
return None
def has_winner(self) -> bool:
"""
Check if the game has a winner
:return: True if the game has a winner, False otherwise
"""
return self.get_winner() is not None
def get_outcome(self) -> GameOutcome:
"""
Get the outcome of the game
:return: The outcome
"""
if self.get_status() is GameStatus.WON:
return GameOutcome.CHALLENGER_WON
if self.get_status() is GameStatus.LOST:
return GameOutcome.OPPONENT_WON
if self.get_status() is GameStatus.DRAW:
return GameOutcome.DRAW
return GameOutcome.NONE
def is_finished(self) -> bool:
"""
Check if the game is finished
:return: True if the game is finished, False otherwise
"""
return self.get_status().is_finished()
def set_board(self, board: GameBoard, user: User) -> None:
"""
Set the board for the user
:param board: The game board
:param user: The user
:return: None
"""
if self.is_challenger(user) or not self.is_global():
self.board = board.get_as_json_string()
else:
self.opponent_board = board.get_as_json_string()
def is_turn_based(self) -> bool:
"""
Check if the game is turn based
:return: True if the game is turn based, False otherwise
"""
return self.get_type().is_turn_based()
def is_guess_based(self) -> bool:
"""
Check if the game is time based
:return: True if the game is time based, False otherwise
"""
return self.get_type().is_guess_based()
def has_opponent(self) -> bool:
"""
Check if the game has an opponent
:return: True if the game has an opponent, False otherwise
"""
return self.opponent is not None
def get_players_time_seconds(self) -> [int, int]:
"""
Get the players time in seconds
:return: The players time in seconds
"""
from src.service.date_service import get_remaining_time_in_seconds
challenger_total_time_seconds = get_remaining_time_in_seconds(
(
self.global_challenger_end_date
if self.global_challenger_end_date
else datetime.datetime.now()
),
start_datetime=self.global_challenger_start_date,
)
opponent_total_time_seconds = get_remaining_time_in_seconds(
(
self.global_opponent_end_date
if self.global_opponent_end_date
else datetime.datetime.now()
),
start_datetime=self.global_opponent_start_date,
)
return challenger_total_time_seconds, opponent_total_time_seconds
def challenger_has_finished(self) -> bool:
"""
Check if the challenger has finished
:return: True if the challenger has finished, False otherwise
"""
return self.is_global() and (
self.global_challenger_end_date is not None or self.is_finished()
)
def opponent_has_finished(self) -> bool:
"""
Check if the opponent has finished
:return: True if the opponent has finished, False otherwise
"""
return self.is_global() and (
self.global_opponent_end_date is not None or self.is_finished()
)
def get_challenger_time_seconds(self) -> int:
"""
Get the challenger time in seconds
:return: The challenger time in seconds
"""
def get_seconds_for_every_hint(self) -> int:
"""
Get the seconds for every hint
:return: The seconds for every hint
"""
match self.get_type():
case GameType.GUESS_OR_LIFE:
return Env.GUESS_OR_LIFE_NEW_LIFE_WAIT_TIME.get_int()
case GameType.PUNK_RECORDS:
return Env.PUNK_RECORDS_NEXT_DETAIL_WAIT_TIME.get_int()
case GameType.SHAMBLES:
return Env.SHAMBLES_NEXT_LEVEL_WAIT_TIME.get_int()
case GameType.WHOS_WHO:
return Env.WHOS_WHO_NEXT_LEVEL_WAIT_TIME.get_int()
case _:
raise ValueError(f"Unsupported game type: {self.get_type()}")
def get_terminology(self) -> Terminology:
"""
Get the terminology from the game
:return: The terminology
"""
# Parse the JSON string and create a Terminology object
json_dict = json.loads(self.board)
if "terminology" in json_dict:
term_dict = json_dict.pop("terminology")
terminology: Terminology = Terminology(**term_dict)
elif "character" in json_dict:
char_dict = json_dict.pop("character")
terminology: Terminology = Character(**char_dict)
else:
raise ValueError("No terminology or character in JSON string")
return terminology
def get_global_item_text(self) -> str:
"""
Get the global item text
:return: The global item text
"""
return phrases.GAME_GLOBAL_ITEM_TEXT.format(
self.get_type().get_name(), self.get_wager_formatted()
)
Game.create_table()
# Auto-migrate: index status. Queried every minute (restart_hint_thread_if_down_all_games) and
# every few minutes (end_inactive_games) with WHERE Game.status == ... and no index, forcing a
# full table scan on the games table on every single run, 24/7.
try:
from playhouse.migrate import MySQLMigrator, migrate as pw_migrate
from src.model.BaseModel import db_obj
_migrator = MySQLMigrator(db_obj.get_db())
pw_migrate(
_migrator.add_index("game", ("status",), unique=False),
)
except Exception:
pass # Index already exists
|