diff --git a/Makhno/Controller.cs b/Makhno/Controller.cs index eb23aff..00e1fdb 100644 --- a/Makhno/Controller.cs +++ b/Makhno/Controller.cs @@ -196,13 +196,16 @@ namespace Makhno return OnError(); } + int maxSeasons = playerData.Voices.Max(v => v.Seasons?.Count ?? 0); + if (maxSeasons <= 0) + return OnError(); + if (season == -1) { - var firstVoice = playerData.Voices.First(); var season_tpl = new SeasonTpl(); - for (int i = 0; i < firstVoice.Seasons.Count; i++) + for (int i = 0; i < maxSeasons; i++) { - var seasonItem = firstVoice.Seasons[i]; + var seasonItem = playerData.Voices.Select(v => v.Seasons.ElementAtOrDefault(i)).FirstOrDefault(s => s != null); string seasonName = seasonItem.Title ?? $"Сезон {i + 1}"; int seasonNumber = i + 1; string link = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}"; @@ -213,7 +216,7 @@ namespace Makhno } int seasonIndex = season > 0 ? season - 1 : season; - if (seasonIndex < 0 || seasonIndex >= playerData.Voices.First().Seasons.Count) + if (seasonIndex < 0 || seasonIndex >= maxSeasons) return OnError(); var voice_tpl = new VoiceTpl(); @@ -229,7 +232,11 @@ namespace Makhno { var voice = playerData.Voices[i]; string voiceName = voice.Name ?? $"Озвучка {i + 1}"; - int seasonNumber = seasonIndex + 1; + int voiceSeasonIndex = GetSeasonIndexForVoice(voice, seasonIndex); + if (voiceSeasonIndex < 0) + continue; + + int seasonNumber = voiceSeasonIndex + 1; string voiceLink = $"{host}/makhno?imdb_id={imdb_id}&title={HttpUtility.UrlEncode(title)}&original_title={HttpUtility.UrlEncode(original_title)}&year={year}&serial=1&season={seasonNumber}&t={i}"; bool isActive = selectedVoice == i.ToString(); voice_tpl.Append(voiceName, isActive, voiceLink); @@ -238,9 +245,10 @@ namespace Makhno if (!string.IsNullOrEmpty(selectedVoice) && int.TryParse(selectedVoice, out int voiceIndex) && voiceIndex < playerData.Voices.Count) { var selectedVoiceData = playerData.Voices[voiceIndex]; - if (seasonIndex < selectedVoiceData.Seasons.Count) + int effectiveSeasonIndex = GetSeasonIndexForVoice(selectedVoiceData, seasonIndex); + if (effectiveSeasonIndex >= 0) { - var selectedSeason = selectedVoiceData.Seasons[seasonIndex]; + var selectedSeason = selectedVoiceData.Seasons[effectiveSeasonIndex]; var sortedEpisodes = selectedSeason.Episodes.OrderBy(e => ExtractEpisodeNumber(e.Title)).ToList(); for (int i = 0; i < sortedEpisodes.Count; i++) @@ -249,7 +257,7 @@ namespace Makhno if (!string.IsNullOrEmpty(episode.File)) { string streamUrl = BuildStreamUrl(init, episode.File); - int seasonNumber = seasonIndex + 1; + int seasonNumber = effectiveSeasonIndex + 1; episode_tpl.Append( episode.Title, title ?? original_title, @@ -278,6 +286,17 @@ namespace Makhno return match.Success ? int.Parse(match.Groups[1].Value) : 0; } + private int GetSeasonIndexForVoice(Voice voice, int requestedSeasonIndex) + { + if (voice?.Seasons == null || voice.Seasons.Count == 0) + return -1; + + if (requestedSeasonIndex >= 0 && requestedSeasonIndex < voice.Seasons.Count) + return requestedSeasonIndex; + + return 0; + } + private async Task ResolvePlaySource(string imdbId, string title, string originalTitle, int year, int serial, MakhnoInvoke invoke) { string playUrl = null;