app.py 1.6 KB

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