1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-04 08:34:02 +00:00

Authorize via steam for the puzzle api

This commit is contained in:
tobspr 2021-05-23 17:34:13 +02:00
parent ec4d198df3
commit 329cefb3c9
5 changed files with 93 additions and 17 deletions

View File

@ -49,6 +49,30 @@ function listen() {
ipcMain.handle("steam:get-achievement-names", getAchievementNames); ipcMain.handle("steam:get-achievement-names", getAchievementNames);
ipcMain.handle("steam:activate-achievement", activateAchievement); ipcMain.handle("steam:activate-achievement", activateAchievement);
function bufferToHex(buffer) {
return Array.from(new Uint8Array(buffer))
.map(b => b.toString(16).padStart(2, "0"))
.join("");
}
ipcMain.on("steam:get-ticket", (event, arg) => {
console.log("Requested steam ticket ...");
greenworks.getAuthSessionTicket(
success => {
const ticketHex = bufferToHex(success.ticket);
event.reply("steam:ticket-success", ticketHex);
},
error => {
console.error("Failed to get steam ticket:", error);
event.reply("steam:ticket-error", "" + error);
}
);
});
ipcMain.on("steam:check-app-ownership", (event, appId) => {
event.reply(greenworks.isSubscribedApp(appId));
});
} }
function isInitialized(event) { function isInitialized(event) {

View File

@ -18,6 +18,7 @@ export const THIRDPARTY_URLS = {
shapeViewer: "https://viewer.shapez.io", shapeViewer: "https://viewer.shapez.io",
standaloneStorePage: "https://store.steampowered.com/app/1318690/shapezio/", standaloneStorePage: "https://store.steampowered.com/app/1318690/shapezio/",
puzzleDlcStorePage: "https://store.steampowered.com/app/1625400/shapezio__Puzzle_DLC",
levelTutorialVideos: { levelTutorialVideos: {
21: "https://www.youtube.com/watch?v=0nUfRLMCcgo&", 21: "https://www.youtube.com/watch?v=0nUfRLMCcgo&",

View File

@ -3,6 +3,7 @@ import { Application } from "../application";
/* typehints:end */ /* typehints:end */
import { createLogger } from "../core/logging"; import { createLogger } from "../core/logging";
import { compressX64 } from "../core/lzstring"; import { compressX64 } from "../core/lzstring";
import { getIPCRenderer } from "../core/utils";
import { T } from "../translations"; import { T } from "../translations";
const logger = createLogger("puzzle-api"); const logger = createLogger("puzzle-api");
@ -109,12 +110,27 @@ export class ClientAPI {
* @returns {Promise<{token: string}>} * @returns {Promise<{token: string}>}
*/ */
apiTryLogin() { apiTryLogin() {
return this._request("/v1/public/login", { if (!G_IS_STANDALONE) {
method: "POST", return Promise.reject("Not possible outside of standalone.");
body: { }
token: this.syncToken,
const renderer = getIPCRenderer();
return renderer.invoke("steam:get-ticket").then(
ticket => {
logger.log("Got auth ticket:", ticket);
return this._request("/v1/public/login", {
method: "POST",
body: {
token: ticket,
},
});
}, },
}); err => {
logger.error("Failed to get auth ticket from steam: ", err);
throw err;
}
);
} }
/** /**

View File

@ -10,6 +10,10 @@ const logger = createLogger("electron-wrapper");
export class PlatformWrapperImplElectron extends PlatformWrapperImplBrowser { export class PlatformWrapperImplElectron extends PlatformWrapperImplBrowser {
initialize() { initialize() {
this.dlcs = {
puzzle: false,
};
this.steamOverlayCanvasFix = document.createElement("canvas"); this.steamOverlayCanvasFix = document.createElement("canvas");
this.steamOverlayCanvasFix.width = 1; this.steamOverlayCanvasFix.width = 1;
this.steamOverlayCanvasFix.height = 1; this.steamOverlayCanvasFix.height = 1;
@ -23,9 +27,9 @@ export class PlatformWrapperImplElectron extends PlatformWrapperImplBrowser {
this.app.storage = new StorageImplElectron(this); this.app.storage = new StorageImplElectron(this);
this.app.achievementProvider = new SteamAchievementProvider(this.app); this.app.achievementProvider = new SteamAchievementProvider(this.app);
return this.initializeAchievementProvider().then(() => return this.initializeAchievementProvider()
PlatformWrapperInterface.prototype.initialize.call(this) .then(() => this.initializeDlcStatus())
); .then(() => PlatformWrapperInterface.prototype.initialize.call(this));
} }
steamOverlayFixRedrawCanvas() { steamOverlayFixRedrawCanvas() {
@ -66,6 +70,22 @@ export class PlatformWrapperImplElectron extends PlatformWrapperImplBrowser {
}); });
} }
initializeDlcStatus() {
const renderer = getIPCRenderer();
logger.log("Checking DLC ownership ...");
// @todo: Don't hardcode the app id
return renderer.invoke("steam:check-app-ownership", 1625400).then(
res => {
logger.log("Got DLC ownership:", res);
this.dlcs.puzzle = Boolean(res);
},
err => {
logger.error("Failed to get DLC ownership:", err);
}
);
}
getSupportsFullscreen() { getSupportsFullscreen() {
return true; return true;
} }

View File

@ -16,6 +16,7 @@ import {
waitNextFrame, waitNextFrame,
} from "../core/utils"; } from "../core/utils";
import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs"; import { HUDModalDialogs } from "../game/hud/parts/modal_dialogs";
import { PlatformWrapperImplElectron } from "../platform/electron/wrapper";
import { getApplicationSettingById } from "../profile/application_settings"; import { getApplicationSettingById } from "../profile/application_settings";
import { T } from "../translations"; import { T } from "../translations";
@ -40,6 +41,11 @@ export class MainMenuState extends GameState {
const showDemoBadges = this.app.restrictionMgr.getIsStandaloneMarketingActive(); const showDemoBadges = this.app.restrictionMgr.getIsStandaloneMarketingActive();
const puzzleDlc =
G_IS_STANDALONE &&
/** @type { PlatformWrapperImplElectron
}*/ (this.app.platformWrapper).dlcs.puzzle;
return ` return `
<div class="topButtons"> <div class="topButtons">
${ ${
@ -86,8 +92,7 @@ export class MainMenuState extends GameState {
</div> </div>
${ ${
// @TODO: Only display if DLC is owned, otherwise show ad for store page G_IS_STANDALONE && puzzleDlc
G_IS_STANDALONE && false
? ` ? `
<div class="puzzleContainer"> <div class="puzzleContainer">
<img class="dlcLogo" src="${cachebust( <img class="dlcLogo" src="${cachebust(
@ -99,8 +104,7 @@ export class MainMenuState extends GameState {
} }
${ ${
// @TODO: Only display if DLC is owned, otherwise show ad for store page G_IS_STANDALONE && !puzzleDlc
G_IS_STANDALONE && true
? ` ? `
<div class="puzzleContainer notOwned"> <div class="puzzleContainer notOwned">
<img class="dlcLogo" src="${cachebust( <img class="dlcLogo" src="${cachebust(
@ -304,6 +308,16 @@ export class MainMenuState extends GameState {
this.trackClicks(producerLink, () => this.app.platformWrapper.openExternalLink("https://tobspr.io"), { this.trackClicks(producerLink, () => this.app.platformWrapper.openExternalLink("https://tobspr.io"), {
preventClick: true, preventClick: true,
}); });
const puzzleModeButton = qs(".puzzleDlcPlayButton");
if (puzzleModeButton) {
this.trackClicks(puzzleModeButton, () => this.onPuzzleModeButtonClicked());
}
const puzzleWishlistButton = qs(".puzzleDlcGetButton");
if (puzzleWishlistButton) {
this.trackClicks(puzzleWishlistButton, () => this.onPuzzleWishlistButtonClicked());
}
} }
renderMainMenu() { renderMainMenu() {
@ -340,11 +354,6 @@ export class MainMenuState extends GameState {
this.trackClicks(playBtn, this.onPlayButtonClicked); this.trackClicks(playBtn, this.onPlayButtonClicked);
buttonContainer.appendChild(importButtonElement); buttonContainer.appendChild(importButtonElement);
} }
const puzzleModeButton = this.htmlElement.querySelector(".puzzleDlcPlayButton");
if (puzzleModeButton) {
this.trackClicks(puzzleModeButton, () => this.onPuzzleModeButtonClicked());
}
} }
onPuzzleModeButtonClicked(force = false) { onPuzzleModeButtonClicked(force = false) {
@ -365,6 +374,12 @@ export class MainMenuState extends GameState {
}); });
} }
onPuzzleWishlistButtonClicked() {
this.app.platformWrapper.openExternalLink(
THIRDPARTY_URLS.puzzleDlcStorePage + "?ref=mmsl2&prc=" + A_B_TESTING_LINK_TYPE
);
}
onBackButtonClicked() { onBackButtonClicked() {
this.renderMainMenu(); this.renderMainMenu();
this.renderSavegames(); this.renderSavegames();