-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
164 lines (129 loc) · 4.31 KB
/
Copy pathindex.js
File metadata and controls
164 lines (129 loc) · 4.31 KB
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
const http = require("http");
const WebSocket = require("websocket").server;
const express = require("express");
const app = express();
app.get("/", (req,res)=> res.sendFile(__dirname + "/index.html"));
app.listen(9091, ()=> console.log("Web server listening on port 9091"));
const httpServer = http.createServer();
const port = 3000;
httpServer.listen(port, ()=>{
console.log(`Socket server listening on port ${port}`);
})
const websocket = new WebSocket({
"httpServer": httpServer,
})
//hashmap
const clients = {};
const games = {};
websocket.on("request", (request)=>{
const connection = request.accept(null, request.origin);
connection.on("open", ()=>{
console.log("connection opened...!");
})
connection.on("close", ()=>{
console.log("connection closed...!");
})
connection.on("message", (message)=>{
//received a msg from the client
const result = JSON.parse(message.utf8Data);
console.log("received a message "+result);
let client;
let gameId;
let conn;
let payload;
switch(result.method){
case "create":
//a user wants to create a new game
client = result.clientId;
gameId = generateRandomId(len);
games[gameId] = {
"id": gameId,
"clients" : [],
"balls": 20,
}
payload = {
"method": "create",
"game" : games[gameId],
}
console.log("game created with id "+ payload.game.id);
conn = clients[clientId].connection;
conn.send(JSON.stringify(payload));
break;
case "join":
// a user wants to join a game
client = result.clientId;
gameId = result.gameId;
const game = games[gameId];
if(!game){
console.log("game not found...");
}
if(game.clients.length>=3){
console.log("too many players in this room already...")
return;
}
const mycolor = {"0": "red", "1":"green", "2":"blue"}[game.clients.length]
game.clients.push({
"clientId": client,
"color": mycolor,
})
if(game.clients.length==3){
updateGameState();
}
payload = {
"method": "join",
"game": game,
}
game.clients.forEach((c)=>{
let hisConn = clients[c.clientId].connection;
hisConn.send(JSON.stringify(payload))
})
console.log("gonna join game", result.gameId);
break;
case "play":
clientId = result.clientId;
gameId = result.gameId;
const ballId = result.ballId;
let color = result.color;
let state = games[gameId].state;
if(!state){
state = {}
}
state[ballId] = color;
games[gameId].state = state;
break;
}
})
//generate a new client id
let clientId = generateRandomId(len);
clients[clientId]={
"connection": connection,
}
const payload = {
"method": "connect",
"client": clientId,
}
connection.send(JSON.stringify(payload));
})
const len = 8;
function generateRandomId(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function updateGameState(){
for (const g of Object.keys(games)){
const game = games[g];
const payload = {
"method": "update",
"game": game
}
game.clients.forEach(c=>{
clients[c.clientId].connection.send(JSON.stringify(payload));
})
}
setTimeout(updateGameState, 500);
}