functions.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """General functions"""
  2. import math
  3. from app import MAX_OFFER
  4. def print_player_market(market):
  5. """Print player market"""
  6. print('id lowest 1T 2T 3T 4T 5T')
  7. for resource_type, offers in market.items():
  8. print('{:2} {:10.2f} {:10.2f} {:10.2f} {:10.2f} {:10.2f} {:10.2f}'.format(
  9. resource_type,
  10. offers[0]['price'] / 100,
  11. calculate_purchage_amount(offers, 1e12) / 100,
  12. calculate_purchage_amount(offers, 2e12) / 100,
  13. calculate_purchage_amount(offers, 3e12) / 100,
  14. calculate_purchage_amount(offers, 4e12) / 100,
  15. calculate_purchage_amount(offers, 5e12) / 100,
  16. ).replace(',', '.'))
  17. for resource_type, offers in market.items():
  18. max_offer = MAX_OFFER[resource_type]
  19. prices = str(resource_type)
  20. one_t_average = calculate_purchage_amount(offers, 1e12) / 100
  21. for i in range(1, 20):
  22. prices += ',{}'.format(
  23. calculate_purchage_amount(offers, i * 3 * max_offer * one_t_average) / 100,
  24. )
  25. print(prices)
  26. def print_state_market(market):
  27. """Print state offers"""
  28. print(' id region_id region_name')
  29. for item in market:
  30. print('{:6} {:8} {:20} {:14.2f} {:10}'.format(
  31. item['item_type'],
  32. item['region_id'],
  33. item['region_name'],
  34. item['price'] / 100,
  35. item['amount']
  36. ).replace(',', '.'))
  37. def calculate_average_price(offers, amount):
  38. """Calculate average price based on amount"""
  39. total = calculate_price(offers, amount)
  40. return total / amount
  41. def calculate_price(offers, amount):
  42. """Calculate price for amount"""
  43. tmp_amount = amount
  44. total_price = 0
  45. for offer in offers:
  46. buy_amount = offer['amount']
  47. if buy_amount > tmp_amount:
  48. buy_amount = tmp_amount
  49. tmp_amount -= buy_amount
  50. total_price += buy_amount * offer['price']
  51. if tmp_amount == 0:
  52. break
  53. return total_price
  54. def calculate_purchage_amount(offers, money):
  55. """Calculate purchage amount"""
  56. tmp_money = money * 100
  57. total_amount = 0
  58. spend_money = 0
  59. for offer in offers:
  60. buy_amount = math.floor(tmp_money / (offer['price']))
  61. if buy_amount > 0:
  62. if buy_amount > offer['amount']:
  63. buy_amount = offer['amount']
  64. tmp_money -= buy_amount * offer['price']
  65. spend_money += buy_amount * offer['price']
  66. total_amount += buy_amount
  67. if tmp_money == 0:
  68. break
  69. else:
  70. break
  71. return round(spend_money / total_amount)