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

feat: online-game 모든 로직 비동기로 변경 #486

Merged
merged 7 commits into from
Mar 12, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 58 additions & 59 deletions frontend/src/pages/online-game/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function OnlineGame($container, info) {

let myNickname = null;
let myMatch = 1;
let bar1, bar2, ball, $toast, toastt, canvas, ctx;
let initMyInfo = async () => {
myNickname = await getUserMe().then((user) => user.data.nickname);
if (
Expand All @@ -38,9 +39,8 @@ export default function OnlineGame($container, info) {
)
myMatch = 3;
};

const init = () => {
initMyInfo();
const init = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분이 async 적용 부분이군요

await initMyInfo();
hideHeader();
this.render();
this.renderScoreBoard();
Expand All @@ -51,10 +51,61 @@ export default function OnlineGame($container, info) {
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);

const $toast = document.querySelector(".toast");
const toast = new bootstrap.Toast($toast);
toast.show(); // Toast를 보여줍니다.
$toast = document.querySelector(".toast");
toastt = new bootstrap.Toast($toast);
toastt.show(); // Toast를 보여줍니다.
window.addEventListener("beforeunload", disconnectWebSocket);
canvas = $container.querySelector("#gameCanvas");
ctx = canvas.getContext("2d");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight * 0.88; // header의 height가 12vh이므로 88%만큼의 height를 가짐
console.log(canvas.width, canvas.height);

bar1 = { x: 10, y: canvas.height / 2 - 50, width: 20, height: 100 };
bar2 = {
x: canvas.width - 30,
y: canvas.height / 2 - 50,
width: 20,
height: 100,
};
ball = { x: canvas.width / 2, y: canvas.height / 2, radius: 10 };
ws.send(
JSON.stringify({
type: myMatch === 3 ? "match3_start" : "start",
data: {
map_width: canvas.width,
map_height: canvas.height,
},
}),
);

ws.onmessage = (event) => {
const data = JSON.parse(event.data);
// console.log(data);
if (data.type === "in_game") {
bar1.x = data.data.left_side_player.x;
bar1.y = data.data.left_side_player.y;
bar1.width = 20;
bar1.height = 100;
bar2.x = data.data.right_side_player.x;
bar2.y = data.data.right_side_player.y;
bar2.width = 20;
bar2.height = 100;
ball.x = data.data.ball.x;
ball.y = data.data.ball.y;
draw(bar1, bar2, ball);
let score = getScore();
let newScore = {
player1: data.data.left_side_player.score,
player2: data.data.right_side_player.score,
};
if (
score.player1 !== newScore.player1 ||
score.player2 !== newScore.player2
)
setScore(newScore);
} else if (data.type === "game_end") endGame(data, ws);
};
};

const disconnectWebSocket = () => {
Expand Down Expand Up @@ -137,12 +188,10 @@ export default function OnlineGame($container, info) {
if (e.key === "ArrowUp") {
if (keyState.up === false) return;
keyState.up = false;
console.log("up");
ws.send(JSON.stringify({ type: "keyboard", data: "up" }));
} else if (e.key === "ArrowDown") {
if (keyState.down === false) return;
keyState.down = false;
console.log("down");
ws.send(JSON.stringify({ type: "keyboard", data: "down" }));
}
};
Expand Down Expand Up @@ -173,57 +222,7 @@ export default function OnlineGame($container, info) {
}

init();
const canvas = $container.querySelector("#gameCanvas");
const ctx = canvas.getContext("2d");
canvas.width = document.body.clientWidth;
canvas.height = document.body.clientHeight * 0.88; // header의 height가 12vh이므로 88%만큼의 height를 가짐
console.log(canvas.width, canvas.height);

let bar1 = { x: 10, y: canvas.height / 2 - 50, width: 20, height: 100 };
let bar2 = {
x: canvas.width - 30,
y: canvas.height / 2 - 50,
width: 20,
height: 100,
};
let ball = { x: canvas.width / 2, y: canvas.height / 2, radius: 10 };
ws.send(
JSON.stringify({
type: myMatch === 3 ? "match3_start" : "start",
data: {
map_width: canvas.width,
map_height: canvas.height,
},
}),
);

ws.onmessage = (event) => {
const data = JSON.parse(event.data);
// console.log(data);
if (data.type === "in_game") {
bar1.x = data.data.left_side_player.x;
bar1.y = data.data.left_side_player.y;
bar1.width = 20;
bar1.height = 100;
bar2.x = data.data.right_side_player.x;
bar2.y = data.data.right_side_player.y;
bar2.width = 20;
bar2.height = 100;
ball.x = data.data.ball.x;
ball.y = data.data.ball.y;
draw(bar1, bar2, ball);
let score = getScore();
let newScore = {
player1: data.data.left_side_player.score,
player2: data.data.right_side_player.score,
};
if (
score.player1 !== newScore.player1 ||
score.player2 !== newScore.player2
)
setScore(newScore);
} else if (data.type === "game_end") endGame(data, ws);
};
let matchEndCnt = 0;
function match3Logic(ws) {
ws.onmessage = null;
Expand All @@ -234,7 +233,6 @@ export default function OnlineGame($container, info) {
};
}
function endGame(data, ws) {
console.log(myMatch, data);
if (myMatch !== data.data.match) {
matchEndCnt++;
return;
Expand All @@ -256,6 +254,7 @@ export default function OnlineGame($container, info) {
};
}
} else {
ws.close();
navigate(
`/histories/details?mode=${endData.game_mode}&gameId=${endData.game_id}`,
{ gameId: endData.game_id },
Expand Down
Loading