|
@@ -2,23 +2,27 @@
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
-from app import session
|
|
|
+from app import Session
|
|
|
from app.models import State, Player, StateRegion, \
|
|
|
PlayerLocation, PlayerResidency, StateWorkPermit
|
|
|
|
|
|
|
|
|
def get_state_regions(state_id):
|
|
|
"""Get regions from state"""
|
|
|
+ session = Session()
|
|
|
state = session.query(State).get(state_id)
|
|
|
- return state.regions.filter(StateRegion.until_date_time == None).all()
|
|
|
+ regions = state.regions.filter(StateRegion.until_date_time == None).all()
|
|
|
+ session.close()
|
|
|
+ return regions
|
|
|
|
|
|
def save_citizens(region_id, citizens):
|
|
|
"""Save citizens to database"""
|
|
|
+ session = Session()
|
|
|
player_ids = []
|
|
|
for player_dict in citizens:
|
|
|
player = session.query(Player).get(player_dict['id'])
|
|
|
if player is None:
|
|
|
- player = save_player(player_dict)
|
|
|
+ player = save_player(session, player_dict)
|
|
|
player_ids.append(player.id)
|
|
|
last_region = player.locations.first()
|
|
|
if not last_region or last_region.id != region_id:
|
|
@@ -27,23 +31,26 @@ def save_citizens(region_id, citizens):
|
|
|
player_location.region_id = region_id
|
|
|
player_location.from_date_time = datetime.now()
|
|
|
session.add(player_location)
|
|
|
- session.commit()
|
|
|
+ session.commit()
|
|
|
current_citizens = session.query(PlayerLocation) \
|
|
|
.filter(PlayerLocation.region_id == region_id) \
|
|
|
.filter(PlayerLocation.until_date_time == None).all()
|
|
|
+ print(player_ids)
|
|
|
for current_citizen in current_citizens:
|
|
|
if current_citizen.player_id not in player_ids:
|
|
|
current_citizen.until_date_time = datetime.now()
|
|
|
session.commit()
|
|
|
+ session.close()
|
|
|
|
|
|
|
|
|
def save_residents(region_id, residents):
|
|
|
"""Save residents to database"""
|
|
|
+ session = Session()
|
|
|
player_ids = []
|
|
|
for player_dict in residents:
|
|
|
player = session.query(Player).get(player_dict['id'])
|
|
|
if player is None:
|
|
|
- player = save_player(player_dict)
|
|
|
+ player = save_player(session, player_dict)
|
|
|
player_ids.append(player.id)
|
|
|
last_residency = player.residencies.first()
|
|
|
if not last_residency or last_residency.id != region_id:
|
|
@@ -60,15 +67,17 @@ def save_residents(region_id, residents):
|
|
|
if current_resident.player_id not in player_ids:
|
|
|
current_resident.until_date_time = datetime.now()
|
|
|
session.commit()
|
|
|
+ session.close()
|
|
|
|
|
|
|
|
|
def save_work_permits(state_id, work_permits):
|
|
|
"""Save residents to database"""
|
|
|
+ session = Session()
|
|
|
player_ids = []
|
|
|
for player_dict in work_permits:
|
|
|
player = session.query(Player).get(player_dict['id'])
|
|
|
if player is None:
|
|
|
- player = save_player(player_dict)
|
|
|
+ player = save_player(session, player_dict)
|
|
|
player_ids.append(player.id)
|
|
|
last_work_permit = player.state_work_permits.first()
|
|
|
if not last_work_permit or last_work_permit.id != state_id:
|
|
@@ -85,8 +94,9 @@ def save_work_permits(state_id, work_permits):
|
|
|
if current_work_permit.player_id not in player_ids:
|
|
|
current_work_permit.until_date_time = datetime.now()
|
|
|
session.commit()
|
|
|
+ session.close()
|
|
|
|
|
|
-def save_player(player_dict):
|
|
|
+def save_player(session, player_dict):
|
|
|
"""Save player to database"""
|
|
|
player = Player()
|
|
|
player.id = player_dict['id']
|