database.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """Main application"""
  2. from datetime import datetime, timedelta, timezone
  3. from sqlalchemy.orm import joinedload
  4. from app import SESSION
  5. from app.models import ResourceTrack, ResourceStat, Region
  6. def save_resources(state_id, regions, resource_id):
  7. """Save resources to database"""
  8. session = SESSION()
  9. resource_track = ResourceTrack()
  10. resource_track.state_id = state_id
  11. resource_track.resource_type = resource_id
  12. resource_track.date_time = datetime.now()
  13. session.add(resource_track)
  14. session.commit()
  15. for region_id, region_dict in regions.items():
  16. region = session.query(Region).get(region_id)
  17. if not region:
  18. region = save_region(session, region_id, region_dict)
  19. resource_stat = ResourceStat()
  20. resource_stat.resource_track_id = resource_track.id
  21. resource_stat.region_id = region.id
  22. resource_stat.explored = region_dict['explored']
  23. resource_stat.deep_exploration = region_dict['deep_exploration']
  24. resource_stat.limit_left = region_dict['limit_left']
  25. session.add(resource_stat)
  26. session.commit()
  27. session.close()
  28. def save_region(session, region_id, region_dict):
  29. """Save player to database"""
  30. region = Region()
  31. region.id = region_id
  32. region.name = region_dict['region_name']
  33. session.add(region)
  34. return region
  35. def get_resources(region_id, date, resource_type):
  36. """Get resources on a date"""
  37. end_date_time = date.replace(hour=19, minute=0, second=0, microsecond=0)
  38. start_date_time = end_date_time - timedelta(1)
  39. session = SESSION()
  40. resource = {}
  41. resource_stats = session.query(ResourceStat) \
  42. .options(joinedload(ResourceStat.resource_track)) \
  43. .join(ResourceStat.resource_track) \
  44. .filter(ResourceStat.region_id == region_id) \
  45. .filter(ResourceTrack.resource_type == resource_type) \
  46. .filter(ResourceTrack.date_time >= start_date_time) \
  47. .filter(ResourceTrack.date_time <= end_date_time) \
  48. .order_by(ResourceTrack.date_time.desc()) \
  49. .all()
  50. for resource_stat in resource_stats:
  51. time = resource_stat.resource_track.date_time
  52. resource[time] = resource_stat.explored + resource_stat.limit_left
  53. session.close()
  54. return resource
  55. def _get_state_stat(session, state_id, resource_type, date_time):
  56. """Get state stats from date"""
  57. ten_minutes = timedelta(minutes=10)
  58. stats = session.query(ResourceStat) \
  59. .options(joinedload(ResourceStat.resource_track), joinedload(ResourceStat.region)) \
  60. .join(ResourceStat.resource_track) \
  61. .filter(ResourceTrack.state_id == state_id) \
  62. .filter(ResourceTrack.resource_type == resource_type) \
  63. .filter(ResourceTrack.date_time >= date_time - ten_minutes) \
  64. .filter(ResourceTrack.date_time <= date_time + ten_minutes) \
  65. .all()
  66. stats_dict = {}
  67. for stat in stats:
  68. stats_dict[stat.region_id] = stat
  69. return stats_dict
  70. def get_work_percentage(state_id, resource_type, end_date_time, hours, times):
  71. """Get work percentage for state in last x hours"""
  72. end_date_time = end_date_time.replace(minute=0, second=0, microsecond=0)
  73. session = SESSION()
  74. data = {
  75. 0: {
  76. 'date': end_date_time,
  77. 'stats': _get_state_stat(session, state_id, resource_type, end_date_time)
  78. }
  79. }
  80. for i in range(times, 0, -1):
  81. current_date_time = end_date_time - timedelta(hours=hours*i)
  82. data[i] = {
  83. 'date': current_date_time,
  84. 'stats': _get_state_stat(session, state_id, resource_type, current_date_time)
  85. }
  86. session.close()
  87. regions = {}
  88. for region_id, stat in data[0]['stats'].items():
  89. regions[region_id] = stat.region.name
  90. for i in range(0, times):
  91. data[i]['progress'] = {}
  92. reset_date_time = data[i+1]['date']
  93. if reset_date_time.hour >= 19:
  94. reset_date_time = reset_date_time.replace(hour=19) + timedelta(1)
  95. else:
  96. reset_date_time = reset_date_time.replace(hour=19)
  97. time_left = reset_date_time - data[i]['date']
  98. if time_left.seconds != 0:
  99. seconds_left = time_left.seconds
  100. else:
  101. seconds_left = 86400
  102. # print('{} time left: {} uur'.format(data[i]['date'], seconds_left // 60))
  103. for region_id, stat in data[i]['stats'].items():
  104. next_stat = data[i+1]['stats'][stat.region_id]
  105. if seconds_left != 86400:
  106. mined = next_stat.total() - stat.total()
  107. required = next_stat.total() / (seconds_left / (hours * 3600))
  108. else:
  109. mined = 2500 + next_stat.explored - stat.total()
  110. required = next_stat.total()
  111. if required != 0:
  112. percentage = (mined / required - 1) * 0.04 * next_stat.total()
  113. else:
  114. percentage = 0
  115. # print('{:4} left: {:3} mined: {:3} required: {:6.2f} percentage: {:6.2f}'.format(
  116. # stat.region_id, next_stat.total(), mined, required, percentage
  117. # ))
  118. data[i]['progress'][stat.region_id] = percentage
  119. message_text = ''
  120. for date in data.values():
  121. if 'progress' in date:
  122. for region_id, progress in sorted(date['progress'].items(), key=lambda x: x[1]):
  123. message_text += '{:21}: {:6.2f}\n'.format(
  124. regions[region_id],
  125. progress
  126. )
  127. return message_text