diff --git a/Lab1/ChatRoom/ChatRoomLogic.cs b/Lab1/ChatRoom/ChatRoomLogic.cs index cb5cabc..edf352f 100644 --- a/Lab1/ChatRoom/ChatRoomLogic.cs +++ b/Lab1/ChatRoom/ChatRoomLogic.cs @@ -62,6 +62,16 @@ public class ChatRoomLogic return null; } + bool GetAndUpdateBlockedState(Client client) + { + if (client.blockedUntil != null && DateTime.UtcNow >= client.blockedUntil) + { + client.blockedUntil = null; + } + + return client.blockedUntil != null; + } + public bool SendMessage(int clientId, string contents, bool needsToBeCensored) { lock (state.accessLock) @@ -72,16 +82,9 @@ public class ChatRoomLogic return false; } - if (client.blockedUntil != null) + if (GetAndUpdateBlockedState(client)) { - if (DateTime.UtcNow < client.blockedUntil) - { - return false; - } - else - { - client.blockedUntil = null; - } + return false; } var message = new Message @@ -107,6 +110,7 @@ public class ChatRoomLogic { if (message.status != MessageStatus.WaitingForModerator) continue; + log.Info($"Message '{message.id}' given to moderator"); message.status = MessageStatus.GivenToModerator; return new ChatRoomContract.Message{ id = message.id, @@ -139,6 +143,54 @@ public class ChatRoomLogic } } + public void RejectMessage(int messageId) + { + lock (state.accessLock) + { + var message = FindMessageById(messageId); + if (message == null) + { + return; + } + + if (message.status != MessageStatus.GivenToModerator) + { + return; + } + + message.status = MessageStatus.Rejected; + log.Info($"Message {message.id} was rejected"); + + var client = FindClientById(message.clientId); + if (client != null && !GetAndUpdateBlockedState(client)) + { + client.strikes++; + + var rnd = new Random(); + if (client.strikes > rnd.Next(0, 10)) + { + log.Info($"Client '{client.name}' ({client.id}) was blocked for {client.strikes}s"); + client.blockedUntil = DateTime.UtcNow.AddSeconds(client.strikes); + client.strikes = 0; + } + } + } + } + + public int GetStrikes(int clientId) + { + lock (state.accessLock) + { + var client = FindClientById(clientId); + if (client == null) + { + return 0; + } + + return client.strikes; + } + } + public void BackgroundTask() { while (true) diff --git a/Lab1/ChatRoom/ChatRoomService.cs b/Lab1/ChatRoom/ChatRoomService.cs index e35a7cd..0fca3e1 100644 --- a/Lab1/ChatRoom/ChatRoomService.cs +++ b/Lab1/ChatRoom/ChatRoomService.cs @@ -22,12 +22,12 @@ public class ChatRoomService : ChatRoomContract.IChatRoomService public int GetStrikes(int clientId) { - throw new NotImplementedException(); + return logic.GetStrikes(clientId); } public void RejectMessage(int messageId) { - throw new NotImplementedException(); + logic.RejectMessage(messageId); } public bool SendMessage(int clientId, string contents, bool needsToBeCensored) diff --git a/Lab1/Moderator/Moderator.cs b/Lab1/Moderator/Moderator.cs index 0951e0b..a061eff 100644 --- a/Lab1/Moderator/Moderator.cs +++ b/Lab1/Moderator/Moderator.cs @@ -64,8 +64,16 @@ internal class Moderator if (message != null) { log.Info($"Checking message ({message.id}): {message.contents}"); + Thread.Sleep(500); - chatRoom.ApproveMessage(message.id); + if (message.needsToBeCensored) + { + chatRoom.RejectMessage(message.id); + } + else + { + chatRoom.ApproveMessage(message.id); + } } Thread.Sleep(1 * 1000); diff --git a/Lab1/Participant/Participant.cs b/Lab1/Participant/Participant.cs index 2c8e60b..e9f370f 100644 --- a/Lab1/Participant/Participant.cs +++ b/Lab1/Participant/Participant.cs @@ -43,9 +43,11 @@ internal class Participant int clientId = chatRoom.RegisterClient(name); log.Info($"Registered with client id {clientId}"); - Console.Title = $"Participant | {name} | {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)) {