Skip to content

Commit

Permalink
Progression on the settings ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Philaeux committed Aug 4, 2023
1 parent 10fea7b commit 70e2b22
Show file tree
Hide file tree
Showing 10 changed files with 3,619 additions and 2,885 deletions.
45 changes: 33 additions & 12 deletions src/dota_notes/app_dota.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from dota2.msg import EDOTAGCMsg

import logging

from dota_notes.data.messages import Message, MessageType, MessageServerIdResponse, MessageConSatus, \
MessageServerIdRequest

logging.basicConfig(format='[%(asctime)s] %(levelname)s %(name)s: %(message)s', level=logging.DEBUG)


Expand All @@ -12,40 +16,57 @@ def dota_process(username, password, match_id_in_queue, server_id_out_queue):


class DotaApp:
def __init__(self, username, password, user_steam_id_in_queue, server_id_out_queue):
def __init__(self, username, password, message_queue_dota, message_queue_qt):
self.username = username
self.password = password
self.user_steam_id_in_queue = user_steam_id_in_queue
self.server_id_out_queue = server_id_out_queue
self.message_queue_dota = message_queue_dota
self.message_queue_qt = message_queue_qt
self.message_buffer_queue = []

self.steam = SteamClient()
self.dota = Dota2Client(self.steam)
self.dota_ready = False
self.match_id = 0
self.currentPage = 0
self.keep_running = True

self.steam.on('logged_on', self.start_dota)
self.steam.on('logged_on', self.on_logged_on)
self.dota.on('ready', self.do_dota_stuff)
self.dota.on('disconnected', self.on_disconnect)
self.dota.on(EDOTAGCMsg.EMsgGCSpectateFriendGameResponse, self.on_spectate_response)

def start_dota(self):
def on_logged_on(self):
self.message_queue_qt.put(Message(MessageType.CLIENTS_STATUS, MessageConSatus("On", "Try")))
self.dota.launch()

def do_dota_stuff(self):
self.message_queue_qt.put(Message(MessageType.CLIENTS_STATUS, MessageConSatus("On", "On")))
self.dota_ready = True

def on_disconnect(self):
self.keep_running = False
self.message_queue_qt.put(Message(MessageType.CLIENTS_STATUS, MessageConSatus("Off", "Off")))

def on_spectate_response(self, response):
self.server_id_out_queue.put(response.server_steamid)
self.message_queue_qt.put(Message(MessageType.SERVER_ID_RESPONSE,
MessageServerIdResponse(response.server_steamid)))

def run(self):
def connect(self):
self.message_queue_qt.put(Message(MessageType.CLIENTS_STATUS, MessageConSatus("Try", "Off")))
self.steam.login(username=self.username, password=self.password)

def run(self):
while self.keep_running:
if self.dota_ready and not self.user_steam_id_in_queue.empty():
user_steam_id = self.user_steam_id_in_queue.get(block=False)
self.dota.send(EDOTAGCMsg.EMsgGCSpectateFriendGame, {'steam_id': int(user_steam_id)})
if not self.message_queue_dota.empty():
message = self.message_queue_dota.get(block=False)
if message.message_type == MessageType.CLIENTS_CONNECT:
self.connect()
else:
self.message_buffer_queue.append(message)
if self.dota_ready and len(self.message_buffer_queue) > 0:
message = self.message_buffer_queue.pop(0)
if message.message_type == MessageType.SERVER_ID_REQUEST:
message_request: MessageServerIdRequest = message.payload
self.dota.send(EDOTAGCMsg.EMsgGCSpectateFriendGame, {'steam_id': int(message_request.account_id)})
self.steam.sleep(1)

def stop(self):
self.keep_running = False
6 changes: 0 additions & 6 deletions src/dota_notes/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ def __init__(self):
with Session(self.engine) as session:
if session.get(Setting, "version") is None:
session.add(Setting("version", "1"))
if session.query(Setting).filter_by(key="steam_user").one_or_none() is None:
session.add(Setting("steam_user", ""))
if session.query(Setting).filter_by(key="steam_password").one_or_none() is None:
session.add(Setting("steam_password", ""))
if session.query(Setting).filter_by(key="steam_api_key").one_or_none() is None:
session.add(Setting("steam_api_key", ""))
session.commit()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dota_notes.data.player_state import PlayerState


class ApplicationState(QObject):
class GameState(QObject):
match_id = 0
server_id = 0
players = []
Expand Down
42 changes: 42 additions & 0 deletions src/dota_notes/data/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional
from enum import Enum


class MessageType(Enum):
UNKNOWN = 0
CLIENTS_STATUS = 1
CLIENTS_CONNECT = 2
SERVER_ID_REQUEST = 3
SERVER_ID_RESPONSE = 4


class Message:
message_type: MessageType = MessageType.UNKNOWN
payload: Optional[object]

def __init__(self, message_type: MessageType, payload: Optional[object] = None):
self.message_type = message_type
self.payload = payload


class MessageConSatus:
steam: str
dota: str

def __init__(self, steam: str, dota: str):
self.steam = steam
self.dota = dota


class MessageServerIdRequest:
account_id: str

def __init__(self, account_id: str):
self.account_id = account_id


class MessageServerIdResponse:
server_id: int

def __init__(self, server_id: int):
self.server_id = server_id
23 changes: 23 additions & 0 deletions src/dota_notes/data/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from dota_notes.data.database import Setting


class Settings:

gsi_port: int = 58765
software_mode: str = "Client"
proxy_url: str = "https://dota-notes.the-cluster.org"
proxy_api_key: str = ""
steam_user: str = ""
steam_password: str = ""
steam_api_key: str = ""

TO_IMPORT_EXPORT = ["software_mode", "proxy_url", "proxy_api_key", "steam_user", "steam_password", "steam_api_key"]

def import_from_database(self, session):

for at in self.TO_IMPORT_EXPORT:
row = session.get(Setting, at)
if row is not None:
setattr(self, at, row.value)
else:
session.add(Setting(at, getattr(self, at)))
37 changes: 14 additions & 23 deletions src/dota_notes/dota_notes.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
import multiprocessing
import sys

from dota_notes.data.application_state import ApplicationState
from dota_notes.data.game_state import GameState
from dota_notes.data.database import Database, Setting
from dota_notes.app_flask import flask_process
from dota_notes.app_dota import dota_process
from dota_notes.data.settings import Settings
from dota_notes.ui.app_qt import QtApp

from sqlalchemy.orm import Session


class DotaNotes:
def __init__(self):
self.state = ApplicationState()
self.state = GameState()
self.database = Database()
self.settings = Settings()
with Session(self.database.engine) as session:
self.settings.import_from_database(session)
session.commit()

self.match_information_from_gsi = multiprocessing.Queue()
self.user_steam_id_to_dota = multiprocessing.Queue()
self.server_id_from_dota = multiprocessing.Queue()
self.database = Database()
self.message_queue_dota = multiprocessing.Queue()
self.message_queue_qt = multiprocessing.Queue()
self.app_qt = QtApp(self)

self.steam_api_key = ""

def run(self):
# Web Server
app_flask = multiprocessing.Process(target=flask_process, args=(58765, self.match_information_from_gsi,))
app_flask = multiprocessing.Process(target=flask_process, args=(self.settings.gsi_port,
self.match_information_from_gsi,))
app_flask.start()

# Dota client
steam_username = ""
steam_password = ""
self.steam_api_key = ""
with Session(self.database.engine) as session:
row = session.get(Setting, "steam_user")
if row is not None:
steam_username = row.value
row = session.get(Setting, "steam_password")
if row is not None:
steam_password = row.value
row = session.get(Setting, "steam_api_key")
if row is not None:
self.steam_api_key = row.value

app_dota = multiprocessing.Process(
target=dota_process,
args=(steam_username, steam_password, self.user_steam_id_to_dota, self.server_id_from_dota,)
args=(self.settings.steam_user, self.settings.steam_password, self.message_queue_dota,
self.message_queue_qt,)
)
app_dota.start()

Expand Down
Loading

0 comments on commit 70e2b22

Please sign in to comment.