def _send_basic_infos(self): ''' Send the basic informations, friends, ... ''' self._send_connected_friends() self._send_ship() # script script = DataBase.get_script(self.username) self._send_script(script) self.send(Message('sc', None), pickling=True) # script status script_status = DataBase.get_script_status(self.username) self.send(Message('scst', script_status), pickling=True) # friend demands dfrs = DataBase.get_friend_demands(self.username) for sender in dfrs: if sender == '': continue Interaction.send_demand_friend(self.username, sender)
def demand_friend(self, content): ''' Manage the friend demand Content: target username ''' is_error = False # check target is not user if content == self.username: is_error = True # check requested user exist if not DataBase.is_user(content): is_error = True # check not already friends if content in DataBase.get_friends(self.username): is_error = True if is_error: # send error in friend demand self.send(Message('rdfr', False), pickling=True) return DataBase.add_friend_demand(content, self.username) # check if requested user is connected if Interaction.is_user(content): Interaction.send_demand_friend(content, self.username) # send friend demand is ok self.send(Message('rdfr', True), pickling=True)
def private_chat(self, content): ''' Send a message to the other user Content: username, msg ''' target, msg = content Interaction.send_private_chat_msg(self.username, target, msg)
def response_demand_game(self, content): ''' Manage the response of a game demand. Content: username, response ''' username, response = content if response: # start a new game Interaction.create_game(self.username, username)
def end_game(self, has_win): ''' Set the user to be out (game finished) of his game in Interaction ''' if has_win: result = 'win' else: result = 'loss' Interaction.set_game_result(self.game_tag, self.username, result) # reset game values self.game_tag = None self.opponent = None
def _log_client(self, username): ''' Internal method. Set attributes to log in. ''' self.logged = True self.username = username Interaction.clients[username] = self Interaction.send_connection_state(self.username, True) self._send_basic_infos() self.print("Logged.")
def _send_connected_friends(self): '''Send the status of each friend of client''' friends = [] # get friends username_friends = DataBase.get_friends(self.username) for username in username_friends: friends.append([username, Interaction.is_user(username)]) self.send(Message('frs', friends), pickling=True)
def response_demand_friend(self, content): ''' Manage the response of a friend demand. Content: username, response ''' username, response = content is_connected = False # if sender is connected DataBase.remove_friend_demand(self.username, username) if response: DataBase.set_as_friends(self.username, username) # send to new friend that we are connected if Interaction.is_user(username): is_connected = True Interaction.send(username, Message('frs', [[self.username, True]])) # send to client if new friend is connected self.send(Message('frs', [[username, is_connected]]), pickling=True)
def demand_game(self, content): ''' Manage the game demand Content: target username ''' is_error = False # check target is not user if content == self.username: is_error = True # check if requested user is connected if not Interaction.is_user(content): is_error = True if is_error: # send error in game demand self.send(Message('rdg', False), pickling=True) return Interaction.send_demand_game(content, self.username) # send game demand is ok self.send(Message('rdg', True), pickling=True)
def login(self, content): ''' Log the user in. Content: username, password ''' username, password = content # check that the username exists and that the password is correct if DataBase.is_user(username): if Interaction.is_user(username): # can't log in -> already connected self.send(Message('rlg', False), pickling=True) return if DataBase.get_password(username) == password: # log in self.send(Message('rlg', True), pickling=True) self._log_client(username) return # can't log in self.send(Message('rlg', False), pickling=True)
def on_disconnect(self, content=None): ''' Executed on disconnection, to have a clean disconnection ''' self.logged = False self.print("Disconnected.") if self.username is None: return # inform friends Interaction.send_connection_state(self.username, False) # remove from Interaction Interaction.remove(self.username) # if in game -> inform opponent that is leaving if not self.game_tag is None: Interaction.send(self.opponent, Message('olg', True)) # count game as a loss DataBase.increment_loss(self.username) self.username = None
def connect_udp(self, content): ''' Set udp client in udp socket ''' address = (self.ip, content) Interaction.connect_udp(address)
def set_waiting_game_state(self, content): ''' Set if the user is waiting to enter a game. ''' Interaction.set_user_waiting_game(self.username, content)
def in_game_error(self, content): ''' Send to the other user the number of errors that occured in the script. ''' Interaction.send(self.opponent, Message('ige', content))
def on_game_init_state(self, content): ''' Send some info to the other user of the game ''' Interaction.send(self.opponent, Message('gis', content))
def general_chat(self, content): ''' Send a message on the general chat. Content: msg ''' Interaction.send_general_chat_msg(self.username, content)