forked from xbeat/GameServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
163 lines (98 loc) · 2.87 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
'user strict';
const express = require( 'express' );
const pg = require( 'pg' );
const WebSocket = require( 'ws' );
const http = require( 'http' );
const uuidv4 = require( 'uuid/v4' );
const url = require( 'url' );
const PrecisionTimer = require( './modules/PrecisionTimer.js' );
let timer = new PrecisionTimer();
timer.Start();
const GameServer = require( './modules/GameServer.js' );
const app = express();
const webServer = http.createServer( app );
app.use( express.static( __dirname + '/' ) );
app.get( '/', function( req, res ){
res.sendFile( __dirname + "/start.html" );
});
class Database {
constructor(){
this.pool = new pg.Pool( {
user: 'giuseppecanale',
host: '127.0.0.1',
database: 'giuseppecanale',
password: '',
idleTimeoutMillis: 30000,
port: '5432' } );
};
select( id, emitter ){
this.pool.query( 'SELECT * FROM parameters WHERE id = $1', [id], function( err, res ) {
if( err ) {
return console.error( 'error running SELECT ', err );
};
return emitter.Parameters( res.rows[0] );
});
};
insert(){
this.pool.query( 'INSERT INTO parameters(username, password) VALUES($1, $2) returning id', [req.body.username, req.body.password], function( err, res ) {
if( err ) {
return console.error( 'error running INSERT', err );
};
});
};
update( skillId, value ){
this.pool.query( `UPDATE parameters SET p${skillId} = $1 WHERE id = 1`, [value], function( err, res ) {
if( err ) {
return console.error( 'error running UPDATE', err );
};
});
};
delete(){
this.pool.query( 'DELETE FROM parameters WHERE id = $1', [req.params.id], function( err, res ) {
if( err ) {
return console.error( 'error running DELETE', err );
};
});
};
};
class Server extends Database {
constructor(){
super();
this.gameServer;
this.emitter = require( './emitter.js' );
this.wss = new WebSocket.Server( { server: webServer } );
this.wss.on( 'connection', function connection( ws, req ) {
this.onConnection( ws );
ws.on( 'message', function message( data ) {
this.onMessage( data, ws );
}.bind( this ) );
}.bind( this ) );
};
onConnection( ws ) {
this.lobby = uuidv4();
//this.emitter.init( ws ); // single connection
this.emitter.init( this.wss ); // for broadcast
this.select( "1", this.emitter );
if ( !this.gameServer ) {
this.gameServer = new GameServer( timer );
};
};
onMessage( data, ws ) {
let JSONdata = JSON.parse( data )
if( "state" in JSONdata ) {
switch( JSONdata.state ) {
case "reset":
this.gameServer == null;
break;
case "update":
this.update( JSONdata.skillId, JSONdata.value );
break;
};
};
};
};
let server = new Server();
const PORT = process.env.PORT || 4000;
webServer.listen( PORT, function listening() {
console.log( 'Listening on %d', webServer.address().port );
});