Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- namespace TestConsole
- {
- class Program
- {
- static void Main()
- {
- Func<string, bool> sameLettersRule = s =>
- {
- if (s.Length < 3)
- return false;
- return s.Select(c => char.ToLower(c))
- .GroupBy(c => c)
- .Select(a => ((double)a.Count() / s.Length) * 100)
- .Max() >= 50;
- };
- Func<string, bool> upperCaseLettersRule = s =>
- {
- if (s.Length < 3)
- return false;
- return ((double)s.Count(c =>
- char.IsUpper(c)) / s.Length * 100 >= 50);
- };
- string dataTest = string.Empty;
- SpamTest spamTest = new SpamTest(dataTest);
- spamTest.ApplyRule(sameLettersRule, "50% of the string must not be the same letter");
- spamTest.ApplyRule(upperCaseLettersRule, "Hop lige ned fra caps");
- User user = new User() { Id = 1, Name="Henrik" };
- spamTest.AddUser(user, user.DisplayTextForUser);
- while (true)
- {
- spamTest.AppendData(user, Console.ReadLine());
- }
- }
- }
- class User
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public void DisplayTextForUser(string text)
- {
- Console.WriteLine(text);
- }
- }
- class SpamTest
- {
- private readonly StringBuilder dataTest;
- private readonly Dictionary<Func<string, bool>, string> contentControllers;
- private readonly Dictionary<User, Action<string>> userOutputWriters;
- private readonly Stopwatch stopWatch;
- private int appendedLineCounter = default(int);
- private const long SPAM_PROTECT_DELAY = 10000;
- public SpamTest(string dataTest)
- {
- this.dataTest = new StringBuilder(dataTest);
- contentControllers = new Dictionary<Func<string, bool>, string>();
- userOutputWriters = new Dictionary<User, Action<string>>();
- stopWatch = new Stopwatch();
- }
- public void AddUser(User user, Action<string> userOutputWriter)
- {
- userOutputWriters.Add(user, userOutputWriter);
- }
- public void AppendData(User sender, string someData)
- {
- Action<string> userOutputWriter;
- if (!userOutputWriters.TryGetValue(sender, out userOutputWriter))
- throw new Exception("User not found");
- if (string.IsNullOrEmpty(someData))
- return;
- if (DoesntHaveIllegalContent(someData, userOutputWriter))
- if (TryAppendData(someData, userOutputWriter))
- Console.WriteLine("Data written to string");
- }
- private bool TryAppendData(string data, Action<string> writer)
- {
- if (stopWatch.ElapsedMilliseconds >= SPAM_PROTECT_DELAY)
- {
- stopWatch.Stop();
- stopWatch.Reset();
- appendedLineCounter = 0;
- }
- else if (stopWatch.ElapsedMilliseconds < SPAM_PROTECT_DELAY && appendedLineCounter >= 5)
- {
- writer("Stop spamming!");
- return false;
- }
- dataTest.AppendLine(data);
- stopWatch.Start();
- appendedLineCounter++;
- return true;
- }
- private bool DoesntHaveIllegalContent(string someData, Action<string> writer)
- {
- var errors = contentControllers.Where(kvp => kvp.Key(someData)).Select(e => e.Value);
- if (errors.Count() < 1)
- return true;
- foreach (var error in errors)
- writer(error);
- return false;
- }
- public void ApplyRule(Func<string, bool> func, string errorMessage)
- {
- contentControllers.Add(func, errorMessage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement