-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
135 lines (117 loc) · 4.64 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
/* SERVER */
const Player = require("./js/player");
const Sqlite3 = require("sqlite3");
const DataFile = "./data/topScores.db";
class Server {
constructor() {
this.express = require('express');
this.app = this.express();
this.server = require('http').Server(this.app);
this.io = require('socket.io').listen(this.server);
this.app.use('/css', this.express.static(__dirname + '/css'));
this.app.use('/js', this.express.static(__dirname + '/js'));
this.app.use('/node_modules', this.express.static(__dirname + '/node_modules'));
this.app.use('/assets', this.express.static(__dirname + '/assets'));
this.app.get('/', (req, res) => {
res.sendFile(__dirname+'/index.html');
});
this.server.listen(80, () => { // Listens to port 80
console.log('Listening on ' + this.server.address().port);
});
this.players = {};
this.setupEvents();
this.db = this.openConn();
this.leaderboard = this.getLeaderBoard();
}
/* EVENTS */
setupEvents() {
this.io.on("connection", (socket) => {
/* RX EVENTS */
socket.on("newplayer", () => {
// Create a new "player" object and assign it to the new socket
// object
socket.player = new Player(socket.id);
// update the total list of players
this.players[socket.id] = socket.player;
// send the client their ID
socket.emit("yourplayer", socket.player); // TX
// send current players to the new player
socket.emit("currentplayers", Object.values(this.players));
// tell pre-existing players about new player
socket.broadcast.emit("newplayer", socket.player);
});
socket.on("killplayer",(data) => {
console.log(data);
if (!(data.id in this.players)) {
// player was probably already killed and deleted
return;
}
this.players[data.id].lives -= data.damage;
this.updatePlayer(socket, this.players[data.id]);
if(this.players[data.id].lives <= 0) {
delete this.players[data.id];
}
//Don't update here for performance reasons. Rely on other updatePlayer calls.
});
socket.on("disconnect", () => {
// save player before deleting them?
let query = "INSERT INTO topPlayers\
VALUES (?, ?, ?)";
this.db.all(query, [socket.player.id, "PLAYERNAME", socket.player.score], (e, msg) => {
if (e) {
console.log("ERROR IN STORING PLAYER RESULT.\t" + e);
}
});
delete this.players[socket.player.id];
// Tell other players that this player died
socket.player.lives = 0;
this.updatePlayer(socket, socket.player);
});
/* TX EVENTS */
socket.on("playerupdate", (data) => {
// data is an updated player object
if (!(data.id in this.players)) {
// return early if player has been deleted or something
// weird happened
return;
}
this.players[data.id].posX = data.posX;
this.players[data.id].posY = data.posY;
this.players[data.id].score = data.score;
this.updatePlayer(socket, this.players[data.id]);
});
});
}
/**
* Tells everyone that a player was updated either in position or lives.
*/
updatePlayer(socket, player) {
socket.emit("updateplayer", player);
socket.broadcast.emit("updateplayer", player);
}
openConn() {
return new Sqlite3.Database(DataFile, Sqlite3.OPEN_READWRITE, (e) => {
if (e) { console.log(e.message); };
});
}
getLeaderBoard() {
// open db to get the top 3 players of all time
let query = "SELECT t.pname as name, t.pscore as score\
FROM topPlayers t\
ORDER BY t.pscore DESC\
LIMIT 3";
this.db.all(query, [], (e, res) => {
if (e) {
console.error("ERROR IN TOP PLAYER QUERY.\t" + e);
return {};
}
// return list of top three players
console.log(res);
return res;
});
}
}
let main = () => {
let server = new Server();
};
main();