__main__.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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, max_refill_seconds, refill, \
  7. save_resources, print_resources
  8. def job_check_resources(state_id, capital_id, resource_id):
  9. """Check resources and refill if necessary"""
  10. regions = download_resources(state_id, resource_id)
  11. save_resources(state_id, regions, resource_id)
  12. print_resources(regions)
  13. if need_refill(regions, 25):
  14. max_seconds = max_refill_seconds(regions, 25, 900)
  15. random_seconds = random.randint(0, max_seconds)
  16. random_time_delta = timedelta(seconds=random_seconds)
  17. scheduled_date = datetime.now() + random_time_delta
  18. job_id = 'refill_{}_{}'.format(capital_id, resource_id)
  19. print('refill resource: {} at {} ({} minutes)'.format(
  20. resource_id,
  21. scheduled_date,
  22. round(random_time.seconds / 60)
  23. ))
  24. job = scheduler.get_job(job_id)
  25. if not job:
  26. scheduler.add_job(
  27. job_refill_resource,
  28. 'date',
  29. args=[state_id, capital_id, resource_id],
  30. id=job_id,
  31. run_date=scheduled_date
  32. )
  33. def job_refill_resource(state_id, capital_id, resource_id):
  34. """Execute refill job"""
  35. refill(state_id, capital_id, resource_id)
  36. if __name__ == '__main__':
  37. # jobs
  38. # job_refill_resource(2788, 4002, 0)
  39. # job_check_resources(2788, 4002, 0)
  40. VN_CHECK_GOLD_JOB = scheduler.get_job('vn_check_gold')
  41. if not VN_CHECK_GOLD_JOB:
  42. scheduler.add_job(
  43. job_check_resources,
  44. 'cron',
  45. args=[2788, 4002, 0],
  46. id='vn_check_gold',
  47. minute='0,15,30,45'
  48. )
  49. VN_CHECK_URANIUM_JOB = scheduler.get_job('vn_check_uranium')
  50. if not VN_CHECK_URANIUM_JOB:
  51. scheduler.add_job(
  52. job_check_resources,
  53. 'cron',
  54. args=[2788, 4002, 11],
  55. id='vn_check_uranium',
  56. minute='0'
  57. )
  58. while True:
  59. time.sleep(100)