-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
199 lines (171 loc) · 6.13 KB
/
index.html
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="btnCreate">New Game</button>
<button id="btnJoin">Join Game</button>
<input type="text" id="txtGameId" placeholder="Enter game ID">
<div id="divPlayers" style="position: absolute; display: flex;"></div>
<div id="divBoard"></div>
<script>
let clientId = null;
let gameId = null;
let playerColor = null
let player = null
let ws = new WebSocket('ws://localhost:9090');
const btnCreate = document.getElementById('btnCreate');
const btnJoin = document.getElementById('btnJoin');
const textGameId = document.getElementById('txtGameId');
const divPlayers = document.getElementById('divPlayers');
const divBoard = document.getElementById('divBoard');
let playersData = [];
let playersDataObj = {};
let mousePos = {
x: 0,
y: 0
}
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = window.innerWidth * 0.7;
canvas.height = window.innerHeight * 0.7;
canvas.style.boxShadow = 'inset 0 0 10px #000';
canvas.style.marginTop = '24px'
divBoard.appendChild(canvas);
function isOpen(ws) { return ws.readyState === ws.OPEN }
btnCreate.addEventListener('click', () => {
const payload = {
"method": "create",
"clientId": clientId
};
if (!isOpen(ws)) return;
ws.send(JSON.stringify(payload));
});
btnJoin.addEventListener('click', () => {
if (gameId === null) {
gameId = textGameId.value;
}
const payload = {
"method": "join",
"clientId": clientId,
"gameId": gameId
};
if (!isOpen(ws)) return;
ws.send(JSON.stringify(payload));
});
ws.onmessage = message => {
const response = JSON.parse(message.data);
if (response.method === 'connect') {
clientId = response.clientId
console.log("client id is set successfully " + clientId);
}
if (response.method === 'create') {
gameId = response.game.id
console.log("game successfully created with id " + gameId);
navigator.clipboard.writeText(gameId).then(function () {
console.log("game id is copied to clipboard");
}, function () {
console.log("clipboard write failed");
});
}
if (response.method === 'update' && response.game?.state) {
const game = response.game
playersData = []
game.clients.forEach(client => {
// playersDataObj[client.player] = {
// color: client.color,
// x: game.state[client.player].x,
// y: game.state[client.player].y
// }
playersData.push({
color: client.color,
x: game.state[client.player].x,
y: game.state[client.player].y
})
})
}
if (response.method === 'join' || response.payload?.method === 'join') {
game = response.payload.game;
while (divPlayers.firstChild) {
divPlayers.removeChild(divPlayers.firstChild);
}
game.clients.forEach(c => {
const divPlayer = document.createElement('div');
divPlayer.style.width = "200px";
divPlayer.style.background = c.color;
divPlayer.textContent = `Player ${c.player + 1}`;
divPlayers.appendChild(divPlayer);
if (c.clientId === clientId) {
playerColor = c.color
player = c.player
}
})
canvas.addEventListener('mousemove', e => {
mousePos.x = e.clientX;
mousePos.y = e.clientY;
const payload = {
"method": "play",
"gameId": gameId,
"player": player,
"x": mousePos.x,
"y": mousePos.y,
}
ws.send(JSON.stringify(payload));
})
let stop = false;
let frameCount = 0;
let fps, fpsInterval, startTime, now, then, elapsed;
const startAnimating = (fps) => {
fpsInterval = 1000 / fps;
then = Date.now();
startTime = then;
animate();
}
// let shrinkTimer = 0
const animate = (a) => {
requestAnimationFrame(animate);
now = Date.now();
elapsed = now - then;
if (elapsed > fpsInterval) {
then = now - (elapsed % fpsInterval);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// console.log(playersDataObj)
// for (const data in playersDataObj) {
// // console.log(`${data}: ${playersDataObj[data]}`);
// ctx.fillStyle = playersDataObj[data].color
// ctx.beginPath()
// if (+data === player) {
// ctx.arc(mousePos.x - canvas.offsetLeft, mousePos.y - canvas.offsetTop, 10, 0, Math.PI * 2)
// } else {
// ctx.arc(playersDataObj[data].x - canvas.offsetLeft, playersDataObj[data].y - canvas.offsetTop, 10, 0, Math.PI * 2)
// }
// ctx.fill()
// }
playersData.forEach((p, pKey) => {
ctx.fillStyle = p.color
ctx.beginPath()
if (pKey === player) {
ctx.arc(mousePos.x - canvas.offsetLeft, mousePos.y - canvas.offsetTop, 10, 0, Math.PI * 2)
} else {
ctx.arc(p.x - canvas.offsetLeft, p.y - canvas.offsetTop, 10, 0, Math.PI * 2)
}
ctx.fill()
})
// segSystem.display()
// if (shrinkTimer % 100 === 0) {
// segSystem.removeSegment()
// }
// shrinkTimer += 2;
}
}
startAnimating(60)
}
}
</script>
<!-- <script src="/clientSide.js"></script> -->
</body>
</html>