database.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """Database functons"""
  2. from datetime import datetime
  3. from app import SESSION, LOGGER
  4. from app.models import Player, TelegramAccount, TelegramHandle, PlayerTelegram
  5. def add_telegram_account(update):
  6. """Add new Telegram account"""
  7. session = SESSION()
  8. telegram_account = TelegramAccount()
  9. telegram_account.id = update.message.from_user.id
  10. telegram_account.name = update.message.from_user.name
  11. telegram_account.registration_date = datetime.now()
  12. session.add(telegram_account)
  13. session.commit()
  14. session.close()
  15. return telegram_account
  16. def get_telegram_account(telegram_id):
  17. """Get Telegram account"""
  18. session = SESSION()
  19. telegram_account = _get_telegram_account(session, telegram_id)
  20. session.close()
  21. return telegram_account
  22. def get_rr_players(telegram_account):
  23. """Get Rival Region players associated with Telegram player"""
  24. LOGGER.info('"%s" get RR accounts', telegram_account.id,)
  25. session = SESSION()
  26. players = _get_rr_players(session, telegram_account.id)
  27. LOGGER.info('"%s" found %s RR accounts', telegram_account.id, len(players))
  28. session.close()
  29. return players
  30. def verify_rr_player(telegram_id, player_id):
  31. """Verify RR player in database"""
  32. session = SESSION()
  33. telegram_account = _get_telegram_account(session, telegram_id)
  34. players = _get_rr_players(session, telegram_id)
  35. for player in players:
  36. if player.id == player_id:
  37. LOGGER.info(
  38. '"%s" player already connected "%s"',
  39. telegram_id,
  40. player_id
  41. )
  42. session.close()
  43. return
  44. active_player_telegrams = session.query(PlayerTelegram) \
  45. .filter(PlayerTelegram.until_date_time == None) \
  46. .filter(PlayerTelegram.player_id == player_id) \
  47. .all()
  48. for active_player_telegram in active_player_telegrams:
  49. LOGGER.info(
  50. '"%s" unconnect player "%s"',
  51. active_player_telegram.telegram_id,
  52. player_id
  53. )
  54. active_player_telegram.until_date_time = datetime.now()
  55. LOGGER.info(
  56. '"%s" connecting player "%s"',
  57. telegram_id,
  58. player_id
  59. )
  60. player_telegram = PlayerTelegram()
  61. player_telegram.telegram_id = telegram_account.id
  62. player_telegram.player_id = player_id
  63. player_telegram.from_date_time = datetime.now()
  64. session.add(player_telegram)
  65. session.commit()
  66. session.close()
  67. def remove_verified_player(telegram_account_id, player_id):
  68. """Remove Telegram player"""
  69. session = SESSION()
  70. player_telegram = session.query(PlayerTelegram) \
  71. .filter(PlayerTelegram.telegram_id == telegram_account_id) \
  72. .filter(PlayerTelegram.player_id == player_id) \
  73. .filter(PlayerTelegram.until_date_time == None) \
  74. .first()
  75. if player_telegram:
  76. player_telegram.until_date_time = datetime.now()
  77. session.commit()
  78. return True
  79. return False
  80. def is_connected(telegram_id, player_id):
  81. """Check if account is already"""
  82. session = SESSION()
  83. player_telegram = session.query(PlayerTelegram) \
  84. .filter(PlayerTelegram.until_date_time == None) \
  85. .filter(PlayerTelegram.telegram_id == telegram_id) \
  86. .filter(PlayerTelegram.player_id == player_id) \
  87. .first()
  88. session.close()
  89. return bool(player_telegram)
  90. def _get_telegram_account(session, telegram_id):
  91. """Return telegram_account"""
  92. return session.query(TelegramAccount).get(telegram_id)
  93. def _get_rr_players(session, telegram_account_id):
  94. """Get Rival Region players associated with Telegram player"""
  95. return session.query(Player) \
  96. .join(Player.player_telegram) \
  97. .filter(PlayerTelegram.telegram_id == telegram_account_id) \
  98. .filter(PlayerTelegram.until_date_time == None) \
  99. .all()