donations.py 2.9 KB

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