Skip to content

Commit

Permalink
Improve Rude
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzuehling committed Jan 19, 2017
1 parent edbe81a commit 805c932
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
62 changes: 48 additions & 14 deletions Rude/RudePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Fancyauth.APIUtil;
using Fancyauth.API;
using System.Collections.Concurrent;

namespace Rude
{
public class RudePlugin : PluginBase
{
private readonly string[] Prefixes = { "[Rude] ", "[Ruderer] ", "[Rüdiger] " };
public static readonly TimeSpan RudeCooldown = TimeSpan.FromMinutes(30);
public const double MinRudeTime = 45;
public const double MaxRudeTime = 120;
public const double MinRudeActionTimeout = 3;
public const double MaxRudeActionTimeout = 18;

public Dictionary<int, RudeStatus> RudeStatus = new Dictionary<int, RudeStatus>();
private ConcurrentDictionary<int, RudeStatus> RudeStatus = new ConcurrentDictionary<int, RudeStatus>();
private ConcurrentDictionary<int, RudeStatus> RudeActorStatus = new ConcurrentDictionary<int, RudeStatus>();

private Random rand = new Random();

[ContextCallback("Rude")]
public async Task RudeUser(IUser actor, IUserShim targetShim)
{
var target = await targetShim.Load();
EnsureRudeStatus(target.UserId);
EnsureRudeStatus(actor.UserId);
var rude = RudeStatus[actor.UserId];

rude.RaiseRudeLevel();
var rude = RudeStatus[target.UserId];
var rudeActor = RudeActorStatus[actor.UserId];

if (rudeActor.RudeLevel > 0)
{
var actorRude = RudeStatus[actor.UserId];

// You shouldn't be able to use this to reset yourself
if (rudeActor.RudeLevel < Prefixes.Length)
actorRude.RaiseRudeLevel(RandomTimespan(MinRudeTime, MaxRudeTime));

return;
}

rudeActor.RaiseRudeLevel(RandomTimespan(MinRudeActionTimeout, MaxRudeActionTimeout));
rude.RaiseRudeLevel(RandomTimespan(MinRudeTime, MaxRudeTime));

if (rude.RudeLevel > Prefixes.Length)
{
RudeStatus.Remove(actor.UserId);
await actor.Kick("You have been too rude.");
rude.Reset();
await target.Kick("You have been too rude.");
}
else
{
actor.Name = GetUserName(actor);
await actor.SaveChanges();
target.Name = GetUserName(target);
await target.SaveChanges();
}
}

Expand All @@ -41,12 +62,11 @@ public override async Task OnUserConnected(IUser user)
await user.SaveChanges();
}



private void EnsureRudeStatus(int userId)
{
if (!RudeStatus.ContainsKey(userId))
RudeStatus[userId] = new RudeStatus
{
RudeStatus.TryAdd(userId, new RudeStatus
{
OnRudeLevelDecrease = async () =>
{
Expand All @@ -58,7 +78,16 @@ private void EnsureRudeStatus(int userId)
user.Name = GetUserName(user);
await user.SaveChanges();
}
};
});
}

if (!RudeActorStatus.ContainsKey(userId))
{
RudeActorStatus.TryAdd(userId, new RudeStatus
{
OnRudeLevelDecrease = () => { }
});
}
}

private string GetUserName(IUser user)
Expand All @@ -80,6 +109,11 @@ private string GetUserName(IUser user)

return name;
}

private TimeSpan RandomTimespan(double minMinues, double maxMinutes)
{
return TimeSpan.FromMinutes(minMinues + rand.NextDouble() * (maxMinutes - minMinues));
}
}
}

27 changes: 16 additions & 11 deletions Rude/RudeStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,43 @@ namespace Rude
{
public class RudeStatus
{
public Action OnRudeLevelDecrease;
public Action OnRudeLevelDecrease { get; set; }

private object RudeLock = new object();

public int RudeLevel { get; private set; }
private int RudeIteration = 0;


public void RaiseRudeLevel()
public void RaiseRudeLevel(TimeSpan length)
{
lock (RudeLock)
{
RudeLevel++;
RudeIteration++;

StartRudeDown(RudeIteration);
StartRudeDown(length, RudeIteration);
}
}

private async void StartRudeDown(int currentRudeInteration)
public void Reset()
{
await Task.Delay(RudePlugin.RudeCooldown);
lock(RudeLock)
{
// Invalidate all old timeouts
RudeIteration++;
RudeLevel = 0;
}
}

private async void StartRudeDown(TimeSpan length, int currentRudeInteration)
{
await Task.Delay(length);
lock (RudeLock)
{
if (RudeIteration != currentRudeInteration)
return;

RudeLevel = RudeLevel - 1;
RudeIteration++;

if (RudeLevel > 0)
StartRudeDown(RudeIteration);
RudeLevel--;

OnRudeLevelDecrease();
}
Expand Down

0 comments on commit 805c932

Please sign in to comment.