-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
131 lines (102 loc) · 2.96 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
var express = require('express')
var app = express()
//Variable so that the controller knows which page you are on
var pageName = null;
//
var server = app.listen(3001, listening);
//Websockets array
//CONSTANTS
const POSITION_X = 0;
const POSITION_Y = 1;
const POSITION_Z = 2;
const SHOT = 3;
const USER_NAME = 4;
const IS_EXIT = 5;
//
var curData = [0, 0];
//
///socket
//DB
const { Client } = require('pg');
const client = new Client({
user: 'postgres',
host: '172.17.0.1',
database: 'duckhunt',
password: '123456',
port: 5432,
})
client.connect();
//TVA E ZA DEBUG ZA MOMENTA NE GO BARAYTE
//app.get('/gun', function (req, res) {
/*client.query("INSERT INTO scores VALUES ('shterkata',12);", (err, res) => {
if (err) {
console.log(err.stack)
} else {
//console.log(res.rows[0])
}
//})
});*/
//SCORE INSERT FROM FRONTEND
app.get('/insert/:name/:score/:gameName', function (req, res) {
var data = req.params;
console.log(data);
client.query("INSERT INTO scores VALUES ('" + data.name + "'," + data.score + ",'" + data.gameName + "');", (err, res) => {
if (err) {
console.log(err.stack)
} else {
// console.log(res);
}
});
});
var socket = require('socket.io');
var io = socket(server);
io.sockets.on('connection', newConnection);
function newConnection(socket) {
console.log('new connection:' + socket.id);
socket.on('dataIn', msg1);
function msg1(data) {
data.x = Number(curData[POSITION_X]);
data.y = Number(curData[POSITION_Y]);
data.z = Number(curData[POSITION_Z]);
data.shot = curData[SHOT];
data.user_name = curData[USER_NAME];
data.isExit = curData[IS_EXIT];
pageName = data.page;
io.sockets.emit('dataIn', data);
}
socket.on('scores', msg2);
function msg2(data) {
// client.query("SELECT * FROM scores WHERE gamename= '" + tableName + "' ORDER BY score DESC FETCH NEXT 5 ROW ONLY;", (err, res) => {
client.query("SELECT * FROM scores ORDER BY score ;",(err, res)=>{
if (err) {
console.log(err.stack)
} else {
data = res.rows;
io.sockets.emit('scores', data);
console.log(data);
}
});
}
}
// app.get('/gun', function (req, res) {
// res.send(cords);
// });
app.use('/mainmenu', express.static("Menu"));
app.use('/games/duckhunt', express.static("Games/DuckHunt"));
app.use('/score', express.static("ScoreBoard"));
app.use('/controller', express.static("WebController"));
app.use('/games/fruitninja', express.static("Games/FruitNinja"));
app.use('/games/racing', express.static("Games/Racing"));
app.use('/games/cloth-testing' , express.static("Games/Cloth-testing"));
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 4000 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
curData = JSON.parse(message);
ws.send(pageName);
console.log(message);
});
});
function listening() {
console.log("Listening...");
}