Skip to content

Commit

Permalink
Merge pull request #103 from karrotmvp/feat/game-2048
Browse files Browse the repository at this point in the history
Feat/game 2048
  • Loading branch information
jongwooha98 authored Nov 22, 2021
2 parents 9b637f4 + 08b8a97 commit 54ec6b3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 34 deletions.
21 changes: 3 additions & 18 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useState } from 'react';
import '@karrotframe/navigator/index.css';
import { Navigator, Screen, useNavigator } from '@karrotframe/navigator';
import { Navigator, Screen } from '@karrotframe/navigator';
import { Home } from 'pages/Home';
import { Game2048Home } from 'pages/Game2048/Home';
import { Game2048Game } from 'pages/Game2048/Game';
Expand Down Expand Up @@ -31,8 +31,7 @@ import { useMinigameApi } from 'services/api/minigameApi';
const App: React.FC = () => {
const minigameApi = useMinigameApi();

const { setRegionInfo, setTownInfo, setUserInfo, setIsInstalled } =
useUserData();
const { setRegionInfo, setTownInfo, setIsInstalled } = useUserData();
const { accessToken } = useAccessToken();
const { signAccessToken, removeCookie } = useSignAccessToken();
const [analytics, setAnalytics] = useState(emptyAnalytics);
Expand Down Expand Up @@ -97,21 +96,6 @@ const App: React.FC = () => {
[minigameApi.regionApi, setTownInfo]
);

// const updateUserInfo = useCallback(async () => {
// console.log('update user info attempt');
// const {
// data: { data },
// } = await minigameApi.userApi.getUserInfoUsingGET();
// console.log(data);
// if (data) {
// setUserInfo(data.id, data.nickname);
// // FA: track user with set user id
// analytics.setUserId(data.id);

// console.log('setuserinfo', data.id, data.nickname);
// }
// }, [analytics, minigameApi.userApi, setUserInfo]);

const fetchData = useCallback(
async (code: string, regionId: string) => {
console.log('fetch data', code, regionId);
Expand Down Expand Up @@ -140,6 +124,7 @@ const App: React.FC = () => {
}

fetchData(code as string, regionId as string);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
// accessToken,
analytics,
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useAccessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export const useSignAccessToken = () => {
if (data) {
setCookie('accessToken', data.accessToken);
console.log('useSignAccessToken, setCookie:', cookies.accessToken);

return data.accessToken;
return true;
}
} catch (error) {
console.log(error);
Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useMini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,25 @@ export const useMini = () => {
};

// Third-party agreement handler
const handleThirdPartyAgreement = async (runOnSuccess: () => void) => {
const handleThirdPartyAgreement = async (runOnSuccess?: () => void) => {
mini.startPreset({
preset: presetUrl!,
params: {
appId: appId!,
},
onSuccess: function (result) {
onSuccess: async function (result) {
if (result && result.code) {
signAccessToken(result.code, regionId);
const response = await signAccessToken(result.code, regionId);
analytics.logEvent('click_karrot_mini_preset_agree_button', {
game_type: 'karrot-clicker',
});
runOnSuccess();
console.log(response);
if (response === true) {
await updateUserInfo();
if (runOnSuccess) {
runOnSuccess();
}
}
}
},
onClose: function () {
Expand Down
34 changes: 34 additions & 0 deletions src/pages/Game2048/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import {
} from './Score';
import refreshGameUrl from 'assets/svg/game2048/refresh_game.svg';
import { useAnalytics } from 'services/analytics';
import { useUserData } from 'hooks';

export const Game: React.FC = () => {
const analytics = useAnalytics();
const { isTop } = useCurrentScreen();
const minigameApi = useMinigameApi();
const { userId, setUserInfo } = useUserData();
const { score: myBestScore, highestScore, gameType } = useMyGame2048Data();
const {
score: currentScore,
Expand Down Expand Up @@ -102,6 +104,38 @@ export const Game: React.FC = () => {
getTownieBestScoreEver();
}
}, [getTownieBestScoreEver, isTop]);

const updateUserInfo = useCallback(async () => {
console.log('update user info attempt, userId:', userId);
if (userId) {
return;
} else {
try {
const {
data: { data },
} = await minigameApi.userApi.getUserInfoUsingGET();
console.log(data);
if (data) {
setUserInfo(data.id, data.nickname);
// FA: track user with set user id
analytics.setUserId(data.id);

console.log('setuserinfo', data.id, data.nickname);
}
} catch (error) {
console.error(error);
}
}
}, [analytics, minigameApi.userApi, setUserInfo, userId]);

useEffect(() => {
if (isTop) {
if (userId === '') {
updateUserInfo();
}
}
}, [isTop, updateUserInfo, userId]);

return (
<>
<Page className="game-page">
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Game2048/Game/Modal/GameOver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const GameOver: React.FC<Props> = (props) => {
} catch (error) {
console.error(error);
}
}, [gameType, minigameApi.gameUserApi]);
}, [gameType, minigameApi.gameUserApi, updateMyScore]);

const handleViewLeaderboard = async () => {
console.log('try to view leaderboard');
Expand Down
5 changes: 1 addition & 4 deletions src/pages/Game2048/Game/hooks/useGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const useGame = () => {
const [state, dispatch] = useReducer(game2048Reducer, initialState);
const { score, tiles, byIds, hasChanged, inMotion } = state;
const [isGameOver, setIsGameOver] = useState<boolean>(false);
const [nextBoard, setNextBoard] = useState<number[]>();

const createTile = useCallback(
({ coordinate, value }: Partial<TileProps>) => {
Expand Down Expand Up @@ -222,10 +221,8 @@ export const useGame = () => {
setTimeout(() => {
dispatch(moveEndAction());
}, animationDuration);

setNextBoard(() => retrieveTileMap());
},
[dispatch, retrieveTileMap, throttledMergeTile, tiles, updateTile]
[dispatch, throttledMergeTile, tiles, updateTile]
);

const moveLeftFactory = (board: number[]) => {
Expand Down
8 changes: 6 additions & 2 deletions src/pages/Game2048/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ export const Home = () => {
is_new_user: false,
});
};
const onNewUserSuccessHandler = () => {
addPlayerCount();
goToGamePage();
};
const handleNewUser = () => {
// if user is new, open third-party agreement preset
analytics.logEvent('click_game_start_button', {
game_type: 'game-2048',
is_new_user: true,
});
handleThirdPartyAgreement(goToGamePage);
handleThirdPartyAgreement(onNewUserSuccessHandler);
};
const handleGameStart = () => {
// bypass in web environment
Expand All @@ -91,7 +95,6 @@ export const Home = () => {
addPlayerCount();
} else {
handleNewUser();
addPlayerCount();
}
};
// =================================================================
Expand Down Expand Up @@ -253,6 +256,7 @@ export const Home = () => {
height: `calc(100vh - ${navHeight}px - 90px)`,
width: `100%`,
}}
onScroll={onScroll}
>
<div
style={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { css } from '@emotion/css';
import styled from '@emotion/styled';
import { TopUserRow } from '../Row/TopRow';
import { DefaultUserRow } from '../Row/DefaultRow';
Expand Down
38 changes: 36 additions & 2 deletions src/pages/KarrotClicker/Game/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import styled from '@emotion/styled';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import Modal from 'react-modal';
import { commafy } from 'utils/numberFunctions';
import { useAnalytics } from 'services/analytics';
Expand All @@ -13,13 +13,16 @@ import { useCurrentScreen } from '@karrotframe/navigator';
import { useMyKarrotClickerData } from '../hooks';
import { GameOver, GamePause } from './Modal';
import { useGame } from './hooks';
import { useUserData } from 'hooks';
import { useMinigameApi } from 'services/api/minigameApi';

Modal.setAppElement(document.createElement('div'));

export const Game = () => {
const { isTop } = useCurrentScreen();
const analytics = useAnalytics();

const { userId, setUserInfo } = useUserData();
const minigameApi = useMinigameApi();
const { score } = useMyKarrotClickerData();
const {
clickCount,
Expand Down Expand Up @@ -61,6 +64,37 @@ export const Game = () => {
}
}, [analytics, isTop]);

const updateUserInfo = useCallback(async () => {
console.log('update user info attempt, userId:', userId);
if (userId) {
return;
} else {
try {
const {
data: { data },
} = await minigameApi.userApi.getUserInfoUsingGET();
console.log(data);
if (data) {
setUserInfo(data.id, data.nickname);
// FA: track user with set user id
analytics.setUserId(data.id);

console.log('setuserinfo', data.id, data.nickname);
}
} catch (error) {
console.error(error);
}
}
}, [analytics, minigameApi.userApi, setUserInfo, userId]);

useEffect(() => {
if (isTop) {
if (userId === '') {
updateUserInfo();
}
}
}, [isTop, updateUserInfo, userId]);

return (
<>
<PageContainer>
Expand Down

0 comments on commit 54ec6b3

Please sign in to comment.