-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexecuteQueue.py
More file actions
76 lines (60 loc) · 2.6 KB
/
executeQueue.py
File metadata and controls
76 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import asyncio
import random
import threading
import time
import discord
from sqlalchemy.orm import sessionmaker
from classes import World
from database import engine
world_generation_interval = 3
class ExecuteQueue(threading.Thread):
from database import engine
world_queue = []
def __init__(self, client):
super().__init__()
self.client = client
@classmethod
def add_to_queue(self, queue_world):
self.world_queue.append(queue_world)
async def notify_user(self, user_id, embed, session):
# notify user that the world is finished
user = await self.client.fetch_user(user_id)
try:
await user.send(embed=embed)
except discord.Forbidden:
pass
def run(self, *args, **kwargs):
while True:
time.sleep(world_generation_interval)
if len(self.world_queue) > 0:
# create a new session for this request
Session = sessionmaker(bind=engine)
session = Session()
try:
# choose a world to generate
print("Generating new world from queue..")
queue_item = random.choice(self.world_queue)
# add new world to db
world = World(name=queue_item.world_name, owner=queue_item.user_id)
session.add(world)
session.commit()
# start generate process
from commands.generate import generate_world
generate_world(session=session, world_id=world.id)
world.spawn_player(session=session, user_id=queue_item.user_id)
self.world_queue.remove(queue_item)
session.commit()
# increment world count for bot status
self.client.world_count += 1
embed = discord.Embed(title="World Generation",
description="Your world has been generated!\nYou can now use `/play` or `/addPlayer`.",
color=discord.Color.green())
asyncio.run_coroutine_threadsafe(self.notify_user(user_id=queue_item.user_id, embed=embed, session=session),
self.client.loop)
except Exception as e:
# rollback the transaction if an error occurs
session.rollback()
raise e
finally:
# close the session
session.close()