app.py 3.7 KB

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