-
Notifications
You must be signed in to change notification settings - Fork 0
/
capra.py
165 lines (143 loc) · 6.76 KB
/
capra.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import asyncio
import json
import re
from io import BytesIO
from typing import Dict
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from tinydb import TinyDB, Query
class Capra(commands.Cog):
def __init__(self, bot):
self.version = "2.0.0"
self.bot: Bot = bot
self.db: TinyDB = TinyDB('./modules/databases/capra')
self.executable_path = "modules/capra/capra-singleplanner"
self.disclaimer_path = "modules/capra/disclaimer.txt"
async def run_dive_planner(self, json_input: str):
b_json_input = str.encode(json_input)
proc = await asyncio.create_subprocess_exec(
self.executable_path,
stdin=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout = "".encode()
stderr = "".encode()
try:
stdout, stderr = await asyncio.wait_for(proc.communicate(input=b_json_input), timeout=15)
except asyncio.TimeoutError:
stderr = str.encode("Timed out.")
finally:
try:
proc.kill()
except ProcessLookupError:
pass
return stdout, stderr
def check_disclaimer(self, userid: int) -> bool:
table = self.db.table('disclaimer')
target_user = Query()
return bool(table.get(target_user.userid == userid))
def check_user_profile(self, userid: int) -> Dict:
table = self.db.table('profile')
target = Query()
return table.get(target.userid == userid)
@staticmethod
def generate_json(gfl: int, gfh: int, asc: int, desc: int, bottom_sac: int, deco_sac: int, user_input: str) -> str:
segments = []
deco_gases = []
data = re.findall(r"(D:\d+, \d+, \d+/\d+)+[ ]*(G:.+)*", user_input)
for section in data:
if section[1] != '': # Deco
deco_match = re.findall(r"(G:)*(.+?)(?:,\s*|$)", section[1])
for gas_string in deco_match:
gas = gas_string[1].split('/')
deco_gases.append({
"o2": int(gas[0]),
"he": int(gas[1])
})
if section[0] != '': # Dive segment
depth, time, mix = re.findall(r"D:(\d+), (\d+), (\d+/\d+)", section[0])[0]
mix = mix.split('/')
segments.append(
{
"depth": int(depth),
"time": int(time),
"o2": int(mix[0]),
"he": int(mix[1])
}
)
return json.dumps({'gfl': gfl, 'gfh': gfh, 'asc': asc, 'desc': desc, 'segments': segments,
'bottom_sac': bottom_sac, 'deco_sac': deco_sac, 'deco_gases': deco_gases})
@commands.group(invoke_without_command=True)
async def plan(self, ctx, *, dive_plan: str): # Plan a dive
if not self.check_disclaimer(ctx.message.author.id):
await ctx.send("[!] You have not read and agreed to the disclaimer. Use help command for more information.")
return
profile = self.check_user_profile(ctx.message.author.id)
if not profile:
await ctx.send("[!] Set a dive profile before planning a dive!")
return
json_string = self.generate_json(profile["gfl"], profile["gfh"], profile["asc"], profile["desc"],
profile['bottom_sac'], profile['deco_sac'], dive_plan)
stdout, stderr = await self.run_dive_planner(json_string)
stdout = stdout.decode()
stderr = stderr.decode()
if len(stderr) != 0:
return_string = f'[!] An error occurred inside the dive planner. \n' \
f'```{stderr}```'
await ctx.send(return_string)
else:
if len(stdout) > 1000:
return_string = f'Powered by Capra: https://github.com/the-emerald/capra\n' \
f'WARNING: DIVE PLAN MAY BE INACCURATE AND MAY CONTAIN ERRORS THAT COULD LEAD ' \
f'TO INJURY OR DEATH.\n\n' \
f'{stdout}'
await ctx.send(content=f"{ctx.author.mention}", file=discord.File(BytesIO(return_string.encode()),
filename="plan.txt"))
else:
return_string = f'{ctx.author.mention}\n' \
f'Powered by Capra 🐐 \n' \
f'**Warning: Dive plan may be inaccurate and may contain errors that could lead ' \
f'to injury or death.**\n\n' \
f'```{stdout}```'
await ctx.send(return_string)
@plan.command(name="set")
async def setprofile(self, ctx, ascent_rate: int, descent_rate: int, gf_low: int, gf_high: int, bottom_sac: int,
deco_sac: int): # Set a profile
if not self.check_disclaimer(ctx.message.author.id):
await ctx.send("[!] You have not read and agreed to the disclaimer. Use help command for more information.")
return
table = self.db.table('profile')
target = Query()
table.upsert(
{'userid': ctx.message.author.id, 'asc': ascent_rate, 'desc': descent_rate, 'gfl': gf_low, 'gfh': gf_high,
'bottom_sac': bottom_sac, 'deco_sac': deco_sac},
target.userid == ctx.message.author.id
)
await ctx.send("[:ok_hand:] User profile updated.")
@plan.command(name="disclaimer")
async def disclaimer(self, ctx): # Show and read disclaimer
try:
f = open(self.disclaimer_path, mode='r')
except FileNotFoundError:
await ctx.send("[!] Disclaimer file not found. Please complain to a dev about this.")
return
disclaimer_text = f.read()
f.close()
disclaimer_message = await ctx.author.send(disclaimer_text)
def check(reaction, user):
return user == ctx.message.author and str(reaction.emoji) == '👌' \
and reaction.message.id == disclaimer_message.id
try:
await self.bot.wait_for('reaction_add', timeout=60, check=check)
except asyncio.TimeoutError:
return # Received nothing. Time out
else:
table = self.db.table('disclaimer')
target = Query()
table.upsert({
'userid': ctx.message.author.id
}, target.userid == ctx.message.author.id)
await ctx.author.send("[:ok_hand:] You have accepted the disclaimer, and can now use the dive planner.")
def setup(bot):
bot.add_cog(Capra(bot))