Bläddra i källkod

Add states, fix issue with new regions

JoostSijm 5 år sedan
förälder
incheckning
8fff5837df
4 ändrade filer med 32 tillägg och 11 borttagningar
  1. 10 3
      app/__main__.py
  2. 2 1
      app/api.py
  3. 1 1
      app/app.py
  4. 19 6
      app/database.py

+ 10 - 3
app/__main__.py

@@ -13,8 +13,8 @@ from app.app import need_refill, max_refill_seconds, print_resources
 def job_check_resources(state_id, capital_id, resource_id, do_refill):
     """Check resources and refill if necessary"""
     regions = download_resources(state_id, resource_id)
-    save_resources(state_id, regions, resource_id)
     print_resources(regions)
+    save_resources(state_id, regions, resource_id)
     if do_refill and need_refill(regions, 25):
         max_seconds = max_refill_seconds(regions, 25, 900)
         random_seconds = random.randint(0, max_seconds)
@@ -53,15 +53,22 @@ def add_check_resources(state_id, capital_id, resource_id, do_refill, minute):
     )
 
 if __name__ == '__main__':
-    # jobs
     # job_refill_resource(2788, 4002, 0)
-    # job_check_resources(2788, 4002, 0, True)
+    # job_check_resources(2788, 4002, 0, False) # VN
+    # job_check_resources(2620, 4002, 0, False) # Zeelandiae
+
+    # VN
     add_check_resources(2788, 4003, 0, True, '0,15,30,45')
     add_check_resources(2788, 4003, 11, True, '0')
+    # Zeelandiae
+    add_check_resources(2620, 0, 0, False, '50')
+    # Belgium
+    add_check_resources(2604, 0, 0, False, '40')
 
     try:
         while True:
             time.sleep(100)
     except KeyboardInterrupt:
         LOGGER.info('Exiting application')
+        SCHEDULER.shutdown()
         exit()

+ 2 - 1
app/api.py

@@ -18,6 +18,7 @@ RESOURCES = {
 
 def download_resources(state_id, resource_id):
     """Download the resource list"""
+    return read_resources()
     response = requests.get(
         '{}listed/stateresources/{}/{}'.format(BASE_URL, state_id, RESOURCES[resource_id]),
         headers=HEADERS
@@ -40,7 +41,7 @@ def parse_resources(html):
         region_id = int(region_tree['user'])
         columns = region_tree.find_all('td')
         regions[region_id] = {
-            'name': re.sub('Factories: .*$', '', columns[1].text),
+            'region_name': re.sub('Factories: .*$', '', columns[1].text),
             'explored': float(columns[2].string),
             'maximum': int(float(columns[3].string)),
             'deep_exploration': int(columns[4].string),

+ 1 - 1
app/app.py

@@ -9,7 +9,7 @@ def print_resources(regions):
         region['total_percentage'] = 100 / 2500 * region['total_left']
 
         print('{:25}: {:7.2f}{:4}{:4}{:5}{:7.2f}{:7.2f}'.format(
-            region['name'],
+            region['region_name'],
             region['explored'],
             region['maximum'],
             region['deep_exploration'],

+ 19 - 6
app/database.py

@@ -1,7 +1,7 @@
 """Main application"""
 
 from app import Session
-from app.models import ResourceTrack, ResourceStat
+from app.models import ResourceTrack, ResourceStat, Region
 
 
 def save_resources(state_id, regions, resource_id):
@@ -13,13 +13,26 @@ def save_resources(state_id, regions, resource_id):
     session.add(resource_track)
     session.commit()
 
-    for region_id, region in regions.items():
+    for region_id, region_dict in regions.items():
+        region = session.query(Region).get(region_id)
+        if not region:
+            region = save_region(session, region_id, region_dict)
+
         resource_stat = ResourceStat()
         resource_stat.resource_track_id = resource_track.id
-        resource_stat.region_id = region_id
-        resource_stat.explored = region['explored']
-        resource_stat.deep_exploration = region['deep_exploration']
-        resource_stat.limit_left = region['limit_left']
+        resource_stat.region_id = region.id
+        resource_stat.explored = region_dict['explored']
+        resource_stat.deep_exploration = region_dict['deep_exploration']
+        resource_stat.limit_left = region_dict['limit_left']
         session.add(resource_stat)
+
     session.commit()
     session.close()
+
+def save_region(session, region_id, region_dict):
+    """Save player to database"""
+    region = Region()
+    region.id = region_id
+    region.name = region_dict['region_name']
+    session.add(region)
+    return region