donation.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Calculate donations"""
  2. import re
  3. import math
  4. def sum_donations():
  5. """Count total donations"""
  6. resources = {
  7. 'bbl': {},
  8. 'kg': {},
  9. 'pcs': {},
  10. 'G': {},
  11. 'g': {},
  12. '$': {},
  13. }
  14. with open('donation.txt', 'r') as file:
  15. for line in file:
  16. try:
  17. donation = re.search(r'\d.*?(bbl|kg|pcs|G|g|\$)', line).group(0)
  18. player = re.search(r'.*?\s\t', line).group(0).strip()
  19. amount, resource = donation.split(' ')
  20. amount = int(amount.replace('.', ''))
  21. if player not in resources[resource]:
  22. resources[resource][player] = 0
  23. resources[resource][player] += amount
  24. except Exception as exception:
  25. print('exception')
  26. print('%s %s' % (line, exception))
  27. return resources
  28. def print_resources(resources):
  29. """Print donations per resource"""
  30. for resource, players in resources.items():
  31. print(resource)
  32. count = 1
  33. for player in sorted(players, key=players.get, reverse=True):
  34. print('%20s %s' % (bucks(players[player]), player))
  35. if count >= 10:
  36. break
  37. count += 1
  38. def calc_reward(resources):
  39. """Calculate reward"""
  40. reward = {
  41. 'bbl': 130,
  42. 'kg': 120,
  43. 'pcs': 850000,
  44. 'g': 1250,
  45. }
  46. resource_koef = {
  47. 'bbl': 1000000000,
  48. 'kg': 1105000000,
  49. 'pcs': 104000000,
  50. 'g': 160000,
  51. }
  52. player_reward = {}
  53. for resource, players in resources.items():
  54. if resource not in reward:
  55. continue
  56. for player, amount in players.items():
  57. if player not in player_reward:
  58. player_reward[player] = 0
  59. player_reward[player] = reward[resource] * amount
  60. koef = math.floor(amount / resource_koef[resource])
  61. player_reward[player] += round(player_reward[player] / 100 * koef * 2)
  62. return player_reward
  63. def print_reward(players):
  64. """print player rewards"""
  65. total = 0
  66. for player, reward in sorted(players.items(), key=lambda x: x[1], reverse=True):
  67. total += reward
  68. print('%20s %s' % (bucks(reward), player))
  69. print('total: %s' % bucks(total))
  70. def bucks(integer):
  71. """Format number"""
  72. return '{:,}'.format(integer).replace(',', '.')
  73. if __name__ == '__main__':
  74. RESOURCES = sum_donations()
  75. PLAYERS = calc_reward(RESOURCES)
  76. print_reward(PLAYERS)
  77. print_resources(RESOURCES)