app.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """General functions module"""
  2. import random
  3. import math
  4. import re
  5. from app import LOGGER, TELEGRAM_BOT, database, api
  6. def update_department(state_id, department_type):
  7. """Update department professors"""
  8. LOGGER.info('"%s": Run update for "%s" department for state', state_id, department_type)
  9. latest_professor = database.get_latest_professor(state_id, department_type)
  10. date = None
  11. if latest_professor:
  12. date = latest_professor.date_time
  13. professors = api.get_professors(state_id, department_type, date)
  14. LOGGER.info(
  15. '"%s": Found "%s" new professors in "%s" department',
  16. state_id, len(professors), department_type
  17. )
  18. # print_professors(professors)
  19. database.save_professors(state_id, department_type, professors)
  20. LOGGER.info('"%s": saved professors', state_id)
  21. def send_progress_message(state_id, department_type, language):
  22. """Update department professors"""
  23. LOGGER.info('"%s": Send progress message for "%s" department', state_id, department_type)
  24. yesterday_professors = database.get_yesterday_professors(state_id, department_type)
  25. if not yesterday_professors:
  26. LOGGER.warning('"%s": 0 professor yesterday in "%s" department', state_id, department_type)
  27. return
  28. yesterday_total = 0
  29. for professor in yesterday_professors:
  30. yesterday_total += professor.points
  31. institutes = api.get_institutes()
  32. uranium_institutes = []
  33. for institute in institutes:
  34. if institute['department_type'] == department_type:
  35. uranium_institutes.append(institute)
  36. if institute['state_id'] == state_id:
  37. state_institute = institute
  38. top_department = uranium_institutes[0]
  39. top_value = math.ceil(top_department['value'] / 10) * 10
  40. points_per_day = round(top_value / 14)
  41. msg_current = "Huidige uranium bonus is {} % met {} punten.".format(
  42. state_institute['current_bonus'],
  43. state_institute['value']
  44. )
  45. if state_institute['current_bonus'] == 10:
  46. msg_required = "Dagelijks zijn er {} punten nodig.".format(
  47. points_per_day
  48. )
  49. else:
  50. msg_required = \
  51. "Benodigde punten voor 10 % bonus: {} wat dagelijks {} punten zijn.".format(
  52. top_value,
  53. points_per_day
  54. )
  55. msg_yesterday = \
  56. "Aantal punten gisteren: {}, wat {} % van de benodigde aantal punten is.".format(
  57. yesterday_total,
  58. round(100 / points_per_day * yesterday_total)
  59. )
  60. message = ' '.join([
  61. msg_current,
  62. msg_required,
  63. msg_yesterday
  64. ])
  65. print(message)
  66. api.send_message(language, message)
  67. def send_lotery_message(state_id, department_type, language, amount):
  68. """Send lotery message"""
  69. LOGGER.info('"%s": Send lotery message for "%s" department', state_id, department_type)
  70. yesterday_professors = database.get_yesterday_professors(state_id, department_type)
  71. professor_count = len(yesterday_professors)
  72. random_index = random.randint(0, professor_count) - 1
  73. winning_professor = yesterday_professors[random_index]
  74. winner = winning_professor.player
  75. winner_name = re.sub(r'\[.*]\s', '', winner.name)
  76. amount_of_points = database.get_amount_of_points(state_id, department_type, winner.id)
  77. LOGGER.info(
  78. '"%s": candidates "%s", winner "%s" with "%s" points',
  79. state_id, professor_count, winner_name, amount_of_points
  80. )
  81. tg_message = '. '.join([
  82. "The daily department lotery is won by: {:}".format(winner_name),
  83. "With {:} points you won $ {:,.0f}".format(
  84. amount_of_points, amount_of_points * amount
  85. ).replace(',', '.'),
  86. "Send @bergjnl a message to receive your price.",
  87. ])
  88. print(tg_message)
  89. TELEGRAM_BOT.sendMessage(chat_id='@vn_lottery', text=tg_message)
  90. rr_message = '. '.join([
  91. "De department loterij is gewonnen door: {:}".format(winner_name),
  92. "Met {:} punten heb je $ {:,.0f} gewonnen".format(
  93. amount_of_points, amount_of_points * amount
  94. ).replace(',', '.'),
  95. "Stuur me een bericht om de prijs te ontvangen.",
  96. ])
  97. print(rr_message)
  98. api.send_message(language, rr_message)