Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    def _send_ship(self):
        '''
        Send the ship grid and ship status
        '''
        # ship grid
        arr = DataBase.get_ship(self.username)
        self.send(Message('sh', arr), pickling=True)

        # ship status
        self.send(Message('shst', DataBase.get_ship_status(self.username)),
                  pickling=True)
Пример #4
0
 def send_game_init_info(self, max_shield_hp):
     '''
     Send some info on the player's state after the
     initalisation.
     ID: gis
     '''
     self.send(Message('gis', max_shield_hp), pickling=True)
Пример #5
0
    def sign_up(self, content):
        '''
        Create a new account.  
        Content: username, password
        '''
        username, password = content

        # try to add the new user
        if DataBase.add_user(username, password):
            # sign up
            self.send(Message('rsg', True), pickling=True)

            self._log_client(username)
        else:
            # can't sign up
            self.send(Message('rsg', False), pickling=True)
Пример #6
0
 def send_private_chat_msg(cls, sender, target, msg):
     '''
     Send a message to the user if connected,  
     else abort msg.
     '''
     if cls.is_user(target):
         cls.clients[target].send(Message('pc', [sender, msg]),
                                  pickling=True)
Пример #7
0
    def set_script_status(self, content):
        '''
        Set the status of the user script.
        '''
        DataBase.set_script_status(self.username, content)

        # send status back to user
        self.send(Message('scst', content), pickling=True)
Пример #8
0
    def ship_config(self, arr):
        '''
        Store the new ship config of the client,  
        Store the ship status, send it to the client
        '''
        DataBase.set_ship(self.username, arr)
        DataBase.set_ship_status(self.username, 1)

        self.send(Message('shst', 1), pickling=True)
Пример #9
0
    def send_connection_state(cls, username, state):
        '''
        Send to every friend of user his state: if he's connected.
        '''
        # get friends
        friends = DataBase.get_friends(username)

        for friend in friends:
            if cls.is_user(friend):
                cls.send(friend, Message('frs', [[username, state]]))
Пример #10
0
    def send_enter_game(self, opp_client, team):
        '''
        Send to client that he's entering in a game.  
        team specify the starting position of the ship.
        '''
        # send script
        script = DataBase.get_script(self.username)
        self._send_script(script)
        self.send(Message('sc', None), pickling=True)

        # send opp ship grid
        arr = DataBase.get_ship(opp_client.username)
        self.send(Message('igsh', arr), pickling=True)

        # send own ship grid
        arr = DataBase.get_ship(self.username)
        self.send(Message('sh', arr), pickling=True)

        # notify in game | opponent username, the position id of the ship
        self.send(Message('ign', [opp_client.username, team]), pickling=True)
Пример #11
0
    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)
Пример #12
0
    def send_general_chat_msg(cls, username, msg):
        '''
        Send a message to all connected clients on the general chat.
        '''

        for client in cls.clients.values():

            if client.username == username:
                continue

            # send message
            client.send(Message('gc', [username, msg]), pickling=True)
Пример #13
0
    def send_script(self, script, analysis=False):
        '''
        Send the script stored on the local file to the server.  
        If analysis=True, send script for analysis -> will receive a response
        ID: sc/sca
        '''
        if analysis:
            identifier = 'sca'
        else:
            identifier = 'sc'

        self._send_script(script)
        self.send(Message(identifier, None), pickling=True)
Пример #14
0
    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)
Пример #15
0
    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)
Пример #16
0
    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)
Пример #17
0
    def script_analysis(self, status):
        '''
        Analyse the script, look for cheating attempts.  
        Send response to user.
        '''
        script = '\n'.join(self._script_cache)
        self._script_cache = []

        fine = True

        script = script.replace('  ', ' ')

        maliscious_lines = [
            'from API import', 'API._ships', 'API as', 'import game',
            'from game.ship', 'from game.game'
        ]

        for line in maliscious_lines:
            if script.find(line) != -1:
                fine = False
                break

        self.send(Message('rsca', fine), pickling=True)
Пример #18
0
    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
Пример #19
0
    def profil_demand(self, username):
        '''
        Get the stats of a user,  
        send stats to user
        '''
        wins = DataBase.get_wins(username)
        loss = DataBase.get_loss(username)
        ship = DataBase.get_ship(username)
        friends = DataBase.get_friends(username)
        grid = DataBase.get_ship(username)

        if self.username in friends:
            friends.remove(self.username)

        msg = Message(
            'rpd', {
                'username': username,
                'wins': wins,
                'loss': loss,
                'friends': friends,
                'grid': grid
            })

        self.send(msg, pickling=True)
Пример #20
0
 def send_script_status(self, status):
     '''
     Send the status of the script
     ID: scst
     '''
     self.send(Message('scst', status), pickling=True)
Пример #21
0
 def send_wait_game_status(self, status):
     '''
     Send if the user is waiting to enter a game
     ID: wg
     '''
     self.send(Message('wg', status), pickling=True)
Пример #22
0
 def send_end_game(self, has_win):
     '''
     Send that the game is finished, send if user has win the game.
     ID: egst
     '''
     self.send(Message('egst', has_win), pickling=True)
Пример #23
0
 def send_ship_config(self, arr):
     '''
     Send the new ship of client.
     ID: shcf
     '''
     self.send(Message('shcf', arr), pickling=True)
Пример #24
0
 def send_response_game_demand(self, username, response):
     '''
     Send if accepted or not a game demand.
     ID: rdg
     '''
     self.send(Message('rdg', [username, response]), pickling=True)
Пример #25
0
 def send_demand_game(self, username):
     '''
     Send a game demand.
     ID: dg
     '''
     self.send(Message('dg', username), pickling=True)
Пример #26
0
 def send_response_dfr(self, username, response):
     '''
     Send if accepted or not a friend demand.
     ID: rdfr
     '''
     self.send(Message('rdfr', [username, response]), pickling=True)
Пример #27
0
 def send_profil_demand(self, username):
     '''
     Send a demand for the server to send info about the user
     ID: pd
     '''
     self.send(Message('pd', username), pickling=True)
Пример #28
0
 def send_private_chat_msg(self, username, message):
     '''
     Send a message on the general chat.
     ID : pc
     '''
     self.send(Message('pc', [username, message]), pickling=True)
Пример #29
0
 def send_in_game_error(self, n_error):
     '''
     Send to the other user the number of error that occured in the script.
     ID: ige
     '''
     self.send(Message('ige', n_error), pickling=True)
Пример #30
0
 def send_demand_friend(self, username):
     '''
     Send a friend demand.
     ID: dfr
     '''
     self.send(Message('dfr', username), pickling=True)