functions.py 1.5 KB

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