From 56b554f6f1d069ac498390a5357948cb75397707 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 2 Jun 2024 00:19:06 -0500 Subject: [PATCH] add scaffolding to load the bot and lightly test it better tests might be coming some time in the future, but I have the whole IRC framework mocked out at the moment Signed-off-by: Brian S. Stephan --- idlerpg/ircplugin.py | 64 +++++++++++++++++++++++++++++++++ tests/test_idlerpg_ircplugin.py | 12 +++++++ 2 files changed, 76 insertions(+) diff --git a/idlerpg/ircplugin.py b/idlerpg/ircplugin.py index 35439cd..69af385 100644 --- a/idlerpg/ircplugin.py +++ b/idlerpg/ircplugin.py @@ -32,6 +32,70 @@ class IdleRPG(Plugin): self.seen_hostmasks = set() super().__init__(bot, connection, event) + def start(self): + """Set up the handlers and start processing game state.""" + self.connection.add_global_handler('join', self.handle_join, -20) + self.connection.add_global_handler('kick', self.handle_kick_penalty, -20) + self.connection.add_global_handler('nick', self.handle_nick_penalty, -20) + self.connection.add_global_handler('part', self.handle_part_penalty, -20) + self.connection.add_global_handler('pubmsg', self.handle_message_penalty, -20) + self.connection.add_global_handler('pubnotice', self.handle_message_penalty, -20) + self.connection.add_global_handler('quit', self.handle_quit_penalty, -20) + self.connection.add_global_handler('whoreply', self.handle_whoreply_response, -20) + + self.connection.reactor.add_global_regex_handler(['privmsg'], self.LOGIN_COMMAND_PATTERN, + self.handle_login, -20) + self.connection.reactor.add_global_regex_handler(['privmsg'], self.LOGOUT_COMMAND_PATTERN, + self.handle_logout, -20) + self.connection.reactor.add_global_regex_handler(['privmsg'], self.REGISTER_COMMAND_PATTERN, + self.handle_register, -20) + self.connection.reactor.add_global_regex_handler(['privmsg'], self.REMOVEME_COMMAND_PATTERN, + self.handle_remove, -20) + self.connection.reactor.add_global_regex_handler(['privmsg'], self.STATUS_COMMAND_PATTERN, + self.handle_status, -20) + + # get a list of who is in the channel and auto-log them in + logger.info("Automatically logging in users already in the channel(s).") + for game in Game.objects.filter(active=True): + self.connection.who(game.channel.name) + + # start the thread to check for level ups + self.check_for_level_ups = True + level_t = threading.Thread(target=self.level_thread) + level_t.daemon = True + level_t.start() + logger.info("Started up the level up checker thread.") + + super(IdleRPG, self).start() + + def stop(self): + """Tear down handlers, stop checking for new levels, and return to a stable game state.""" + self.check_for_level_ups = False + + self.connection.remove_global_handler('join', self.handle_join) + self.connection.remove_global_handler('kick', self.handle_kick_penalty) + self.connection.remove_global_handler('nick', self.handle_nick_penalty) + self.connection.remove_global_handler('part', self.handle_part_penalty) + self.connection.remove_global_handler('pubmsg', self.handle_message_penalty) + self.connection.remove_global_handler('pubnotice', self.handle_message_penalty) + self.connection.remove_global_handler('quit', self.handle_quit_penalty) + self.connection.remove_global_handler('whoreply', self.handle_whoreply_response) + + self.connection.reactor.remove_global_regex_handler(['privmsg'], self.handle_login) + self.connection.reactor.remove_global_regex_handler(['privmsg'], self.handle_logout) + self.connection.reactor.remove_global_regex_handler(['privmsg'], self.handle_register) + self.connection.reactor.remove_global_regex_handler(['privmsg'], self.handle_remove) + self.connection.reactor.remove_global_regex_handler(['privmsg'], self.handle_status) + + # log everyone out (no penalty) + Character.objects.log_out_everyone() + + # unset the hostmask tracking + self.seen_hostmasks = set() + logger.info("Reset the set of seen hostmasks.") + + super(IdleRPG, self).stop() + def handle_join(self, connection, event): """Track a character as online if their player joins the game channel.""" self.seen_hostmasks.add(event.source) diff --git a/tests/test_idlerpg_ircplugin.py b/tests/test_idlerpg_ircplugin.py index c6cb12f..71ec6e6 100644 --- a/tests/test_idlerpg_ircplugin.py +++ b/tests/test_idlerpg_ircplugin.py @@ -32,6 +32,18 @@ class IrcPluginTest(TestCase): self.plugin = IdleRPG(self.mock_bot, self.mock_connection, mock.MagicMock()) self.game = Game.objects.get(pk=1) + def test_start_stop(self): + """Test that handlers are registered, roughly, as expected.""" + self.plugin.start() + self.plugin.stop() + + assert self.mock_connection.add_global_handler.call_count == 8 + assert (self.mock_connection.add_global_handler.call_count == + self.mock_connection.remove_global_handler.call_count) + assert self.mock_connection.reactor.add_global_regex_handler.call_count == 5 + assert (self.mock_connection.reactor.add_global_regex_handler.call_count == + self.mock_connection.reactor.remove_global_regex_handler.call_count) + def test_kick_penalty(self): """Test that if a character is kicked from the game channel, they get penalized.""" mock_event = mock.MagicMock()