protected virtual void HandleCounterStatsCommand(CommandMatch cmd, IChannelMessageEventArgs args) { var counter = (string)cmd.Arguments[0]; using (CountersContext ctx = GetNewContext()) { IQueryable <CounterEntry> relevantEntries = ctx.Entries .Where(e => e.Command == counter && e.Channel == args.Channel && !e.Expunged); int entryCount = relevantEntries.Count(); List <KeyValuePair <string, int> > usersCounts = relevantEntries .GroupBy(e => e.PerpUsername ?? e.PerpNickname, (key, es) => new KeyValuePair <string, int>(key, es.Count())) .OrderByDescending(uc => uc.Value) .Take(Config.TopCount + 1) .ToList(); string tops = usersCounts .Select(uc => $"{uc.Key}: {uc.Value}") .Take(Config.TopCount) .StringJoin(", "); string topText; if (usersCounts.Count == 0) { topText = ""; } else if (usersCounts.Count <= Config.TopCount) { topText = $" (top: {tops})"; } else { topText = $" (top {Config.TopCountText}: {tops})"; } ConnectionManager.SendChannelMessage( args.Channel, $"{args.SenderNickname}: '{counter}': {entryCount}{topText}" ); } }
protected virtual void HandleUncountCommand(CommandMatch cmd, IChannelMessageEventArgs args) { ChannelUserLevel level = ConnectionManager.GetChannelLevelForUser(args.Channel, args.SenderNickname); if (level < ChannelUserLevel.HalfOp) { ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: You need to be a channel operator."); return; } var counter = (string)cmd.Arguments[0]; CounterEntry entry; using (CountersContext ctx = GetNewContext()) { entry = ctx.Entries .Where(e => e.Command == counter && e.Channel == args.Channel) .OrderByDescending(e => e.ID) .FirstOrDefault() ; if (entry == null) { ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: Found nothing for counter '{counter}' in {args.Channel}."); return; } if (entry.Expunged) { ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: Already expunged: <{entry.PerpNickname}> {entry.Message}"); return; } entry.Expunged = true; ctx.SaveChanges(); ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: Okay, expunged <{entry.PerpNickname}> {entry.Message}"); } }
protected virtual void HandleCountedCommand(CommandMatch cmd, IChannelMessageEventArgs args) { var counter = (string)cmd.Arguments[0]; CounterEntry entry; using (CountersContext ctx = GetNewContext()) { entry = ctx.Entries .Where(e => e.Command == counter && e.Channel == args.Channel && !e.Expunged) .OrderByDescending(e => e.ID) .FirstOrDefault() ; if (entry == null) { ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: Found nothing for counter '{counter}' in {args.Channel}."); return; } ConnectionManager.SendChannelMessage(args.Channel, $"{args.SenderNickname}: <{entry.PerpNickname}> {entry.Message}"); } }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IDataBase dataBase, CountersContext countersContext, UsersContext userContext) { countersContext.Database.Migrate(); userContext.Database.Migrate(); app.UseStaticFiles(); app.UseDefaultFiles(); app.UseSerilogRequestLogging(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); }
protected virtual void TryToMatch(Counter counter, string channel, string senderNickname, string messageSubstring = null, Regex messageRegex = null) { // go back through time RingBuffer <ChannelMessage> messages; if (!ChannelsMessages.TryGetValue(channel, out messages)) { return; } IEnumerable <ChannelMessage> messagesReversed = messages .Reverse(); ChannelMessage foundMessage = null; foreach (ChannelMessage message in messagesReversed) { if (messageSubstring != null) { if (!message.Body.Contains(messageSubstring)) { continue; } } if (messageRegex != null) { if (!messageRegex.IsMatch(message.Body)) { continue; } } if (counter.MessageRegex != null && !counter.MessageRegex.IsMatch(message.Body)) { continue; } if ( counter.NicknameRegex != null && !counter.NicknameRegex.IsMatch(message.Nickname) && (message.Username == null || !counter.NicknameRegex.IsMatch(message.Username)) ) { continue; } foundMessage = message; break; } if (foundMessage == null) { ConnectionManager.SendChannelMessage( channel, $"{senderNickname}: Nothing to count." ); return; } if (foundMessage.Counted) { ConnectionManager.SendChannelMessage( channel, $"{senderNickname}: That's been counted already." ); return; } using (CountersContext ctx = GetNewContext()) { ctx.Entries.Add(new CounterEntry { Command = counter.CommandName, HappenedTimestamp = foundMessage.Timestamp, CountedTimestamp = DateTimeOffset.Now, Channel = channel, PerpNickname = foundMessage.Nickname, PerpUsername = foundMessage.Username, CounterNickname = senderNickname, CounterUsername = ConnectionManager.RegisteredNameForNick(senderNickname), Message = foundMessage.Body, Expunged = false }); ctx.SaveChanges(); } foundMessage.Counted = true; }
public DataBaseService(CountersContext countersContext) { this.CountersContext = countersContext; }