Skip to content

Commit

Permalink
修复自动选取英雄时不会避开已经 ban 的和选过的英雄的问题 (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzaphkiel committed Nov 14, 2024
1 parent ff8f56b commit f954bb4
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 19 deletions.
164 changes: 149 additions & 15 deletions app/lol/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,7 @@ def getNameTagLineFromGame(game, puuid):

class ChampionSelection:
def __init__(self):
self.isChampionShowed = False
self.isChampionBanned = False
self.isChampionPicked = False
self.isChampionPickedCompleted = False
Expand Down Expand Up @@ -1421,6 +1422,7 @@ async def showOpggBuild(data, selection: ChampionSelection):

# 只有在英雄已经选定后才会尝试刷新 OPGG 界面
for actionGroup in data['actions']:
# 这里必须遍历完所有的 actorCellId == cellId 的所有 action
for action in actionGroup:
if not action['actorCellId'] == cellId:
continue
Expand Down Expand Up @@ -1475,6 +1477,7 @@ async def autoPick(data, selection: ChampionSelection):
"""
自动选用英雄
"""

if not cfg.get(cfg.enableAutoSelectChampion) or selection.isChampionPicked:
return

Expand All @@ -1484,10 +1487,8 @@ async def autoPick(data, selection: ChampionSelection):
if player["cellId"] != localPlayerCellId:
continue

if bool(player['championId']):
return

if bool(player['championPickIntent']):
if bool(player['championId']) or bool(player['championPickIntent']):
selection.isChampionPicked = True
return

break
Expand Down Expand Up @@ -1528,7 +1529,6 @@ async def autoPick(data, selection: ChampionSelection):
and action['type'] == "pick"):

await connector.selectChampion(action['id'], championId)

selection.isChampionPicked = True
return True

Expand All @@ -1544,29 +1544,105 @@ async def autoComplete(data, selection: ChampionSelection):
localPlayerCellId = data['localPlayerCellId']
for actionGroup in reversed(data['actions']):
for action in actionGroup:
if (action['actorCellId'] == localPlayerCellId
and action['type'] == "pick"
and action['isInProgress']
and not action['completed']):
if action['actorCellId'] != localPlayerCellId:
continue

if action['type'] != 'pick':
continue

if not action['isInProgress']:
return False

if action['completed']:
selection.isChampionPickedCompleted = True
await asyncio.sleep(int(data['timer']['adjustedTimeLeftInPhase'] / 1000) - 1)
return False

break

if selection.isChampionPickedCompleted:
await connector.selectChampion(action['id'], action['championId'], True)
selection.isChampionPickedCompleted = True

return True
sleepTime = int(data['timer']['adjustedTimeLeftInPhase'] / 1000) - 2
await asyncio.sleep(sleepTime)

data = await connector.getChampSelectSession()

if not data:
return

# 双方选过的英雄
cantSelect = []

# 双方 ban 掉的英雄
bans = itertools.chain(data["bans"]['myTeamBans'],
data["bans"]['theirTeamBans'])

championIntent = 0
for actionGroup in data['actions']:
for action in actionGroup:
if (action['type'] == 'pick' and action['completed']
and action['actorCellId'] != localPlayerCellId):
cantSelect.append(action['championId'])

if action['actorCellId'] != localPlayerCellId:
continue

if action['type'] != 'pick':
continue

if action['completed']:
return

# 现在亮着的英雄
championIntent = action['championId']
actionId = action['id']

if not championIntent:
return

cantSelect.extend(bans)

if championIntent not in cantSelect:
await connector.selectChampion(actionId, championIntent, True)
return True

pos = next(filter(lambda x: x['cellId'] ==
localPlayerCellId, data['myTeam']), None)
pos = pos.get('assignedPosition')

if pos == 'top':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionTop))
elif pos == 'jungle':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionJug))
elif pos == 'middle':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionMid))
elif pos == 'bottom':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionBot))
elif pos == 'utility':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionSup))
else:
candidates = []

candidates.extend(cfg.get(cfg.autoSelectChampion))

candidates = [x for x in candidates if x not in cantSelect]

if len(candidates) == 0:
return

await connector.selectChampion(actionId, candidates[0], True)

return True


async def autoBan(data, selection: ChampionSelection):
"""
自动禁用英雄
"""
isAutoBan = cfg.get(cfg.enableAutoBanChampion)

if not isAutoBan or selection.isChampionBanned:
return

selection.isChampionBanned = True

localPlayerCellId = data['localPlayerCellId']
for actionGroup in data['actions']:
for action in actionGroup:
Expand Down Expand Up @@ -1599,8 +1675,10 @@ async def autoBan(data, selection: ChampionSelection):
isFriendly = cfg.get(cfg.pretentBan)
if isFriendly:
myTeam = (await connector.getChampSelectSession()).get("myTeam")

if not myTeam:
return

intents = [player["championPickIntent"]
for player in myTeam]
candidates = [x for x in candidates if x not in intents]
Expand All @@ -1610,6 +1688,62 @@ async def autoBan(data, selection: ChampionSelection):

championId = candidates[0]
await connector.banChampion(action['id'], championId, True)
selection.isChampionBanned = True

return True


async def autoShow(data, selection: ChampionSelection):
'''在 B/P 前展示英雄'''
if selection.isChampionShowed:
return

if not cfg.get(cfg.enableAutoSelectChampion):
return

cellId = data['localPlayerCellId']

for player in data['myTeam']:
if player['cellId'] != cellId:
continue

if (player['championId'] != 0
or player['championPickIntent'] != 0):
selection.isChampionShowed = True
return

pos = player.get("assignedPosition", None)

break

if pos == 'top':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionTop))
elif pos == 'jungle':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionJug))
elif pos == 'middle':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionMid))
elif pos == 'bottom':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionBot))
elif pos == 'utility':
candidates = deepcopy(cfg.get(cfg.autoSelectChampionSup))
else:
candidates = []

default = deepcopy(cfg.get(cfg.autoSelectChampion))
candidates.extend(default)

if len(candidates) == 0:
selection.isChampionShowed = True
return

championId = candidates[0]
for actionGroup in reversed(data['actions']):
for action in actionGroup:
if (action['actorCellId'] == cellId and
action['type'] == 'pick'):

await connector.selectChampion(action['id'], championId)
selection.isChampionShowed = True

return True

Expand Down
3 changes: 2 additions & 1 deletion app/view/auxiliary_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ async def __onApplyButtonClicked(self):

msg.exec_()

InfoBar.success(title=self.tr("Apply"), content=self.tr("Successfully"), orient=Qt.Vertical, isClosable=True,
InfoBar.success(title=self.tr("Apply"), content=self.tr("Successfully"),
orient=Qt.Vertical, isClosable=True,
position=InfoBarPosition.TOP_RIGHT, duration=5000,
parent=self.window().auxiliaryFuncInterface)

Expand Down
5 changes: 2 additions & 3 deletions app/view/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from app.lol.tools import (parseAllyGameInfo, parseGameInfoByGameflowSession,
getAllyOrderByGameRole, getTeamColor, autoBan, autoPick,
autoComplete, autoSwap, autoTrade, ChampionSelection,
SERVERS_NAME, SERVERS_SUBSET, showOpggBuild)
SERVERS_NAME, SERVERS_SUBSET, showOpggBuild, autoShow)
from app.lol.aram import AramBuff
from app.lol.champions import ChampionAlias
from app.lol.opgg import opgg
Expand Down Expand Up @@ -867,10 +867,9 @@ async def __onChampSelectChanged(self, data):
data = data['data']

phase = {
'PLANNING': [autoPick],
'PLANNING': [autoShow],
'BAN_PICK': [autoBan, autoPick, autoComplete, autoSwap, showOpggBuild],
'FINALIZATION': [autoTrade, showOpggBuild],
# 'GAME_STARTING': []
}

for func in phase.get(data['timer']['phase'], []):
Expand Down

0 comments on commit f954bb4

Please sign in to comment.