From f9021f50782dda8312d3e57e7e991735d10c185d Mon Sep 17 00:00:00 2001 From: Naviary2 Date: Wed, 8 Jan 2025 23:30:21 -0700 Subject: [PATCH] Fixed your rotation being incorrect when you are black in an online game --- src/client/scripts/esm/game/chess/gameslot.ts | 27 ++++++++++++------- src/client/scripts/esm/game/gui/guipause.js | 3 ++- .../esm/game/misc/onlinegame/onlinegame.ts | 3 --- .../game/misc/onlinegame/onlinegamerouter.ts | 6 ++--- .../scripts/esm/game/rendering/perspective.js | 3 ++- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/client/scripts/esm/game/chess/gameslot.ts b/src/client/scripts/esm/game/chess/gameslot.ts index 72efd7fc..9c47d526 100644 --- a/src/client/scripts/esm/game/chess/gameslot.ts +++ b/src/client/scripts/esm/game/chess/gameslot.ts @@ -7,6 +7,11 @@ * It also has the loader and unloader methods for the gamefile. */ + +import type { MetaData } from "../../chess/util/metadata.js"; +import type { ClockValues } from "../../chess/logic/clock.js"; + + // @ts-ignore import gamefile from "../../chess/logic/gamefile.js"; // @ts-ignore @@ -50,20 +55,14 @@ import clock from "../../chess/logic/clock.js"; import guigameinfo from "../gui/guigameinfo.js"; // @ts-ignore import guipause from "../gui/guipause.js"; +// @ts-ignore +import perspective from "../rendering/perspective.js"; import guinavigation from "../gui/guinavigation.js"; import guipromotion from "../gui/guipromotion.js"; import loadingscreen from "../gui/loadingscreen.js"; import spritesheet from "../rendering/spritesheet.js"; -// Type Definitions --------------------------------------------------------------- - - -import type { MetaData } from "../../chess/util/metadata.js"; -import type { ClockValues } from "../../chess/logic/clock.js"; -import gameloader from "./gameloader.js"; - - // Variables --------------------------------------------------------------- @@ -102,15 +101,20 @@ function getGamefile(): gamefile | undefined { return loadedGamefile; } -function areInGame() { +function areInGame(): boolean { return loadedGamefile !== undefined; } /** Returns true if a new gamefile is currently being loaded */ -function areWeLoading() { +function areWeLoading(): boolean { return gameIsLoading; } +/** Returns what color we are viewing the current loaded game by default. */ +function getOurColor(): string { + return youAreColor; +} + function isLoadedGameViewingWhitePerspective() { if (!loadedGamefile) throw Error("Cannot ask if loaded game is from white's perspective when there isn't a loaded game."); return youAreColor === 'white'; @@ -189,6 +193,8 @@ async function loadGamefile( startStartingTransition(); console.log('Finished loading'); + perspective.resetRotations(); + // Regenerate the mesh of all the pieces. piecesmodel.regenModel(newGamefile, options.getPieceRegenColorArgs()); @@ -283,6 +289,7 @@ export default { getGamefile, areInGame, areWeLoading, + getOurColor, isLoadedGameViewingWhitePerspective, loadGamefile, unloadGame, diff --git a/src/client/scripts/esm/game/gui/guipause.js b/src/client/scripts/esm/game/gui/guipause.js index 770a7652..80da915a 100644 --- a/src/client/scripts/esm/game/gui/guipause.js +++ b/src/client/scripts/esm/game/gui/guipause.js @@ -58,7 +58,8 @@ function toggle() { function updatePasteButtonTransparency() { const moves = gameslot.getGamefile().moves; - const legalInPrivateMatch = onlinegame.getIsPrivate() && moves.length === 0; + + const legalInPrivateMatch = onlinegame.areInOnlineGame() && onlinegame.getIsPrivate() && moves.length === 0; if (onlinegame.areInOnlineGame() && !legalInPrivateMatch) element_pastegame.classList.add('opacity-0_5'); else element_pastegame.classList.remove('opacity-0_5'); diff --git a/src/client/scripts/esm/game/misc/onlinegame/onlinegame.ts b/src/client/scripts/esm/game/misc/onlinegame/onlinegame.ts index b67914a9..c41bb393 100644 --- a/src/client/scripts/esm/game/misc/onlinegame/onlinegame.ts +++ b/src/client/scripts/esm/game/misc/onlinegame/onlinegame.ts @@ -150,9 +150,6 @@ function initOnlineGame(options: { afk.onGameStart(); tabnameflash.onGameStart({ isOurMove: isItOurTurn() }); - - // These make sure it will place us in black's perspective - // perspective.resetRotations(); serverHasConcludedGame = false; diff --git a/src/client/scripts/esm/game/misc/onlinegame/onlinegamerouter.ts b/src/client/scripts/esm/game/misc/onlinegame/onlinegamerouter.ts index d8622243..3031938a 100644 --- a/src/client/scripts/esm/game/misc/onlinegame/onlinegamerouter.ts +++ b/src/client/scripts/esm/game/misc/onlinegame/onlinegamerouter.ts @@ -111,18 +111,18 @@ interface DrawOfferInfo { * This handles all messages related to the active game we're in. * @param {WebsocketMessage} data - The incoming server websocket message */ -function routeMessage(data: WebsocketMessage) { // { sub, action, value, id } +function routeMessage(data: WebsocketMessage): void { // { sub, action, value, id } // console.log(`Received ${data.action} from server! Message contents:`) // console.log(data.value) // This action is listened to, even when we're not in a game. - if (data.action === 'joingame') handleJoinGame(data.value); + if (data.action === 'joingame') return handleJoinGame(data.value); // All other actions should be ignored if we're not in a game... if (!onlinegame.areInOnlineGame()) { - console.log(`Received server 'game' message when we're not in an online game. Ignoring. Message: ${JSON.stringify(data.value)}`); + console.log(`Received server 'game' message when we're not in an online game. Ignoring. Message: ${JSON.stringify(data)}`); return; } diff --git a/src/client/scripts/esm/game/rendering/perspective.js b/src/client/scripts/esm/game/rendering/perspective.js index 611405a2..c55ee286 100644 --- a/src/client/scripts/esm/game/rendering/perspective.js +++ b/src/client/scripts/esm/game/rendering/perspective.js @@ -97,7 +97,8 @@ function disable() { // Sets rotations to orthographic view. Sensitive to if we're white or black. function resetRotations() { rotX = 0; - rotZ = onlinegame.getOurColor() === 'black' ? 180 : 0; // Will be undefined if not in online game + rotZ = gameslot.getOurColor() === 'black' ? 180 : 0; // Will be undefined if not in online game + console.log(rotZ); updateIsViewingBlackPerspective();