From e61e45794c962f9f22f88cd5a57da3cf2050f21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Mon, 6 May 2019 12:41:12 +0300 Subject: [PATCH] Start integrating gameloop.py into the bot --- bot.conf.example | 7 +++- botcmd.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++-- gameloop.py | 18 ++++----- 3 files changed, 105 insertions(+), 16 deletions(-) diff --git a/bot.conf.example b/bot.conf.example index accc678..b96e530 100644 --- a/bot.conf.example +++ b/bot.conf.example @@ -1,7 +1,10 @@ [server] host = irc.freenode.net port = 6667 -nick = o3-base +nick = taash-e-aakramak username = o3-base realname = IRC bot based on o3-base -channels = ##ingsoc +channels = ###cards + +[nickserv] +password = diff --git a/botcmd.py b/botcmd.py index 217ab41..1b9d52d 100644 --- a/botcmd.py +++ b/botcmd.py @@ -1,9 +1,86 @@ +import threading +import time + +import channel + +import gameloop + +nickserv_pass = None +irc_chan = None + +game_channel = None + +class GameLoop(threading.Thread): + def __init__(self, irc, chan, irc_chan): + self.irc = irc + self.chan = chan + self.irc_chan = irc_chan + + threading.Thread.__init__(self) + + def send(self, message): + self.irc.bot_response(self.irc_chan, message) + + def notice(self, recipient, message): + self.irc.send_raw(b'NOTICE %s :%s' % (recipient.encode(), message.encode())) + + def get_event(self): + event = self.chan.recv() + print(event)#debg + return event + + def run(self): + try: + gameloop.game(self.send, self.notice, self.get_event) + except Exception as err: + self.send('Crash! (%s, %s)' % (type(err), repr(err))) + raise err #debg + finally: + self.chan.close() + +def start_gameloop(irc): + global game_channel, irc_chan + + if game_channel is not None: + return + + chan = channel.Channel() + GameLoop(irc, chan, irc_chan).start() + + game_channel = chan + +def stop_gameloop(): + global game_channel + + if game_channel is None: + return + + game_channel.send((gameloop.events.quit,)) + + game_channel = None + +def send_event(event): + global game_channel + + game_channel.send(event) + +def parse_command(message, nick): + events = gameloop.events + + if message == '!start': + send_event((events.start, nick)) + + elif message == '!kill': + send_event((events.kill,)) + # initialize(*, config) # Called to initialize the IRC bot # Runs before even logger is brought up, and blocks further bringup until it's done # config is a configpatser.ConfigParser object containig contents of bot.conf def initialize(*, config): - ... + global nickserv_pass, irc_chan + nickserv_pass = config['nickserv']['password'] + irc_chan = config['server']['channels'].split()[0].encode() # on_connect(*, irc) # Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else @@ -11,14 +88,22 @@ def initialize(*, config): # Blocks the bot until it's done, including PING/PONG handling # irc is the IRC API object def on_connect(*, irc): - ... + global nickserv_pass + + """#debg + irc.msg(b'nickserv', b'IDENTIFY ' + nickserv_pass.encode()) + time.sleep(20) # One day I will do this correctly. Today is not the day + """#debg + + stop_gameloop() + start_gameloop(irc) # on_quit(*, irc) # Called just before IRC bot sends QUIT # Blocks the bot until it's done, including PING/PONG handling # irc is the IRC API object def on_quit(*, irc): - ... + stop_gameloop() # handle_message(*, prefix, message, nick, channel, irc) # Called for PRIVMSGs. @@ -29,7 +114,10 @@ def on_quit(*, irc): # irc is the IRC API object # All strings are bytestrings def handle_message(*, prefix, message, nick, channel, irc): - ... + global irc_chan + + if channel == irc_chan: + parse_command(message.decode(), nick.decode()) # handle_nonmessage(*, prefix, command, arguments, irc) # Called for all other commands than PINGs and PRIVMSGs. diff --git a/gameloop.py b/gameloop.py index 157a488..ac620d6 100644 --- a/gameloop.py +++ b/gameloop.py @@ -88,15 +88,15 @@ def get_event(): else: print('?') +def send(text): + print(text) + +def notice(nick, text): + print('\t', nick, text) + class Error: pass -def game(): - def send(text): - print(text) - - def notice(nick, text): - print('\t', nick, text) - +def game(send, notice, get_event): def error(message): send('Error: %s' % message) @@ -702,7 +702,5 @@ def game(): while state != quit: state = state() - send('Quiting') - if __name__ == '__main__': - game() + game(send, notice, get_event)