database.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Database module"""
  2. from datetime import datetime
  3. from app import SESSION, functions, MAX_OFFER
  4. from app.models import Region, Player, MarketTrack, StateMarketStat, PlayerMarketStat
  5. def get_new_market_track(session, resources, state_resources, items):
  6. """Get ner market track"""
  7. market_track = MarketTrack()
  8. market_track.date_time = datetime.now()
  9. market_track.resources = resources
  10. market_track.state_resources = state_resources
  11. market_track.items = items
  12. session.add(market_track)
  13. return market_track
  14. def save_resource_market(player_market, state_market):
  15. """Save factories to database"""
  16. session = SESSION()
  17. market_track = get_new_market_track(session, True, True, False)
  18. market_track.player_resources = True
  19. market_track.state_resources = True
  20. _save_player_market(session, market_track, player_market)
  21. _save_state_market(session, market_track, state_market)
  22. session.commit()
  23. session.close()
  24. def save_player_market(market):
  25. """Save player market"""
  26. session = SESSION()
  27. market_track = get_new_market_track(session, True, False, False)
  28. session.add(market_track)
  29. _save_player_market(session, market_track, market)
  30. def _save_player_market(session, market_track, market):
  31. """Save player market to database"""
  32. for item_type, offers in market.items():
  33. if not offers:
  34. continue
  35. item_dict = offers[0]
  36. market_stat = PlayerMarketStat()
  37. player = session.query(Player).get(item_dict['player_id'])
  38. if not player:
  39. player = save_player(session, item_dict)
  40. market_stat.player_id = player.id
  41. market_stat.item_type = item_type
  42. market_stat.amount = item_dict['amount']
  43. market_stat.price = item_dict['price']
  44. market_stat.total_offers = len(offers)
  45. one_t_average = functions.calculate_purchage_amount(offers, 1e12) / 100
  46. base_average = 3 * MAX_OFFER[item_type] * one_t_average
  47. market_stat.base_average = base_average
  48. market_stat.one_average = functions.calculate_purchage_amount(offers, 1 * base_average)
  49. market_stat.two_average = functions.calculate_purchage_amount(offers, 2 * base_average)
  50. market_stat.three_average = functions.calculate_purchage_amount(offers, 3 * base_average)
  51. market_stat.four_average = functions.calculate_purchage_amount(offers, 4 * base_average)
  52. market_stat.five_average = functions.calculate_purchage_amount(offers, 5 * base_average)
  53. market_stat.six_average = functions.calculate_purchage_amount(offers, 6 * base_average)
  54. market_stat.seven_average = functions.calculate_purchage_amount(offers, 7 * base_average)
  55. market_stat.eight_average = functions.calculate_purchage_amount(offers, 8 * base_average)
  56. market_stat.nine_average = functions.calculate_purchage_amount(offers, 9 * base_average)
  57. market_stat.ten_average = functions.calculate_purchage_amount(offers, 10 * base_average)
  58. market_stat.market_track_id = market_track.id
  59. session.add(market_stat)
  60. def _save_state_market(session, market_track, market):
  61. """Save state market"""
  62. for item_dict in market:
  63. market_stat = StateMarketStat()
  64. region = session.query(Region).get(item_dict['region_id'])
  65. if not region:
  66. region = save_region(session, item_dict)
  67. market_stat.region_id = region.id
  68. market_stat.market_track_id = market_track.id
  69. market_stat.item_type = item_dict['item_type'] - 1000
  70. market_stat.amount = item_dict['amount']
  71. market_stat.price = item_dict['price']
  72. session.add(market_stat)
  73. def save_player(session, item_dict):
  74. """Save player to database"""
  75. player = Player()
  76. player.id = item_dict['player_id']
  77. player.name = item_dict['player_name']
  78. session.add(player)
  79. return player
  80. def save_region(session, item_dict):
  81. """Save player to database"""
  82. region = Region()
  83. region.id = item_dict['region_id']
  84. region.name = item_dict['region_name']
  85. session.add(region)
  86. return region