-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (102 loc) · 3.39 KB
/
index.js
File metadata and controls
127 lines (102 loc) · 3.39 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
const http = require("http");
const app = require("express")();
app.get("/", (req,res)=> res.sendFile(__dirname + "/index.html"))
app.listen(9091, ()=>console.log("Listening on http port 9091"))
const websocketServer = require("websocket").server
const httpServer = http.createServer();
httpServer.listen(9090, () => console.log("Listening... on 9090"))
const clients = {};
const games = {};
const wsServer = new websocketServer({
"httpServer": httpServer
})
wsServer.on("request", request => {
// Client connects
const connection = request.accept(null, request.origin);
connection.on("open", () => console.log("opened!"))
connection.on("close", () => console.log("closed!"))
connection.on("message", message => {
const result = JSON.parse(message.utf8Data)
// Client creates a game
if (result.method === "create"){
const clientId = result.clientId
const gameId = guid();
games[gameId] = {
"id": gameId,
"bullets": 8,
"clients": []
}
const payLoad = {
"method": "create",
"game": games[gameId]
}
const con = clients[clientId].connection;
con.send(JSON.stringify(payLoad));
}
// Client wants to join
if (result.method === "join"){
const clientId = result.clientId;
const gameId = result.gameId;
const game = games[gameId];
if (game.clients.length >= 3){
return;
}
const color = {"0": "Red", "1": "Green", "2": "Blue"}[game.clients.length];
game.clients.push({
"clientId": clientId,
"color": color
})
if (game.clients.length === 3){
updateGameState();
}
const payLoad = {
"method": "join",
"game": game
}
// Announce new player to all clients
game.clients.forEach(c=> {
clients[c.clientId].connection.send(JSON.stringify(payLoad));
})
}
// A user plays
if (result.method === "play"){
const gameId = result.gameId;
const bulletId = result.bulletId;
const color = result.color;
let state = games[gameId].state;
if (!state){
state = {}
}
state[bulletId] = color;
games[gameId].state = state;
}
})
// genereate a new client id
const clientId = guid();
clients[clientId] = {
"connection": connection
}
const payLoad = {
"method": "connect",
"clientId": clientId
}
connection.send(JSON.stringify(payLoad))
})
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);
}
// Unique GUID generator
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
const guid = () => (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();