player.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. INITIAL_RATING = 228
  2. class Player:
  3. def __init__(self, index: int, name: str):
  4. self.index = index # ID игрока
  5. self.name = name # Никнейм
  6. self.rating = INITIAL_RATING # Рейтинг
  7. self.games_count = 0 # Кол-во игр
  8. self.personal_wins = 0 # Личных побед
  9. self.team_wins = 0 # Командных побед
  10. self.highest_score_take = [-INITIAL_RATING, -1] # Наибольшее кол-во очков за партию
  11. self.highest_score_loss = [INITIAL_RATING, -1] # Наименьшее кол-во очков за игру
  12. self.peak_score = INITIAL_RATING # Пиковый рейтинг
  13. self.changes_history = [] # История изменения рейтинга [индекс игры, изменение]
  14. self.top_position = 228 # Наивысшая позиция
  15. self.lowest_position = -1 # Низшая позиция
  16. self.previous_position = 0 # Позиция в результате прошлой партии
  17. self.change_position = 0 # Позиция в результате прошлой партии
  18. self.games_info = []
  19. def __lt__(self, other):
  20. return self.rating > other.rating
  21. def get_serializable(self):
  22. d = dict()
  23. d['player_id'] = self.index
  24. d['name'] = self.name
  25. d['rating'] = self.rating
  26. d['count'] = self.games_count
  27. d['personal_wins'] = self.personal_wins
  28. d['team_wins'] = self.team_wins
  29. d['total_wins'] = self.get_wins_count()
  30. d['highest_score_take'] = self.highest_score_take[0]
  31. d['highest_score_game'] = self.highest_score_take[1]
  32. d['lowest_score_take'] = self.highest_score_loss[0]
  33. d['lowest_score_game'] = self.highest_score_loss[1]
  34. d['peak_score'] = self.peak_score
  35. d['changes'] = self.changes_history
  36. d['top_position'] = self.top_position
  37. d['lowest_position'] = self.lowest_position
  38. d['average'] = self.get_average()
  39. d['win_rate'] = self.get_win_rate()
  40. d['change_position'] = self.change_position
  41. arr = []
  42. for i in self.games_info:
  43. arr.append(i.get_serializable())
  44. d['games_info'] = arr
  45. return d
  46. def get_wins_count(self):
  47. return self.team_wins + self.personal_wins
  48. def get_win_rate(self):
  49. if self.games_count == 0:
  50. return 0
  51. return round(self.get_wins_count() / self.games_count, 2)
  52. def get_average(self):
  53. if self.games_count == 0:
  54. return 0
  55. res = 0
  56. for i in self.changes_history:
  57. res += i['rating_change']
  58. return round(res / self.games_count, 2)