Skip to content

Commit

Permalink
updating hathora sdk and fixing sdk errors (#57)
Browse files Browse the repository at this point in the history
* updating hathora sdk and fixing sdk errors

* add back google auth logic

* add login to debug

* fix sdk usage in utils.ts
  • Loading branch information
jchu231 committed May 9, 2024
1 parent 9e906e3 commit a416c42
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 896 deletions.
430 changes: 52 additions & 378 deletions client/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"dependencies": {
"@hathora/client-sdk": "^1.0.0",
"@hathora/cloud-sdk-typescript": "^2.2.0",
"@hathora/cloud-sdk-typescript": "^2.2.8",
"@heroicons/react": "^2.0.17",
"@react-oauth/google": "^0.10.0",
"dayjs": "^1.11.7",
Expand Down
31 changes: 17 additions & 14 deletions client/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ReactDOM from "react-dom/client";
import React, { useEffect, useState } from "react";
import { GoogleOAuthProvider } from "@react-oauth/google";
import * as errors from "@hathora/cloud-sdk-typescript/models/errors";
import { HathoraConnection } from "@hathora/client-sdk";

import { SessionMetadata, RoomConfig } from "../../common/types";
Expand Down Expand Up @@ -61,18 +62,15 @@ function App() {
const connect = new HathoraConnection(roomIdFromUrl, connectionInfo);
connect.onClose(async () => {
// If game has ended, we want updated lobby state
const { lobbyV3: updatedLobbyInfo } = await hathoraSdk.lobbyV3.getLobbyInfoByRoomId(roomIdFromUrl);
if (updatedLobbyInfo == null) {
return;
}
const updatedRoomConfig = JSON.parse(updatedLobbyInfo.roomConfig) as RoomConfig;
const updatedLobbyInfo = await hathoraSdk.lobbyV3.getLobbyInfoByRoomId(roomIdFromUrl);
const updatedRoomConfig = JSON.parse(updatedLobbyInfo.roomConfig ?? "") as RoomConfig;
setSessionMetadata({
serverUrl: `${connectionInfo.host}:${connectionInfo.port}`,
region: updatedLobbyInfo.region,
roomId: updatedLobbyInfo.roomId,
capacity: updatedRoomConfig.capacity,
winningScore: updatedRoomConfig.winningScore,
isGameEnd: !!updatedRoomConfig.isGameEnd,
isGameEnd: updatedRoomConfig.isGameEnd,
winningPlayerId: updatedRoomConfig.winningPlayerId,
playerNicknameMap: updatedRoomConfig.playerNicknameMap,
creatorId: updatedLobbyInfo.createdBy,
Expand Down Expand Up @@ -222,20 +220,25 @@ async function getToken(googleIdToken: string | undefined): Promise<Token> {
value: maybeToken,
} as Token;
}
let loginResponse;
if (googleIdToken == null) {
const { loginResponse } = await hathoraSdk.authV1.loginAnonymous();
if (loginResponse == null) {
try {
loginResponse = await hathoraSdk.authV1.loginAnonymous();
return { value: loginResponse.token, type: "anonymous" };
} catch (err) {
console.log(err);
throw new Error("Failed to login anonymously");
}
return { value: loginResponse.token, type: "anonymous" };
}
const { loginResponse } = await hathoraSdk.authV1.loginGoogle({ idToken: googleIdToken });
if (loginResponse == null) {
try {
loginResponse = await hathoraSdk.authV1.loginGoogle({ idToken: googleIdToken });
sessionStorage.setItem("bullet-mania-token", loginResponse.token);
sessionStorage.setItem("bullet-mania-token-type", "google");
return { value: loginResponse.token, type: "google" };
} catch (err) {
console.log(err);
throw new Error("Failed to login with google");
}
sessionStorage.setItem("bullet-mania-token", loginResponse.token);
sessionStorage.setItem("bullet-mania-token-type", "google");
return { value: loginResponse.token, type: "google" };
}

function getRoomIdFromUrl(): string | undefined {
Expand Down
32 changes: 18 additions & 14 deletions client/src/components/lobby/GameCreator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import { GoogleLogin } from "@react-oauth/google";
import { LobbyVisibility, Region } from "@hathora/cloud-sdk-typescript/dist/sdk/models/shared";
import * as errors from "@hathora/cloud-sdk-typescript/models/errors";
import { LobbyVisibility, Region } from "@hathora/cloud-sdk-typescript/models/components";

import { getHathoraSdk, isReadyForConnect, Token } from "../../utils";
import { RoomConfig } from "../../../../common/types";
Expand Down Expand Up @@ -82,24 +83,27 @@ export function GameCreator(props: GameCreatorProps) {
playerNicknameMap: {},
isGameEnd: false,
};
const { lobbyV3 } = await hathoraSdk.lobbyV3.createLobby(
const lobbyV3 = await hathoraSdk.lobbyV3.createLobby(
{ playerAuth: playerToken.value },
{
createLobbyV3Params: {
region,
visibility: visibility as LobbyVisibility,
roomConfig: JSON.stringify(roomConfig),
},
},
{ playerAuth: playerToken.value }
region,
visibility: visibility as LobbyVisibility,
roomConfig: JSON.stringify(roomConfig),
}
);
if (lobbyV3 == null) {
throw new Error("Failed to create lobby");
}
// Wait until lobby connection details are ready before redirect player to match
await isReadyForConnect(appId, lobbyV3.roomId, hathoraSdk);
window.location.href = `/${lobbyV3.roomId}`; //update url
} catch (e) {
setError(e instanceof Error ? e.toString() : typeof e === "string" ? e : "Unknown error");
} catch (err) {
switch (true) {
case err instanceof errors.ApiError: {
console.error(err); // handle exception
throw new Error("Failed to create lobby");
}
default: {
setError(err instanceof Error ? err.toString() : typeof err === "string" ? err : "Unknown error");
}
}
} finally {
setIsLoading(false);
}
Expand Down
32 changes: 16 additions & 16 deletions client/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LobbyV3 } from "@hathora/cloud-sdk-typescript/dist/sdk/models/shared";
import { LobbyV3 } from "@hathora/cloud-sdk-typescript/models/components";
import { HathoraCloud } from "@hathora/cloud-sdk-typescript";
import { ConnectionDetails } from "@hathora/client-sdk";

Expand Down Expand Up @@ -36,23 +36,23 @@ export async function isReadyForConnect(
const MAX_CONNECT_ATTEMPTS = 50;
const TRY_CONNECT_INTERVAL_MS = 1000;

const { lobbyV3: lobbyInfo } = await hathoraSdk.lobbyV3.getLobbyInfoByRoomId(roomId);
if (lobbyInfo === undefined) {
throw new Error("Lobby not found: " + roomId);
}

if (lobbyInfo.visibility === "local") {
return new Promise<{ lobbyInfo: LobbyV3; connectionInfo: ConnectionDetails }>((resolve) =>
resolve({ lobbyInfo, connectionInfo: LOCAL_CONNECTION_DETAILS })
);
}
try {
const lobbyInfo = await hathoraSdk.lobbyV3.getLobbyInfoByRoomId(roomId);
if (lobbyInfo.visibility === "local") {
return new Promise<{ lobbyInfo: LobbyV3; connectionInfo: ConnectionDetails }>((resolve) =>
resolve({ lobbyInfo, connectionInfo: LOCAL_CONNECTION_DETAILS })
);
}

for (let i = 0; i < MAX_CONNECT_ATTEMPTS; i++) {
const { connectionInfoV2 } = await hathoraSdk.roomV2.getConnectionInfo(roomId);
if (connectionInfoV2?.exposedPort !== undefined) {
return { lobbyInfo, connectionInfo: connectionInfoV2.exposedPort };
for (let i = 0; i < MAX_CONNECT_ATTEMPTS; i++) {
const connectionInfoV2 = await hathoraSdk.roomV2.getConnectionInfo(roomId);
if (connectionInfoV2.exposedPort !== undefined) {
return { lobbyInfo, connectionInfo: connectionInfoV2.exposedPort };
}
await new Promise((resolve) => setTimeout(resolve, TRY_CONNECT_INTERVAL_MS));
}
await new Promise((resolve) => setTimeout(resolve, TRY_CONNECT_INTERVAL_MS));
} catch (err) {
throw new Error("Lobby not found: " + roomId);
}
throw new Error("Polling timed out");
}
Expand Down
22 changes: 17 additions & 5 deletions common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"dependencies": {
"@hathora/hathora-cloud-sdk": "^0.0.3"
"@hathora/cloud-sdk-typescript": "^2.2.8"
},
"devDependencies": {
"typescript": "^4.8.3"
Expand Down
2 changes: 1 addition & 1 deletion common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Region } from "@hathora/hathora-cloud-sdk";
import {Region} from "@hathora/cloud-sdk-typescript/models/components";

export type Direction = {
x: number;
Expand Down

0 comments on commit a416c42

Please sign in to comment.