Advertisement
Ayuto

Example Input API

Nov 26th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. # =============================================================================
  2. # >> API
  3. # =============================================================================
  4. import collections
  5.  
  6. from commands.say import SayFilter
  7. from commands import CommandReturn
  8.  
  9. from listeners import OnClientDisconnect
  10.  
  11.  
  12. request_queue = collections.defaultdict(collections.deque)
  13.  
  14. class InputRequest(object):
  15.     def __init__(self, index, callback, message, data):
  16.         self.index = index
  17.         self.callback = callback
  18.         self.message = message
  19.         self.data = data
  20.  
  21.     def ask(self):
  22.         if self.message is not None:
  23.             self.message.send(self.index)
  24.  
  25.     def receive(self, result, team_only):
  26.         self.callback(self, result, team_only)
  27.  
  28.  
  29. def request_user_input(index, callback, message=None, data=None, top=False):
  30.     """Request input from a user. The input needs to be entered in the chat.
  31.  
  32.    :param int index:
  33.        The index of the player to ask for input.
  34.    :param callback:
  35.        A callable object that will be called as soon as the player has
  36.        entered his input.
  37.    :param UserMessageCreator message:
  38.        If not None, this message will be used to send the player a message.
  39.    :param object data:
  40.        Any data you would like to access in the callback.
  41.    :param bool top:
  42.        If ``True``, this request has a high priority and will be put before
  43.        any other request. Use this if you want to request a new input,
  44.        because the entered input was invalid.
  45.    :return:
  46.        Return ``True`` if the request has been sent immediately, which means
  47.        that no other request is pending.
  48.    :rtype: bool
  49.    """
  50.     queue = request_queue[index]
  51.  
  52.     request = InputRequest(index, callback, message, data)
  53.     if top:
  54.         queue.appendleft(request)
  55.     else:
  56.         queue.append(request)
  57.  
  58.     if len(queue) == 1:
  59.         request.ask()
  60.         return True
  61.  
  62.     return False
  63.  
  64.  
  65. @SayFilter
  66. def on_say(command, index, team_only):
  67.     if index not in request_queue:
  68.         return CommandReturn.CONTINUE
  69.  
  70.     queue = request_queue[index]
  71.     if not queue:
  72.         return CommandReturn.CONTINUE
  73.  
  74.     request = queue.popleft()
  75.     request.receive(command.command_string, team_only)
  76.  
  77.     if queue:
  78.         queue[0].ask()
  79.  
  80.     return CommandReturn.BLOCK
  81.  
  82.  
  83. @OnClientDisconnect
  84. def on_client_disconnect(index):
  85.     request_queue.pop(index)
  86.  
  87.  
  88.  
  89. # =============================================================================
  90. # >> TEST
  91. # =============================================================================
  92. from commands.typed import TypedSayCommand
  93. from players.entity import Player
  94. from messages import SayText2
  95.  
  96. m1 = SayText2('Enter something1')
  97. m2 = SayText2('Enter something2')
  98.  
  99. @TypedSayCommand(['test'])
  100. def on_test(info):
  101.     request_user_input(info.index, on_input_entered, m1)
  102.     request_user_input(info.index, on_input_entered, m2)
  103.  
  104. def on_input_entered(request, result, team_only):
  105.     index = request.index
  106.     if result.casefold() == 'invalid':
  107.         print('Entered input was invalid.')
  108.         request_user_input(index, on_input_entered, m1, top=True)
  109.     else:
  110.         player = Player(index)
  111.         print('Player {} has entered "{}".'.format(player.name, result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement