Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # =============================================================================
- # >> API
- # =============================================================================
- import collections
- from commands.say import SayFilter
- from commands import CommandReturn
- from listeners import OnClientDisconnect
- request_queue = collections.defaultdict(collections.deque)
- class InputRequest(object):
- def __init__(self, index, callback, message, data):
- self.index = index
- self.callback = callback
- self.message = message
- self.data = data
- def ask(self):
- if self.message is not None:
- self.message.send(self.index)
- def receive(self, result, team_only):
- self.callback(self, result, team_only)
- def request_user_input(index, callback, message=None, data=None, top=False):
- """Request input from a user. The input needs to be entered in the chat.
- :param int index:
- The index of the player to ask for input.
- :param callback:
- A callable object that will be called as soon as the player has
- entered his input.
- :param UserMessageCreator message:
- If not None, this message will be used to send the player a message.
- :param object data:
- Any data you would like to access in the callback.
- :param bool top:
- If ``True``, this request has a high priority and will be put before
- any other request. Use this if you want to request a new input,
- because the entered input was invalid.
- :return:
- Return ``True`` if the request has been sent immediately, which means
- that no other request is pending.
- :rtype: bool
- """
- queue = request_queue[index]
- request = InputRequest(index, callback, message, data)
- if top:
- queue.appendleft(request)
- else:
- queue.append(request)
- if len(queue) == 1:
- request.ask()
- return True
- return False
- @SayFilter
- def on_say(command, index, team_only):
- if index not in request_queue:
- return CommandReturn.CONTINUE
- queue = request_queue[index]
- if not queue:
- return CommandReturn.CONTINUE
- request = queue.popleft()
- request.receive(command.command_string, team_only)
- if queue:
- queue[0].ask()
- return CommandReturn.BLOCK
- @OnClientDisconnect
- def on_client_disconnect(index):
- request_queue.pop(index)
- # =============================================================================
- # >> TEST
- # =============================================================================
- from commands.typed import TypedSayCommand
- from players.entity import Player
- from messages import SayText2
- m1 = SayText2('Enter something1')
- m2 = SayText2('Enter something2')
- @TypedSayCommand(['test'])
- def on_test(info):
- request_user_input(info.index, on_input_entered, m1)
- request_user_input(info.index, on_input_entered, m2)
- def on_input_entered(request, result, team_only):
- index = request.index
- if result.casefold() == 'invalid':
- print('Entered input was invalid.')
- request_user_input(index, on_input_entered, m1, top=True)
- else:
- player = Player(index)
- print('Player {} has entered "{}".'.format(player.name, result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement