Advertisement
Guest User

StringSpam??

a guest
Nov 24th, 2010
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace TestConsole
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Func<string, bool> sameLettersRule = s =>
  14.             {
  15.                 if (s.Length < 3)
  16.                     return false;
  17.                 return s.Select(c => char.ToLower(c))
  18.                                          .GroupBy(c => c)
  19.                                          .Select(a => ((double)a.Count() / s.Length) * 100)
  20.                                          .Max() >= 50;
  21.             };
  22.  
  23.             Func<string, bool> upperCaseLettersRule = s =>
  24.                 {
  25.                     if (s.Length < 3)
  26.                         return false;
  27.                     return ((double)s.Count(c =>
  28.                         char.IsUpper(c)) / s.Length * 100 >= 50);
  29.                 };
  30.            
  31.             string dataTest = string.Empty;
  32.            
  33.             SpamTest spamTest = new SpamTest(dataTest);
  34.             spamTest.ApplyRule(sameLettersRule, "50% of the string must not be the same letter");
  35.             spamTest.ApplyRule(upperCaseLettersRule, "Hop lige ned fra caps");
  36.  
  37.             User user = new User() { Id = 1, Name="Henrik" };
  38.             spamTest.AddUser(user, user.DisplayTextForUser);
  39.  
  40.             while (true)
  41.             {
  42.                 spamTest.AppendData(user, Console.ReadLine());
  43.             }
  44.         }
  45.     }
  46.  
  47.     class User
  48.     {
  49.         public int Id { get; set; }
  50.         public string Name { get; set; }
  51.  
  52.         public void DisplayTextForUser(string text)
  53.         {
  54.             Console.WriteLine(text);
  55.         }
  56.     }
  57.  
  58.     class SpamTest
  59.     {
  60.         private readonly StringBuilder dataTest;
  61.         private readonly Dictionary<Func<string, bool>, string> contentControllers;
  62.         private readonly Dictionary<User, Action<string>> userOutputWriters;
  63.         private readonly Stopwatch stopWatch;
  64.         private int appendedLineCounter = default(int);
  65.         private const long SPAM_PROTECT_DELAY = 10000;
  66.  
  67.         public SpamTest(string dataTest)
  68.         {
  69.             this.dataTest = new StringBuilder(dataTest);
  70.             contentControllers = new Dictionary<Func<string, bool>, string>();
  71.             userOutputWriters = new Dictionary<User, Action<string>>();
  72.             stopWatch = new Stopwatch();
  73.         }
  74.  
  75.         public void AddUser(User user, Action<string> userOutputWriter)
  76.         {
  77.             userOutputWriters.Add(user, userOutputWriter);
  78.         }
  79.  
  80.         public void AppendData(User sender, string someData)
  81.         {
  82.             Action<string> userOutputWriter;
  83.             if (!userOutputWriters.TryGetValue(sender, out userOutputWriter))
  84.                 throw new Exception("User not found");                
  85.  
  86.             if (string.IsNullOrEmpty(someData))
  87.                 return;
  88.             if (DoesntHaveIllegalContent(someData, userOutputWriter))
  89.                 if (TryAppendData(someData, userOutputWriter))
  90.                     Console.WriteLine("Data written to string");
  91.         }
  92.  
  93.         private bool TryAppendData(string data, Action<string> writer)
  94.         {
  95.             if (stopWatch.ElapsedMilliseconds >= SPAM_PROTECT_DELAY)
  96.             {
  97.                 stopWatch.Stop();
  98.                 stopWatch.Reset();
  99.                 appendedLineCounter = 0;
  100.             }
  101.  
  102.             else if (stopWatch.ElapsedMilliseconds < SPAM_PROTECT_DELAY && appendedLineCounter >= 5)
  103.             {
  104.                 writer("Stop spamming!");
  105.                 return false;
  106.             }
  107.  
  108.             dataTest.AppendLine(data);
  109.             stopWatch.Start();
  110.             appendedLineCounter++;
  111.             return true;
  112.         }
  113.  
  114.         private bool DoesntHaveIllegalContent(string someData, Action<string> writer)
  115.         {
  116.             var errors = contentControllers.Where(kvp => kvp.Key(someData)).Select(e => e.Value);
  117.             if (errors.Count() < 1)
  118.                 return true;
  119.  
  120.             foreach (var error in errors)
  121.                 writer(error);
  122.             return false;
  123.         }
  124.  
  125.         public void ApplyRule(Func<string, bool> func, string errorMessage)
  126.         {
  127.             contentControllers.Add(func, errorMessage);
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement