app.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. """General function module"""
  2. import random
  3. from datetime import datetime, timedelta
  4. # libraries and data
  5. import matplotlib.pyplot as plt
  6. import matplotlib.dates as mdates
  7. import pandas as pd
  8. from pandas.plotting import register_matplotlib_converters
  9. from telegram import ParseMode
  10. from app import LOGGER, SCHEDULER, TELEGRAM_BOT, RESOURCE_NAMES, jobs, api, database
  11. register_matplotlib_converters()
  12. def check_resources(state_id, capital_id, resource_id, do_refill, alt):
  13. """Check resources and refill if necessary"""
  14. regions = api.download_resources(state_id, resource_id)
  15. print_resources(regions)
  16. refill_percentage = 25
  17. database.save_resources(state_id, regions, resource_id)
  18. if do_refill and need_refill(regions, refill_percentage):
  19. max_seconds = max_refill_seconds(regions, refill_percentage, 900)
  20. random_seconds = random.randint(0, max_seconds)
  21. random_time_delta = timedelta(seconds=random_seconds)
  22. scheduled_date = datetime.now() + random_time_delta
  23. job_id = 'refill_{}_{}'.format(capital_id, resource_id)
  24. LOGGER.info(
  25. 'Refil resource %s at %s (%s minutes)',
  26. resource_id,
  27. scheduled_date,
  28. round(random_time_delta.seconds / 60)
  29. )
  30. job = SCHEDULER.get_job(job_id)
  31. if not job:
  32. SCHEDULER.add_job(
  33. jobs.refill_resource,
  34. 'date',
  35. args=[state_id, capital_id, resource_id, alt],
  36. id=job_id,
  37. run_date=scheduled_date
  38. )
  39. def print_resources(regions):
  40. """print resources"""
  41. if regions:
  42. print('region expl max D left c % t %')
  43. for region in regions.values():
  44. region['explored_percentage'] = 100 / region['maximum'] * region['explored']
  45. region['total_left'] = region['explored'] + region['limit_left']
  46. region['total_percentage'] = 100 / 2500 * region['total_left']
  47. print('{:25}: {:7.2f}{:4}{:4}{:5}{:7.2f}{:7.2f}'.format(
  48. region['region_name'],
  49. region['explored'],
  50. region['maximum'],
  51. region['deep_exploration'],
  52. region['limit_left'],
  53. region['explored_percentage'],
  54. region['total_percentage'],
  55. ))
  56. else:
  57. LOGGER.error('no region to print data')
  58. def need_refill(regions, limit):
  59. """Check if refill is needed"""
  60. for region in regions.values():
  61. percentage = 100 / region['maximum'] * region['explored']
  62. if percentage < limit and region['limit_left']:
  63. return True
  64. return False
  65. def max_refill_seconds(regions, limit, max_time):
  66. """Give random seconds for next refill"""
  67. lowest_percentage = limit
  68. for region in regions.values():
  69. percentage = 100 / region['maximum'] * region['explored']
  70. if percentage < lowest_percentage:
  71. lowest_percentage = percentage
  72. return int(max_time / limit * lowest_percentage)
  73. def send_telegram_update(state_id, group_id, resource_name):
  74. """Send resource update to telegram"""
  75. resource_id = RESOURCE_NAMES[resource_name]
  76. # date = datetime.now()
  77. date = datetime.today().replace(hour=15, minute=5) - timedelta(1)
  78. print(date)
  79. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  80. date = datetime.today().replace(hour=16, minute=5) - timedelta(1)
  81. print(date)
  82. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  83. date = datetime.today().replace(hour=17, minute=5) - timedelta(1)
  84. print(date)
  85. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  86. date = datetime.today().replace(hour=18, minute=5) - timedelta(1)
  87. print(date)
  88. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  89. date = datetime.today().replace(hour=19, minute=5) - timedelta(1)
  90. print(date)
  91. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  92. date = datetime.today().replace(hour=20, minute=5) - timedelta(1)
  93. print(date)
  94. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  95. date = datetime.today().replace(hour=21, minute=5) - timedelta(1)
  96. print(date)
  97. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  98. date = datetime.today().replace(hour=22, minute=5) - timedelta(1)
  99. print(date)
  100. message = database.get_work_percentage(state_id, resource_id, date, 1, 1)
  101. return
  102. if message:
  103. print(message)
  104. TELEGRAM_BOT.sendMessage(
  105. chat_id=group_id,
  106. text='```\n{}```'.format(message),
  107. parse_mode=ParseMode.MARKDOWN
  108. )
  109. else:
  110. LOGGER.error('no data for Telegram message')
  111. def graph():
  112. """make graph"""
  113. date = datetime.now() # - timedelta(1)
  114. region_4001 = database.get_resources(4001, date, 0)
  115. region_4002 = database.get_resources(4002, date, 0)
  116. region_4003 = database.get_resources(4003, date, 0)
  117. region_4004 = database.get_resources(4004, date, 0)
  118. region_4008 = database.get_resources(4008, date, 0)
  119. # resource_tmp = np.random.randn(2499)+range(2500, 1, -1)
  120. # Make a data frame
  121. data_frame = pd.DataFrame({
  122. # 'x': range(1, 2500),
  123. # '4001': resource_tmp,
  124. 'x': list(region_4001.keys()),
  125. 'Northern Netherlands': list(region_4001.values()),
  126. 'Eastern Netherlands': list(region_4002.values()),
  127. 'Western Netherlands': list(region_4003.values()),
  128. 'Southern Netherlands': list(region_4004.values()),
  129. 'Amsterdam': list(region_4008.values()),
  130. })
  131. major_fmt = mdates.DateFormatter('%m-%d %H:%M')
  132. fig, ax = plt.subplots()
  133. ax.xaxis.set_major_formatter(major_fmt)
  134. fig.autofmt_xdate()
  135. end_date_time = date.replace(hour=19, minute=0, second=0, microsecond=0)
  136. start_date_time = end_date_time - timedelta(hours=24)
  137. ax.set_xlim([start_date_time, end_date_time])
  138. ax.set_ylim([0, 2500])
  139. # style
  140. plt.style.use('seaborn-darkgrid')
  141. # create a color palette
  142. palette = plt.get_cmap('Set1')
  143. # multiple line plot
  144. num = 0
  145. for column in data_frame.drop('x', axis=1):
  146. num += 1
  147. plt.plot(
  148. data_frame['x'],
  149. data_frame[column],
  150. marker='',
  151. color=palette(num),
  152. linewidth=1,
  153. alpha=0.9,
  154. label=column
  155. )
  156. # Add legend
  157. plt.legend(loc=3, ncol=1)
  158. # plt.setp(plt.xticks()[1], rotation=30, ha='right')
  159. # Add titles
  160. plt.title(
  161. 'Resource limit left | {}'.format(date.strftime('%Y-%m-%d')),
  162. loc='left',
  163. fontsize=12,
  164. fontweight=1
  165. )
  166. plt.xlabel("Time")
  167. plt.ylabel("Resources")
  168. plt.savefig('foo.png')