Advertisement
horozov86

Medieval_games_class_controller

Jul 31st, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. from project.player import Player
  2.  
  3.  
  4. class Controller:
  5.     def __init__(self):
  6.         self.players = []
  7.         self.supplies = []
  8.  
  9.     def add_player(self, *players):
  10.         added_players = []
  11.         for player in players:
  12.             if player in self.players:
  13.                 continue
  14.             self.players.append(player)
  15.             added_players.append(player.name)
  16.         return f"Successfully added: {', '.join(added_players)}"
  17.  
  18.     def add_supply(self, *supplies):
  19.         self.supplies.extend(supplies)
  20.  
  21.     def sustain(self, player_name: str, sustenance_type: str):
  22.         player = self.__find_player_by_name(player_name)
  23.         if player is None:
  24.             return
  25.         if sustenance_type != 'Food' and sustenance_type != 'Drink':
  26.             return
  27.  
  28.         idx, supply = self.__find_supply_by_type(sustenance_type)
  29.         if supply is None:
  30.             raise Exception(f"There are no {sustenance_type.lower()} supplies left!")
  31.         if not player.need_sustenance:
  32.             return f"{player_name} have enough stamina."
  33.  
  34.         player.stamina = min(player.stamina + supply.energy, Player.MAX_STAMINA)
  35.         self.supplies.pop(idx)
  36.         return f"{player_name} sustained successfully with {supply.name}."
  37.  
  38.     def duel(self, first_player_name: str, second_player_name: str):
  39.         pass
  40.         # # изваждаме играчите по име
  41.         # # for player in self.players:
  42.         #     #if player.name == first_player_name:
  43.         #         #return player
  44.         first_player = self.__find_player_by_name(first_player_name) # за всеки един player(обект) в self.player, ако
  45.         # # player.name (името на този играч е равно на first_player_name, който се подава, тогава return ....
  46.         second_player = self.__find_player_by_name(second_player_name)
  47.         # # така имаме вече двете инстанции
  48.         #
  49.         result = ''
  50.         if first_player.stamina == 0:
  51.             result += f"Player {first_player.name} does not have enough stamina."
  52.         if second_player.stamina == 0:
  53.             result += '\n' + f"Player {second_player.name} does not have enough stamina."
  54.         if result:
  55.             return result.strip()
  56.  
  57.         if second_player.stamina < first_player.stamina:
  58.             first_player, second_player = second_player, first_player
  59.  
  60.         first_player_damage = first_player.stamina / 2
  61.         second_player.stamina = max(second_player.stamina - first_player_damage, 0)
  62.         if second_player.stamina == 0:
  63.             return f"Winner: {first_player.name}"
  64.  
  65.         second_player_damage = second_player.stamina / 2
  66.         first_player.stamina = max(first_player.stamina - second_player_damage, 0)
  67.         if first_player.stamina == 0:
  68.             return f"Winner: {second_player.name}"
  69.  
  70.         winner = first_player if first_player.stamina > second_player.stamina else second_player
  71.         return f"Winner: {winner.name}"
  72.  
  73.     def next_day(self):
  74.         for player in self.players:
  75.             player.stamina = max(player.stamina - player.age * 2, 0)
  76.             self.sustain(player.name, 'Food')
  77.             self.sustain(player.name, 'Drink')
  78.  
  79.     def __str__(self):
  80.         result = ''
  81.         for player in self.players:
  82.             result += str(player) + '\n'
  83.  
  84.         for supply in self.supplies:
  85.             result += supply.details() + '\n'
  86.  
  87.         return result.strip()
  88.  
  89.     def __find_player_by_name(self, player_name):
  90.         for player in self.players:
  91.             if player.name == player_name:
  92.                 return player
  93.  
  94.     def __find_supply_by_type(self, sustenance_type):
  95.         for idx in range(len(self.supplies) -1, -1, -1):
  96.             supply = self.supplies[idx]
  97.             if supply.__class__.__name__ == sustenance_type:
  98.                 return (idx, supply)
  99.  
  100.         return (-1, None)
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement