-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
147 lines (111 loc) · 3.64 KB
/
app.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
require('dotenv').config()
const fs = require('fs');
if(process.argv[2] == "TEST_MODE"){
process.TEST_MODE = true;
console.log("running in test mode");
}
var express = require("express");
var bodyparser = require("body-parser");
var session = require("express-session");
//TODO: install cookie-parser
var cookieParser = require('cookie-parser');
const MongoStore = require('connect-mongo')(session);
var passport = require("passport");
require("./API/passport_conf.js");
var passportSocketIo = require("passport.socketio");
var app = express();
app.set('trust proxy',true);
var http = require("http").Server(app);
var login = require("./API/login.api.js");
var register = require("./API/register.api.js");
var faucet = require("./API/faucet.api.js");
var referrer = require("./API/referrer.api.js");
var gamereplays = require("./API/gamereplays.api.js");
var db = require("./db.js");
db.promise.then( function(){
http.listen(3006, function(){
console.log("App listen on 3006 port");
});
})
var secret = "u7ga782";
console.log(db.clientPromise)
var session_store = new MongoStore(
{
clientPromise: db.clientInstancePromise,
dbName: db.dbName
}
);
var session_middleware = session({
secret: secret,
store: session_store,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 30 // one month
},
resave: false,
saveUninitialized: false
});
app.use(express.static("dist"));
app.use(cookieParser());
app.use(session_middleware);
app.use(bodyparser.json());
app.use(bodyparser.urlencoded());
app.use(passport.initialize());
app.use(passport.session());
const ApiRouter = express.Router();
ApiRouter.use("/login", login);
ApiRouter.use("/register", register);
ApiRouter.use("/faucet", faucet);
ApiRouter.use("/referrer", referrer);
ApiRouter.use("/gamereplays", gamereplays);
app.use('/api', ApiRouter);
// Mount io server
var io = require("socket.io")(http);
io.set('origins', '*:*');
io.use(passportSocketIo.authorize({
key: 'connect.sid', // the name of the cookie where express/connect stores its session_id
secret: secret, // the session_secret to parse the cookie
store: session_store, // we NEED to use a sessionstore. no memorystore please
success: onAuthorizeSuccess, // *optional* callback on success - read more below
fail: onAuthorizeFail, // *optional* callback on fail/error - read more below
}));
function onAuthorizeSuccess(data, accept){
// console.log("Authorized socket io connection");
accept();
}
function onAuthorizeFail(data, message, error, accept){
// console.log("Non-Authorized socket io connection");
// console.log(message);
// error indicates whether the fail is due to an error or just a unauthorized client
if(error) throw new Error(message);
// send the (not-fatal) error-message to the client and deny the connection
return accept(new Error(message));
}
var games = require("./games.js");
io.use( (socket, next)=>{
socket.playername = socket.request.user.username;
//FIXME after refrehing page already connected is emitted
// for(var key in socket.nsp.connected){
//
// if(socket.playername == socket.nsp.connected[key].playername){
// next(new Error("already connected"));
// return;
// }
// }
next();
})
io.use( (socket,next)=>{
var isPlayerInGame = games.isPlayerInGame(socket.playername);
if(isPlayerInGame){
var game_name = isPlayerInGame;
next( new Error("already in game") );
}
else{
next();
}
})
io.on("connection", function(socket){
for(var key in socket.nsp.connected){
console.log(socket.nsp.connected[key].playername);
}
var lobby = require("./lobby.js")(io, socket);
})