-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
174 lines (140 loc) · 5.15 KB
/
server.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// server.ts
import express, { Request, Response } from 'express';
import { createServer } from 'http';
import { Server, Socket } from 'socket.io';
import next from 'next';
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();
const PORT = 3001;
interface PlayerData {
roomName: string;
tapCount: number;
role: 'buyer' | 'seller'; // 역할 추가
}
const waitingPlayers: { id: string; roomId: string; role: 'buyer' | 'seller' }[] = []; // 방 ID와 역할을 함께 대기열에서 관리
const rooms: { [roomName: string]: string[] } = {};
const players: { [socketId: string]: PlayerData } = {};
nextApp.prepare().then(() => {
const app = express();
const server = createServer(app);
const io = new Server(server);
io.on('connection', (socket: Socket) => {
console.log(`User connected: ${socket.id}`);
// 클라이언트로부터 방 ID와 역할을 전달받음
socket.on('joinRoom', ({ roomId, role }: { roomId: string; role: 'buyer' | 'seller' }) => {
waitingPlayers.push({ id: socket.id, roomId, role });
attemptMatchmaking(socket, io);
});
socket.on('tap', () => {
const roomName = findPlayerRoom(socket.id);
if (roomName) {
const playerData = players[socket.id];
incrementTapCount(socket.id, playerData.role); // 역할에 따라 탭 카운트 업데이트
const opponentId = getOpponentId(roomName, socket.id);
const opponentData = players[opponentId];
// 상대방의 역할에 따라 카운트를 전송
if (opponentData.role === 'seller') {
io.to(opponentId).emit('opponentTap', { sellerTapCount: opponentData.tapCount });
} else {
io.to(opponentId).emit('opponentTap', { buyerTapCount: opponentData.tapCount });
}
}
});
socket.on('disconnect', () => {
console.log(`User disconnected: ${socket.id}`);
handleDisconnect(socket, io);
});
});
app.all('*', (req: Request, res: Response) => {
return handle(req, res);
});
server.listen(PORT, () => {
console.log(`> Ready on http://localhost:${PORT}`);
});
});
function attemptMatchmaking(socket: Socket, io: Server) {
const playerData = waitingPlayers.find(p => p.id === socket.id);
if (!playerData) return;
const { roomId } = playerData;
const waitingInRoom = waitingPlayers.filter(p => p.roomId === roomId);
if (waitingInRoom.length >= 2) {
const player1 = waitingInRoom[0];
const player2 = waitingInRoom[1];
const roomName = `room-${player1.id}-${player2.id}`;
rooms[roomName] = [player1.id, player2.id];
const player1Socket = io.sockets.sockets.get(player1.id);
const player2Socket = io.sockets.sockets.get(player2.id);
if (player1Socket && player2Socket) {
player1Socket.join(roomName);
player2Socket.join(roomName);
initializePlayerData(player1.id, roomName, player1.role); // 역할 전달
initializePlayerData(player2.id, roomName, player2.role); // 역할 전달
io.to(roomName).emit('gameStart');
startGameTimer(roomName, io, roomId); // roomId 전달
// 대기열에서 제거
waitingPlayers.splice(waitingPlayers.indexOf(player1), 1);
waitingPlayers.splice(waitingPlayers.indexOf(player2), 1);
} else {
console.error('Player socket not found during matchmaking.');
}
}
}
function initializePlayerData(playerId: string, roomName: string, role: 'buyer' | 'seller') {
players[playerId] = {
roomName: roomName,
tapCount: 0,
role: role, // 역할 저장
};
}
function incrementTapCount(playerId: string, role: 'buyer' | 'seller') {
if (players[playerId]) {
players[playerId].tapCount++; // Buyer 또는 Seller의 탭 카운트만 증가
}
}
function getPlayerTapCount(playerId: string): number {
return players[playerId]?.tapCount || 0;
}
function getOpponentId(roomName: string, playerId: string): string {
return rooms[roomName].find((id) => id !== playerId)!;
}
function findPlayerRoom(playerId: string): string | null {
return players[playerId]?.roomName || null;
}
function startGameTimer(roomName: string, io: Server, roomId: string) {
const GAME_TIME = 13000;
setTimeout(() => {
io.to(roomName).emit('gameEnd');
const playerIds = rooms[roomName];
const results = playerIds.map((id) => ({
playerId: id,
tapCount: players[id]?.tapCount || 0,
role: players[id]?.role // 역할 추가
}));
io.to(roomName).emit('gameResult', results);
cleanupGameData(roomName);
}, GAME_TIME);
}
function cleanupGameData(roomName: string) {
const playerIds = rooms[roomName];
playerIds.forEach((id) => {
delete players[id];
});
delete rooms[roomName];
}
function handleDisconnect(socket: Socket, io: Server) {
const index = waitingPlayers.findIndex(p => p.id === socket.id);
if (index !== -1) {
waitingPlayers.splice(index, 1);
}
const roomName = findPlayerRoom(socket.id);
if (roomName) {
const opponentId = getOpponentId(roomName, socket.id);
if (opponentId) {
io.to(opponentId).emit('gameEnd');
delete players[opponentId];
}
cleanupGameData(roomName);
}
delete players[socket.id];
}