Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PongのフロントエンドのNextでの実装 #23

Merged
merged 9 commits into from
Oct 26, 2022
Merged

Conversation

takashi247
Copy link
Collaborator

@takashi247 takashi247 commented Oct 24, 2022

概要

  • 元々 React で実装していた Pong のフロントエンドを Next && TypeScript で実装し直しました
  • バックエンド用にbackend_nodjsというディレクトリを作って、一旦 Node.js で簡易的に動くサーバーを作っています

受入条件

下記の手順で、ローカルでもちゃんと動くか確認

  • frontendディレクトリでyarn && yarn build && yarn start
  • backend_nodejsディレクトリでyarn && node server.js
  • その後 2 つのブラウザのウィンドウで http:localhost:3000/pong にアクセス
  • 各ウィンドウで上下のキーを押すと、一方のバーが操作できるか確認

ゲームの描画の流れに関するざっくり解説

  • クライアントとサーバーは30FPS (=33msに1回)の頻度で下記の情報を送り合う
    • サーバー→クライアント: クライアント側で描画するボール、及び、各プレーヤーが操作するバーの位置に関する情報
    • クライアント→サーバー: 前回情報を送った時点から今回送る時点までに、バーが移動した距離
  • 描画に関する計算のほとんどはサーバーサイドで実施。サーバーは各プレーヤーから送られてきたバーの位置の情報と、跳ね回るボールの位置の情報を統合して、次のタイミングでボールがどこにいくのかを計算し、各プレーヤーのバーの位置と合わせてプレーヤーに送信
  • クライアントサイドではサーバーから送られてきた情報を描画するだけ
    • 現状、ステージの描画はフロントエンドのロジックで実施しているが、最終的にはこの部分もサーバーサイドに移管予定

参考URL

備考

  • 一旦動かすこと重視で実装しているので、諸々のパラメータがハードコードされている状況です。こちらは ブラウザの画面サイズに応じたResponsiveデザインの導入 #17 に対応する際に修正したいと思います
  • 関数名、ファイル名、タイプ名、変数名等の諸々の命名規則について、なにかこういう形で揃えた方がいい、というようなコメントあればください
  • ファイルの分け方とかもアドバイスあれば頂きたいです

close #13

Comment on lines 10 to 12
interface Player {
height: number;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

型を拡張しない場合は typeを使ってみてもいいかもしれません。
理由としてはinterfaceが簡単に型を拡張できてしまうので、予期せぬバグを生む可能性があるからです。

// こんな感じ
type Player = {
  heght: number;
};

参考:https://zenn.dev/luvmini511/articles/6c6f69481c2d17

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!ここ、モヤモヤしてたところなので、参考リンク、勉強になりました。修正しておきます。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryo-manba こちら、先程修正をプッシュしました。お手すきの際に確認よろしくお願いします。

Comment on lines +2 to +4
// import Head from 'next/head';
// import Image from 'next/image';
// import styles from '../styles/Home.module.css';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、これリンターに怒られるやつですよね。
pre-commit導入前に出したやつだったので、対応漏れてました。すみません。。

@ryo-manba
Copy link
Owner

受入条件と同じ方法でやってみたんですが、何も表示されないです...。
サーバーサイドでリクエストは受け入れてるっぽいんですが、、
スクリーンショット 2022-10-25 11 48 23

スクリーンショット 2022-10-25 11 50 30

@@ -0,0 +1,12 @@
import type { NextPage } from 'next';
import Canvas from './Canvas';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

小文字にしないとbuild時に怒られました。ファイル名と合わせる必要があるかもです

'./Canvas' -> './canvas';

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ、ファイル名の方を大文字スタートに変えたつもりだったのですが、変更漏れしてました。修正します。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryo-manba こちらも、先程修正をプッシュしました。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すいません、これ、なぜかまたファイル名の変更、反映されてませんでした。。。
index.tsxの方を修正してプッシュし直しました。

@takashi247
Copy link
Collaborator Author

takashi247 commented Oct 25, 2022

受入条件と同じ方法でやってみたんですが、何も表示されないです...。 サーバーサイドでリクエストは受け入れてるっぽいんですが、、

@ryo-manba 早速の確認、ありがとうございます!サーバーのログを見ると、サーバー立ち上げた途端に20近くのアクセスが3000番ポートに来ているようなので、VSCodeを再起動、またはMac自体を再起動してみて、もう一度試してもらえないでしょうか?それでもうまく動かなければ、ブラウザーのdeveloper toolのコンソールの画面のスクショもアップロードして頂けるとありがたいです。

@ryo-manba
Copy link
Owner

@takashi247
原因特定できました。単にglobal.cssで画面が黒くなっているから見えてないだけでした。

サーバー立ち上げた途端に20近くのアクセスが3000番ポートに来ているようなので

スクショが悪かったです。何度かリロードしてたので、アクセスが多いように見えてます。
実際はアクセスしたタイミングのみ増えるようになってます。お手数おかけしました。。

@rakushoo
Copy link
Collaborator

確認しました。
frontend:frontend/pages/pong/index.tsx の from ./Canvas を小文字にしたらサーバー起動しました。
CHAT,GAME,FRIENDのボタンが表示されました。

backend_nodejs:
yarn && node server.js コマンドでは、モジュールが見つからず起動できてないです。。
何かインストールが必要でしょうか?


shogo@[22:18:12]:~/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs% yarn && node server.js
yarn install v1.22.19
[1/4] 🔍 Resolving packages...
success Already up-to-date.
✨ Done in 0.12s.
node:internal/modules/cjs/loader:988
throw err;
^

Error: Cannot find module '/Users/shogo/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs/server.js'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15)
at Function.Module._load (node:internal/modules/cjs/loader:833:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:22:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}

@yuta-fujimoto
Copy link
Collaborator

yuta-fujimoto commented Oct 25, 2022

確認しました。 frontend:frontend/pages/pong/index.tsx の from ./Canvas を小文字にしたらサーバー起動しました。 CHAT,GAME,FRIENDのボタンが表示されました。

backend_nodejs: yarn && node server.js コマンドでは、モジュールが見つからず起動できてないです。。 何かインストールが必要でしょうか?

shogo@[22:18:12]:~/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs% yarn && node server.js yarn install v1.22.19 [1/4] 🔍 Resolving packages... success Already up-to-date. ✨ Done in 0.12s. node:internal/modules/cjs/loader:988 throw err; ^

Error: Cannot find module '/Users/shogo/work/development/42Tokyo/ft_transcnendence/pong-frontend/backend_nodejs/server.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:985:15) at Function.Module._load (node:internal/modules/cjs/loader:833:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:22:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }

node server.jsで自分は動かせました。

@takashi247
Copy link
Collaborator Author

@rakushoo 確認、ありがとうございます!

確認しました。 frontend:frontend/pages/pong/index.tsx の from ./Canvas を小文字にしたらサーバー起動しました。 CHAT,GAME,FRIENDのボタンが表示されました。

先程追加でプッシュした分がローカルにプルで来ていないのではないかと思います。
最新のブランチはファイル名を修正しているので、小文字にしないでもサーバー起動すると思います。

backend_nodejs: yarn && node server.js コマンドでは、モジュールが見つからず起動できてないです。。 何かインストールが必要でしょうか?

ログを見る限り、yarnは成功しているようなので、node server.jsだけで動くかやってみてもらえないでしょうか?

barMove: (move: number) => void;
};

const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このtypeの引数っていままで知らなくて、統合して通信するデータの型が増えたときにそれを使うべきなのかわかってないです。使う理由はc++のインターフェースみたいにその実装を強制できるという点なんでしょうか?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使う理由はc++のインターフェースみたいにその実装を強制できるという点なんでしょうか?

使う理由としては、私も同じ理解です。事前にemitとonで情報を送受信する型を指定しておくことで、フロントエンドとバックエンドのAPI的なものを決めて実装できる、というのが嬉しいポイントなのではないかと思っています。
私は公式ページを参考に実装したのですが、ここでも、サーバーサイドとクライアントサイドで同じ型を使うことで、実装のずれが起きないようにしてます。
なので、どこかのタイミングでフロントエンドとバックエンドで使う型を揃えて、別ファイルに定義して、フロントエンド、バックエンド双方で、その型定義をimportして使う、という感じにできると、TypeScriptの効能を最大限活かしながらSocket.ioを使っている感が出せるのではないかと、書いていて思いました。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます、共通で定義できるのは確かに大きいですね。

@yuta-fujimoto
Copy link
Collaborator

socketやnext対応など参考になりました。
また、Canvasに関しては完全にはわかってないのですが動作は確認ができました。
つなげることに関してですが、見た限り、フロントエンドのページ遷移のみできればできるのではと思いました。
ただ、

  • socket周りのコードの書き方に関してコメントに書きましたが、統一したい
  • ディレクトリ構成が微妙に違うので後で合わせたい
    というのが考えているところです。

特に修正箇所化などは見当たりませんでしたが、こうらくさんも見ていらっしゃるのでマージは待っておきます

@takashi247
Copy link
Collaborator Author

@yuta-fujimoto 確認、ありがとうございます!

socketやnext対応など参考になりました。 また、Canvasに関しては完全にはわかってないのですが動作は確認ができました。 つなげることに関してですが、見た限り、フロントエンドのページ遷移のみできればできるのではと思いました。

私も先程ゆうたさんのプルリクも一通り確認させて頂きました。まだGatewaysの理解が完全ではないですが、私もかなりスムーズにつなぎ合わせられそうな印象を持ちました。
イメージとしては

  • フロントエンド: CanvasのコンポーネントをPlay.tsxの中で表示させる
  • バックエンド: sever.jsのロジックをgame.gateway.tsに統合

という感じになるかな、と思っています。

  • socket周りのコードの書き方に関してコメントに書きましたが、統一したい
  • ディレクトリ構成が微妙に違うので後で合わせたい
    というのが考えているところです。

同意です。Socket周りの型定義については先程コメントバックしましたが、一応、私の実装の方では丁寧に書いては見ましたが、今回のプロジェクトはそんな巨大プロジェクトでもないので、別にリンターに怒られないならいちいち型定義を書かなくてもいいかなぁ、とも思ってます。
ディレクトリ構成については、基本的にゆうたさんの構成を活かしつつ、私の方のファイルはマージする感じにできればと思っています。

@rakushoo
Copy link
Collaborator

rakushoo commented Oct 25, 2022

backend_nodejs は起動できました。ありがとうございます。

frontend ですが、canvasの小文字大文字の件は解消しました。
yarn && yarn build && yarn start で実施したところ、以下のエラーで起動できず、
"> Build optimization failed: found pages without a React Component as default export in" とあるので
reactの必要なライブラリなどが私のPCにはインストールされてないからかと思います。
こちらでReact用開発環境のインストールを試してみます。


shogo@[23:24:47]:~/work/development/42Tokyo/ft_transcnendence/pong-frontend2/frontend% yarn && yarn build && yarn start pong-frontend
yarn install v1.22.19
$ typesync || :
/bin/sh: typesync: command not found
[1/4] 🔍 Resolving packages...
success Already up-to-date.
$ cd .. && husky install .config/.husky
husky - Git hooks installed
✨ Done in 0.62s.
yarn run v1.22.19
$ next build
info - Linting and checking validity of types .warn - The Next.js plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/basic-features/eslint#migrating-existing-config
info - Linting and checking validity of types ..Warning: React version not specified in eslint-plugin-react settings. See https://github.com/jsx-eslint/eslint-plugin-react#configuration .
info - Linting and checking validity of types
info - Creating an optimized production build
info - Compiled successfully

Build optimization failed: found pages without a React Component as default export in
pages/game/home/components/Layout
pages/game/ClientSocket
pages/game/home/components/Wait
pages/game/home/components/Watch
pages/game/home/components/Display
pages/game/home/components/Play
pages/game/home/components/Start

See https://nextjs.org/docs/messages/page-without-valid-component for more info.

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@rakushoo
Copy link
Collaborator

rakushoo commented Oct 25, 2022

追加情報ですが、エラーログの最後に参照しろ、とあった、
% yarn run dev
を試したら、無事にページが表示されました。

http://localhost:3000/
動作→ボタンが3つあるが、押しても何も起こらない

http://localhost:3000/game/home
動作→fyutaさんのマッチングのページが表示される

http://localhost:3000/pong
動作→pongの画面が表示。ボールは動くが、バーは動かせない。(マウス、カーソルキー共に)

@yuta-fujimoto
Copy link
Collaborator

yuta-fujimoto commented Oct 25, 2022

@takashi247
了解しました。現状でもfront, backend1ファイルずつで型定義は大丈夫そうなのでServetToClientEventはなしでいいかと思います。
またマージ方法についてもありがとうございます。それではマージのほうはお願いしても大丈夫でしょうか?

つなげる形で大丈夫とのことでしたが一応、僕のほうは無駄にJSON.stringfyしてたりNext対応してなかったりすることが分かったのでマージが完了し次第、リファクタリングをしようと思ってます

@takashi247
Copy link
Collaborator Author

@yuta-fujimoto
了解しました。 #14 へ対応する中で、ゆうたさんのコードへのマージをやってみます。Socketの型定義についても了解です。一旦やってみて、やりにくい部分が出てきたらまた相談させてください。

@rakushoo
Copy link
Collaborator

PC再起動を行い、backend側で、Palyerのログが出ない状態から始めて確認したところ、
2つのページでそれぞれカーソルが動かせることが確認できました。

@rakushoo rakushoo merged commit 72b4705 into main Oct 26, 2022
@rakushoo rakushoo deleted the pong-frontend branch October 26, 2022 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pongのフロントエンド側のNextへの移行
4 participants