database.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. """Database module"""
  2. from datetime import datetime
  3. from app import SESSION, LOGGER
  4. from app.models import State, Region, Player, StateRegion, \
  5. PlayerLocation, PlayerResidency, StateWorkPermit
  6. def get_regions(region_ids):
  7. """Get regions from list"""
  8. session = SESSION()
  9. regions = []
  10. for region_id in region_ids:
  11. region = session.query(Region).get(region_id)
  12. if not region:
  13. region = save_region(session, region_id)
  14. regions.append(region)
  15. session.close()
  16. return regions
  17. def get_state_regions(state_id):
  18. """Get regions from state"""
  19. session = SESSION()
  20. state = session.query(State).get(state_id)
  21. region_list = []
  22. if state:
  23. region_list = state.regions.filter(StateRegion.until_date_time == None).all()
  24. else:
  25. save_state(session, state_id)
  26. LOGGER.error('State %6s: not found', state_id)
  27. session.close()
  28. return region_list
  29. def save_citizens(region_id, citizens):
  30. """Save citizens to database"""
  31. session = SESSION()
  32. player_ids = []
  33. new_citizens = 0
  34. for player_dict in citizens:
  35. player = session.query(Player).get(player_dict['id'])
  36. if player is None:
  37. player = save_player(session, player_dict)
  38. player_ids.append(player.id)
  39. last_location = player.locations \
  40. .filter(PlayerLocation.region_id == region_id) \
  41. .filter(PlayerLocation.until_date_time == None) \
  42. .first()
  43. if not last_location:
  44. new_citizens += 1
  45. player_location = PlayerLocation()
  46. player_location.player_id = player.id
  47. player_location.region_id = region_id
  48. player_location.from_date_time = datetime.now().replace(second=0, minute=0)
  49. session.add(player_location)
  50. LOGGER.info('regio %6s: "%s" new citizens', region_id, new_citizens)
  51. session.commit()
  52. current_citizens = session.query(PlayerLocation) \
  53. .filter(PlayerLocation.region_id == region_id) \
  54. .filter(PlayerLocation.until_date_time == None).all()
  55. left_citizens = 0
  56. for current_citizen in current_citizens:
  57. if current_citizen.player_id not in player_ids:
  58. left_citizens += 1
  59. current_citizen.until_date_time = datetime.now().replace(second=0, minute=0)
  60. LOGGER.info('regio %6s: "%s" citizens left', region_id, left_citizens)
  61. session.commit()
  62. session.close()
  63. def save_residents(region_id, residents):
  64. """Save residents to database"""
  65. session = SESSION()
  66. player_ids = []
  67. new_residents = 0
  68. for player_dict in residents:
  69. player = session.query(Player).get(player_dict['id'])
  70. if player is None:
  71. player = save_player(session, player_dict)
  72. player_ids.append(player.id)
  73. last_residency = player.residencies \
  74. .filter(PlayerResidency.region_id == region_id) \
  75. .filter(PlayerResidency.until_date_time == None) \
  76. .first()
  77. if not last_residency:
  78. new_residents += 1
  79. player_location = PlayerResidency()
  80. player_location.player_id = player.id
  81. player_location.region_id = region_id
  82. player_location.from_date_time = datetime.now().replace(second=0, minute=0)
  83. session.add(player_location)
  84. LOGGER.info('regio %6s: "%s" new residents', region_id, new_residents)
  85. session.commit()
  86. current_residents = session.query(PlayerResidency) \
  87. .filter(PlayerResidency.region_id == region_id) \
  88. .filter(PlayerResidency.until_date_time == None).all()
  89. for current_resident in current_residents:
  90. if current_resident.player_id not in player_ids:
  91. current_resident.until_date_time = datetime.now().replace(second=0, minute=0)
  92. session.commit()
  93. session.close()
  94. def save_work_permits(state_id, work_permits):
  95. """Save residents to database"""
  96. session = SESSION()
  97. player_ids = []
  98. new_work_permits = 0
  99. for player_dict in work_permits:
  100. player = session.query(Player).get(player_dict['id'])
  101. if player is None:
  102. player = save_player(session, player_dict)
  103. player_ids.append(player.id)
  104. last_work_permit = player.state_work_permits \
  105. .filter(StateWorkPermit.state_id == state_id) \
  106. .filter(StateWorkPermit.until_date_time == None) \
  107. .first()
  108. if not last_work_permit:
  109. new_work_permits += 1
  110. state_work_permit = StateWorkPermit()
  111. state_work_permit.player_id = player.id
  112. state_work_permit.state_id = state_id
  113. state_work_permit.from_date_time = player_dict['from']
  114. session.add(state_work_permit)
  115. session.commit()
  116. LOGGER.info('state %6s: "%s" new work permits', state_id, new_work_permits)
  117. current_work_permits = session.query(StateWorkPermit) \
  118. .filter(StateWorkPermit.state_id == state_id) \
  119. .filter(StateWorkPermit.until_date_time == None).all()
  120. for current_work_permit in current_work_permits:
  121. if current_work_permit.player_id not in player_ids:
  122. current_work_permit.until_date_time = datetime.now().replace(second=0, minute=0)
  123. session.commit()
  124. session.close()
  125. def save_player(session, player_dict):
  126. """Save player to database"""
  127. player = Player()
  128. player.id = player_dict['id']
  129. player.name = player_dict['name']
  130. player.nation = player_dict['nation']
  131. if 'registration_date' in player_dict:
  132. player.registration_date = player_dict['registration_date']
  133. session.add(player)
  134. session.commit()
  135. return player
  136. def save_state(session, state_id):
  137. """Save state to database"""
  138. state = State()
  139. state.id = state_id
  140. session.add(state)
  141. session.commit()
  142. return state
  143. def save_region(session, region_id):
  144. """Save region to database"""
  145. region = Region()
  146. region.id = region_id
  147. session.add(region)
  148. session.commit()
  149. return region