1
0
tinklu-paslaugos/Lab4/Participant/ParticipantLogic.cs
2024-12-07 17:21:09 +02:00

55 lines
1.6 KiB
C#

using ChatRoomContract;
using Bogus;
using NLog;
namespace Participant;
public class ParticipantLogic
{
private IChatRoomService chatRoom;
public ParticipantLogic(IChatRoomService chatRoom)
{
this.chatRoom = chatRoom;
}
public void Run()
{
Logger log = LogManager.GetCurrentClassLogger();
var faker = new Faker("en");
var rnd = new Random();
var name = faker.Name.FullName();
int clientId = chatRoom.RegisterClient(name);
log.Info($"Registered with client id {clientId}");
while (true)
{
int strikes = chatRoom.GetStrikes(clientId);
Console.Title = $"Participant | {name} | {clientId} | {strikes} Strikes";
var message = string.Join(" ", faker.Lorem.Words(5));
bool needsToBeCensored = rnd.Next(0, 100) > 50;
if (chatRoom.SendMessage(clientId, message, needsToBeCensored))
{
log.Info("Sent message");
}
else
{
log.Info("Failed to send message, blocked");
var blockedUntil = chatRoom.GetBlockedUntil(clientId);
var now = DateTime.UtcNow;
if (blockedUntil != null && blockedUntil > now)
{
var delta = blockedUntil.Value - now;
log.Info($"Waiting {delta.TotalSeconds:F3}s until block expires");
Thread.Sleep((int)delta.TotalMilliseconds);
}
}
Thread.Sleep(2 * 1000);
}
}
}