Skip to content

Commit

Permalink
#9 added !mmr and !flip commands to bot
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleVasya committed Dec 21, 2016
1 parent 3d4ff18 commit 1b9a46b
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions app/balancer/management/commands/dota_bot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse
from app.balancer.managers import BalanceResultManager
from app.balancer.models import BalanceAnswer
from app.ladder.managers import MatchManager
from enum import IntEnum
import gevent
Expand Down Expand Up @@ -33,7 +32,7 @@ def __init__(self):

def add_arguments(self, parser):
parser.add_argument('-n', '--number',
nargs='?', type=int, default=1, const=1)
nargs='?', type=int, default=2, const=2)

def handle(self, *args, **options):
bots_num = options['number']
Expand All @@ -59,7 +58,10 @@ def handle(self, *args, **options):
def start_bot(self, credentials):
client = SteamClient()
dota = Dota2Client(client)

dota.balance_answer = None
dota.min_mmr = 0
dota.lobby_options = {}

self.bots.append(dota)

Expand Down Expand Up @@ -104,6 +106,10 @@ def chat_message(channel, sender, text, msg_obj):
self.balance_command(dota, text)
elif text.startswith('!start'):
self.start_command(dota)
elif text.startswith('!mmr'):
self.mmr_command(dota, text)
elif text.startswith('!flip'):
dota.flip_lobby_teams()

client.login(credentials['login'], credentials['password'])
client.run_forever()
Expand All @@ -113,10 +119,8 @@ def create_new_lobby(bot):
print 'Making new lobby\n'

bot.balance_answer = None

lobby_password = os.environ.get('LOBBY_PASSWORD', '')
bot.create_practice_lobby(password=lobby_password, options={
'game_name': 'Inhouse Ladder',
bot.lobby_options = {
'game_name': Command.generate_lobby_name(bot),
'game_mode': dota2.enums.DOTA_GameMode.DOTA_GAMEMODE_CD,
'server_region': int(dota2.enums.EServerRegion.Europe),
'fill_with_bots': False,
Expand All @@ -125,7 +129,11 @@ def create_new_lobby(bot):
'allchat': True,
'dota_tv_delay': 2,
'pause_setting': 1,
})
}

bot.create_practice_lobby(
password=os.environ.get('LOBBY_PASSWORD', ''),
options=bot.lobby_options)

@staticmethod
def balance_command(bot, command):
Expand Down Expand Up @@ -176,18 +184,37 @@ def balance_command(bot, command):
bot.send_lobby_message('Team %d: %s' % (i+1, ' | '.join(player_names)))
bot.send_lobby_message(url)

def start_command(self, bot):
@staticmethod
def start_command(bot):
if not bot.balance_answer:
bot.send_lobby_message('Please balance teams first.')
return

if not self.check_teams_setup(bot):
if not Command.check_teams_setup(bot):
bot.send_lobby_message('Please join slots according to balance.')
return

bot.send_lobby_message('Ready to start')
bot.launch_practice_lobby()

@staticmethod
def mmr_command(bot, command):
print
print 'Setting lobby MMR: '
print command

try:
min_mmr = int(command.split(' ')[1])
min_mmr = max(0, min(9000, min_mmr))
except (IndexError, ValueError):
return

bot.min_mmr = min_mmr
bot.lobby_options['game_name'] = Command.generate_lobby_name(bot)
bot.config_practice_lobby(bot.lobby_options)

bot.send_lobby_message('Min MMR set to %d' % min_mmr)

@staticmethod
def process_game_result(bot):
print 'Game is finished!\n'
Expand Down Expand Up @@ -247,4 +274,12 @@ def check_teams_setup(bot):

return False

@staticmethod
def generate_lobby_name(bot):
lobby_name = 'Inhouse Ladder'

if bot.min_mmr > 0:
lobby_name += ' %d+ MMR' % bot.min_mmr

return lobby_name

0 comments on commit 1b9a46b

Please sign in to comment.