ValvePython / dota2

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

Lobby #10

Closed Philaeux closed 7 years ago

Philaeux commented 7 years ago

This is a small progressin into the lobby features. Because this is the first time contributing to this project, I am doing a first pull request to ensure I am not doing something stupid.

With this first patch, I am mainly managing lobby invites, and lobby creation.

rossengeorgiev commented 7 years ago

There was no need to remove that lobby variable, that reference is useful to have. So you can do

if client.lobby:
    do_stuff()

Otherwise, you will need to know the lobby_id to access it via .socache. Pretty much the same as the implementation for party.

Philaeux commented 7 years ago

Thanks for the feedbacks, I'm getting through now.

Philaeux commented 7 years ago

I have a question about the config_practice_lobby option paramaters. I see 2 possibilities to write the function.

First we let the user enter a dictionary, like in create_practice_lobby

    def create_practice_lobby(self, password="", options=None):
        """
        Sends a message to the Game Coordinator requesting to create a lobby.

        :param password: password of lobby
        :type password: :class:`str`
        :param options: options
        :type options: :class:`dict`
        """

Or we specify the arguments explicitly to add documentation about parameter types and we rebuild the dictionary after:

   def config_practice_lobby(self, lobby_id, game_name=None, server_region=None, 
                                             game_mode=None, ...):
        """
        Change settings of the selected lobby.

        :param lobby_id: target lobby
        :type lobby_id: :class:`int`
        :param game_name: name of the lobby
        :type game_name: :class:`str`
        :param server_region: region to host the game on
        :type server_region: :class:`int`
        :param game_mode: game mode of the region
        :type game_mode: :class:`DOTA_GameMode`
        ....
        """

I think the second one is easier to understand for the user.

Philaeux commented 7 years ago

I think the PR is quite good with this. I will only add a small helper function/class to manage lobby options.

Philaeux commented 7 years ago

Fixed with your feedbacks.

Philaeux commented 7 years ago

Checking if the lobby exist for some operations, and if leader in some cases.

rossengeorgiev commented 7 years ago

This looks good. I will try it out soon.

Philaeux commented 7 years ago

Here is the kind of script I use to test:

import logging
import dota2
import steam
import gevent
import sys

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

first = len(sys.argv) == 1
client = steam.SteamClient()
dota = dota2.Dota2Client(client)
dota.verbose_debug = True

@client.on('connected')
def login():
    if first:
        client.login('account1', 'password1')
    else:
        client.login('account2', 'password2')

@client.on('logged_on')
def start_dota():
    dota.launch()

@dota.on('ready')
def ready():
    if first:
        dota.create_practice_lobby(password="toto")

@dota.on('lobby_new')
def lobby_ready(message):
    logging.debug('%s', message)
    dota.config_practice_lobby(options={'game_name': 'alibaba'})
    gevent.sleep(60)
    if first:
        dota.invite_to_lobby(<replace this after first python run, you have 60 sec go>)
    #dota.practice_lobby_kick(account_id=<account2 steamid>)
    gevent.sleep(60)
    dota.leave_practice_lobby()

@dota.on('lobby_changed')
def lobby_changed(message):
    logging.debug('%s', message)

@dota.on('lobby_invite')
def invite(message):
    logging.debug('%s', message)
    dota.respond_lobby_invite(message.group_id, accept=False)

@dota.on('lobby_invite_removed')
def invite_removed(message):
    logging.debug('%s', message)

client.connect(retry=None)
client.run_forever()