Skip to content

Commit

Permalink
refactoring & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rayer456 committed Oct 22, 2023
1 parent 96f62b3 commit 5d940c5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 77 deletions.
73 changes: 73 additions & 0 deletions AutomaticPrediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import socket
from time import sleep
from datetime import timedelta
import json


class Launcher():
def __init__(self):
self.get_auto_predictions()

def get_auto_predictions(self):
with open('predictions/predictions.json', 'r') as file:
data = json.load(file)

# auto start based on current split
self.auto_predictions = [
{
"name": p['name'],
"split_name": p['auto_predict']['split_name']
}
for p in data['predictions']
if p['auto_predict']['auto_start']
]


def launch(self, q, CFG, LOG):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((CFG['livesplit']['HOST'], CFG['livesplit']['PORT']))
sock.settimeout(0.5)
LOG.logger.info("Connected to Livesplit Server")

while True:
try:
sleep(1.5)
sock.send(b"getcurrentsplitname\r\n")
split_name = sock.recv(1024).decode().rstrip() #remove \r\n

#sock.send(b"getcurrenttime\r\n")
#raw_time = sock.recv(1024).decode().rstrip()[:-3] #no ms
#current_time = convert_to_hms(raw_time)
#print(f"{split_name} : {current_time}")

for pred in self.auto_predictions:
if pred['split_name'].lower() == split_name.lower():
q.put(AutomaticPrediction(pred['name'])) #send to other process
self.auto_predictions.remove(pred)
LOG.logger.info(f"Auto started at {split_name}")

#do things based on name and/or time
except TimeoutError: #inactive timer
self.get_auto_predictions()
continue
except ConnectionRefusedError:
LOG.logger.warning("Predictions will not start automatically by split")
LOG.logger.warning("Start Livesplit Server and restart the bot")

def convert_to_hms(self, raw_time):
hms_list = raw_time.split(':') #[1,22,33] or [18,16]
hours_in_seconds = 0
minutes_in_seconds = int(hms_list[-2]) * 60
seconds = int(hms_list[-1])

if len(hms_list) == 3: #hh:mm:ss
hours_in_seconds = int(hms_list[0]) * 3600

total_seconds = hours_in_seconds + minutes_in_seconds + seconds
return str(timedelta(seconds=total_seconds))


class AutomaticPrediction():
def __init__(self, split):
self.split = split
17 changes: 9 additions & 8 deletions electrobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import global_hotkeys as hkeys

from config import config_file as CFG
import predictions
import AutomaticPrediction
import logger as LOG


Expand Down Expand Up @@ -235,9 +235,9 @@ def read_data():
if q.qsize() != 0: #from livesplit or eventsub
data = q.get() #collision?

if type(data) == str: #livesplit
LOG.logger.debug(f"in queue from livesplit: {data}")
create_prediction(data)
if type(data) == AutomaticPrediction.AutomaticPrediction: #livesplit
LOG.logger.debug(f"in queue from livesplit: {data.split}")
create_prediction(data.split)
else: #eventsub
e_status = data['status'] #.end resolved/canceled
outcomes = data['outcomes']
Expand Down Expand Up @@ -649,8 +649,9 @@ def eventsub(q):
asyncio.run(event_handler(q))


def auto_predictions(q, CFG):
predictions.main(q, CFG, LOG)
def livesplit_predictions(q, CFG):
auto_prediction_launcher = AutomaticPrediction.Launcher()
auto_prediction_launcher.launch(q, CFG, LOG)


if __name__ == "__main__":
Expand All @@ -667,7 +668,7 @@ def auto_predictions(q, CFG):
event_process = mp.Process(target=eventsub, daemon=True, args=(q,))
event_process.start()

prediction_process = mp.Process(target=auto_predictions, daemon=True, args=(q,CFG))
prediction_process.start()
livesplit_prediction_process = mp.Process(target=livesplit_predictions, daemon=True, args=(q,CFG))
livesplit_prediction_process.start()

read_data()
69 changes: 0 additions & 69 deletions predictions.py

This file was deleted.

0 comments on commit 5d940c5

Please sign in to comment.