Advertisement
KirillMysnik

Untitled

Sep 4th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. from commands.say import SayCommand
  2. from listeners import OnClientActive, OnClientDisconnect
  3. from messages import SayText2
  4. from players.entity import Player
  5.  
  6.  
  7. READY_PLAYERS_CAP = 10
  8. CHAT_PREFIX = "[\x02PUG\x01] "
  9. ST2_MATCH_START = SayText2(
  10.     "{}{}".format(CHAT_PREFIX, "The match will now begin"))
  11.  
  12. ST2_STILL_WAITING = SayText2(
  13.     "{}{}".format(CHAT_PREFIX,
  14.         "Still waiting for 10 players to be ready to start the match"))
  15.  
  16. ST2_ALREADY_READY = SayText2(
  17.     "{}{}".format(CHAT_PREFIX, "You're already \x06READY"))
  18.  
  19. ST2_MARKED_READY = SayText2(
  20.     "{}{}".format(CHAT_PREFIX, "You have been marked as \x06READY"))
  21.  
  22. ST2_ALREADY_NOTREADY = SayText2(
  23.     "{}{}".format(CHAT_PREFIX, "You're already \x02NOT READY"))
  24.  
  25. ST2_MARKED_NOTREADY = SayText2(
  26.     "{}{}".format(CHAT_PREFIX, "You have been marked as \x02NOT READY"))
  27.  
  28.  
  29. class MyPlayer:
  30.     def __init__(self, player):
  31.         self.player = player
  32.         self.ready = False
  33.         self.permissions = 0
  34.  
  35.  
  36. def broadcast(message):
  37.     if not isinstance(message, SayText2):
  38.         message = SayText2("{}{}".format(CHAT_PREFIX, message))
  39.  
  40.     player_indexes = [my_player.player.index for my_player in players.values()]
  41.     message.send(*player_indexes)
  42.  
  43.  
  44. def tell(my_player, message):
  45.     if not isinstance(message, SayText2):
  46.         message = SayText2("{}{}".format(CHAT_PREFIX, message))
  47.  
  48.     message.send(my_player.player.index)
  49.  
  50.  
  51. def try_start_match(issued_by=None):
  52.     ready_players = list(
  53.         filter(lambda my_player: my_player.ready, players.values()))
  54.  
  55.     if len(ready_players) >= READY_PLAYERS_CAP:
  56.         broadcast(ST2_MATCH_START)
  57.     else:
  58.         if issued_by is not None:
  59.             tell(issued_by, ST2_STILL_WAITING)
  60.  
  61.  
  62. @OnClientActive
  63. def on_client_active(index):
  64.     player = Player(index)
  65.     players[index] = MyPlayer(player)
  66.  
  67.  
  68. @OnClientDisconnect
  69. def on_client_disconnect(index):
  70.     if index in players:
  71.         del players[index]
  72.  
  73.  
  74. @SayCommand('.players')
  75. def say_players(command, index, team):
  76.     ready_player_names = []
  77.     for my_player in players.values():
  78.         if my_player.ready:
  79.             ready_player_names.append(my_player.player.name)
  80.  
  81.     my_player = players[index]
  82.     tell(my_player, "Ready players: {}".format(','.join(ready_player_names)))
  83.  
  84.  
  85. @SayCommand('.ready')
  86. def say_ready(command, index, team):
  87.     my_player = players[index]
  88.    
  89.     if my_player.ready:
  90.         tell(my_player, ST2_ALREADY_READY)
  91.     else:
  92.         tell(my_player, ST2_MARKED_READY)
  93.  
  94.         my_player.ready = True
  95.         try_start_match(my_player)
  96.  
  97.  
  98. @SayCommand('.notready')
  99. def say_notready(command, index, team):
  100.     my_player = players[index]
  101.    
  102.     if not my_player.ready:
  103.         tell(my_player, ST2_ALREADY_NOTREADY)
  104.     else:
  105.         tell(my_player, ST2_MARKED_NOTREADY)
  106.  
  107.         my_player.ready = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement