Skip to content

Commit

Permalink
fix uno and poker
Browse files Browse the repository at this point in the history
  • Loading branch information
hpx7 committed Jul 13, 2023
1 parent d3dfef0 commit 25c93b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
19 changes: 8 additions & 11 deletions examples/poker/client/web/src/context/GameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { toast, ToastContainer } from "react-toastify";
import { useSessionstorageState } from "rooks";
import { HathoraClient, HathoraConnection } from "../../../.hathora/client";
import { ConnectionFailure } from "../../../.hathora/failures";
import { IInitializeRequest, PlayerState, RoundStatus } from "../../../../api/types";
import { lookupUser, Response, UserData } from "../../../../api/base";
import { PlayerState, RoundStatus } from "../../../../api/types";
import { Response } from "../../../../api/base";

interface GameContext {
token?: string;
Expand All @@ -19,7 +19,7 @@ interface GameContext {
connectionError?: ConnectionFailure;
endGame: () => void;
getUserName: (id: string) => string;
user?: UserData;
user?: { id: string };
connecting?: boolean;
loggingIn?: boolean;
fold: () => Promise<void>;
Expand Down Expand Up @@ -60,11 +60,11 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi
const [connectionError, setConnectionError] = useState<ConnectionFailure>();
const [connecting, setConnecting] = useState<boolean>();
const [loggingIn, setLoggingIn] = useState<boolean>();
const [playerNameMapping, setPlayerNameMapping] = useSessionstorageState<Record<string, UserData>>(
const [playerNameMapping, setPlayerNameMapping] = useSessionstorageState<Record<string, { id: string }>>(
`${client.appId}_player_mapping`,
{}
);
const [user, setUserInfo] = useState<UserData>();
const [user, setUserInfo] = useState<{id: string}>();
const isLoginIn = useRef(false);

const login = async () => {
Expand Down Expand Up @@ -112,11 +112,11 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi

const createGame = useCallback(async () => {
if (token) {
return client.create(token, IInitializeRequest.default());
return client.createPrivateLobby(token);
} else {
const token = (await login()) ?? "";

return client.create(token, IInitializeRequest.default());
return client.createPrivateLobby(token);
}
}, [token]);

Expand Down Expand Up @@ -179,11 +179,8 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi
const getUserName = useCallback(
(userId: string) => {
if (Boolean(playerNameMapping[userId])) {
return playerNameMapping[userId].name;
return playerNameMapping[userId].id;
} else {
lookupUser(userId).then((response) => {
setPlayerNameMapping((curr) => ({ ...curr, [userId]: response }));
});
return userId;
}
},
Expand Down
14 changes: 6 additions & 8 deletions examples/uno/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
FROM node:16
FROM node:18

WORKDIR /app

RUN npm i -g [email protected]

ENV NODE_ENV=production
COPY . .

ARG APP_SECRET
ENV APP_SECRET=${APP_SECRET}
RUN npm i -g [email protected]
RUN npx hathora build --only server

COPY . .
RUN hathora build --only server
ENV NODE_ENV=production
ENV DATA_DIR=/app/data

CMD ["node", "server/dist/index.mjs"]
17 changes: 7 additions & 10 deletions examples/uno/client/web/src/context/GameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSessionstorageState } from "rooks";
import { HathoraClient, HathoraConnection } from "../../../.hathora/client";
import { ConnectionFailure } from "../../../.hathora/failures";
import { Card, PlayerState, IInitializeRequest } from "../../../../api/types";
import { lookupUser, UserData, Response } from "../../../../api/base";
import { Response, UserData } from "../../../../api/base";

interface GameContext {
token?: string;
Expand All @@ -20,7 +20,7 @@ interface GameContext {
drawCard: () => Promise<void>;
endGame: () => void;
getUserName: (id: string) => string;
user?: UserData;
user?: { id: string };
connecting?: boolean;
loggingIn?: boolean;
}
Expand Down Expand Up @@ -58,11 +58,11 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi
const [connectionError, setConnectionError] = useState<ConnectionFailure>();
const [connecting, setConnecting] = useState<boolean>();
const [loggingIn, setLoggingIn] = useState<boolean>();
const [playerNameMapping, setPlayerNameMapping] = useSessionstorageState<Record<string, UserData>>(
const [playerNameMapping, setPlayerNameMapping] = useSessionstorageState<Record<string, { id: string }>>(
`${client.appId}_player_mapping`,
{}
);
const [user, setUserInfo] = useState<UserData>();
const [user, setUserInfo] = useState<{ id: string }>();
const isLoginIn = useRef(false);

const login = async () => {
Expand Down Expand Up @@ -110,10 +110,10 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi

const createGame = useCallback(async () => {
if (token) {
return client.create(token, IInitializeRequest.default());
return client.createPrivateLobby(token);
} else {
const token = await login()!;
return client.create(token!, IInitializeRequest.default());
return client.createPrivateLobby(token!);
}
}, [token]);

Expand Down Expand Up @@ -160,11 +160,8 @@ export default function HathoraContextProvider({ children }: HathoraContextProvi
const getUserName = useCallback(
(userId: string) => {
if (Boolean(playerNameMapping[userId])) {
return playerNameMapping[userId].name;
return playerNameMapping[userId].id;
} else {
lookupUser(userId).then((response) => {
setPlayerNameMapping((curr) => ({ ...curr, [userId]: response }));
});
return userId;
}
},
Expand Down

0 comments on commit 25c93b1

Please sign in to comment.