From ff3ee7b443f512657abb1c6cdfa83f5e456fe223 Mon Sep 17 00:00:00 2001 From: Madpeterz <57852827+Madpeterz@users.noreply.github.com> Date: Tue, 30 Mar 2021 19:34:29 +0100 Subject: [PATCH] new adverts service --- BetterSecondbot/Adverts/advertsService.cs | 256 ++++++++++++++++++++++ BetterSecondbot/Cli.cs | 3 + Shared/IO/SimpleIO.cs | 10 + Shared/Json/JsonConfig.cs | 18 ++ 4 files changed, 287 insertions(+) create mode 100644 BetterSecondbot/Adverts/advertsService.cs diff --git a/BetterSecondbot/Adverts/advertsService.cs b/BetterSecondbot/Adverts/advertsService.cs new file mode 100644 index 0000000..ff23e8e --- /dev/null +++ b/BetterSecondbot/Adverts/advertsService.cs @@ -0,0 +1,256 @@ +using BetterSecondBot; +using BetterSecondBotShared.IO; +using BetterSecondBotShared.Json; +using BetterSecondBotShared.Static; +using Newtonsoft.Json; +using OpenMetaverse; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BetterSecondbot.Adverts +{ + + + public class advertsService + { + public advertsService(Cli Master, bool LoadFromDocker) + { + controler = Master; + if(LoadFromDocker == true) + { + loadFromDockerEnv(); + } + else + { + loadFromDisk(); + } + } + + protected void loadFromDisk() + { + advertsBlob loadedAdverts = new advertsBlob(); + advertConfig demoAdvert = new advertConfig(); + demoAdvert.attachment = UUID.Zero.ToString(); + demoAdvert.content = "Content"; + demoAdvert.title = "Title"; + demoAdvert.groups = new[] { UUID.Zero.ToString(), UUID.Zero.ToString() }; + demoAdvert.days = "0,1,2,3,4,5,6"; + demoAdvert.hour = "12"; + demoAdvert.min = "30"; + demoAdvert.notice = "false"; + demoAdvert.enabled = "true"; + loadedAdverts.adverts = new advertConfig[] { demoAdvert }; + + + string targetfile = "adverts.json"; + SimpleIO io = new SimpleIO(); + if (SimpleIO.FileType(targetfile, "json") == false) + { + io.WriteJsonAdverts(loadedAdverts, targetfile); + return; + } + if (io.Exists(targetfile) == false) + { + io.WriteJsonAdverts(loadedAdverts, targetfile); + return; + } + string json = io.ReadFile(targetfile); + if (json.Length > 0) + { + try + { + loadedAdverts = JsonConvert.DeserializeObject(json); + foreach (advertConfig loaded in loadedAdverts.adverts) + { + Unpack(loaded); + } + } + catch + { + io.makeOld(targetfile); + io.WriteJsonAdverts(loadedAdverts, targetfile); + } + return; + } + } + + protected void Unpack(advertConfig config) + { + titles.Add(config.title); + UUID attachment = UUID.Zero; + UUID.TryParse(config.attachment, out attachment); + attachments.Add(attachment); + content.Add(config.content); + bool asGroupNotice = false; + bool.TryParse(config.notice, out asGroupNotice); + asNotice.Add(asGroupNotice); + string[] unpackDays = config.days.Split(','); + List activeondays = new List(); + foreach(string Uday in unpackDays) + { + if(int.TryParse(Uday,out int dayid) == true) + { + activeondays.Add(dayid); + } + } + activeDays.Add(activeondays); + int activeHourid = -2; + int.TryParse(config.hour, out activeHourid); + activeHours.Add(activeHourid); + int activeMinid = -2; + int.TryParse(config.min, out activeMinid); + activeMins.Add(activeMinid); + bool asEnabled = false; + bool.TryParse(config.enabled, out asEnabled); + enabled.Add(asEnabled); + List selectedGroups = new List(); + foreach(string groupA in config.groups) + { + UUID groupUUID = UUID.Zero; + UUID.TryParse(groupA, out groupUUID); + if(groupUUID != UUID.Zero) + { + selectedGroups.Add(groupUUID); + } + } + groups.Add(selectedGroups); + } + + protected void loadFromDockerEnv() + { + int loop = 1; + bool found = true; + while (found == true) + { + string title = getEnv("advert_" + loop.ToString() + "_title"); + if (helpers.notempty(title) == true) + { + advertConfig config = new advertConfig(); + config.title = title; + config.attachment = getEnv("advert_" + loop.ToString() + "_attachment"); + config.content = getEnv("advert_" + loop.ToString() + "_content"); + config.notice = getEnv("advert_" + loop.ToString() + "notice"); + config.days = getEnv("advert_" + loop.ToString() + "days"); + config.hour = getEnv("advert_" + loop.ToString() + "hour"); + config.min = getEnv("advert_" + loop.ToString() + "min"); + List groupentrys = new List(); + int loop2 = 1; + bool exitNow = false; + while(exitNow == false) + { + exitNow = true; + string value = getEnv("advert_" + loop.ToString() + "_group_" + loop2.ToString()); + if (helpers.notempty(value) == true) + { + groupentrys.Add(value); + exitNow = false; + } + loop2++; + } + config.groups = groupentrys.ToArray(); + Unpack(config); + } + else + { + found = false; + } + loop++; + } + } + + protected string getEnv(string name) + { + return Environment.GetEnvironmentVariable(name); + } + + protected List titles = new List(); + protected List attachments = new List(); + protected List content = new List(); + protected List asNotice = new List(); + protected List> activeDays = new List>(); + protected List activeHours = new List(); + protected List activeMins = new List(); + protected List enabled = new List(); + protected List> groups = new List>(); + protected Cli controler = null; + + protected int lastTickMin = -1; + public void Tick() + { + DateTime moment = DateTime.Now; + if (lastTickMin != moment.Minute) + { + lastTickMin = moment.Minute; + checkForWork(); + } + } + + protected void checkForWork() + { + DateTime moment = DateTime.Now; + int dayofweek = (int)(moment.DayOfWeek + 6) % 7; + int loop = 0; + while (loop < titles.Count) + { + if (enabled[loop] == false) + { + continue; + } + if (activeDays[loop].Contains(dayofweek) == false) + { + continue; + } + if ((activeHours[loop] != moment.Hour) && (activeHours[loop] != -1)) + { + continue; + } + if ((activeHours[loop] != moment.Minute) && (activeHours[loop] != -1)) + { + continue; + } + if (activeHours[loop] == -1) + { + if ((moment.Minute != 0) && (moment.Minute != 30)) + { + continue; + } + } + TriggerAdvert(loop); + loop++; + } + } + + protected void TriggerAdvert(int advertID) + { + + foreach (UUID group in groups[advertID]) + { + if (asNotice[advertID] == true) + { + if (attachments[advertID] != UUID.Zero) + { + controler.getBot().CallAPI( + "GroupnoticeWithAttachment", + new[] { group.ToString(), titles[advertID], content[advertID], attachments[advertID].ToString() } + ); + } + else + { + controler.getBot().CallAPI( + "Groupnotice", + new[] { group.ToString(), titles[advertID], content[advertID] } + ); + } + } + else + { + controler.getBot().CallAPI( + "Groupchat", + new[] { group.ToString(), content[advertID] } + ); + } + } + } + } +} diff --git a/BetterSecondbot/Cli.cs b/BetterSecondbot/Cli.cs index 650b16a..d16759e 100644 --- a/BetterSecondbot/Cli.cs +++ b/BetterSecondbot/Cli.cs @@ -8,6 +8,7 @@ using static BetterSecondBot.Program; using BetterSecondbot.BetterAtHome; using BetterSecondbot.DataStorage; using BetterSecondbot.Tracking; +using BetterSecondbot.Adverts; namespace BetterSecondBot { @@ -18,6 +19,7 @@ namespace BetterSecondBot protected SecondBot Bot; protected BetterAtHome betterAtHomeService; protected BetterTracking betterTracking; + protected advertsService Adverts; protected Datastorage datastorage; public SecondBot getBot() @@ -58,6 +60,7 @@ namespace BetterSecondBot new HttpAsService(Bot, Config, as_docker); }).Start(); } + Adverts = new advertsService(this, as_docker); betterAtHomeService = new BetterAtHome(this, Config); betterTracking = new BetterTracking(this); datastorage = new Datastorage(this); diff --git a/Shared/IO/SimpleIO.cs b/Shared/IO/SimpleIO.cs index 961e90c..557e255 100644 --- a/Shared/IO/SimpleIO.cs +++ b/Shared/IO/SimpleIO.cs @@ -79,6 +79,16 @@ namespace BetterSecondBotShared.IO } } + public void WriteJsonAdverts(advertsBlob adverts, string targetfile) + { + JsonSerializer serializer = new JsonSerializer(); + using (StreamWriter sw = new StreamWriter(@"" + root_folder + "" + targetfile)) + using (JsonWriter writer = new JsonTextWriter(sw) { Formatting = Formatting.Indented }) + { + serializer.Serialize(writer, adverts); + } + } + public void makeOld(string targetfile) diff --git a/Shared/Json/JsonConfig.cs b/Shared/Json/JsonConfig.cs index efdad55..ae37f0e 100644 --- a/Shared/Json/JsonConfig.cs +++ b/Shared/Json/JsonConfig.cs @@ -20,6 +20,24 @@ namespace BetterSecondBotShared.Json public string[] CustomRelays { get; set; } } + public class advertConfig + { + public string title; + public string attachment; + public string content; + public string notice; + public string enabled; + public string[] groups; + public string days; + public string hour; + public string min; + } + + public class advertsBlob + { + public advertConfig[] adverts = new advertConfig[] { }; + } + public class JsonConfig {