Start integrating gameloop.py into the bot

This commit is contained in:
Juhani Krekelä 2019-05-06 12:41:12 +03:00
parent c09ddf4bda
commit e61e45794c
3 changed files with 105 additions and 16 deletions

View File

@ -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 =

View File

@ -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.

View File

@ -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)