-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocketTest.js
82 lines (77 loc) · 2.47 KB
/
socketTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import readline from "node:readline";
import { io } from "socket.io-client";
const question = (question) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(question, (answer) => {
rl.close();
resolve(answer);
});
});
};
const clientSocket = io("http://127.0.0.1:3000");
clientSocket.on("join");
clientSocket.on("makedMatch", (data) => {
let jsonData = JSON.parse(data);
console.log("makedMatch:", data);
clientSocket.emit("matchEntry", data);
});
clientSocket.on("matchNotification", (data) => {
console.log("알림: ", data);
});
clientSocket.on("rpsMatchStart", async (data) => {
let jsonData = JSON.parse(data);
let player;
let playerElo;
let opponent;
let opponentElo;
console.log("닉네임을 입력하세요");
let inputNickname = await question("> ");
if (inputNickname === jsonData.player1) {
player = jsonData.player1;
playerElo = jsonData.player1Elo;
opponent = jsonData.player2;
opponentElo = jsonData.player2Elo;
} else {
player = jsonData.player2;
playerElo = jsonData.player2Elo;
opponent = jsonData.player1;
opponentElo = jsonData.player1Elo;
}
let roomId = jsonData.roomId;
console.log("---------매치 시작!---------");
let point = 0;
clientSocket.on("rpsMatchRoundResult", (data) => {
let { round, result } = JSON.parse(data);
point += result;
console.log(`포인트 변화 ${result}`);
if (round >= 5) {
if (point > 0) console.log("승리!");
else if (point < 0) console.log("패배!");
else console.log("무승부!");
let sendData = { roomId, player, playerElo, opponent, opponentElo, point };
clientSocket.emit("rpsMatchResult", JSON.stringify(sendData));
}
});
for (let round = 1; round < 6; round++) {
console.log(`${round}R 패를 입력하세요.(0: 묵, 1: 찌, 2: 빠) `);
let handInput = await question(`> `);
const hand = parseInt(handInput);
let sendData = { roomId, player, opponent, round, hand };
clientSocket.emit("rpsMatchRound", JSON.stringify(sendData));
}
});
(async function () {
let input = await question("command 입력 > ");
let command = input.split(" ")[0];
switch (command) {
// matchMaking nickname sport 순으로 입력
case "matchMaking":
const [_, nickname, sport] = input.split(" ");
clientSocket.emit("matchMaking", JSON.stringify({ nickname, sport }));
break;
}
})();