department.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Calculate percentage level of index for regions
  3. inside the Verenigde Nederlanden.
  4. """
  5. import sys
  6. import math
  7. import re
  8. from datetime import timedelta
  9. import dateutil.parser
  10. # Config
  11. PLAYER = True
  12. DATE = True
  13. def calculate_buildings(filename='department.txt'):
  14. """Count working in departments"""
  15. total = 0
  16. players = {}
  17. days = {}
  18. with open(filename, 'r') as file:
  19. for line in file:
  20. try:
  21. date_str = re.search(r'\s\d\d.*', line).group(0)
  22. date_str = re.sub(r'\s\d\d\s', '', date_str)
  23. date = dateutil.parser.parse(date_str)
  24. if date.hour >= 20:
  25. date += timedelta(days=1)
  26. line = re.sub(r'\s\d\d.*', '', line)
  27. line = re.sub(r'\[.*\]', '', line)
  28. line = line.strip()
  29. count = re.search(r'\+\d+', line).group(0)
  30. count = count.replace('+', '')
  31. count = int(count)
  32. total += count
  33. if PLAYER:
  34. player = re.sub(r'\s\(.*', '', line)
  35. if player in players:
  36. players[player] += count
  37. else:
  38. players[player] = count
  39. if DATE:
  40. date_format = date.strftime("%Y-%m-%d")
  41. if date_format in days:
  42. days[date_format] += count
  43. else:
  44. days[date_format] = count
  45. except Exception as exception:
  46. print('%s %s' % (line, exception))
  47. if PLAYER:
  48. print_players(players)
  49. if DATE:
  50. print_days(days)
  51. print(total)
  52. def print_players(players):
  53. """Print players and reward"""
  54. total_reward = 0
  55. print('punten,beloning,naam')
  56. for player in sorted(players, key=players.get, reverse=True):
  57. worked_times = math.floor(players[player] / 10)
  58. reward = worked_times * 2000000000
  59. if worked_times >= 6:
  60. reward += 10000000000
  61. total_reward += reward
  62. print('%3s,$ %5s,%s' % (players[player], bucks(reward), player))
  63. print(total_reward)
  64. def print_days(days):
  65. """Print date and points"""
  66. for date in sorted(days, reverse=True):
  67. print('%s,%3s' % (date, days[date]))
  68. def bucks(money):
  69. """Format money"""
  70. str_format = '{:,}'.format(money)
  71. new_str = ''
  72. for i in range(len(str_format), 0, -4):
  73. if str_format[i-4:i] == ',000':
  74. new_str = 'k' + new_str
  75. else:
  76. new_str = str_format[:i] + new_str
  77. break
  78. new_str = new_str.replace('kkkk', 't')
  79. new_str = new_str.replace(',', '.')
  80. return new_str
  81. if __name__ == '__main__':
  82. if len(sys.argv) < 2:
  83. calculate_buildings()
  84. else:
  85. calculate_buildings(sys.argv[1])