# Path: modules/game/AnalysedPosition.py
# class AnalysedPosition(NamedTuple('AnalysedPosition', [
#         ('id', AnalysedPositionID),
#         ('analyses', List[Analysis])
#     ])):
#     """
#     Like an analysed move, but only with SF analysis. Does not contain any other move data.
#     This is used for accelerating stockfish analysis.
#     """
#     @staticmethod
#     def fromBoardAndAnalyses(board: Board, analyses: List[Analysis]):
#         return AnalysedPosition(
#             id=AnalysedPosition.idFromBoard(board),
#             analyses=analyses)
# 
#     @staticmethod
#     def idFromBoard(board: Board) -> AnalysedPositionID:
#         return str(polyglot.zobrist_hash(board))


from chess.pgn import read_game
from modules.game.AnalysedPosition import AnalysedPosition
                from StringIO import StringIO
                from io import StringIO
import logging

def buildAnalysedPositionTable(env):
    logging.info("buildAnalysedPositionColl")
    logging.info("Getting AnalysedGames")
    batch = 908
    while True:
        logging.info("Processing Batch: " + str(batch))
        analysedGames = env.analysedGameDB.allBatch(batch)
        batch += 1
        if len(analysedGames) == 0:
            logging.info("reached end of analysedGameDB")
            return
        analysedGamesLength = str(len(analysedGames))
        for i, analysedGame in enumerate(analysedGames):
            game = env.gameDB.byId(analysedGame.gameId)
            white = analysedGame.userId == game.white # is the player black or white
            try:
            except ImportError:

            try:
                playableGame = read_game(StringIO(" ".join(game.pgn)))
            except ValueError:
                continue

            node = playableGame

            index = 0
            analysedPositions = []

# Path: modules/game/AnalysedPosition.py
# class AnalysedPosition(NamedTuple('AnalysedPosition', [
#         ('id', AnalysedPositionID),
#         ('analyses', List[Analysis])
#     ])):
#     """
#     Like an analysed move, but only with SF analysis. Does not contain any other move data.
#     This is used for accelerating stockfish analysis.
#     """
#     @staticmethod
#     def fromBoardAndAnalyses(board: Board, analyses: List[Analysis]):
#         return AnalysedPosition(
#             id=AnalysedPosition.idFromBoard(board),
#             analyses=analyses)
# 
#     @staticmethod
#     def idFromBoard(board: Board) -> AnalysedPositionID:
#         return str(polyglot.zobrist_hash(board))


# Path: utils/buildAnalysedPositionTable.py
from chess.pgn import read_game
from modules.game.AnalysedPosition import AnalysedPosition
                from StringIO import StringIO
                from io import StringIO
import logging


def buildAnalysedPositionTable(env):
    logging.info("buildAnalysedPositionColl")
    logging.info("Getting AnalysedGames")
    batch = 908
    while True:
        logging.info("Processing Batch: " + str(batch))
        analysedGames = env.analysedGameDB.allBatch(batch)
        batch += 1
        if len(analysedGames) == 0:
            logging.info("reached end of analysedGameDB")
            return
        analysedGamesLength = str(len(analysedGames))
        for i, analysedGame in enumerate(analysedGames):
            game = env.gameDB.byId(analysedGame.gameId)
            white = analysedGame.userId == game.white # is the player black or white
            try:
            except ImportError:

            try:
                playableGame = read_game(StringIO(" ".join(game.pgn)))
            except ValueError:
                continue

            node = playableGame

            index = 0
            analysedPositions = []

            logging.info("walking through game - " + game.id + " - " + str(i) + "/" + analysedGamesLength)