mirror of
https://git.sdf.org/tamservo/robottas.git
synced 2025-11-08 07:03:47 -05:00
Compare commits
5 Commits
c3747ee90e
...
4d602b7094
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d602b7094 | ||
|
|
dfd0f054a9 | ||
|
|
cfa71c5b23 | ||
|
|
c4955c4298 | ||
|
|
66eee52bb5 |
2
RobottasSignalr.py
Normal file → Executable file
2
RobottasSignalr.py
Normal file → Executable file
@@ -105,7 +105,7 @@ class SignalRClient:
|
||||
self._output_file.write(msg + '\n')
|
||||
self._output_file.flush()
|
||||
"""
|
||||
print(msg)
|
||||
#print(msg)
|
||||
con = sqlite3.connect('messages.db')
|
||||
cur = con.cursor()
|
||||
cur.execute("insert into messages (message) values(?)", (msg,))
|
||||
|
||||
2
requirements.txt
Normal file → Executable file
2
requirements.txt
Normal file → Executable file
@@ -1,3 +1,5 @@
|
||||
beautifulsoup4==4.9.3
|
||||
discord.py==2.2.3
|
||||
fastf1==3.0.3
|
||||
jdata==0.5.3
|
||||
Requests==2.31.0
|
||||
|
||||
156
robottas.py
156
robottas.py
@@ -5,6 +5,8 @@ from asyncio.subprocess import PIPE, STDOUT
|
||||
import collections.abc
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import sqlite3
|
||||
from subprocess import Popen
|
||||
import time
|
||||
@@ -42,7 +44,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
async def send_message(self, message):
|
||||
print(f"in send_message {message} {self.channel}")
|
||||
if self.channel is None:
|
||||
return
|
||||
|
||||
@@ -50,17 +51,13 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
def send_delay_message(self, message):
|
||||
print("in send_delay_message")
|
||||
self.message_queue.append((time.time(), message))
|
||||
print(f"adding message: {message} at {time.time()}")
|
||||
|
||||
|
||||
async def process_delay_messages(self):
|
||||
print("in process_delay_queue")
|
||||
while len(self.message_queue) > 0 and \
|
||||
self.message_queue[0][0] < time.time() - self.delay:
|
||||
|
||||
print(f"removing at {time.time()}")
|
||||
message = self.message_queue.pop(0)[1]
|
||||
await self.send_message(message)
|
||||
await asyncio.sleep(1)
|
||||
@@ -156,7 +153,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
async def load_lap_data(self, data):
|
||||
print(f"load_lap_data: {str(data)}")
|
||||
if "CurrentLap" in data.keys():
|
||||
current_lap = data["CurrentLap"]
|
||||
|
||||
@@ -174,7 +170,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
def load_podium_data(self, data):
|
||||
print("in load_podium_data")
|
||||
if "Lines" in data.keys():
|
||||
for position in data["Lines"].keys():
|
||||
if 'RacingNumber' in data["Lines"][position].keys():
|
||||
@@ -207,7 +202,7 @@ class Robottas(commands.Bot):
|
||||
return message
|
||||
|
||||
except:
|
||||
print("Error in sending podium message.")
|
||||
pass
|
||||
|
||||
return "I don't know the podium yet :("
|
||||
|
||||
@@ -253,7 +248,6 @@ class Robottas(commands.Bot):
|
||||
report = None
|
||||
while (len(session_data) > 0 or len(status_data) > 0) and self.is_reporting:
|
||||
# If only one of them has data, use that one
|
||||
print("starting loop")
|
||||
if len(session_data) == 0:
|
||||
event = status_data.pop(0)
|
||||
event_type = "STATUS"
|
||||
@@ -277,7 +271,6 @@ class Robottas(commands.Bot):
|
||||
report = None
|
||||
|
||||
if event_type == "SESSION":
|
||||
print("Session event")
|
||||
# Get the lap from the event
|
||||
cur_lap = event["Lap"]
|
||||
|
||||
@@ -294,7 +287,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
# Must be a status event
|
||||
else:
|
||||
print("Status event")
|
||||
key = None
|
||||
if "TrackStatus" in event.keys():
|
||||
key = "TrackStatus"
|
||||
@@ -332,24 +324,19 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
def load_timing_stats_data(self, data):
|
||||
print("in timing stats")
|
||||
if "Lines" in data.keys():
|
||||
lines = data["Lines"]
|
||||
for driver_num in lines.keys():
|
||||
line = lines[driver_num]
|
||||
print(f"driver_num {driver_num}")
|
||||
if "PersonalBestLapTime" in line.keys():
|
||||
position = -1
|
||||
try:
|
||||
position = line["PersonalBestLapTime"]["Position"]
|
||||
print(f"got position {position}")
|
||||
except:
|
||||
pass
|
||||
|
||||
if position == 1:
|
||||
print(f"setting fastest_lap {driver_num}")
|
||||
self.fastest_lap = self.driver_dict[driver_num]
|
||||
print(f"flap {driver_num} {self.fastest_lap}")
|
||||
return
|
||||
|
||||
|
||||
@@ -382,7 +369,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
|
||||
def load_initial(self, message):
|
||||
print( f"in load_initial: {message['R'].keys()}" )
|
||||
# Load podium data
|
||||
if 'R' in message.keys():
|
||||
if 'TopThree' in message['R'].keys():
|
||||
@@ -405,12 +391,10 @@ class Robottas(commands.Bot):
|
||||
if 'LapCount' in message['R'].keys():
|
||||
if 'TotalLaps' in message['R']['LapCount'].keys():
|
||||
self.total_laps = int(message['R']['LapCount']['TotalLaps'])
|
||||
print(f"self.total_laps: {self.total_laps}")
|
||||
|
||||
|
||||
# Load weather data
|
||||
if 'WeatherData' in message['R'].keys():
|
||||
print( 'WeatherData in keys' )
|
||||
weather_obj = message['R']['WeatherData']
|
||||
weather_text = "Track Weather Report\n"
|
||||
for k in weather_obj.keys():
|
||||
@@ -421,7 +405,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
# Load fastest lap data
|
||||
if 'TimingStats' in message['R'].keys():
|
||||
print( 'Flap in keys' )
|
||||
flap_obj = message['R']['TimingStats']
|
||||
self.load_timing_stats_data(flap_obj)
|
||||
|
||||
@@ -429,8 +412,6 @@ class Robottas(commands.Bot):
|
||||
async def process_message(self, message):
|
||||
try:
|
||||
if isinstance(message, collections.abc.Sequence):
|
||||
print("process_message - in isinstance")
|
||||
|
||||
if message[0] == 'Heartbeat':
|
||||
return
|
||||
|
||||
@@ -453,7 +434,7 @@ class Robottas(commands.Bot):
|
||||
self.load_timing_stats_data(message[1])
|
||||
|
||||
else:
|
||||
print(f"Not sure how to handle message:{message[0]}")
|
||||
pass
|
||||
|
||||
# Check to see if this is the initial "R" record from the response
|
||||
elif "R" in message.keys():
|
||||
@@ -461,7 +442,7 @@ class Robottas(commands.Bot):
|
||||
self.load_initial(message)
|
||||
|
||||
except Exception as e:
|
||||
print(f"process_message error {e}\n\n")
|
||||
pass
|
||||
|
||||
|
||||
def get_messages_from_db(self):
|
||||
@@ -483,7 +464,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
return messages
|
||||
except:
|
||||
print("db error... continuing")
|
||||
return []
|
||||
|
||||
|
||||
@@ -513,7 +493,6 @@ class Robottas(commands.Bot):
|
||||
|
||||
while self.is_reporting:
|
||||
# Do processing
|
||||
print("reporting loop")
|
||||
|
||||
# process any new messages in the db
|
||||
messages = self.get_messages_from_db()
|
||||
@@ -523,7 +502,7 @@ class Robottas(commands.Bot):
|
||||
|
||||
await asyncio.sleep(3)
|
||||
except:
|
||||
print(f"problem with messages")
|
||||
pass
|
||||
|
||||
# process any messages in the delay queue
|
||||
await self.process_delay_messages()
|
||||
@@ -546,7 +525,6 @@ class Robottas(commands.Bot):
|
||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
||||
command_txt = os.path.join(dir_path, self.collector_command)
|
||||
command_txt += self.collector_params
|
||||
print(f"command_txt: {command_txt}")
|
||||
self.collector_proc = Popen(command_txt.split())
|
||||
|
||||
|
||||
@@ -556,7 +534,14 @@ class Robottas(commands.Bot):
|
||||
if self.collector_proc != None:
|
||||
self.collector_proc.kill()
|
||||
except:
|
||||
print("Tried to kill collection process")
|
||||
pass
|
||||
|
||||
|
||||
def decode_watched(self, w):
|
||||
if w == 0:
|
||||
return 'Not Watched Yet'
|
||||
else:
|
||||
return 'Watched Already'
|
||||
|
||||
|
||||
def __init__(self):
|
||||
@@ -692,6 +677,10 @@ class Robottas(commands.Bot):
|
||||
|
||||
# Test file
|
||||
self.test_file = "test.json"
|
||||
|
||||
# Race db patterns
|
||||
self.loc_pattern = re.compile('[\w\s\-\d]+')
|
||||
self.yr_pattern = re.compile('\d{4}')
|
||||
|
||||
### END FastF1 adapted section ###
|
||||
|
||||
@@ -811,7 +800,7 @@ class Robottas(commands.Bot):
|
||||
@self.command()
|
||||
async def flap(ctx):
|
||||
if self.fastest_lap != '':
|
||||
await ctx.send( self.fastest_lap + self.flag_dict['FLAP'] )
|
||||
await ctx.send( self.flag_dict['FLAP'] + self.fastest_lap )
|
||||
else:
|
||||
await ctx.send( "No " + self.flag_dict['FLAP'] + " yet." )
|
||||
|
||||
@@ -840,6 +829,113 @@ class Robottas(commands.Bot):
|
||||
await ctx.send(file = df)
|
||||
|
||||
|
||||
@self.command()
|
||||
async def danger(ctx):
|
||||
await ctx.send("That's some dangerous driving! " + self.name_dict["HAM"])
|
||||
|
||||
|
||||
@self.command()
|
||||
async def dotd(ctx):
|
||||
await ctx.send( "I don't know, probably " + self.name_dict["ALB"] )
|
||||
|
||||
|
||||
@self.command()
|
||||
async def flip(ctx):
|
||||
await ctx.send( random.choice(["Heads","Tails"]))
|
||||
|
||||
|
||||
@self.command()
|
||||
async def gp2(ctx):
|
||||
await ctx.send("GP2 engine! GP2! ARGHHH! " + self.name_dict["ALO"])
|
||||
|
||||
|
||||
@self.command()
|
||||
async def quote(ctx):
|
||||
try:
|
||||
con = sqlite3.connect('races.db')
|
||||
cur = con.cursor()
|
||||
for row in cur.execute('select * from quotes order by Random() limit 1'):
|
||||
|
||||
message = row[0] + " -- " + row[1] + " " + row[2]
|
||||
await ctx.send(message)
|
||||
break # There should only be one row anyway
|
||||
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
except:
|
||||
ctx.send("Can't think of a quote for some reason...")
|
||||
|
||||
|
||||
@self.command()
|
||||
@commands.has_permissions(administrator=True)
|
||||
async def race_watched(ctx, loc, yr):
|
||||
if re.match(self.loc_pattern, loc) and \
|
||||
re.match(self.yr_pattern, yr):
|
||||
|
||||
try:
|
||||
con = sqlite3.connect('races.db')
|
||||
cur = con.cursor()
|
||||
cur.execute('update races ' +
|
||||
'set watched = 1 ' +
|
||||
'where location = ? ' +
|
||||
'year = ?', (loc,yr))
|
||||
|
||||
con.commit()
|
||||
cur.close()
|
||||
con.close()
|
||||
await ctx.send(f"{loc} {yr} marked as watched.")
|
||||
|
||||
except:
|
||||
await ctx.send(f"Couldn't mark {loc} {yr} as watched for some reason.")
|
||||
|
||||
|
||||
@self.command()
|
||||
async def rand_race(ctx):
|
||||
try:
|
||||
con = sqlite3.connect('races.db')
|
||||
cur = con.cursor()
|
||||
for row in cur.execute('select * from races ' +
|
||||
'order by Random() ' +
|
||||
'limit 1'):
|
||||
|
||||
watched = self.decode_watched(row[2])
|
||||
await ctx.send(f"Location: {row[0]} Year: {row[1]} {watched}")
|
||||
break
|
||||
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
except:
|
||||
ctx.send("Can't find a random race for some reason.")
|
||||
|
||||
|
||||
@self.command()
|
||||
async def rand_race_new(ctx):
|
||||
try:
|
||||
con = sqlite3.connect('races.db')
|
||||
cur = con.cursor()
|
||||
for row in cur.execute('select * from races ' +
|
||||
'where watched = 0 ' +
|
||||
'order by Random() ' +
|
||||
'limit 1'):
|
||||
|
||||
watched = self.decode_watched(row[2])
|
||||
await ctx.send(f"Location: {row[0]} Year: {row[1]}")
|
||||
break
|
||||
|
||||
cur.close()
|
||||
con.close()
|
||||
|
||||
except:
|
||||
ctx.send("Can't pick a race for some reason.")
|
||||
|
||||
|
||||
@self.command()
|
||||
async def tyres(ctx):
|
||||
await ctx.send("Bono, my tyres are gone " + self.name_dict["HAM"])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
rb = Robottas()
|
||||
rb.run_robottas()
|
||||
|
||||
Reference in New Issue
Block a user