-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
213 lines (159 loc) · 4.79 KB
/
server.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
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
200
201
202
203
204
205
206
207
208
209
210
211
/*global require:false, console:false, __dirname: false, document:false, window: false, Selector: false*/
// jshint esversion: 6
/*jslint node:true*/
"use strict";
//Paint server for testing
//require server components
const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const ip = require('ip');
const port = process.env.PORT || 8080;
//let the server listen on port 80
server.listen(port);
//tell console the server is running
console.log(`Hackathon 2! IP is ${ip.address()}:${port}`);
//tell where to find page depended info. css/javascript/images/audio/and other stuff
app.use(express.static('Public'));
//do this on a connection from the client
app.get('/', (req, res) => {
//respond with this for the client
res.sendFile(__dirname + '/index.html');
console.log("Connection!");
});
app.get('/index.html', (req, res) => {
//respond with this for the client
res.sendFile(__dirname + '/index.html');
console.log("Connection!");
});
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/Socket IO CODE\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
//init variables
var users = {};
var nextClientId = 0;
var curGraveId = 0;
var graves = [];
//process socket request
io.on("connection", function(socket){//this runs on first connection
//give client a socket id
users[nextClientId] = {};
users[nextClientId].socketId = socket.id;
console.log("new connection with Id: " + nextClientId);
socket.emit("idAssign", {"id" : nextClientId});
nextClientId++;
socket.on("addUser", function(userData) {
console.log("Adding user to game with id: " + userData.id);
var clientId = userData.id;
users[clientId].data = userData
// Send new user data to all sockets
io.sockets.emit("addUsers", [userData]);
// Send all connected users to the new user
var userDatas = [];
for (var key in Object.keys(users)) {
if ((key == null) || (users[key] == null)) {
continue;
}
userDatas.push(users[key].data);
}
socket.emit("addUsers", userDatas);
socket.emit("addGraves", graves);
});
socket.on("sendGrave", function(data) {
io.sockets.emit("sendGraves", data);
});
socket.on("userDataUpdate", function(userData) {
//console.log("Updating for user with id: " + userData.id);
users[userData.id].data = userData;
io.sockets.emit("updateUser", userData);
});
socket.on("nextGraveId", function() {
curGraveId++;
socket.emit("nextGraveId", curGraveId);
});
socket.on("disconnect", function() {
console.log("User disconnected")
var remainingUsers = {};
for (var id in Object.keys(users)) {
console.log(id);
if ((id == null) || (users[id] == null)) {
continue;
} else if (users[id].socketId == socket.id) {
io.sockets.emit("removeUser", {"id" : id})
} else {
remainingUsers[id] = users[id];
}
}
users = remainingUsers;
console.log(remainingUsers);
});
socket.on("getMap", function(){
socket.emit("sendMap",{
"map":mapData,
"obstacle":obstacleData});
});
});
var mapData = '';
for (var y = 0; y < 128; y++)
{
for (var x = 0; x < 128; x++)
{
var randomNum = Math.floor(Math.random() * 100) ;
if ((x === 0) && (y === 0)) { //Top left fence
mapData += '6';
} else if ((x === 0) && (y === 127)) { //Bottom left fence
mapData += '8';
} else if ((x === 127) && (y === 0)) { //Top right fence
mapData += '5';
} else if ((x === 127) && (y === 127)) { //Bottom right fence
mapData += '7';
} else if((y === 0) || (y === 127)) { //Top and Bottom fence
mapData += '9';
} else if ((x === 0) || (x === 127)) { //Left and Right fence
mapData += '3';
} else if(randomNum < 80) {
mapData += '1';
} else if(randomNum < 85) {
mapData += '2';
} else if(randomNum < 95) {
mapData += '0';
} else if(randomNum <= 100) {
mapData += '4';
}
if (x < 127)
{
mapData += ',';
}
}
if (y < 127)
{
mapData += "\n";
}
}
var obstacleData = [];
for (var i = 0; i < 133; i++)
{
var randomNumx = (Math.floor(Math.random() * 126) + 1) * 30;
var randomNumy = (Math.floor(Math.random() * 126) + 1) * 30;
var randomNumtype = Math.floor(Math.random() * 4);
obstacleData[i] = {
"x": randomNumx,
"y": randomNumy,
"type": randomNumtype
};
}
var potionData = [];
function getPotions(){
for (var i = 0; i < 10; i++)
{
var randomNumx = (Math.floor(Math.random() * 126) + 1) * 30;
var randomNumy = (Math.floor(Math.random() * 126) + 1) * 30;
var randomNumtype = Math.floor(Math.random() * 2);
potionData[i] = {
"x": randomNumx,
"y": randomNumy,
"type": randomNumtype
};
}
io.sockets.emit("addPotions", potionData);
}
setInterval(getPotions, 10000);