Compare commits

...

2 Commits

Author SHA1 Message Date
2372363898
the kick event source is the kicker, so find the kickee by nick
Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
2024-07-15 18:44:58 -05:00
d9a66d0c74
message the character's player when penalties are applied
Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
2024-07-15 18:44:20 -05:00
2 changed files with 68 additions and 20 deletions

View File

@ -122,17 +122,28 @@ class IdleRPG(Plugin):
def handle_kick_penalty(self, connection, event):
"""Penalize characters for their user getting kicked from the channel."""
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, 250,
logger.debug("kick: %s", event)
# somewhat hacky attempt to find the hostmask for the kicked nick
kicked_nick_hostmask_prefix = f'{event.arguments[0]}!'
try:
source = [x for x in self.seen_hostmasks if x.startswith(kicked_nick_hostmask_prefix)][0]
except IndexError:
return
self.seen_hostmasks.discard(source)
logger.info("Removed %s from the set of seen hostmasks.", source)
return self._handle_generic_penalty_and_logout(source, event.target, 250,
"getting kicked from the game channel",
'time_penalized_kicked')
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 +166,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 +223,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 +295,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
@ -47,21 +45,30 @@ class IrcPluginTest(TestCase):
def test_kick_penalty(self):
"""Test that if a character is kicked from the game channel, they get penalized."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@test_kick_penalty'
mock_event.source = 'admin!admin@the_kicker'
mock_event.target = '#test'
mock_event.arguments = ['bss', '']
mock_event.recursing = False
# make a character so as to not disturb other tests
test_char = Character.objects.register('testkickpen', self.game, 'test',
'bss!bss@test_kick_penalty', 'tester')
with mock.patch('idlerpg.models.Character.penalize', return_value=5) as mock_penalize:
self.plugin.seen_hostmasks.add('bss!bss@test_kick_penalty')
with mock.patch('idlerpg.models.Character.penalize', return_value=250) as mock_penalize:
with mock.patch('idlerpg.models.Character.log_out') as mock_log_out:
self.plugin.handle_kick_penalty(self.mock_connection, mock_event)
mock_penalize.assert_called_with(250, "getting kicked from the game channel")
mock_log_out.assert_called()
test_char = Character.objects.get(name='testkickpen')
assert test_char.time_penalized_kicked == 5
assert test_char.time_penalized_kicked == 250
assert 'bss!bss@test_kick_penalty' not in self.plugin.seen_hostmasks
self.mock_bot.reply.assert_called_once_with(
None,
"testkickpen, the level 0 tester has been penalized 250 seconds for getting kicked from the game channel, "
"and has been logged out.",
explicit_target='bss',
)
test_char.delete()
@ -89,14 +96,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 +125,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 +155,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 +385,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):