-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
183 lines (137 loc) · 6.32 KB
/
server.js
File metadata and controls
183 lines (137 loc) · 6.32 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ws://127.0.0.1:52300/socket.io/?EIO=4&transport=websocket
// ws://unity-node-game-server.herokuapp.com:80/socket.io/?EIO=4&transport=websocket
// https://unity-node-game-server.herokuapp.com/
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* server.js - This script is responsible for starting the web server and game server. It establishes connections to the database and sets up the routing for our web server.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*----------------*/
/*Static Variables*/
/*----------------*/
const PORT = process.env.PORT || 52300; //If we are provided a port by a third party, then use that. If not, use port 52300.
const DB_NAME = "unity-node-database"; //Specify the name of our mongoose database
//File imports
const Server = require("./Classes/Server");
/*---------------*/
/*--NPM Imports--*/
/*---------------*/
const express = require("express") //Import the 'express' npm package. Used to establish server connections for web client.
const app = express(); //Establish express server.
const path = require("path"); //Import for file paths.
const fs = require("fs");
//Mongoose requires
const mongojs = require("mongojs");
const mongoose = require("mongoose");
const logger = require("morgan");
//---Connecting web server and game server to one port
const server = require("http").createServer(app); //Wrap our express server in an http server. This will allow us to connect our game server and web server with sockets.
const io = require("socket.io")(server); //Wrap our server in a socket element so we may use sockets to connect our servers.
/*--------------------*/
/*--- Extract Zip ----*/
/*--------------------*/
// if(fs.existsSync("./client/public/assets/game-builds/unzipped/")){
// console.log("File is already unzipped");
// }
// else{
// fs.createReadStream(`./client/public/assets/game-builds/${BUILD_NAME}.zip`)
// .pipe(unzipper.Extract({path : "./client/public/assets/game-builds/unzipped"})).promise().then(
// () => {
// console.log("finished extracting file");
// }
// )
// }
/*--------------------*/
/*Middleware functions*/
/*--------------------*/
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(logger("dev"));
// Serve up static assets (usually on heroku)
app.use(express.static("client/build"));
/*------------------------*/
/*--------Routing---------*/
/*------------------------*/
const routes = require("./routes");
app.use(routes); //Use our routes established in our 'routes' directory.
// '*' catches all paths which are not caught previously.
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html")); //Respond to the route 'get' request with our React index.html file. This can be accessed by specifiying our current directory, "__dirname"
});
/*------------------------*/
/*---- Custom Classes ----*/
/*------------------------*/
console.log(`Server has started on http://localhost:${PORT}`);
const cServer = new Server(); //Create an instance of our 'Server' class
/*------------------------*/
/*-------- Update --------*/
/*------------------------*/
setInterval(() => { //Each 100ms, repeated infintely, do the following
cServer.onUpdate(); //Run the update method on our instanced 'Server' class
}, 100, 0);
/*------------------------*/
/*-------- Socket --------*/
/*------------------------*/
//Upon a socket.io connection, do the following, passing in the 'socket' connection
io.on("connection", function (iSocket/* The socket connecting */) {
const connection = cServer.onConnected(iSocket); //Run the "onConnected" method on our server, passing in our 'socket' connection. Establish a reference to the connection made with the socket.
connection.db = require("./models"); //Set a reference, for our connection, to our database.
connection.createEvents(); //Create the events for our connection.
connection.socket.emit("register", { 'id': connection.player.id }); //Register our connection with the ID of our player, found from our connection.
});
/*---------------------------*/
/*Establish server connection*/
/*---------------------------*/
server.listen(PORT); //Sets up our server to listen for events.
/*-------------------------*/
/*-------- Methods --------*/
/*-------------------------*/
function interval(func, pWait, pTimes) {
const iInterval = function (iWait, iTimes) {
return function () {
if (typeof iTimes === "undefined" || iTimes-- > 0) {
setTimeout(iInterval, iWait);
try {
func.call(null);
} catch (error) {
t = 0;
throw error.toString();
}
}
}
}(pWait, pTimes);
setTimeout(iInterval, pWait);
}
/* ------------------ */
/* ---- Database ---- */
/* ------------------ */
//Connect to database.
mongoose.connect(process.env.MONGODB_URI || `mongodb://localhost/${DB_NAME}`, {
useNewUrlParser: true,
useFindAndModify: true
});
//Set up Mongo database.
const databaseUrl = process.env.MONGODB_URI || DB_NAME;
const collections = [DB_NAME];
//Set reference to our database.
const db = require("./models");
DB_WipeOnStart();
/**
* Wipes our database when called. Should be called at start.
*/
function DB_WipeOnStart() {
console.warn(`!!! Wiping our MongoDB !!!`);
db.Player.remove({}, function (err, data) {
if (err) throw err;
console.log(`Removed Player collection from DB ...`);
console.log(data);
})
db.Messages.remove({}, function(err, data){
if(err) throw err;
console.log(`Removed Messages collection from DB ...`);
console.log(data);
})
db.User.remove({}, function(err, data){
if(err) throw err;
console.log(`Removed Messages collection from DB ...`);
console.log(data);
})
}