message the character's player when penalties are applied

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-07-15 17:56:00 -05:00
parent 3f2c9369bd
commit d9a66d0c74
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 44 additions and 14 deletions

View File

@ -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)

View File

@ -4,14 +4,12 @@ SPDX-FileCopyrightText: © 2024 Brian S. Stephan <bss@incorporeal.org>
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):