コード例 #1
0
ファイル: api.py プロジェクト: drazvan/Connect-IG4
    def connect(self):
        """This command must be sent to the server whenever a player connects 
        for the first time. 
        
        If the player with the given nickname does not already exist then it is 
        created with the given password. If it already exists and the password 
        is incorrect FAIL is returned. If he's already online, he won't be connected
        to prevent messing up with the IP and Port.
        
        This is the only time we do not check if the player is online to act on the system.

        Parameters
            - nickname : is the name of the player and it must be unique.
            - password: is the password for the given player.
            - ip : is the local IP address of the player. It is a string like 
              192.168.10.104.
            - port : is the port on which the player waits for connections from 
            other players.
            
        Return value
            - OK if everything is ok and the player has been added.
            - FAIL for any other errors (i.e. bad password, bad IP, bad port, etc.)
        """  
        
        nickname = self.request.get("nickname")
        password = self.request.get("password")
        ip_address = self.request.get("ip")
        listen_port = self.request.get("port")
        
        # check if the player already exists
        player = self._get(Player, "nickname = ", nickname)
        
        # if it's a new player
        if player == None:
            player = Player(nickname=nickname, password=password)
        
        # It's a known player
        if password != player.password:
            self.response.out.write("FAIL Incorrect password")
            return 
        elif player.online == True:
            # It will be triggered if the player has left without saying 'disconnect'
            # 90 seconds later, the player will be able to connect himself if someone triggers a purge
            
            # THIS IS REALLY ANNOYING DURING DEVELOPEMENT
            
            # self.response.out.write("FAIL Already connected elsewhere")
            # return
            pass
        
        # set new properties, user authenticated  
        player.ip_address = ip_address
        player.listen_port = listen_port
        player.last_online = datetime.now()
        player.online = True
        
        # save
        player.put()    
        
        # respond with OK
        self.response.out.write("OK")