1
0
Fork 0
mirror of synced 2024-06-18 02:24:43 +12:00
Rare/rare/components/tabs/games/integrations/ubisoft_group.py

221 lines
8 KiB
Python
Raw Normal View History

2021-12-14 09:57:21 +13:00
import time
2021-12-14 08:53:21 +13:00
import webbrowser
from logging import getLogger
from PyQt5.QtCore import QObject, pyqtSignal, QRunnable, QThreadPool, QSize
from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QSizePolicy, QPushButton, QGroupBox, QVBoxLayout
2021-12-14 08:53:21 +13:00
from legendary.models.game import Game
from rare.shared import LegendaryCoreSingleton, ArgumentsSingleton
from rare.utils.misc import icon
2021-12-14 08:53:21 +13:00
logger = getLogger("Ubisoft")
class UbiGetInfoSignals(QObject):
2021-12-14 08:53:21 +13:00
worker_finished = pyqtSignal(set, set, str)
class UbiGetInfoWorker(QRunnable):
def __init__(self):
super(UbiGetInfoWorker, self).__init__()
self.signals = UbiGetInfoSignals()
2021-12-14 08:53:21 +13:00
self.setAutoDelete(True)
self.core = LegendaryCoreSingleton()
2021-12-14 08:53:21 +13:00
def run(self) -> None:
try:
external_auths = self.core.egs.get_external_auths()
2021-12-14 08:53:21 +13:00
for ext_auth in external_auths:
2021-12-24 22:09:50 +13:00
if ext_auth["type"] != "ubisoft":
2021-12-14 08:53:21 +13:00
continue
2021-12-24 22:09:50 +13:00
ubi_account_id = ext_auth["externalAuthId"]
2021-12-14 08:53:21 +13:00
break
else:
self.signals.worker_finished.emit(set(), set(), "")
return
uplay_keys = self.core.egs.store_get_uplay_codes()
2021-12-24 22:09:50 +13:00
key_list = uplay_keys["data"]["PartnerIntegration"]["accountUplayCodes"]
redeemed = {k["gameId"] for k in key_list if k["redeemedOnUplay"]}
2021-12-14 08:53:21 +13:00
entitlements = self.core.egs.get_user_entitlements()
2021-12-24 22:09:50 +13:00
entitlements = {i["entitlementName"] for i in entitlements}
2021-12-14 08:53:21 +13:00
self.signals.worker_finished.emit(redeemed, entitlements, ubi_account_id)
except Exception as e:
logger.error(str(e))
self.signals.worker_finished.emit(set(), set(), "error")
class UbiConnectSignals(QObject):
linked = pyqtSignal(str)
2021-12-14 08:53:21 +13:00
class UbiConnectWorker(QRunnable):
def __init__(self, ubi_account_id, partner_link_id):
super(UbiConnectWorker, self).__init__()
self.signals = UbiConnectSignals()
2021-12-14 08:53:21 +13:00
self.setAutoDelete(True)
self.core = LegendaryCoreSingleton()
2021-12-14 08:53:21 +13:00
self.ubi_account_id = ubi_account_id
self.partner_link_id = partner_link_id
def run(self) -> None:
2021-12-14 09:57:21 +13:00
if not self.ubi_account_id: # debug
time.sleep(2)
self.signals.linked.emit("")
return
2021-12-14 08:53:21 +13:00
try:
self.core.egs.store_claim_uplay_code(
2021-12-24 22:09:50 +13:00
self.ubi_account_id, self.partner_link_id
)
self.core.egs.store_redeem_uplay_codes(self.ubi_account_id)
2021-12-14 08:53:21 +13:00
except Exception as e:
2021-12-14 09:57:21 +13:00
self.signals.linked.emit(str(e))
2021-12-14 08:53:21 +13:00
return
else:
2021-12-14 09:57:21 +13:00
self.signals.linked.emit("")
2021-12-14 08:53:21 +13:00
class UbiLinkWidget(QWidget):
def __init__(self, game: Game, ubi_account_id):
super(UbiLinkWidget, self).__init__()
self.args = ArgumentsSingleton()
2022-03-19 06:12:19 +13:00
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
2021-12-14 08:53:21 +13:00
self.game = game
self.ubi_account_id = ubi_account_id
2021-12-14 09:57:21 +13:00
self.title_label = QLabel(game.app_title)
2022-03-19 06:12:19 +13:00
layout.addWidget(self.title_label, stretch=1)
2021-12-14 09:57:21 +13:00
2021-12-14 08:53:21 +13:00
self.ok_indicator = QLabel()
2021-12-14 09:57:21 +13:00
self.ok_indicator.setPixmap(icon("fa.info-circle", color="grey").pixmap(20, 20))
2021-12-14 08:53:21 +13:00
self.ok_indicator.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Preferred)
2022-03-19 06:12:19 +13:00
layout.addWidget(self.ok_indicator)
2021-12-14 08:53:21 +13:00
2021-12-24 22:09:50 +13:00
self.link_button = QPushButton(
self.tr("Redeem to Ubisoft") + ": Test" if self.args.debug else ""
2021-12-24 22:09:50 +13:00
)
2022-03-19 06:12:19 +13:00
layout.addWidget(self.link_button)
2021-12-14 09:57:21 +13:00
self.link_button.clicked.connect(self.activate)
2022-03-19 06:12:19 +13:00
self.setLayout(layout)
2021-12-14 08:53:21 +13:00
def activate(self):
2021-12-14 09:57:21 +13:00
self.link_button.setDisabled(True)
# self.ok_indicator.setPixmap(icon("mdi.loading", color="grey").pixmap(20, 20))
2021-12-24 22:09:50 +13:00
self.ok_indicator.setPixmap(
icon("mdi.transit-connection-horizontal", color="grey").pixmap(20, 20)
)
2021-12-14 09:57:21 +13:00
if self.args.debug:
2021-12-14 09:57:21 +13:00
worker = UbiConnectWorker(None, None)
else:
worker = UbiConnectWorker(self.ubi_account_id, self.game.partner_link_id)
worker.signals.linked.connect(self.worker_finished)
2021-12-14 08:53:21 +13:00
QThreadPool.globalInstance().start(worker)
def worker_finished(self, error):
if not error:
2021-12-24 22:09:50 +13:00
self.ok_indicator.setPixmap(
icon("ei.ok-circle", color="green").pixmap(QSize(20, 20))
)
2021-12-14 09:57:21 +13:00
self.link_button.setDisabled(True)
self.link_button.setText(self.tr("Already activated"))
2021-12-14 08:53:21 +13:00
else:
2021-12-24 22:09:50 +13:00
self.ok_indicator.setPixmap(
icon("fa.info-circle", color="red").pixmap(QSize(20, 20))
)
2021-12-14 08:53:21 +13:00
self.ok_indicator.setToolTip(error)
2021-12-14 09:57:21 +13:00
self.link_button.setText(self.tr("Try again"))
self.link_button.setDisabled(False)
2021-12-14 08:53:21 +13:00
class UbisoftGroup(QGroupBox):
def __init__(self, parent=None):
super(UbisoftGroup, self).__init__(parent=parent)
self.setTitle(self.tr("Link Ubisoft Games"))
self.setLayout(QVBoxLayout())
self.core = LegendaryCoreSingleton()
self.args = ArgumentsSingleton()
2021-12-14 08:53:21 +13:00
self.thread_pool = QThreadPool.globalInstance()
worker = UbiGetInfoWorker()
worker.signals.worker_finished.connect(self.show_ubi_games)
self.thread_pool.start(worker)
def show_ubi_games(self, redeemed: set, entitlements: set, ubi_account_id: str):
if not redeemed and ubi_account_id != "error":
2021-12-24 22:09:50 +13:00
logger.error(
"No linked ubisoft account found! Link your accounts via your browser and try again."
)
self.layout().addWidget(
2021-12-24 22:09:50 +13:00
QLabel(
self.tr(
"Your account is not linked with Ubisoft. Please link your account first"
)
)
)
2021-12-14 08:53:21 +13:00
open_browser_button = QPushButton(self.tr("Open link page"))
2021-12-24 22:09:50 +13:00
open_browser_button.clicked.connect(
lambda: webbrowser.open("https://www.epicgames.com/id/link/ubisoft")
)
self.layout().addWidget(open_browser_button)
2021-12-14 08:53:21 +13:00
return
elif ubi_account_id == "error":
self.layout().addWidget(QLabel(self.tr("An error occurred")))
2021-12-14 08:53:21 +13:00
return
games = self.core.get_game_list(False)
uplay_games = []
activated = 0
for game in games:
2021-12-24 22:09:50 +13:00
for dlc_data in game.metadata.get("dlcItemList", []):
if dlc_data["entitlementName"] not in entitlements:
2021-12-14 08:53:21 +13:00
continue
try:
2021-12-24 22:09:50 +13:00
app_name = dlc_data["releaseInfo"][0]["appId"]
2021-12-14 08:53:21 +13:00
except (IndexError, KeyError):
2021-12-24 22:09:50 +13:00
app_name = "unknown"
2021-12-14 08:53:21 +13:00
2021-12-24 22:09:50 +13:00
dlc_game = Game(
app_name=app_name, app_title=dlc_data["title"], metadata=dlc_data
)
if dlc_game.partner_link_type != "ubisoft":
2021-12-14 08:53:21 +13:00
continue
if dlc_game.partner_link_id in redeemed:
continue
uplay_games.append(dlc_game)
if game.partner_link_type != "ubisoft":
continue
if game.partner_link_id in redeemed:
activated += 1
continue
uplay_games.append(game)
if not uplay_games:
if activated >= 1:
self.layout().addWidget(
2021-12-24 22:09:50 +13:00
QLabel(
self.tr("All your Ubisoft games have already been activated")
)
)
2021-12-14 08:53:21 +13:00
else:
self.layout().addWidget(
2021-12-24 22:09:50 +13:00
QLabel(self.tr("You don't own any Ubisoft games"))
)
if self.args.debug:
2021-12-24 22:09:50 +13:00
widget = UbiLinkWidget(
Game(app_name="Test", app_title="This is a test game"),
ubi_account_id,
)
self.layout().addWidget(widget)
2021-12-14 08:53:21 +13:00
return
2021-12-24 22:09:50 +13:00
logger.info(f"Found {len(uplay_games)} game(s) to redeem")
2021-12-14 08:53:21 +13:00
for game in uplay_games:
widget = UbiLinkWidget(game, ubi_account_id)
self.layout().addWidget(widget)