app.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """General function module"""
  2. import time
  3. import random
  4. from datetime import datetime, timedelta
  5. from app import LOGGER, SCHEDULER, RESOURCE_IDS, DEEP_EXPLORATION_MAX, KOEF_FACTOR, \
  6. jobs, api, database
  7. def schedule_orders():
  8. """start deep exploration orders"""
  9. LOGGER.info('Start schedule orders')
  10. orders = database.get_orders()
  11. for job in SCHEDULER.get_jobs():
  12. print(job)
  13. if 'deep_exploration' in job.id:
  14. job.remove()
  15. for order in orders:
  16. schedule_order(order)
  17. LOGGER.info('Finish schedule orders')
  18. def sync_deep_exploration(region_id):
  19. """Check resources and refill if necessary"""
  20. LOGGER.info('%s: sync deep exploration history', region_id)
  21. deep_explorations = api.download_deep_explorations(region_id)
  22. database.save_deep_explorations(region_id, deep_explorations)
  23. def schedule_order(order):
  24. """start deep exploration order"""
  25. deep_exploration = database.get_active_deep_exploration(order.region_id)
  26. if deep_exploration is None:
  27. sync_deep_exploration(order.region_id)
  28. deep_exploration = database.get_active_deep_exploration(order.region_id)
  29. start_date = deep_exploration.until_date_time if deep_exploration else datetime.now()
  30. max_seconds = 5 # 300
  31. random_seconds = random.randint(0, max_seconds)
  32. scheduled_date = start_date + timedelta(seconds=random_seconds)
  33. LOGGER.info(
  34. '%s: schedule deep exploration at %s for %s',
  35. order.region_id,
  36. scheduled_date.strftime("%Y-%m-%d %H:%M:%S"),
  37. RESOURCE_IDS[order.resource_type]
  38. )
  39. SCHEDULER.add_job(
  40. jobs.start_deep_exploration_order,
  41. 'date',
  42. args=[order.id],
  43. id='deep_exploration_{}_{}'.format(order.region_id, order.resource_type),
  44. replace_existing=True,
  45. run_date=scheduled_date
  46. )
  47. def start_deep_exploration(order_id):
  48. """Start deep exploration"""
  49. LOGGER.info('Start order %s', order_id)
  50. order = database.get_order(order_id)
  51. order_types = {
  52. 0: get_max_points, # max
  53. 1: get_fixed_points, # fixed
  54. 2: get_percentage_points, # percentage
  55. 3: get_auto_points, # auto
  56. }
  57. if order.order_type in order_types:
  58. points = order_types[order.order_type](order)
  59. state = database.get_state(order.region_id)
  60. LOGGER.info(
  61. '%s: deep explorate %s points for %s',
  62. order.region_id,
  63. points,
  64. RESOURCE_IDS[order.resource_type]
  65. )
  66. api.deep_explorate(state.id, order.region_id, order.resource_type, points, False)
  67. time.sleep(10)
  68. schedule_order(order)
  69. def get_max_points(order):
  70. """Get deep exploration points for order"""
  71. region = database.get_region(order.region_id)
  72. resource_limit = region.get_limit(order.resource_type)
  73. return DEEP_EXPLORATION_MAX[order.resource_type] - resource_limit
  74. def get_fixed_points(order):
  75. """Get deep exploration points for order"""
  76. return order.amount
  77. def get_percentage_points(order):
  78. """Get deep exploration points for order"""
  79. region = database.get_region(order.region_id)
  80. return calc_deep_exploration(
  81. region.get_limit(order.resource_type),
  82. order.amount,
  83. order.resource_type
  84. )
  85. def get_auto_points(order):
  86. """Get deep exploration points for order"""
  87. return 1
  88. def calc_deep_exploration(resource_limit, percentage, resource_type):
  89. """Calculate deep expo for resource"""
  90. koef_factor = KOEF_FACTOR[resource_type]
  91. max_deep_exploration = DEEP_EXPLORATION_MAX[resource_type] - resource_limit
  92. initial_resource_factor = pow(resource_limit * koef_factor / 10, 0.8)
  93. new_resource_factor = initial_resource_factor * (percentage / 100 + 1)
  94. points = (10 * (new_resource_factor ** (1/0.8))) / koef_factor - resource_limit
  95. return max_deep_exploration if points > max_deep_exploration else round(points)