Esempio n. 1
0
    def load_db_menu(self, context):
        message = ''.join([
            self.yellow,
            'Please, enter game name or type \'back\' to go back: ',
            self.color_end
        ])
        players = self.get_players(PLAYERS_DEFAULT)     # TODO
        with closing(make_db_session(DB_PATH)) as db_session:
            while True:
                user_input = self.prompt(message)
                if not user_input:
                    continue
                elif user_input.lower() == 'back':
                    return False

                query = db_session.query(Game)
                game = query.filter_by(description=user_input).first()
                if not game:
                    print self.messages['game_not_found']
                    continue
                placed_walls = []
                if game.start_state.placed_walls:
                    placed_walls = game.start_state.placed_walls.split(',')
                state = (
                    game.start_state.on_move,
                    game.start_state.yellow_position,
                    game.start_state.green_position,
                    game.start_state.yellow_walls,
                    game.start_state.green_walls,
                    frozenset([int(wall) for wall in placed_walls])
                )
                context.reset(state=state, players=players)
                for move in game.moves:
                    context.update(move.action, checks_on=False)
                return True
Esempio n. 2
0
    def save_db_menu(self, context):
        message = ''.join([
            self.yellow,
            'Please, enter game name or type \'back\' to go back: ',
            self.color_end
        ])
        user_input = self.prompt(message)
        if user_input.lower() == 'back':
            return False

        with closing(make_db_session(DB_PATH)) as db_session:
            db_save_game(
                db_session,
                start_state=context['start_state'],
                yellow=context.yellow['name'],
                green=context.green['name'],
                winner=context.winner,
                actions=context.history,
                description=user_input,
            )
            db_session.commit()
            return True