From d9a66d0c74d5ccb87cdfc4f04eef324f59abc720 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Mon, 15 Jul 2024 17:56:00 -0500 Subject: [PATCH] message the character's player when penalties are applied Signed-off-by: Brian S. Stephan --- idlerpg/ircplugin.py | 16 ++++++++++--- tests/test_idlerpg_ircplugin.py | 42 ++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/idlerpg/ircplugin.py b/idlerpg/ircplugin.py index ea4cf3a..defa580 100644 --- a/idlerpg/ircplugin.py +++ b/idlerpg/ircplugin.py @@ -130,9 +130,11 @@ class IdleRPG(Plugin): def handle_nick_penalty(self, connection, event): """Penalize characters for changing their nick while in the channel.""" - # TODO: figure out how to update the character and seen hostmasks + logger.debug("nick change: %s", event) + self.seen_hostmasks.discard(event.source) + logger.info("Removed %s from the set of seen hostmasks.", event.source) return self._handle_generic_penalty_and_logout(event.source, event.target, 30, - "changing their nick", + "a nick change", 'time_penalized_nick_change') def handle_part_penalty(self, connection, event): @@ -155,6 +157,9 @@ class IdleRPG(Plugin): penalty = character.penalize(len(message), "sending a privmsg to the game channel") character.time_penalized_privmsg += penalty character.save() + nick = irc.client.NickMask(hostmask).nick + self.bot.reply(None, f"{character} has been penalized {penalty} seconds for talking in the game channel.", + explicit_target=nick) except Character.DoesNotExist: logger.debug("no character found for %s", hostmask) return @@ -209,7 +214,8 @@ class IdleRPG(Plugin): penalty = character.penalize(20, "logging out") character.time_penalized_logout += penalty character.save() - return self.bot.reply(event, f"{character}, has been successfully logged out.") + return self.bot.reply(event, f"{character}, has been successfully logged out. " + f"They have been penalized {penalty} seconds.") def handle_register(self, connection, event, match): """Register a character for a user.""" @@ -280,6 +286,10 @@ class IdleRPG(Plugin): except ValueError: logger.debug("tried to log out %s but they already were", character) character.save() + nick = irc.client.NickMask(hostmask).nick + self.bot.reply(None, + f"{character} has been penalized {penalty} seconds for {reason}, and has been logged out.", + explicit_target=nick) except Character.DoesNotExist: logger.debug("no character found for %s", hostmask) diff --git a/tests/test_idlerpg_ircplugin.py b/tests/test_idlerpg_ircplugin.py index 94aa2f2..f92c365 100644 --- a/tests/test_idlerpg_ircplugin.py +++ b/tests/test_idlerpg_ircplugin.py @@ -4,14 +4,12 @@ SPDX-FileCopyrightText: © 2024 Brian S. Stephan SPDX-License-Identifier: AGPL-3.0-or-later """ import datetime -import logging import re import unittest.mock as mock from django.test import TestCase -from django.utils import timezone +from ircbot.models import IrcServer -from ircbot.models import IrcChannel, IrcServer from idlerpg.ircplugin import IdleRPG from idlerpg.models import Character, Game @@ -89,14 +87,22 @@ class IrcPluginTest(TestCase): # make a character so as to not disturb other tests test_char = Character.objects.register('testnickpen', self.game, 'test', 'bss!bss@test_nick_penalty', 'tester') - with mock.patch('idlerpg.models.Character.penalize', return_value=5) as mock_penalize: + self.plugin.seen_hostmasks.add('bss!bss@test_nick_penalty') + with mock.patch('idlerpg.models.Character.penalize', return_value=30) as mock_penalize: with mock.patch('idlerpg.models.Character.log_out') as mock_log_out: self.plugin.handle_nick_penalty(self.mock_connection, mock_event) - mock_penalize.assert_called_with(30, "changing their nick") + mock_penalize.assert_called_with(30, "a nick change") mock_log_out.assert_called() test_char = Character.objects.get(name='testnickpen') - assert test_char.time_penalized_nick_change == 5 + assert test_char.time_penalized_nick_change == 30 + assert 'bss!bss@test_nick_penalty' not in self.plugin.seen_hostmasks + self.mock_bot.reply.assert_called_once_with( + None, + "testnickpen, the level 0 tester has been penalized 30 seconds for a nick change, " + "and has been logged out.", + explicit_target='bss', + ) test_char.delete() @@ -110,14 +116,22 @@ class IrcPluginTest(TestCase): # make a character so as to not disturb other tests test_char = Character.objects.register('testpartpen', self.game, 'test', 'bss!bss@test_part_penalty', 'tester') - with mock.patch('idlerpg.models.Character.penalize', return_value=5) as mock_penalize: + self.plugin.seen_hostmasks.add('bss!bss@test_part_penalty') + with mock.patch('idlerpg.models.Character.penalize', return_value=200) as mock_penalize: with mock.patch('idlerpg.models.Character.log_out') as mock_log_out: self.plugin.handle_part_penalty(self.mock_connection, mock_event) mock_penalize.assert_called_with(200, "parting the game channel") mock_log_out.assert_called() test_char = Character.objects.get(name='testpartpen') - assert test_char.time_penalized_part == 5 + assert test_char.time_penalized_part == 200 + assert 'bss!bss@test_part_penalty' not in self.plugin.seen_hostmasks + self.mock_bot.reply.assert_called_once_with( + None, + "testpartpen, the level 0 tester has been penalized 200 seconds for parting the game channel, " + "and has been logged out.", + explicit_target='bss', + ) test_char.delete() @@ -132,13 +146,18 @@ class IrcPluginTest(TestCase): # make a character so as to not disturb other tests test_char = Character.objects.register('testpubmsgpen', self.game, 'test', 'bss!bss@test_pubmsg_penalty', 'tester') - with mock.patch('idlerpg.models.Character.penalize', return_value=5) as mock_penalize: + with mock.patch('idlerpg.models.Character.penalize', return_value=22) as mock_penalize: self.plugin.handle_message_penalty(self.mock_connection, mock_event) # 22 is the len of the message mock_penalize.assert_called_with(22, "sending a privmsg to the game channel") test_char = Character.objects.get(name='testpubmsgpen') - assert test_char.time_penalized_privmsg == 5 + assert test_char.time_penalized_privmsg == 22 + self.mock_bot.reply.assert_called_once_with( + None, + "testpubmsgpen, the level 0 tester has been penalized 22 seconds for talking in the game channel.", + explicit_target='bss', + ) test_char.delete() @@ -357,7 +376,8 @@ class IrcPluginTest(TestCase): assert refetch.next_level > test_char.next_level assert refetch.status == Character.CHARACTER_STATUS_OFFLINE self.mock_bot.reply.assert_called_once_with( - mock_event, "test_logout, the level 0 tester, has been successfully logged out." + mock_event, ("test_logout, the level 0 tester, has been successfully logged out. " + "They have been penalized 20 seconds.") ) def test_register(self):