ФЭНДОМ


local seriesApi = {}
 
seriesApi._episodesTable = mw.loadData( 'Модуль:Серии/Таблица' )
seriesApi._library = require( 'Модуль:Библиотека' )
 
function seriesApi:_getEpisodeByName( name, searchParametres )
    if type( searchParametres ) == 'string' then
        local parameter = searchParametres
        searchParametres = {}
        searchParametres[ parameter ] = true
    end
    local episodesCounter = 1
    for i, season in ipairs( self._episodesTable ) do
        for j, episode in ipairs( season ) do
            if ( not searchParametres or searchParametres.ruName ) and name == episode.ruName then
                return { episode,  i, j, episodesCounter }
            end
            if ( not searchParametres or searchParametres.enName ) and name == episode.enName then
                return { episode,  i, j, episodesCounter }
            end
            if ( not searchParametres or searchParametres.pageName ) and name == episode.pageName then
                return { episode,  i, j, episodesCounter }
            elseif searchParametres and searchParametres.pageName and not episode.pageName then
                if name == episode.ruName then
                    return { episode,  i, j, episodesCounter }
                elseif not episode.ruName and name == episode.enName then
                    return { episode,  i, j, episodesCounter }
                end
            end
            episodesCounter = episodesCounter + 1
        end
    end
end
 
function seriesApi:_getEpisodeByNumbers( seasonNumber, episodeNumber )
    if not self._episodesTable[ seasonNumber ] then
        return
    end
    if not self._episodesTable[ seasonNumber ][ episodeNumber ] then
        return
    end
    local totalEpisodeNumber = 0
    for i = 1, seasonNumber - 1 do
        totalEpisodeNumber = totalEpisodeNumber + self._episodesTable[ i ].length
    end
    totalEpisodeNumber = totalEpisodeNumber + episodeNumber
    return { self._episodesTable[ seasonNumber ][ episodeNumber ], seasonNumber, episodeNumber, totalEpisodeNumber }
end
 
function seriesApi:_getEpisodeByTotalNumber( totalEpisodeNumber )
    if totalEpisodeNumber <= 0 then
        return
    end
    local episodeNumber = totalEpisodeNumber
    for i, season in ipairs( self._episodesTable ) do
        if episodeNumber <= season.length then
            return { season[ episodeNumber ], i, episodeNumber, totalEpisodeNumber }
        else
            episodeNumber = episodeNumber - season.length
        end
    end
end
 
function seriesApi:getEpisode( parameter1, parameter2 )
    local selectedEpisode
    local episodeTable = {}
    if not parameter1 then
        return
    end
    if type( parameter1 ) == 'string' then
        selectedEpisode = self:_getEpisodeByName( parameter1, parameter2 )
    elseif type( parameter1 ) == 'number' then
        if parameter2 then
            selectedEpisode = self:_getEpisodeByNumbers( parameter1, parameter2 )
        else
            selectedEpisode = self:_getEpisodeByTotalNumber( parameter1 )
        end
    end
    if not selectedEpisode then
        return
    end
    if selectedEpisode[1].ruName then
        episodeTable.name = selectedEpisode[1].ruName
        episodeTable.ruName = selectedEpisode[1].ruName
    else
        episodeTable.name = selectedEpisode[1].enName
    end
    episodeTable.enName = selectedEpisode[1].enName
    if selectedEpisode[1].pageName then
        episodeTable.pageName = selectedEpisode[1].pageName
    else
        episodeTable.pageName = episodeTable.name
    end
    episodeTable.seasonNumber = selectedEpisode[ 2 ]
    episodeTable.episodeNumber = selectedEpisode[ 3 ]
    episodeTable.totalEpisodeNumber = selectedEpisode[ 4 ]
    return episodeTable
end
 
function seriesApi:forEach( func, seasons )
    if type( seasons ) == 'number' then
        seasons = { [1]=seasons }
    end
    local iterationsCounter = 1
    for i, season in ipairs( self._episodesTable ) do
        if not seasons or self._library.table.indexOf( seasons, i ) then
            for j, episode in ipairs( season ) do
                local episode = self:getEpisode( i, j )
                func( episode, iterationsCounter )
                iterationsCounter = iterationsCounter + 1
            end
        end
    end
end
 
function seriesApi:getLength( season )
    if not season then
        return self._episodesTable.length
    elseif self._episodesTable[ season ] then
        return self._episodesTable[ season ].length
    end
end
 
return seriesApi
Материалы сообщества доступны в соответствии с условиями лицензии CC-BY-SA , если не указано иное.