__main__.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_delta.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. scheduler.add_job(
  41. job_check_resources,
  42. 'cron',
  43. args=[2788, 4002, 0],
  44. id='vn_check_gold',
  45. replace_existing=True,
  46. minute='0,15,30,45'
  47. )
  48. scheduler.add_job(
  49. job_check_resources,
  50. 'cron',
  51. args=[2788, 4002, 11],
  52. id='vn_check_uranium',
  53. replace_existing=True,
  54. minute='0'
  55. )
  56. while True:
  57. time.sleep(100)