forked from felixmariotto/Edelweiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
231 lines (136 loc) · 4.42 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const express = require('express');
const path = require('path');
const socketIO = require('socket.io');
const PORT = process.env.PORT || 5050;
var geoip = require('geoip-lite');
const { Pool } = require('pg');
const POOL = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: true
});
/////////////////
/// APP
/////////////////
const app = express()
.use(express.static('public'))
.get('/', (req, res)=> {
res.sendFile(path.join(__dirname + '/public/index.html'));
})
.listen(PORT, ()=> {
console.log('App listening on port ' + PORT);
})
//////////////////
/// SOCKET.IO
//////////////////
const io = socketIO( app );
io.on( 'connection', async (client)=> {
var lang = client.handshake.headers['accept-language'].split(",")[0];
var ip = client.handshake.headers["x-forwarded-for"].split(",")[0] ;
var geo = geoip.lookup( ip );
// console.log( `User ${ client.id } connected` );
var clientID;
var startTime = Date.now();
var postgresClient = await POOL.connect();
postgresClient.query( `INSERT INTO analytics (
environment,
game_version,
timestamp,
ip,
country,
language
) VALUES (
'${ process.env.ENVIRONMENT }',
'${ process.env.VERSION || 'undefined' }',
NOW(),
'${ ip }',
'${ geo.country }',
'${ lang }'
) RETURNING id` ).then( (value)=> {
clientID = value.rows[ 0 ].id ; // clientID is a number
});
postgresClient.release();
//
client.on( 'init', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics SET
browser = '${ message.browser }',
browser_version = '${ message.browser_version }',
local_time = '${ message.time }'
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'bonus', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET bonuses = array_append( bonuses, '${ message }' )
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'dialogue', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET dialogues = array_append( dialogues, '${ message }' )
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'save', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET saves = array_append( saves, '${ message }' )
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'death', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET deaths = array_append( deaths, '${ message }' )
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'touchscreen', async ()=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET touchscreen = true
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on( 'opti', async (message)=> {
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics
SET opti_levels = array_append( opti_levels, '${ message }' )
WHERE id = ${ clientID }` );
postgresClient.release();
});
//
client.on('playerInfo', (message)=> {
// join the room with the requested game name
io.sockets.sockets[ client.id ].join( message.pass );
client.roomId = message.pass ;
// record the ID created on client side.
// when the client quit, its game ID will be broadcasted to
// every other player in the same room.
client.gameId = message.id ;
// broadcast player position to every player in the same socket io "room"
client.broadcast.to( message.pass ).emit( 'playerInfo', message );
});
//
client.on( 'disconnect', async function() {
// broadcast the disconnection information to the other
// players of the same room
if ( client.roomId ) {
client.broadcast.to( client.roomId ).emit( 'playerLeft', client.gameId );
};
//
var postgresClient = await POOL.connect();
postgresClient.query( `UPDATE analytics SET
duration = '${ Date.now() - startTime }'
WHERE id = ${ clientID }` );
postgresClient.release();
});
})