Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzaphkiel committed Nov 2, 2023
2 parents 96256b8 + 91e5273 commit 587ba0a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 4 deletions.
10 changes: 10 additions & 0 deletions app/lol/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class LolClientEventListener(QThread):
currentSummonerProfileChanged = pyqtSignal(dict)
gameStatusChanged = pyqtSignal(str)
champSelectChanged = pyqtSignal(dict)
goingSwap = pyqtSignal(dict)

def __init__(self, parent) -> None:
super().__init__(parent)
Expand All @@ -68,8 +69,14 @@ async def onGameFlowPhaseChanged(data):
async def onChampSelectChanged(data):
self.champSelectChanged.emit(data["data"])

async def onGoingSwap(info):
self.goingSwap.emit(info)

async def defaultHandler(data):
print(data)
# uri = data.get("uri")
# if uri:
# print(uri)

async def main():
wllp = await willump.start()
Expand All @@ -92,6 +99,9 @@ async def main():
wllp.subscription_filter_endpoint(
allEventSubscription, '/lol-champ-select/v1/session', onChampSelectChanged)

# 订阅选择英雄阶段的交换位置消息
wllp.subscription_filter_endpoint(allEventSubscription, '/lol-champ-select/v1/ongoing-swap', onGoingSwap)

# print("[INFO] Event listener initialized.")
while True:
await asyncio.sleep(10)
Expand Down
32 changes: 31 additions & 1 deletion app/view/game_info_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


class GameInfoInterface(SmoothScrollArea):
allyOrderUpdate = pyqtSignal(tuple)
allySummonersInfoReady = pyqtSignal(dict)
enemySummonerInfoReady = pyqtSignal(dict)
summonerViewClicked = pyqtSignal(str)
Expand All @@ -28,13 +29,16 @@ class GameInfoInterface(SmoothScrollArea):

def __init__(self, parent=None):
super().__init__(parent)

self.allySummonersInfo = {}
self.swapBuffer = {}

self.pageState = 1

self.hBoxLayout = QHBoxLayout(self)

self.summonersView = SummonersView()
self.summonersGamesView = QStackedWidget()
# TODO 动画 行为

self.allySummonerGamesView = SummonersGamesView()
self.enemySummonerGamesView = SummonersGamesView()
Expand Down Expand Up @@ -66,15 +70,32 @@ def __connectSignalToSlot(self):
self.__onCurrentTeamChanged)
self.allySummonersInfoReady.connect(self.__onAllySummonerInfoReady)
self.enemySummonerInfoReady.connect(self.__onEnemiesSummonerInfoReady)
self.allyOrderUpdate.connect(self.__onAllyOrderUpdate)

self.gameEnd.connect(self.__onGameEnd)

def __onAllyOrderUpdate(self, order: tuple):
"""
更新队友页排序
@param order: (summonerId,)
@return:
"""
if self.allySummonersInfo:
self.allySummonersInfo["summoners"] = sorted(
self.allySummonersInfo["summoners"], key=lambda x: order.index(x) if x in order else 5
)
self.__onAllySummonerInfoReady(self.allySummonersInfo)


def __onAllySummonerInfoReady(self, info):
self.allySummonersInfo = info
self.summonersView.allySummoners.updateSummoners(info['summoners'])
self.allySummonerGamesView.updateSummoners(info['summoners'])

self.summonersView.allyButton.setVisible(True)
self.summonersView.enemyButton.setVisible(True)
self.summonersView.allyButton.setEnabled(True)

def __onEnemiesSummonerInfoReady(self, info):
self.queueId = info['queueId']
Expand All @@ -84,8 +105,12 @@ def __onEnemiesSummonerInfoReady(self, info):

self.summonersView.allyButton.setVisible(True)
self.summonersView.enemyButton.setVisible(True)
self.summonersView.enemyButton.setEnabled(True)

def __onGameEnd(self):
self.allySummonersInfo = {}
self.swapBuffer = {}

self.summonersView.allySummoners.clear()
self.summonersView.enemySummoners.clear()
self.allySummonerGamesView.clear()
Expand All @@ -94,6 +119,8 @@ def __onGameEnd(self):
self.summonersView.allyButton.click()
self.summonersView.allyButton.setVisible(False)
self.summonersView.enemyButton.setVisible(False)
self.summonersView.allyButton.setEnabled(False)
self.summonersView.enemyButton.setEnabled(False)

def __onCurrentTeamChanged(self, ally: bool):
index = 0 if ally else 1
Expand Down Expand Up @@ -161,6 +188,9 @@ def __init__(self, parent=None):
self.__initLayout()

def __initWidget(self):
self.allyButton.setEnabled(False)
self.enemyButton.setEnabled(False)

self.allyButton.setVisible(False)
self.enemyButton.setVisible(False)

Expand Down
64 changes: 61 additions & 3 deletions app/view/main_window.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import json
import os
import sys
Expand Down Expand Up @@ -144,6 +145,10 @@ def __conncetSignalToSlot(self):
self.__onChampSelectChanged
)

self.eventListener.goingSwap.connect(
self.__onGoingSwap
)

self.nameOrIconChanged.connect(self.__onNameOrIconChanged)
self.lolInstallFolderChanged.connect(self.__onLolInstallFolderChanged)
self.showUpdateMessageBox.connect(self.__onShowUpdateMessageBox)
Expand Down Expand Up @@ -804,7 +809,39 @@ def _():
if switch:
self.checkAndSwitchTo(self.careerInterface)

def __onGoingSwap(self, info: dict):
"""
bp阶段交换选用位置事件
@param info:
@return:
"""
data = info.get("data")
event = info.get("eventType")

if not event:
return

if event == "Create":
self.gameInfoInterface.swapBuffer[data["id"]] = {
'src': data["requestorIndex"],
'dst': data["responderIndex"]
}
elif event == "Update" and data["state"] == "ACCEPTED":

# 必须deepcopy, 否则操作的实例仍是allySummonersInfo, 通过信号传递到实例赋值后, 随着tmp释放, 会变为空列表!!
# 这应该算是python的bug... 也或者是pyqt的?
tmp = copy.deepcopy(self.gameInfoInterface.allySummonersInfo["summoners"])
buf = self.gameInfoInterface.swapBuffer.get(data["id"])
if not buf:
return

tmp[buf["src"]], tmp[buf["dst"]] = tmp[buf["dst"]], tmp[buf["src"]]
self.gameInfoInterface.allySummonersInfoReady.emit({"summoners": tmp})

def __onChampSelectChanged(self, data):
# FIXME
# # 129
# 若在BP进行到一半才打开软件, 进入游戏后仍会有部分队友的头像不是英雄头像
for t in data["myTeam"]:
if t['championId']:
# 控件可能未绘制, 判断一下避免报错
Expand Down Expand Up @@ -834,7 +871,6 @@ def __onGameStatusChanged(self, status):
title = title + " - " + mapSide

self.__onChampionSelectBegin()
self.isChampSelected = True
elif status == 'GameStart':
title = self.tr("Gaming")
self.__onGameStart()
Expand Down Expand Up @@ -993,7 +1029,8 @@ def process_item(item):
"puuid": puuid,
"summonerId": summonerId,
"teammatesMarker": teammatesMarker,
"kda": [kill, deaths, assists]
"kda": [kill, deaths, assists],
"cellId": item["cellId"]
}

with ThreadPoolExecutor() as executor:
Expand All @@ -1007,12 +1044,16 @@ def process_item(item):

assignTeamId(summoners)

summoners = sorted(summoners, key=lambda x: x["cellId"]) # 按照选用顺序排序

self.gameInfoInterface.allySummonersInfoReady.emit(
{'summoners': summoners})

if callback:
callback()

self.isChampSelected = True

# if cfg.get(cfg.enableCopyPlayersInfo):
# msg = self.gameInfoInterface.getPlayersInfoSummary()
# pyperclip.copy(msg)
Expand All @@ -1030,6 +1071,8 @@ def selectChampion():
threading.Thread(target=selectChampion).start()

def __onGameStart(self):
pos = ("TOP", "JUNGLE", "MIDDLE", "UTILITY", "BOTTOM")

def _(callback=None):
session = connector.getGameflowSession()
data = session['gameData']
Expand All @@ -1042,6 +1085,7 @@ def _(callback=None):
team1 = data['teamOne']
team2 = data['teamTwo']
enemies = None
allys = None

# 判断哪边是敌方队伍
for summoner in team1:
Expand Down Expand Up @@ -1152,7 +1196,8 @@ def process_item(item, isAllys=False):
"puuid": puuid,
"summonerId": summoner["summonerId"],
"teammatesMarker": teammatesMarker,
"kda": [kill, deaths, assists]
"kda": [kill, deaths, assists],
"order": pos.index(item['selectedPosition']) if item["selectedPosition"] in pos else len(pos) # 上野中辅下
}

with ThreadPoolExecutor() as executor:
Expand All @@ -1166,6 +1211,8 @@ def process_item(item, isAllys=False):

assignTeamId(summoners)

summoners = sorted(summoners, key=lambda x: x["order"]) # 按照 上野中辅下 排序

if not self.isChampSelected:
allySummoners = []
with ThreadPoolExecutor() as executor:
Expand All @@ -1179,8 +1226,19 @@ def process_item(item, isAllys=False):

assignTeamId(allySummoners)

allySummoners = sorted(allySummoners, key=lambda x: x["order"]) # 按照 上野中辅下 排序

self.gameInfoInterface.allySummonersInfoReady.emit(
{'summoners': allySummoners})
else:
# 按照selectedPosition排序
sorted_allys = sorted(
allys, key=lambda x: pos.index(x['selectedPosition']) if x['selectedPosition'] in pos else len(pos))

# 取出summonerId
result = tuple((player['summonerId'] for player in sorted_allys))

self.gameInfoInterface.allyOrderUpdate.emit(result)

self.gameInfoInterface.enemySummonerInfoReady.emit(
{'summoners': summoners, 'queueId': queueId})
Expand Down

0 comments on commit 587ba0a

Please sign in to comment.