diff --git a/Config.cs b/Config.cs index 6f631b2..dda220d 100644 --- a/Config.cs +++ b/Config.cs @@ -14,6 +14,8 @@ namespace director public string calendar_twitch { get; set; } public string calendar_other { get; set; } public TimeSpan preshowBufferTime { get; set; } + public string show_template_yt_release { get; set; } + public string show_template_twitch_streams { get; set; } public Dictionary phase_handlers { get; set; } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index fe8fc2f..806f2bf 100644 --- a/Program.cs +++ b/Program.cs @@ -93,13 +93,20 @@ namespace director } } Console.WriteLine("calendars checked"); - #if(DEBUG) - var psuedo = new Schedulable.Schedulable(); - psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30); - psuedo.ScedulableType = Schedulable.ScedulableType.YTRelease; - workQueue.Enqueue(psuedo); - _signal.Set(); - #endif +#if (DEBUG) + for (int i = 0; i < 3; i++) + { + var psuedo = new Schedulable.Schedulable(); + psuedo.Showtime = DateTime.Now + TimeSpan.FromSeconds(30); + var partiallyCopiable = scratch.agenda?.FirstOrDefault(s => s.ScedulableType == Schedulable.ScedulableType.YTRelease); + psuedo.Occurrence = partiallyCopiable.Occurrence; + psuedo.Occurrence.OccurrenceStart = psuedo.Showtime; + psuedo.Occurrence.OccurrenceEnd = psuedo.Showtime; + psuedo.ScedulableType = Schedulable.ScedulableType.YTRelease; + workQueue.Enqueue(psuedo); + _signal.Set(); + } +#endif } private delegate void schedulableCreate(ref Schedulable.Schedulable creating); @@ -133,10 +140,10 @@ namespace director _signal.WaitOne(calendarNaptime); if (!workQueue.TryDequeue(out todo)) { continue; } - + var napLength = (todo.Showtime - conf.preshowBufferTime) - DateTime.Now; Console.WriteLine($"threadwork consumes! showtime at {todo.Showtime}; napping until {todo.Showtime - conf.preshowBufferTime} ({napLength})"); - switch(todo.ScedulableType) + switch (todo.ScedulableType) { case Schedulable.ScedulableType.TwitchStream: Console.WriteLine("it's a twitch stream"); @@ -145,37 +152,30 @@ namespace director Console.WriteLine("it's a yt release"); break; } - if(napLength.TotalMinutes > 0) + if (napLength.TotalMinutes > 0) { Task.WaitAll(Task.Delay(napLength)); } - - switch (todo.ScedulableType) + + try { - case Schedulable.ScedulableType.TwitchStream: - try - { - TwitchStreamHandler.Handle(todo.Occurrence); - } - catch(Exception e) - { - Log.Panic($"error in twitch stream handler! Panicking!\n{JsonConvert.SerializeObject(e)}"); - } - break; - case Schedulable.ScedulableType.YTRelease: - try - { - YoutubeHandler.Handle(todo.Occurrence); - } - catch(Exception e) - { - Log.Panic($"error in youtube release handler! Panicking!\n{JsonConvert.SerializeObject(e)}"); - } - break; - default: - Log.Panic($"unknown schedulable type! abandoning!"); - break; + switch (todo.ScedulableType) + { + case Schedulable.ScedulableType.TwitchStream: + new TwitchStreamHandler().Handle(todo.Occurrence); + break; + case Schedulable.ScedulableType.YTRelease: + new YoutubeHandler().Handle(todo.Occurrence); + break; + default: + throw new NotImplementedException($"unknown schedulable type!\n{JsonConvert.SerializeObject(todo)}"); + } } + catch (Exception e) + { + Log.Panic($"error in show handler! Panicking!\n{JsonConvert.SerializeObject(e)}"); + } + break; } } } diff --git a/appsettings.example.json b/appsettings.example.json index 736a1ac..4edacd4 100644 --- a/appsettings.example.json +++ b/appsettings.example.json @@ -7,5 +7,7 @@ "calendar_youtube": "calendars/1wingedangle/youtube-channel_shared_by_adam", "calendar_twitch": "calendars/1wingedangle/twitch-channel_shared_by_adam", "calendar_other": "calendars/1wingedangle/other_shared_by_adam", - "preshowBufferTime": "04:00:00" + "preshowBufferTime": "04:00:00", + "show_template_yt_release": "./yt release/config.json", + "show_template_twitch_streams": "./twitch stream configs/" } \ No newline at end of file diff --git a/director.csproj b/director.csproj index 7a9fb58..1414a4e 100644 --- a/director.csproj +++ b/director.csproj @@ -7,7 +7,7 @@ - + diff --git a/schedulable/Show.cs b/schedulable/Show.cs new file mode 100644 index 0000000..ae83493 --- /dev/null +++ b/schedulable/Show.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +namespace Schedulable.Show +{ + public class Show + { + public PreshowTaskList PreShow { get; set; } + public TaskList PostShow { get; set; } + + #region components + public class TaskList + { + public IEnumerable Manual { get; set; } + public IEnumerable Commands { get; set; } + } + public class PreshowTaskList : TaskList + { + public IEnumerable AgentsNeeded { get; set; } + public IEnumerable Checks { get; set; } + } + public class Checklistable + { + //for humans + public string Label { get; set; } + public IEnumerable Items { get; set; } //if no items, just throw up a checkbox + } + #endregion + } + + public class TwitchStream : Show + { + public string TwitchTitle { get; set; } + public string TwitchCategory { get; set; } + } + public class YoutubeRelease : Show { } +} \ No newline at end of file diff --git a/schedulable/show/Show.cs b/schedulable/show/Show.cs deleted file mode 100644 index 2c4b504..0000000 --- a/schedulable/show/Show.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Collections.Generic; - -namespace Schedulable.Show -{ - public class Show - { - public string Name { get; set; } - public Preparation Preperation { get; set; } - public IEnumerable Procedure { get; set; } - public TaskList Postshow { get; set; } - } - public class Phase - { - public string Name { get; set; } - } - public class TaskList - { - public IEnumerable Manual { get; set; } - public IEnumerable Commands { get; set; } - } - public class Preparation : TaskList - { - public IEnumerable AgentsNeeded { get; set; } - public IEnumerable Checks { get; set; } - } - public class Checklistable - { - //for humans - public string Label { get; set; } - public IEnumerable Items { get; set; } //if no items, just throw up a checkbox - } -} \ No newline at end of file diff --git a/schedulable/show/TwitchStream.cs b/schedulable/show/TwitchStream.cs deleted file mode 100644 index 053f76f..0000000 --- a/schedulable/show/TwitchStream.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Schedulable.Show -{ - public class TwitchStream : Show - { - public string TwitchCategory { get; set; } - } -} \ No newline at end of file diff --git a/schedulable/show/YoutubeRelease.cs b/schedulable/show/YoutubeRelease.cs deleted file mode 100644 index 260ea3f..0000000 --- a/schedulable/show/YoutubeRelease.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Schedulable.Show -{ - public class YoutubeRelease : Show - { - //do I even need anything? - } -} \ No newline at end of file diff --git a/showHandlers/ShowHandler.cs b/showHandlers/ShowHandler.cs new file mode 100644 index 0000000..39b64c6 --- /dev/null +++ b/showHandlers/ShowHandler.cs @@ -0,0 +1,10 @@ +using System; +using director; + +namespace ShowHandlers +{ + public abstract class ShowHandler + { + public abstract void Handle(iCalHoopJumping.CalendarOccurrence evt); + } +} \ No newline at end of file diff --git a/showHandlers/TwitchStreamHandler.cs b/showHandlers/TwitchStreamHandler.cs index d8271e4..7b8761c 100644 --- a/showHandlers/TwitchStreamHandler.cs +++ b/showHandlers/TwitchStreamHandler.cs @@ -3,9 +3,9 @@ using director; namespace ShowHandlers { - public class TwitchStreamHandler + public class TwitchStreamHandler : ShowHandler { - public static void Handle(iCalHoopJumping.CalendarOccurrence evt) + public override void Handle(iCalHoopJumping.CalendarOccurrence evt) { Console.WriteLine("I am a twitch stream handler, it falls to me to..."); Console.WriteLine(" H A N D L E "); diff --git a/showHandlers/YoutubeHandler.cs b/showHandlers/YoutubeHandler.cs index e18dcd0..7a4cbf3 100644 --- a/showHandlers/YoutubeHandler.cs +++ b/showHandlers/YoutubeHandler.cs @@ -1,15 +1,73 @@ using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; using director; +using franz; +using Newtonsoft.Json; +using Schedulable.Show; namespace ShowHandlers { - public class YoutubeHandler + public class YoutubeHandler : ShowHandler { - public static void Handle(iCalHoopJumping.CalendarOccurrence evt) + private YoutubeRelease config; + private List agentsPresent = new List(); + private bool essentialChecksPassed = false; + private bool allChecksPassed = false; + private readonly AutoResetEvent checkSignal = new AutoResetEvent(false); + public override void Handle(iCalHoopJumping.CalendarOccurrence evt) { - Console.WriteLine("I am a YT handler, it falls to me to..."); - Console.WriteLine(" H A N D L E "); - Console.WriteLine($"current time is {DateTime.Now}"); + config = JsonConvert.DeserializeObject(Program.conf.show_template_yt_release); + config.PreShow.AgentsNeeded = config.PreShow.AgentsNeeded.Select(s => s.ToLower()); + Telefranz.Instance.addHandler(agentReports); + Task.Run(() => PreShow(evt)); + if(evt.OccurrenceStart > DateTime.Now) + { + Task.WaitAll(Task.Delay(evt.OccurrenceStart - DateTime.Now)); + } + PostShow(evt); + } + ~YoutubeHandler() + { + Telefranz.Instance.removeHandler(agentReports); + } + protected void PreShow(iCalHoopJumping.CalendarOccurrence evt) + { + Console.WriteLine($"it's the pre-show, showtime at: {evt.OccurrenceStart}"); + agentsPresent.Clear(); + essentialChecksPassed = false; + allChecksPassed = false; + Task.WaitAny( + Task.Delay(10) + ); + Telefranz.Instance.ProduceMessage(new silver_messages.global.sound_off()); + while(agentsPresent.Where(ap => !config.PreShow.AgentsNeeded.Contains(ap))?.Count() > 0) + { + checkSignal.WaitOne(TimeSpan.FromSeconds(45)); + } + // //await sound offs for some timeout + // config.PreShow.AgentsNeeded; + // //once that's ok, await checks + // config.PreShow.Checks; + // //once that's ok, await first command, then start running them + // config.PreShow.Commands; + } + protected void agentReports(silver_messages.global.report r) + { + if(config.PreShow.AgentsNeeded?.FirstOrDefault(an => an.ToLower() == r.name.ToLower()) != null) + { + lock(agentsPresent) + { + agentsPresent.Add(r.name.ToLower()); + checkSignal.Set(); + } + } + } + protected void PostShow(iCalHoopJumping.CalendarOccurrence evt) + { + Console.WriteLine("can't do this bit without understanding when a show ends, sooo."); } } } \ No newline at end of file