def _invite(self): """ Invite another player or players to join a session. """ this_player = self.user_id my_entry = users.get_user_entry(this_player) players = set(users.get_user_ids()) players.discard(this_player) if len(players) == 0: self.output = "No other players to invite at this time." self.update_display() return players = list(players) players.sort() user_entry = users.get_user_entry(this_player) if my_entry.joined_id is None: session_entry = session.create_session() user_entry.joined_id = session_entry.session_id session_entry.owner = this_player session_entry.members.add(this_player) self.create_session() dialog = ChoosePlayerDialog() self.dialog = dialog self.dialog.parent = self self.dialog.players = players
def _start_session(self): user_id = self.user_id my_entry = users.get_user_entry(user_id) session_id = my_entry.joined_id session_entry = session.get_entry(session_id) members = session_entry.members for member in members: entry = users.get_user_entry(member) entry.app_protocol.start_session()
def _cancel_session(self): user_id = self.user_id my_entry = users.get_user_entry(user_id) session_id = my_entry.joined_id session_entry = session.get_entry(session_id) members = session_entry.members session.destroy_entry(session_id) for member in members: entry = users.get_user_entry(member) entry.joined_id = None entry.invited_id = None entry.app_protocol.cancel()
def _leave_session(self): user_id = self.user_id my_entry = users.get_user_entry(user_id) session_id = my_entry.joined_id my_entry.joined_id = None session_entry = session.get_entry(session_id) session_entry.members.discard(user_id) self.cancel()
def _accept_invitation(self): user_id = self.user_id my_entry = users.get_user_entry(user_id) session_id = my_entry.invited_id session_entry = session.get_entry(session_id) my_entry.joined_id = session_id my_entry.invited_id = None session_entry.members.add(user_id) self.accept()
def _send_invite_to_player(self): parent = self.parent user_id = self.parent.user_id my_entry = users.get_user_entry(user_id) player = self.players[self.player_pos] other_entry = users.get_user_entry(player) if other_entry.invited_id is not None: parent.output = "'{}' has already been invited to a session.".format( player) self._cancel_dialog() return if other_entry.joined_id is not None: parent.output = "'{}' has already joined a session.".format(player) self._cancel_dialog() return other_entry.invited_id = my_entry.joined_id other_entry.app_protocol.receive_invitation() parent.output = "Sent invite to '{}'.".format(player) my_entry.app_protocol.send_invitation() self.parent.dialog = None self.parent = None
def handle_accepted(self): my_entry = users.get_user_entry(self.user_id) self.status = "Joined session '{}'".format(my_entry.joined_id) self.instructions = textwrap.dedent("""\ Valid commands are: * (j)oined - List players that have joined the session. * (c)ancel - Leave the session. """) self.valid_commands = { 'j': self._show_joined, 'c': self._leave_session, } self.update_display()
def handle_invited(self): user_entry = users.get_user_entry(self.user_id) self.status = "Invited to join session '{}'.".format( user_entry.invited_id) self.instructions = textwrap.dedent("""\ Valid commands are: * (a)ccept - Accept invitation to join session. * (r)eject - Reject invitation to join session. """) self.valid_commands = { 'a': self._accept_invitation, 'r': self._reject_invitation, } self.update_display()
def _show_joined(self): """ List the players that have joined the session. """ user_id = self.user_id my_entry = users.get_user_entry(user_id) session_id = my_entry.joined_id session_entry = session.get_entry(session_id) members = list(session_entry.members) members.sort() lines = [] for n, player in enumerate(members): lines.append("{}) {}".format(n + 1, player)) self.output = '\n'.join(lines) self.update_display()
def handle_waiting_for_accepts(self): user_id = self.user_id user_entry = users.get_user_entry(user_id) self.status = "Session {} - Waiting for Responses".format( user_entry.joined_id) self.instructions = textwrap.dedent("""\ Valid commands are: * (s)tart - Start the session with the current members. * (i)nvite - Invite another player. * (j)oined - Show players that have joined the session. * (c)ancel - Cancel the session. """) self.valid_commands = { 's': self._start_session, 'i': self._invite, 'j': self._show_joined, 'c': self._cancel_session, } self.update_display()
def _init_app_protocol(self): """ Initialize the application protocol. """ user_id = self.user_id entry = users.get_user_entry(user_id) need_init = False if entry.app_protocol is None: app_protocol = lobby.SSHLobbyProtocol() entry.app_protocol = app_protocol need_init = True app_protocol = entry.app_protocol app_protocol.reactor = self.reactor app_protocol.terminal = self.terminal app_protocol.user_id = self.user_id self.app_protocol = app_protocol app_protocol.parent = weakref.ref(self) if need_init: app_protocol.initialize() else: self.terminal.reset() app_protocol.update_display()
def _reject_invitation(self): user_id = self.user_id my_entry = users.get_user_entry(user_id) my_entry.invited_id = None self.reject()