/// <summary> /// Sends the requested ad. /// </summary> /// <param name="timer">The timer calling this function.</param> /// <param name="adId">The ad identifier.</param> private static void SendAd(Timer timer, int adId) { Entites.Ad ad = Get(adId); if (ad.AdSystem.BothConditions && ad.AdSystem.MinNbOfMessage > Message.NbMessagesSince(ad.AdSystem.Channel, ad.LastSent)) { timer.Interval = 60 * 1000; return; } if (ad.MaxShow > 0) { ad.MaxShow--; } ad.Message.RepliesTo = null; Entites.Message.SendMessage(ad.Message); ad.LastSent = DateTime.UtcNow; Update(ad); if (ad.MaxShow == 0) { timer.Stop(); timer.Dispose(); } else { timer.Interval = ad.AdSystem.MinTime.TotalMilliseconds; } }
/// <summary> /// Clears the references of the ad. /// </summary> /// <param name="ad">The ad system type to ad.</param> /// <returns>A copy of the ad given in entry with only the references.</returns> private static Entites.Ad ClearReferences(Entites.Ad ad) { Entites.Ad reference = new Entites.Ad(0, null, DateTime.MinValue, ad.Message, ad.AdSystem); ad.Message = null; ad.AdSystem = null; return(reference); }
/// <summary> /// Mod command. Add a new ad to repeat on the channel following the ad system's settings. /// </summary> /// <param name="command">The command.</param> /// <param name="core">The core.</param> public static void AddAd(Command command, Core core = null) { if (Tools.IsNotModThenSendWarning(command)) { return; } DAL.Channel.LoadAdSystem(command.Message.UserToChannel.Channel); DAL.Message.LoadRepliesTo(command.Message); if (command.Arguement == null || command.Message.UserToChannel.Channel.AdSystem == null || command.Message.RepliesTo == null) { Entites.Message.SendMessage(Message.Answer(command.Message, "Your add is not valid; you must reply to the message you want to set as an ad and give it a name like so: \"/addad name\".")); return; } string[] arguements = command.Arguement.Split(new[] { ' ' }, 1, StringSplitOptions.RemoveEmptyEntries); Entites.Ad ad = Create(new Entites.Ad(-1, arguements[0], SqlDateTime.MinValue.Value, command.Message.RepliesTo, command.Message.UserToChannel.Channel.AdSystem)); Timer t = new Timer { AutoReset = true }; t.Elapsed += delegate { SendAd(t, ad.AdId); }; t.Start(); }
/// <summary> /// Loads the ad system associated to the ad. /// </summary> /// <param name="ad">The ad.</param> /// <returns>The same ad with the ad system reference loaded.</returns> public static Entites.Ad LoadAdSystem(Entites.Ad ad) { using (TerministratorContext context = new TerministratorContext(true)) { context.Ad.Attach(ad); context.Entry(ad).Reference(p => p.AdSystem).Load(); } return(ad); }
/// <summary> /// Creates the specified ad. /// </summary> /// <param name="ad">The ad.</param> /// <returns>The same ad with an updated ID.</returns> public static Entites.Ad Create(Entites.Ad ad) { Entites.Ad reference = ClearReferences(ad); using (TerministratorContext context = new TerministratorContext(true)) { ad.AdId = context.Ad.Add(ad).AdId; context.SaveChanges(); } return(AddReferences(ad, reference)); }
/// <summary> /// Updates the specified ad. /// </summary> /// <param name="ad">The ad.</param> /// <returns>The same ad.</returns> public static Entites.Ad Update(Entites.Ad ad) { using (TerministratorContext context = new TerministratorContext(true)) { Entites.Ad old = context.Ad.Find(ad.AdId); if (old != null) { old.MaxShow = ad.MaxShow; old.Name = ad.Name; old.LastSent = ad.LastSent; context.SaveChanges(); } } return(ad); }
/// <summary> /// Gets the specified ad. /// </summary> /// <param name="adId">The ad identifier.</param> /// <returns>The requested ad.</returns> public static Entites.Ad Get(int adId) { using (TerministratorContext context = new TerministratorContext(true)) { Entites.Ad ad = context.Ad.Find(adId); if (ad == null) { return(null); } context.Entry(ad).Reference(x => x.AdSystem).Load(); context.Entry(ad).Reference(x => x.Message).Load(); context.Entry(ad.Message).Reference(x => x.UserToChannel).Load(); context.Entry(ad.Message).Collection(x => x.Texts).Load(); context.Entry(ad.Message).Reference(x => x.Application).Load(); context.Entry(ad.Message.UserToChannel).Reference(x => x.Channel).Load(); return(ad); } }
/// <summary> /// Updates the specified ad. /// </summary> /// <param name="ad">The ad.</param> /// <returns>The first arguement.</returns> public static Entites.Ad Update(Entites.Ad ad) { return(DAL.Ad.Update(ad)); }
/// <summary> /// Creates the specified ad. /// </summary> /// <param name="ad">The ad.</param> /// <returns>The newly created ad.</returns> public static Entites.Ad Create(Entites.Ad ad) { return(DAL.Ad.Create(ad)); }
/// <summary> /// Adds the references of the second arguement in the first one. /// </summary> /// <param name="ad">The ad to add the references in.</param> /// <param name="reference">The references.</param> /// <returns>The first arguement.</returns> private static Entites.Ad AddReferences(Entites.Ad ad, Entites.Ad reference) { ad.Message = reference.Message; ad.AdSystem = reference.AdSystem; return(ad); }