コード例 #1
0
ファイル: api.py プロジェクト: drazvan/Connect-IG4
    def create(self):
        """Indicates that the given player is willing to start a game and is 
        waiting for an opponent.
        
        Only online players can create, hence the True in the authenticate
        
        Parameters
            - nickname : is the name of the player.
            - password : is the password for the given player.
            
        Return value
            - OK followed by a game id.
            - FAIL for any errors.
        """
        
        nickname = self.request.get("nickname")
        password = self.request.get("password")

        # authenticate        
        player = self._authenticate(nickname, password)
        if player == None:
            self.response.out.write("FAIL Authentication failed")
            return
        
        # check if he's already playing or waiting
        games = Game.all().filter("creator =", player).filter("status =", 'WAITING')
        if games.count() > 0:
            self.response.out.write("FAIL Already created a game")
            return
        
        games = Game.all().filter("creator =", player).filter("status =", 'PLAYING')
        if games.count() > 0:
            self.response.out.write("FAIL Already playing")
            return

        
        # create a new game
        config = self._config()
        game = Game(id = config.current_game_id)
        
        # save new config
        config.current_game_id = config.current_game_id + 1
        config.put();
        
        game.creator = player
        game.status = 'WAITING'
        
        # save game
        game.put()
        
        # increment the number of games created by the player
        if player.created:
            player.created = player.created + 1
        else: 
            player.created = 1
            
        player.put()
        
        # respond with OK + game id
        self.response.out.write("OK\n%s" % game.id )
コード例 #2
0
ファイル: connectig4.py プロジェクト: drazvan/Connect-IG4
 def get(self):
     
     
     self.response.out.write(template.render(template_path + 'games.html', 
             { 'Games' : 'active',
              'games_waiting' : Game.all().filter("status =", "WAITING"), 
              'games_playing' : Game.all().filter("status =", "PLAYING"),
              'games_finished' : Game.all().filter("status =", "FINISHED"),
                    }))
コード例 #3
0
ファイル: api.py プロジェクト: drazvan/Connect-IG4
    def _purge(self):
        """ Purge all dead players (last_online > 30 secs) 
        
        The ping can be efficiently made every 30s in C. 
        """

        # Get players who haven't pinged in more than 30secs and who are currently marked as online
        players = Player.all().filter("last_online < ", datetime.now() - timedelta(seconds=30)).filter("online = ", True)
        for player in players:
            player.online = False # Not online anymore
            player.put() # save
        
            # Get his former game
            player_game = Game.all().filter("creator =", player).filter("status = ", 'PLAYING')
            
            for game in player_game:
                game.status = 'ABANDONED' # game OVER
                game.put() # save
                
                # increment the abandoned counter
                if player.abandoned:
                    player.abandoned = player.abandoned + 1
                else:
                    player.abandoned = 1
                    
                player.put()
                
            # Get his former game
            player_game = Game.all().filter("creator =", player).filter("status = ", 'WAITING')
            
            for game in player_game:
                game.status = 'ABANDONED' # game OVER
                game.put() # save
                
                # increment the abandoned counter
                if player.abandoned:
                    player.abandoned = player.abandoned + 1
                else:
                    player.abandoned = 1
                    
                player.put()
コード例 #4
0
ファイル: team.py プロジェクト: podgib/dedicationsupercoach
 def copy_to_next_round(self):
     game = Game.all().filter("round =", self.game.round + 1).get()
     t = Team(
         user=self.user,
         batsmen=self.batsmen,
         bowlers=self.bowlers,
         fielders=self.fielders,
         game=game,
         captain=self.captain,
         captain_type=self.captain_type,
     )
     t.put()
     self.user.round_trades = min(self.user.total_trades, 2)
コード例 #5
0
ファイル: connectig4.py プロジェクト: drazvan/Connect-IG4
    def get(self):
        
        game_id = self.request.get("game")
        board = "------------------------------------------"
        
        games = Game.all().filter("id = ", int(game_id))
        if games.count() > 0:
            game = games[0]
            board = game.board

            # ignore invalid boards            
            if board == None or len(board) < 42:
                board = "------------------------------------------"
                
        self.response.out.write(board)
コード例 #6
0
ファイル: api.py プロジェクト: drazvan/Connect-IG4
    def list(self):
        """Gives the list of all online players that have created a game and 
        are waiting for an opponent. It takes no parameters.

        Return value
            - OK followed by the list of players.
            - FAIL for any errors.
        """
        
        # Purge dead players from the list to prevent listing dead clients
        self._purge()
        
        self.response.out.write("OK\n")
        
        for game in Game.all().filter("status = ", "WAITING"):
            self.response.out.write("%s\n" % game.creator.nickname) 
コード例 #7
0
ファイル: connectig4.py プロジェクト: drazvan/Connect-IG4
    def get(self):
        
        game_id = self.request.get("game")
        board = "------------------------------------------"
        
        games = Game.all().filter("id = ", int(game_id))
        if games.count() > 0:
            game = games[0]
            board = game.board

            # ignore invalid boards            
            if board == None or len(board) < 42:
                board = "------------------------------------------"
                
            self.response.out.write(template.render(template_path + 'grid.html', 
                                                { 'Games' : 'active', 
                                                  'board' : board,
                                                  'game' : game 
                                                       }))
コード例 #8
0
ファイル: api.py プロジェクト: drazvan/Connect-IG4
 def disconnect(self):
     """
         This command needs to be called before leaving the client, it marks the player as offline.
         If the user doesn't disconnect, he'll need to wait 90 seconds to be purged automatically
         We can disconnect only online players, hence the True in the authenticate
         
         Parameters
             - nickname : is the name of the player.
             - password : is the password for the given player.
         
         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")
     
     # authenticate        
     player = self._authenticate(nickname, password)
     if player == None:
         self.response.out.write("FAIL Authentication failed")
         return
     
     # Mark as offline
     player.online = False
     
     # Save
     player.put()
     
     # Select his former game
     player_game = Game.all().filter("creator =", player.nickname).filter("status = ", 'PLAYING')
     
     for game in player_game:
         game.status = 'ABANDONNED' # game OVER
         game.put() # Save
      
     # respond with OK
     self.response.out.write("OK")