add a bot command to allow a user to register in a game

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-06-02 00:16:27 -05:00
parent f22ca875e3
commit 731e434c8d
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 112 additions and 0 deletions

View File

@ -128,6 +128,30 @@ class IdleRPG(Plugin):
character.save()
return self.bot.reply(event, f"{character}, has been successfully logged out.")
def handle_register(self, connection, event, match):
"""Register a character for a user."""
hostmask = event.source
nick = irc.client.NickMask(hostmask).nick
try:
game = Game.objects.get(active=True)
except Game.DoesNotExist:
return self.bot.reply(event, "No active game exists.")
if hostmask not in self.seen_hostmasks:
return self.bot.reply(event, f"Please join {game.channel.name} before registering.")
try:
character = Character.objects.register(match.group('name'), game, match.group('password'),
hostmask, match.group('char_class'))
except IntegrityError:
return self.bot.reply(event, "Registration failed; either you already have a character, "
"or the character name is already taken.")
self.bot.reply(None, f"{nick}'s newest character, {character.name}, the {character.character_class}, "
f"has been summoned! Next level at {character.next_level_str()}.",
explicit_target=game.channel.name)
return self.bot.reply(event, f"You have registered {character}, and been automatically logged in.")
def handle_remove(self, connection, event, match):
"""Handle the disabling of a character."""
hostmask = event.source

View File

@ -332,6 +332,94 @@ class IrcPluginTest(TestCase):
mock_event, "test_logout, the level 0 tester, has been successfully logged out."
)
def test_register(self):
"""Test the ability to register a new character."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_register'
mock_event.recursing = False
self.plugin.seen_hostmasks.add('bss!bss@bss_register')
now = datetime.datetime.fromisoformat('2024-05-17 17:00:00-00:00')
with mock.patch('django.utils.timezone.now', return_value=now):
match = re.match(IdleRPG.REGISTER_COMMAND_PATTERN, 'REGISTER reg_test test tester')
self.plugin.handle_register(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_has_calls([
mock.call(None, "bss's newest character, reg_test, the tester, "
"has been summoned! Next level at 2024-05-18 06:15:58 UTC.",
explicit_target='#test'),
mock.call(mock_event, "You have registered reg_test, the level 0 tester, "
"and been automatically logged in."),
])
def test_register_no_active(self):
"""Test the behavior of a failed registration because there's no active game."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_register'
mock_event.recursing = False
# manipulate the game temporarily
game = Game.objects.get(active=True)
game.active = False
game.save()
self.plugin.seen_hostmasks.add('bss!bss@bss_register')
now = datetime.datetime.fromisoformat('2024-05-17 17:00:00-00:00')
with mock.patch('django.utils.timezone.now', return_value=now):
match = re.match(IdleRPG.REGISTER_COMMAND_PATTERN, 'REGISTER reg_test test tester')
self.plugin.handle_register(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_has_calls([
mock.call(mock_event, "No active game exists."),
])
# undo manipulation
game.active = True
game.save()
def test_register_not_in_channel(self):
"""Test the behavior of a failed registration because the user isn't in the game channel."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_register'
mock_event.recursing = False
now = datetime.datetime.fromisoformat('2024-05-17 17:00:00-00:00')
with mock.patch('django.utils.timezone.now', return_value=now):
match = re.match(IdleRPG.REGISTER_COMMAND_PATTERN, 'REGISTER reg_test test tester')
self.plugin.handle_register(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_has_calls([
mock.call(mock_event, "Please join #test before registering.")
])
def test_register_cant_double_register(self):
"""Test the ability to register a new character fails if you try to have two."""
mock_event = mock.MagicMock()
mock_event.source = 'bss!bss@bss_dupe_register'
mock_event.recursing = False
self.plugin.seen_hostmasks.add('bss!bss@bss_dupe_register')
now = datetime.datetime.fromisoformat('2024-05-17 17:00:00-00:00')
with mock.patch('django.utils.timezone.now', return_value=now):
match = re.match(IdleRPG.REGISTER_COMMAND_PATTERN, 'REGISTER reg_test test tester')
self.plugin.handle_register(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_has_calls([
mock.call(None, "bss's newest character, reg_test, the tester, "
"has been summoned! Next level at 2024-05-18 06:15:58 UTC.",
explicit_target='#test'),
mock.call(mock_event, "You have registered reg_test, the level 0 tester, "
"and been automatically logged in."),
])
with mock.patch('django.utils.timezone.now', return_value=now):
match = re.match(IdleRPG.REGISTER_COMMAND_PATTERN, 'REGISTER reg_test2 test tester')
self.plugin.handle_register(self.mock_connection, mock_event, match)
self.mock_bot.reply.assert_has_calls([
mock.call(mock_event, "Registration failed; either you already have a character, "
"or the character name is already taken.")
])
def test_remove(self):
"""Test the remove command."""
mock_event = mock.MagicMock()