__main__.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Main app"""
  2. from datetime import datetime, timedelta
  3. import random
  4. import time
  5. from hvs import scheduler
  6. from hvs.app import download_resources, need_refill, refill, save_resources, print_resources
  7. def job_check_resources(state_id, resource_id, capital_id):
  8. """Check resources and refill if necessary"""
  9. regions = download_resources(state_id, resource_id)
  10. save_resources(state_id, regions)
  11. print_resources(regions)
  12. if need_refill(regions):
  13. random_time = timedelta(seconds=random.randint(0, 300))
  14. scheduled_date = datetime.now() + random_time
  15. job_id = 'refill_{}_{}'.format(capital_id, resource_id)
  16. print('refill resource: {} at {} ({} minutes)'.format(
  17. resource_id,
  18. scheduled_date,
  19. round(random_time.seconds / 60)
  20. ))
  21. job = scheduler.get_job(job_id)
  22. if not job:
  23. scheduler.add_job(
  24. job_refill_resource,
  25. 'date',
  26. args=[4002, resource_id],
  27. id=job_id,
  28. run_date=scheduled_date
  29. )
  30. def job_refill_resource(capital_id, resource_id):
  31. """Execute refill job"""
  32. refill(capital_id, resource_id)
  33. if __name__ == '__main__':
  34. # jobs
  35. VN_CHECK_GOLD_JOB = scheduler.get_job('vn_check_gold')
  36. if not VN_CHECK_GOLD_JOB:
  37. scheduler.add_job(
  38. job_check_resources,
  39. 'cron',
  40. args=[2788, 0, 4002],
  41. id='vn_check_gold',
  42. minute='0,15,30,45'
  43. )
  44. VN_CHECK_URANIUM_JOB = scheduler.get_job('vn_check_uranium')
  45. if not VN_CHECK_URANIUM_JOB:
  46. scheduler.add_job(
  47. job_check_resources,
  48. 'cron',
  49. args=[2788, 11, 4002],
  50. id='vn_check_uranium',
  51. minute='0'
  52. )
  53. while True:
  54. time.sleep(100)