Browse Source

event loop

master
Hendrik Langer 2 years ago
parent
commit
4f9e374ec2
  1. 4
      bot.conf2
  2. 34
      matrix_pygmalion_bot/core.py
  3. 26
      matrix_pygmalion_bot/helpers.py

4
bot.conf2

@ -1,6 +1,6 @@
[Julia] [Julia]
timestamp = 1680134005.6927342 current_tick = 630
[Hendrik] [Hendrik]
timestamp = 1680149214.3503995 current_tick = 350

34
matrix_pygmalion_bot/core.py

@ -86,7 +86,7 @@ class Callbacks(object):
return return
elif event.body.startswith('!begin'): elif event.body.startswith('!begin'):
self.bot.chat_history.room(room.display_name).clear() self.bot.chat_history.room(room.display_name).clear()
self.bot.timestamp = time.time() self.bot.tick = 0
await self.bot.write_conf2(self.bot.name) await self.bot.write_conf2(self.bot.name)
await self.bot.send_message(self.client, room.room_id, self.bot.greeting) await self.bot.send_message(self.client, room.room_id, self.bot.greeting)
return return
@ -179,7 +179,6 @@ class ChatBot(object):
self.callbacks = None self.callbacks = None
self.config = None self.config = None
self.not_synced = True self.not_synced = True
self.timestamp = time.time()
self.owner = None self.owner = None
self.translate = None self.translate = None
@ -189,6 +188,7 @@ class ChatBot(object):
self.scenario = None self.scenario = None
self.greeting = None self.greeting = None
self.events = [] self.events = []
self.tick = 0
self.chat_history = None self.chat_history = None
if STORE_PATH and not os.path.isdir(STORE_PATH): if STORE_PATH and not os.path.isdir(STORE_PATH):
@ -201,18 +201,24 @@ class ChatBot(object):
self.greeting = greeting self.greeting = greeting
async def event_loop(self): async def event_loop(self):
while True: try:
await asyncio.sleep(60) while True:
#print(time.time()) await asyncio.sleep(60)
for event in self.events: for event in self.events:
event.loop(self) event.loop(self, self.tick)
self.tick += 1
if self.tick % 10 == 0:
await self.write_conf2(self.name)
finally:
await self.write_conf2(self.name)
async def add_event(self, event_string): async def add_event(self, event_string):
items = event_string.split(',', 3) items = event_string.split(',', 4)
for item in items: for item in items:
item = item.strip() item = item.strip()
event = Event(float(items[0]), float(items[1]), float(items[2]), items[3]) event = Event(int(items[0]), int(items[1]), float(items[2]), int(items[3]), items[4].lstrip())
self.events.append(event) self.events.append(event)
logger.info("event added to event_loop")
pass pass
async def login(self): async def login(self):
@ -246,16 +252,14 @@ class ChatBot(object):
async def read_conf2(self, section): async def read_conf2(self, section):
config2 = configparser.ConfigParser() config2 = configparser.ConfigParser()
config2.read('bot.conf2') config2.read('bot.conf2')
if config2.has_section(section) and config2.has_option(section, 'timestamp'): if config2.has_section(section) and config2.has_option(section, 'current_tick'):
self.timestamp = float(config2[section]['timestamp']) self.tick = int(config2[section]['current_tick'])
else:
self.timestamp = time.time()
async def write_conf2(self, section): async def write_conf2(self, section):
config2 = configparser.ConfigParser() config2 = configparser.ConfigParser()
config2.read('bot.conf2') config2.read('bot.conf2')
config2[section] = {} config2[section] = {}
config2[section]['timestamp'] = str(self.timestamp) config2[section]['current_tick'] = str(self.tick)
with open('bot.conf2', 'w') as configfile: with open('bot.conf2', 'w') as configfile:
config2.write(configfile) config2.write(configfile)
@ -345,7 +349,7 @@ async def main() -> None:
if config.has_option('DEFAULT', 'runpod_api_key'): if config.has_option('DEFAULT', 'runpod_api_key'):
bot.runpod_api_key = config['DEFAULT']['runpod_api_key'] bot.runpod_api_key = config['DEFAULT']['runpod_api_key']
await bot.read_conf2(section) await bot.read_conf2(section)
await bot.write_conf2(section) #await bot.write_conf2(section)
bots.append(bot) bots.append(bot)
await bot.login() await bot.login()
print("gather") print("gather")

26
matrix_pygmalion_bot/helpers.py

@ -1,21 +1,23 @@
import time import time
class Event: class Event:
def __init__(self, time_start, time_stop, chance, command): def __init__(self, tick_start, tick_stop, chance, repeat, command):
self.time_start = time_start self.tick_start = tick_start
self.time_stop = time_stop self.tick_stop = tick_stop
self.chance = chance self.chance = chance
self.repeat = repeat
self.command = command self.command = command
self.executed = 0 self.executed = 0
def __str__(self): def __str__(self):
return str("Event starting at timestamp {}".format(self.time_start)) return str("Event starting at time {}".format(self.tick_start))
def loop(self, bot): def loop(self, bot, tick):
if time.time() - bot.timestamp >= self.time_start: if tick >= self.tick_start:
if time.time() - bot.timestamp < self.time_stop: if tick < self.tick_stop:
self.execute() self.execute(bot, tick)
elif (self.time_stop == 0 or self.time_stop == self.time_start) and self.executed == 0: elif (self.tick_stop == 0 or self.tick_stop == self.tick_start) and self.executed == 0:
self.execute() self.execute(bot, tick)
def execute(self, bot): def execute(self, bot, tick):
print("event executed for " + bot.name + ". current tick: " + str(tick) + " event: " + str(self.command))
if self.command.startswith('printtime'): if self.command.startswith('printtime'):
print(time.time()) print(time.time()//1000)
self.executed += 1 self.executed += 1

Loading…
Cancel
Save