app.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """general methods"""
  2. import math
  3. from app import MAX_OFFER
  4. def print_offers(market):
  5. """Print offers"""
  6. for resource_type, offers in market.items():
  7. purchage_money = 2e12
  8. purchage_average = calculate_purchage_amount(offers, purchage_money)
  9. money = MAX_OFFER[resource_type] * 5
  10. price = calculate_price(offers, money)
  11. print('{:2} {:15,} {:25,} {:15,}'.format(
  12. resource_type,
  13. purchage_average / 100,
  14. price,
  15. offers[0]['price'] / 100,
  16. ).replace(',', '.'))
  17. def calculate_price(offers, amount):
  18. """Calculate price for amount"""
  19. tmp_amount = amount
  20. total_price = 0
  21. for offer in offers:
  22. buy_amount = offer['amount']
  23. if buy_amount > tmp_amount:
  24. buy_amount = tmp_amount
  25. tmp_amount -= buy_amount
  26. total_price += buy_amount * offer['price']
  27. if tmp_amount == 0:
  28. break
  29. return total_price
  30. def calculate_purchage_amount(offers, money):
  31. """Calculate purchage amount"""
  32. tmp_money = money * 100
  33. total_amount = 0
  34. for offer in offers:
  35. buy_amount = math.floor(tmp_money / (offer['price']))
  36. if buy_amount > 0:
  37. if buy_amount > offer['amount']:
  38. buy_amount = offer['amount']
  39. tmp_money -= buy_amount * (offer['price'])
  40. total_amount += buy_amount
  41. if tmp_money == 0:
  42. break
  43. else:
  44. break
  45. return round(money * 100 / total_amount)