__main__.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, capital_id, resource_id):
  8. """Check resources and refill if necessary"""
  9. regions = download_resources(state_id, resource_id)
  10. save_resources(state_id, regions, resource_id)
  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=[state_id, capital_id, resource_id],
  27. id=job_id,
  28. run_date=scheduled_date
  29. )
  30. def job_refill_resource(state_id, capital_id, resource_id):
  31. """Execute refill job"""
  32. refill(state_id, capital_id, resource_id)
  33. if __name__ == '__main__':
  34. # jobs
  35. # job_refill_resource(2788, 4002, 0)
  36. # job_check_resources(2788, 4002, 0)
  37. VN_CHECK_GOLD_JOB = scheduler.get_job('vn_check_gold')
  38. if not VN_CHECK_GOLD_JOB:
  39. scheduler.add_job(
  40. job_check_resources,
  41. 'cron',
  42. args=[2788, 4002, 0],
  43. id='vn_check_gold',
  44. minute='0,15,30,45'
  45. )
  46. VN_CHECK_URANIUM_JOB = scheduler.get_job('vn_check_uranium')
  47. if not VN_CHECK_URANIUM_JOB:
  48. scheduler.add_job(
  49. job_check_resources,
  50. 'cron',
  51. args=[2788, 4002, 11],
  52. id='vn_check_uranium',
  53. minute='0'
  54. )
  55. while True:
  56. time.sleep(100)