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]
timestamp = 1680134005.6927342
current_tick = 630
[Hendrik]
timestamp = 1680149214.3503995
current_tick = 350

34
matrix_pygmalion_bot/core.py

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

26
matrix_pygmalion_bot/helpers.py

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

Loading…
Cancel
Save