Python Telegram Minesweeper Bot https://github.com/isjerryxiao/tgmsbot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from peewee import *
  4. SQLITE_MAX_INT: int = 2**63 -1
  5. SQLITE_MIN_INT: int = -2**63
  6. SQLITE_MAX_INT = SQLITE_MAX_INT // 2
  7. SQLITE_MIN_INT = SQLITE_MIN_INT // 2
  8. db = SqliteDatabase('tgmsbot.db', pragmas={
  9. 'journal_mode': 'wal',
  10. 'cache_size': -32 * 1000})
  11. class SafeIntegerField(IntegerField):
  12. field_type = 'INT'
  13. def adapt(self, value):
  14. try:
  15. ivalue = int(value)
  16. if ivalue > SQLITE_MAX_INT:
  17. rvalue = SQLITE_MAX_INT
  18. elif ivalue < SQLITE_MIN_INT:
  19. rvalue = SQLITE_MIN_INT
  20. else:
  21. rvalue = value
  22. return rvalue
  23. except ValueError:
  24. return value
  25. class Player(Model):
  26. user_id = SafeIntegerField(unique=True, primary_key=True)
  27. mines = SafeIntegerField()
  28. death = SafeIntegerField()
  29. wins = SafeIntegerField()
  30. restricted_until = SafeIntegerField()
  31. immunity_cards = SafeIntegerField()
  32. permission = SafeIntegerField()
  33. class Meta:
  34. database = db
  35. db.connect()
  36. db.create_tables([Player])
  37. def get_player(user_id):
  38. player = Player.get_or_none(Player.user_id == user_id)
  39. if player is None:
  40. player = Player.create(user_id=user_id, mines=0, death=0, wins=0,
  41. restricted_until=0, immunity_cards=0, permission=0)
  42. return player
  43. else:
  44. return player