Count Token minted in a range

Hello Everybody,

By advance sorry for asking and thanks for your help, I find/try to resolve my problem from a few days.

I try to add on my smart contract a fonction to count the number of mint on a specific range (to limit the mint possible on a free mint period from a uniq wallet).

by exemple, i open a free mint periode from token n°100 to n°150, i need to limit the mint on a uniq wallet at 5 (by exemple).

I try to start this from tokensByOwner fonction, maybe is not the good solution, if you have an idea. thanks a lot

function tokensByOwner(address _owner) external view returns(uint256[] memory ) {
        uint256 tokenCount = balanceOf(_owner);
        if (tokenCount == 0) {
            return new uint256[](0);
        } else {
            uint256[] memory result = new uint256[](tokenCount);
            uint256 index;
            for (index = 0; index < tokenCount; index++) {
                result[index] = tokenOfOwnerByIndex(_owner, index);
            }
            return result;
        }
    }

I think i need to add an if in a for loop but maybe its wrong

if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT && index <= RANGE_MAX_TOKEN_FOR_FREE_MINT) {

I have try to out of the loops with this code but my test are not ok ( result after max range is 0 0 0 .....)

                if (index >= RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;

Hi,

I have again try with differents test/parameter, is not again good.

     function atokensByOwnerAAAA(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index < RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
                if (index > RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;
                }
            } 
        return count;
        }

   function atokensByOwnerBBBB(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index < RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                /*if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {*/
                    count++;
                /*}*/
                if (index > RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                // Exit loop with break
                break;
                }
            } 
        return count;
        }

    function atokensByOwnerCCCC(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 count;
            for (index = 0; index < tokenCount && index <= RANGE_MAX_TOKEN_FOR_FREE_MINT; index++) {
                if (index >= RANGE_MIN_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
            } 
        return count;
        }

do you have an idea?

solution by myself (tested ok)

    function tokensByOwnerForFreeMint(address _owner) external view returns(uint256) {
        uint256 tokenCount = balanceOf(_owner);
            uint256 index;
            uint256 indextwo;
            uint256 count;
            for (index = 0; index < tokenCount; index++) {
                indextwo = tokenOfOwnerByIndex(_owner, index);
                if (indextwo >= RANGE_MIN_TOKEN_FOR_FREE_MINT && indextwo <= RANGE_MAX_TOKEN_FOR_FREE_MINT) {
                    count++;
                }
            }
            return count;
        }