ValvePython / dota2

🐸 Python package for interacting with Dota 2 Game Coordinator
http://dota2.readthedocs.io
198 stars 32 forks source link

Cannot process emited events while being hosting as Flask/FastAPI app #95

Open akhlebka opened 1 year ago

akhlebka commented 1 year ago

I have an issue with creating a small web application whuch will wait for request with profile id and return it's profile card, but it seems like jobs and events are not working when hosting it as Flask or FastAPI apps. For me it seems like a bug but I'm not sure. Do you have ideas?

from steam.client import SteamClient
from dota2.client import Dota2Client
from flask import Flask, Response

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

username = "login"
password = "password"

app = Flask(__name__)
steamClient = SteamClient()
dota = Dota2Client(steamClient)

@dota.on('ready')
def fetch_profile_card():
    logging.info('Logged in to Dota 2')
    pass

@dota.on('profile_card')
def print_profile_card(account_id, profile_card):
    if profile_card:
        logging.log("Got the profile card")

# @app.get("/getPlayerProfile/{playerId}") # use this for FaskAPI
@app.route('/getPlayerProfile/<int:playerId>', methods=['GET'])
def getUserProfile(playerId):
    logging.log(logging.INFO, 'Got GETUSERPROFILE event with %d player ID' % int(playerId))
    # we hardcode here so you can send everything you want it will try to obtain profile card from tutorial
    jobid = dota.request_profile_card(70388657)
    profile_card = dota.wait_msg(jobid, timeout=10)
    if profile_card:
        return Response(status=200, response='event emited and got profile card')
    else:
        return Response(status=404, response='event emited and got no profile card')

try:
    if dota.ready is not True:
        steamClient.login(username, password, login_id=774)
        dota.launch()
        dota.wait_event('ready')
    app.run()
except KeyboardInterrupt:
    steamClient.logout()
rossengeorgiev commented 1 year ago

See this example: https://github.com/ValvePython/steam/tree/master/recipes/2.SimpleWebAPI

I would also make sure the credentials for login are always used as remember password tends to disconnect once in a while. Monkey patching is recommended (you have to do that yourself, see https://steam.readthedocs.io/en/latest/api/steam.monkey.html#steam.monkey.patch_minimal). I would also recommend using https://www.gevent.org/api/gevent.pywsgi.html#gevent.pywsgi.WSGIServer to run the flask app. Can't comment on fastapi as I haven't tried it.

akhlebka commented 1 year ago

@rossengeorgiev thanks for the answer. It was really helpful. But I'm also not sure what's the idea of set_credential_location() method. It does not create any file in my file system or even if I create cm_servers.json file myself (because this is the only files related error I see in logs) SteamClient does not override it Where can I find any info about how this file works or how should I fill it to ex. not pass login and password everytime and just use client.login()?