app.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """General function module"""
  2. import random
  3. from datetime import datetime, timedelta
  4. from app import LOGGER, SCHEDULER, RESOURCE_IDS, DEEP_EXPLORATION_MAX , jobs, api, database
  5. def sync_deep_exploration(region_id):
  6. """Check resources and refill if necessary"""
  7. deep_explorations = api.download_deep_explorations(region_id)
  8. database.save_deep_explorations(region_id, deep_explorations)
  9. def start_orders():
  10. """start deep exploration orders"""
  11. orders = database.get_orders()
  12. for order in orders:
  13. deep_exploration = database.get_active_deep_exploration(order.region_id)
  14. if deep_exploration is None:
  15. sync_deep_exploration(order.region_id)
  16. deep_exploration = database.get_active_deep_exploration(order.region_id)
  17. start_date = deep_exploration.until_date_time if deep_exploration else datetime.now()
  18. max_seconds = 300
  19. random_seconds = random.randint(0, max_seconds)
  20. scheduled_date = start_date + timedelta(seconds=random_seconds)
  21. LOGGER.info(
  22. 'Deep exploration at %s for %s in %s',
  23. scheduled_date.strftime("%Y-%m-%d %H:%M"),
  24. RESOURCE_IDS[order.resource_type],
  25. order.region_id
  26. )
  27. SCHEDULER.add_job(
  28. jobs.start_deep_exploration,
  29. 'date',
  30. args=[order.id],
  31. id='deep_exploration_{}_{}'.format(order.region_id, order.resource_type),
  32. replace_existing=True,
  33. run_date=scheduled_date
  34. )
  35. def start_deep_exploration(order_id):
  36. """Start deep exploration"""
  37. LOGGER.info('Start order %s', order_id)
  38. order = database.get_order(order_id)
  39. order_types = {
  40. 0: get_max_points, # max
  41. 1: get_fixed_points, # fixed
  42. 2: get_percentage_points, # percentage
  43. 3: get_auto_points, # auto
  44. }
  45. if order.order_type in order_types:
  46. points = order_types[order.order_type](order)
  47. print(points)
  48. state = database.get_state(order.region_id)
  49. api.deep_explorate(
  50. state.id, order.region_id, order.resource_type, points, False
  51. )
  52. def get_max_points(order):
  53. """Get deep exploration points for order"""
  54. region = database.get_region(order.region_id)
  55. resource_limit = region.get_limit(order.resource_type)
  56. return DEEP_EXPLORATION_MAX[order.resource_type] - resource_limit
  57. def get_fixed_points(order):
  58. """Get deep exploration points for order"""
  59. return order.amount
  60. def get_percentage_points(order):
  61. """Get deep exploration points for order"""
  62. return 1
  63. def get_auto_points(order):
  64. """Get deep exploration points for order"""
  65. return 1