-
Notifications
You must be signed in to change notification settings - Fork 8
/
Lobby.py
103 lines (92 loc) · 4.4 KB
/
Lobby.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from ConnectionManager import ConnectionManager
from exceptions.ClientNotRunningException import ClientNotRunningException
from random import choice
class Lobby:
def __init__(self, logger, config) -> None:
self.logger = logger
self.config = config
self.cm = ConnectionManager()
self.defaultBotDifficulty = self.config.get("defaultBotDifficulty", "MEDIUM")
def createLobby(self, gameMode="PRACTICETOOL", spectatorPolicy="AllAllowed", teamSize=5, lobbyName="League of Poro's Practice Tool", lobbyPassword=""):
if self.cm.isConnected():
data = {
"customGameLobby": {
"configuration": {
"gameMode": gameMode,
"gameMutator": "",
"gameServerRegion": "",
"mapId": 11,
"mutators": {
"id": 1
},
"spectatorPolicy": spectatorPolicy,
"teamSize": teamSize,
},
"lobbyName": lobbyName,
"lobbyPassword": lobbyPassword
},
"isCustom": True,
}
lobby = self.cm.post('/lol-lobby/v2/lobby', data)
if lobby.status_code == 200:
self.logger.info('The lobby was created correctly')
else:
self.logger.error('Cannot create the lobby')
else:
self.logger.critical("Could not communicate with the client")
raise ClientNotRunningException()
def createLobbyFromConfig(self):
self.createLobby(gameMode=self.config.get("gameMode", "PRACTICETOOL"), spectatorPolicy=self.config.get("spectatorPolicy", "AllAllowed"), teamSize=self.config.getInt(
"teamSize", 5), lobbyName=self.config.get("lobbyName", "League of Poro's Practice Tool"), lobbyPassword=self.config.get("lobbyPassword", ""))
def addBotsFromConfig(self):
freeSlots = self.getFreeSlots()
if self.config.getInt('maxBotsBlue', freeSlots[0]) < freeSlots[0]:
numBotsBlue = self.config.getInt('maxBotsBlue')
else:
numBotsBlue = freeSlots[0]
if self.config.getInt('maxBotsRed', freeSlots[1]) < freeSlots[1]:
numBotsRed = self.config.getInt('maxBotsRed')
else:
numBotsRed = freeSlots[1]
usedBots = []
availableBots = self.getAvailableBots()
# Add Blue bots
for i in range(numBotsBlue):
bot = self.config.getNested('botsBlue', i)
if bot:
self.addBot(100, bot["championId"], bot.get("botDifficulty", self.defaultBotDifficulty))
else:
usedBots.append(self.addRandomBot(100, usedBots=usedBots, availableBots=availableBots, difficulty=self.defaultBotDifficulty))
# Add Red bots
for i in range(numBotsRed):
bot = self.config.getNested('botsRed', i)
if bot:
self.addBot(200, bot["championId"], bot.get("botDifficulty", self.defaultBotDifficulty))
else:
usedBots.append(self.addRandomBot(200, usedBots=usedBots, availableBots=availableBots, difficulty=self.defaultBotDifficulty))
def addBot(self, teamId, championId, difficulty="MEDIUM"):
data = {
"botDifficulty": difficulty,
"championId": championId,
"teamId": str(teamId)
}
if self.cm.post("/lol-lobby/v1/lobby/custom/bots", data).status_code == 204:
self.logger.info("Bot added")
else:
self.logger.error("Failed to add bot")
def addRandomBot(self, teamId, usedBots = [], availableBots = None, difficulty="MEDIUM"):
if not availableBots:
availableBots = self.getAvailableBots()
while (championId := choice(availableBots)["id"]) in usedBots:
pass
self.addBot(teamId, championId, difficulty)
return championId
def getFreeSlots(self):
gameCfg = self.cm.get("/lol-lobby/v2/lobby").json()["gameConfig"]
team100 = 5 - len(gameCfg["customTeam100"])
team200 = 5 - len(gameCfg["customTeam200"])
return (team100, team200)
def getAvailableBots(self):
return self.cm.get("/lol-lobby/v2/lobby/custom/available-bots").json()
def testCall(self):
return self.cm.get("/lol-lobby/v2/lobby").json()