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 <bss@incorporeal.org>
This commit is contained in:
parent
731e434c8d
commit
56b554f6f1
@ -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)
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user